Files

64 lines
2.3 KiB
SQL

-- Records that a consumer applies an event, in the transaction of the effect.
-- It returns 0 rows when the marker exists. While another transaction holds
-- an uncommitted marker for the same key, the insert waits for it: after a
-- commit it returns 0, after a rollback it inserts. So a concurrent duplicate
-- never runs the effect at the same time as the first delivery.
-- name: InsertProcessedEventMarker :execrows
INSERT INTO consumer_processed_events (consumer_name, event_id)
VALUES (sqlc.arg(consumer_name), sqlc.arg(event_id))
ON CONFLICT (consumer_name, event_id) DO NOTHING;
-- Stores a record that a consumer could not apply. A second delivery of the
-- same record (same position and value) refreshes the outcome of its row:
-- after a rewind the handler may fail differently. A different value at a
-- reused position, after the topic was recreated, gets its own row.
-- name: InsertConsumerDeadLetter :exec
INSERT INTO consumer_dead_letters (
consumer_name,
topic,
kafka_partition,
kafka_offset,
event_id,
event_type,
error_class,
reason,
attempts,
value_size,
value_sha256
) VALUES (
sqlc.arg(consumer_name),
sqlc.arg(topic),
sqlc.arg(kafka_partition),
sqlc.arg(kafka_offset),
sqlc.narg(event_id),
sqlc.narg(event_type),
sqlc.arg(error_class),
sqlc.arg(reason),
sqlc.arg(attempts),
sqlc.arg(value_size),
sqlc.arg(value_sha256)
)
ON CONFLICT (
consumer_name, topic, kafka_partition, kafka_offset, value_sha256
) DO UPDATE SET
event_id = excluded.event_id,
event_type = excluded.event_type,
error_class = excluded.error_class,
reason = excluded.reason,
attempts = excluded.attempts,
dead_lettered_at = clock_timestamp();
-- Bounds a consumer transaction on the server, local to the transaction.
-- statement_timeout stops a slow statement; idle_in_transaction_session_timeout
-- ends the session of a process that froze or lost the network inside the
-- handler, which releases the processed-event marker that a concurrent
-- duplicate waits for.
-- name: SetConsumerTransactionTimeouts :exec
SELECT
set_config('statement_timeout', sqlc.arg(statement_timeout_ms)::text, true),
set_config(
'idle_in_transaction_session_timeout',
sqlc.arg(idle_timeout_ms)::text,
true
);