T-57: M1.3b — trial_ledger + vault_evaluations append-only PostgreSQL migration (research track)

This commit is contained in:
Studio 2026-11-27 17:08:47 -05:00 • committed by Boki
parent ed56ec1bbf
commit bb6e91ef53
4 changed files with 155 additions and 22 deletions

View file

@ -71,14 +71,20 @@ LANGUAGE plpgsql AS $$
DECLARE
recent_count BIGINT;
BEGIN
IF current_setting('transaction_isolation') NOT IN ('read committed', 'read uncommitted') THEN
RAISE EXCEPTION 'Vault evaluations must be inserted under READ COMMITTED isolation (got %)',
current_setting('transaction_isolation')
USING ERRCODE = '25000';
END IF;
PERFORM pg_advisory_xact_lock(
hashtext('stock_bot_vault_evaluations_cap'),
hashtext(TG_TABLE_SCHEMA)
);
NEW.evaluated_at := now();
SELECT count(*) INTO recent_count
FROM vault_evaluations
WHERE evaluated_at > now() - interval '1 year';
EXECUTE format(
'SELECT count(*) FROM %s WHERE evaluated_at > now() - interval ''1 year''',
TG_RELID::regclass
) INTO recent_count;
IF recent_count >= 10 THEN
RAISE EXCEPTION 'Vault evaluation cap exceeded: at most 10 evaluations are permitted per rolling year'
USING ERRCODE = '23514';
@ -96,9 +102,10 @@ LANGUAGE plpgsql AS $$
DECLARE
recent_count BIGINT;
BEGIN
SELECT count(*) INTO recent_count
FROM vault_evaluations
WHERE evaluated_at > now() - interval '1 year';
EXECUTE format(
'SELECT count(*) FROM %s WHERE evaluated_at > now() - interval ''1 year''',
TG_RELID::regclass
) INTO recent_count;
IF recent_count > 10 THEN
RAISE EXCEPTION 'Vault evaluation cap exceeded: at most 10 evaluations are permitted per rolling year'
USING ERRCODE = '23514';