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

193 lines
6.8 KiB
SQL

-- Writers lock the organization first, then, for an E0 organization, the holding
-- whose pool it shares, then invitation/card-view state, and finally usage rows.
-- The organization lock serializes quota decisions for the tenant, the holding
-- lock serializes the E0 pool across the organizations of one holding, and
-- together they make the subsequent count and insert atomic.
-- Takes the organization lock ahead of every share lock of a reader that will
-- reserve usage later. FOR NO KEY UPDATE conflicts with itself and with FOR
-- SHARE, so readers of one organization queue up instead of deadlocking on a
-- share lock that each then tries to raise, but it does not conflict with the
-- FOR KEY SHARE that a foreign key takes, such as the hidden-organization
-- insert of a candidate, which holds the candidate's row and then needs this
-- one. The reservation itself still takes FOR UPDATE inside the same
-- transaction. The result carries the holding, whose pool an E0 organization
-- shares; the caller locks that holding right after this lock.
-- name: LockCompanyQuotaOrganizationFirst :one
SELECT
id,
verification_level,
holding_id
FROM organizations
WHERE id = sqlc.arg(organization_id)
FOR NO KEY UPDATE;
-- name: LockCompanyQuotaOrganization :one
SELECT
id,
verification_level,
verification_basis,
verification_changed_at,
holding_id
FROM organizations
WHERE id = sqlc.arg(organization_id)
FOR UPDATE;
-- Takes the pool lock of an E0 holding. It follows the organization lock, so
-- every writer locks an organization first and its holding second. Two
-- reservations of one holding queue here, and the second one counts the
-- usage that the first one committed. The lock does not conflict with the
-- FOR KEY SHARE that organizations.holding_id takes when an organization joins
-- the holding.
-- name: LockCompanyQuotaHolding :one
SELECT id
FROM holdings
WHERE id = sqlc.arg(holding_id)
FOR NO KEY UPDATE;
-- name: FindCompanyInvitationQuotaUsage :one
SELECT invitation_id
FROM company_invitation_quota_usage
WHERE
organization_id = sqlc.arg(organization_id)
AND invitation_id = sqlc.arg(invitation_id)
FOR UPDATE;
-- name: CountCompanyInvitationQuotaWindow :one
SELECT
count(*)::bigint AS usage_count,
min(reserved_at) AS earliest_at
FROM company_invitation_quota_usage
WHERE
organization_id = sqlc.arg(organization_id)
AND reserved_at > sqlc.arg(window_start);
-- Counts the invitation usage of the E0 pool of a holding: the usage of every
-- organization of the holding whose trust level is E0 when the count runs. Each
-- row stays attributed to the organization that consumed it.
-- name: CountCompanyInvitationQuotaHoldingWindow :one
SELECT
count(*)::bigint AS usage_count,
min(usage.reserved_at) AS earliest_at
FROM company_invitation_quota_usage AS usage
INNER JOIN organizations AS organization ON organization.id = usage.organization_id
WHERE
organization.holding_id = sqlc.arg(holding_id)
AND organization.verification_level = 'e0'
AND usage.reserved_at > sqlc.arg(window_start);
-- name: InsertCompanyInvitationQuotaUsage :exec
INSERT INTO company_invitation_quota_usage (
organization_id,
invitation_id,
reserved_at,
policy_version_id,
verification_level,
verification_basis,
verification_changed_at,
quota_tier
) VALUES (
sqlc.arg(organization_id),
sqlc.arg(invitation_id),
sqlc.arg(reserved_at),
sqlc.arg(policy_version_id),
sqlc.arg(verification_level),
sqlc.arg(verification_basis),
sqlc.arg(verification_changed_at),
sqlc.arg(quota_tier)
);
-- name: FindCompanyCardViewQuotaUsage :one
SELECT last_seen_at
FROM company_card_view_quota_usage
WHERE
organization_id = sqlc.arg(organization_id)
AND candidate_id = sqlc.arg(candidate_id)
FOR UPDATE;
-- name: RefreshCompanyCardViewQuotaUsage :execrows
UPDATE company_card_view_quota_usage
SET
last_seen_at = greatest(last_seen_at, sqlc.arg(last_seen_at)),
policy_version_id = sqlc.arg(policy_version_id),
verification_level = sqlc.arg(verification_level),
verification_basis = sqlc.arg(verification_basis),
verification_changed_at = sqlc.arg(verification_changed_at),
quota_tier = sqlc.arg(quota_tier)
WHERE
organization_id = sqlc.arg(organization_id)
AND candidate_id = sqlc.arg(candidate_id)
AND last_seen_at > sqlc.arg(window_start);
-- name: CountCompanyCardViewQuotaWindow :one
SELECT
count(*)::bigint AS usage_count,
min(last_seen_at) AS earliest_at
FROM company_card_view_quota_usage
WHERE
organization_id = sqlc.arg(organization_id)
AND last_seen_at > sqlc.arg(window_start);
-- Counts the distinct-card usage of the E0 pool of a holding, with the same
-- rule as CountCompanyInvitationQuotaHoldingWindow. A view is still counted
-- per organization: the same candidate seen by two organizations of one
-- holding is two charges.
-- name: CountCompanyCardViewQuotaHoldingWindow :one
SELECT
count(*)::bigint AS usage_count,
min(usage.last_seen_at) AS earliest_at
FROM company_card_view_quota_usage AS usage
INNER JOIN organizations AS organization ON organization.id = usage.organization_id
WHERE
organization.holding_id = sqlc.arg(holding_id)
AND organization.verification_level = 'e0'
AND usage.last_seen_at > sqlc.arg(window_start);
-- name: SaveCompanyCardViewQuotaUsage :exec
INSERT INTO company_card_view_quota_usage (
organization_id,
candidate_id,
last_seen_at,
policy_version_id,
verification_level,
verification_basis,
verification_changed_at,
quota_tier
) VALUES (
sqlc.arg(organization_id),
sqlc.arg(candidate_id),
sqlc.arg(last_seen_at),
sqlc.arg(policy_version_id),
sqlc.arg(verification_level),
sqlc.arg(verification_basis),
sqlc.arg(verification_changed_at),
sqlc.arg(quota_tier)
)
ON CONFLICT (organization_id, candidate_id)
DO UPDATE SET
last_seen_at = greatest(
company_card_view_quota_usage.last_seen_at,
EXCLUDED.last_seen_at
),
policy_version_id = EXCLUDED.policy_version_id,
verification_level = EXCLUDED.verification_level,
verification_basis = EXCLUDED.verification_basis,
verification_changed_at = EXCLUDED.verification_changed_at,
quota_tier = EXCLUDED.quota_tier;
-- name: DeleteExpiredCompanyCardViewQuotaUsage :execrows
WITH expired AS (
SELECT quota_usage.organization_id, quota_usage.candidate_id
FROM company_card_view_quota_usage AS quota_usage
WHERE
quota_usage.organization_id = sqlc.arg(organization_id)
AND quota_usage.last_seen_at <= sqlc.arg(window_start)
ORDER BY quota_usage.last_seen_at, quota_usage.candidate_id
LIMIT sqlc.arg(row_limit)::integer
)
DELETE FROM company_card_view_quota_usage AS usage
USING expired
WHERE
usage.organization_id = expired.organization_id
AND usage.candidate_id = expired.candidate_id;