Files
lincodex/backend/db/queries/mail_quota.sql
T

194 lines
6.6 KiB
SQL

-- Lock order. Every transaction that writes mail_quota_usage first creates
-- and locks all the usage rows it will touch with EnsureMailQuotaRows and
-- LockMailQuotaRows, which both work in (period_kind, period_start) order,
-- and only then writes them or a mail_quota_reservations row. Later UPDATE
-- statements of the same transaction find their rows locked already, so the
-- scan order of an UPDATE never decides the lock order.
-- Creates the listed window rows of a provider account with no usage when
-- they do not exist yet, in (period_kind, period_start) order. A window key
-- is "<period_kind>/<YYYY-MM-DD>". A concurrent insert of the same row waits
-- for the first transaction and then inserts nothing. Existing rows are
-- neither changed nor locked here.
-- name: EnsureMailQuotaRows :exec
INSERT INTO mail_quota_usage (
provider,
provider_account,
period_kind,
period_start,
used_recipients,
updated_at
)
SELECT
sqlc.arg(provider)::text,
sqlc.arg(provider_account)::text,
split_part(window_key, '/', 1),
split_part(window_key, '/', 2)::date,
0,
sqlc.arg(updated_at)::timestamptz
FROM unnest(sqlc.arg(window_keys)::text []) AS window_key
ORDER BY split_part(window_key, '/', 1), split_part(window_key, '/', 2)::date
ON CONFLICT (
provider, provider_account, period_kind, period_start
) DO NOTHING;
-- Locks the listed window rows until the transaction ends, in
-- (period_kind, period_start) order, the same order for every caller, and
-- returns their usage. Under READ COMMITTED a waiting caller reads the usage
-- the first caller committed. period_starts narrows the index scan;
-- window_keys selects the exact rows.
-- name: LockMailQuotaRows :many
SELECT
period_kind,
period_start,
used_recipients
FROM mail_quota_usage
WHERE
provider = sqlc.arg(provider)
AND provider_account = sqlc.arg(provider_account)
AND period_start = any(sqlc.arg(period_starts)::date [])
AND period_kind || '/' || to_char(period_start, 'YYYY-MM-DD')
= any(sqlc.arg(window_keys)::text [])
ORDER BY period_kind, period_start
FOR UPDATE;
-- Adds recipients to the day and the month row. The caller holds their
-- locks from LockMailQuotaRows and has checked both limits.
-- name: AddMailQuotaUsage :execrows
UPDATE mail_quota_usage
SET
used_recipients = mail_quota_usage.used_recipients
+ sqlc.arg(recipients)::bigint,
updated_at = sqlc.arg(updated_at)
WHERE
provider = sqlc.arg(provider)
AND provider_account = sqlc.arg(provider_account)
AND (
(period_kind = 'day' AND period_start = sqlc.arg(day_start))
OR (period_kind = 'month' AND period_start = sqlc.arg(month_start))
);
-- Gives back recipients of a released reservation. The caller holds the row
-- locks. A window that the provider reported exhausted keeps its usage: the
-- provider's answer outranks the local count until the window resets. The
-- count never drops below zero.
-- name: ReleaseMailQuotaUsage :execrows
UPDATE mail_quota_usage
SET
used_recipients = greatest(
mail_quota_usage.used_recipients - sqlc.arg(recipients)::bigint, 0
),
updated_at = sqlc.arg(updated_at)
WHERE
provider = sqlc.arg(provider)
AND provider_account = sqlc.arg(provider_account)
AND provider_exhausted_at IS NULL
AND (
(period_kind = 'day' AND period_start = sqlc.arg(day_start))
OR (period_kind = 'month' AND period_start = sqlc.arg(month_start))
);
-- Records that the provider refused a send because the quota of a window is
-- used up: the usage rises to at least the configured limit, so the local
-- gate stops admitting sends until the window resets or an operator raises
-- the limit. The caller holds the row lock.
-- name: MarkMailQuotaExhausted :execrows
UPDATE mail_quota_usage
SET
used_recipients = greatest(
mail_quota_usage.used_recipients, sqlc.arg(limit_recipients)::bigint
),
provider_exhausted_at = sqlc.arg(exhausted_at),
updated_at = sqlc.arg(exhausted_at)
WHERE
provider = sqlc.arg(provider)
AND provider_account = sqlc.arg(provider_account)
AND period_kind = sqlc.arg(period_kind)
AND period_start = sqlc.arg(period_start);
-- name: InsertMailQuotaReservation :exec
INSERT INTO mail_quota_reservations (
id,
delivery_id,
provider,
provider_account,
day_start,
month_start,
recipients,
reserved_at
) VALUES (
sqlc.arg(id),
sqlc.arg(delivery_id),
sqlc.arg(provider),
sqlc.arg(provider_account),
sqlc.arg(day_start),
sqlc.arg(month_start),
sqlc.arg(recipients),
sqlc.arg(reserved_at)
);
-- Returns the newest open reservation of a delivery under a provider account
-- made at or after reserved_since. A retry of the delivery inside the
-- provider's idempotency window reuses it instead of counting again.
-- name: FindOpenMailQuotaReservation :one
SELECT
id,
day_start,
month_start,
recipients,
reserved_at
FROM mail_quota_reservations
WHERE
delivery_id = sqlc.arg(delivery_id)
AND provider = sqlc.arg(provider)
AND provider_account = sqlc.arg(provider_account)
AND released_at IS NULL
AND reserved_at >= sqlc.arg(reserved_since)
ORDER BY reserved_at DESC
LIMIT 1;
-- Marks a reservation released. It matches only an open reservation, so a
-- repeated release changes nothing and returns 0 rows.
-- name: ReleaseMailQuotaReservation :execrows
UPDATE mail_quota_reservations
SET released_at = sqlc.arg(released_at)
WHERE
id = sqlc.arg(id)
AND released_at IS NULL;
-- Records the latest settled outcome of a send under the reservation.
-- name: RecordMailQuotaOutcome :execrows
UPDATE mail_quota_reservations
SET
settled_outcome = sqlc.arg(settled_outcome),
settled_at = sqlc.arg(settled_at)
WHERE id = sqlc.arg(id);
-- Reads the usage of the day and the month window without locking.
-- name: GetMailQuotaUsage :many
SELECT
period_kind,
used_recipients,
provider_exhausted_at
FROM mail_quota_usage
WHERE
provider = sqlc.arg(provider)
AND provider_account = sqlc.arg(provider_account)
AND (
(period_kind = 'day' AND period_start = sqlc.arg(day_start))
OR (period_kind = 'month' AND period_start = sqlc.arg(month_start))
);
-- Lists the other accounts of a provider that have usage in a month, so
-- startup can warn about a renamed EMAIL_ACCOUNT.
-- name: ListOtherMailQuotaAccounts :many
SELECT DISTINCT provider_account
FROM mail_quota_usage
WHERE
provider = sqlc.arg(provider)
AND provider_account <> sqlc.arg(provider_account)
AND period_kind = 'month'
AND period_start = sqlc.arg(month_start)
AND used_recipients > 0
ORDER BY provider_account;