Files
simstudioai--sim/packages/db/migrations/0198_tables_race_free_trigger.sql
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

68 lines
2.4 KiB
PL/PgSQL

-- Replace increment_user_table_row_count() with a race-free conditional UPDATE.
-- The prior version did SELECT row_count -> IF check -> UPDATE, which is
-- TOCTOU-vulnerable: two concurrent inserts near max_rows could both pass the
-- check and push row_count past the cap. Application code compensated with
-- SELECT ... FOR UPDATE on user_table_definitions, creating a serialization
-- hotspot on every insert.
--
-- The new version performs capacity check and increment as a single
-- conditional UPDATE. The UPDATE takes a row-level exclusive lock atomically,
-- so concurrent inserts serialize on that lock; once row_count reaches
-- max_rows, the WHERE clause returns zero rows and we RAISE. This lets the
-- application drop its FOR UPDATE without losing correctness.
CREATE OR REPLACE FUNCTION increment_user_table_row_count()
RETURNS TRIGGER AS $$
DECLARE
updated_count INTEGER;
max_allowed INTEGER;
BEGIN
UPDATE user_table_definitions
SET row_count = row_count + 1,
updated_at = now()
WHERE id = NEW.table_id
AND row_count < max_rows
RETURNING row_count, max_rows INTO updated_count, max_allowed;
IF NOT FOUND THEN
SELECT max_rows INTO max_allowed
FROM user_table_definitions
WHERE id = NEW.table_id;
IF NOT FOUND THEN
RAISE EXCEPTION 'Table % not found', NEW.table_id
USING ERRCODE = 'foreign_key_violation';
END IF;
RAISE EXCEPTION 'Maximum row limit (%) reached for table %',
max_allowed, NEW.table_id
USING ERRCODE = 'check_violation';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
--> statement-breakpoint
-- One-shot reconcile: ensure user_table_definitions.row_count matches the
-- actual count of user_table_rows. Guards against any historical drift before
-- downstream readers (listTables, getTableById) start trusting the column.
UPDATE user_table_definitions d
SET row_count = sub.c
FROM (
SELECT table_id, count(*)::integer AS c
FROM user_table_rows
GROUP BY table_id
) sub
WHERE d.id = sub.table_id
AND d.row_count IS DISTINCT FROM sub.c;
--> statement-breakpoint
-- Tables that exist but have zero rows should report row_count = 0 (not a
-- stale non-zero value left from rows deleted while the trigger was broken).
UPDATE user_table_definitions d
SET row_count = 0
WHERE d.row_count <> 0
AND NOT EXISTS (
SELECT 1 FROM user_table_rows r WHERE r.table_id = d.id
);