fix(postgres): lock keys follow D3.7/D4.3; event reader never sees a later id before an earlier one

This commit is contained in:
sam committed 2026-09-24 03:23:38 +10:00
1 parent 5cbc9f9d6c
commit 8b006b35aa
4 files changed
+14 -4

No files matched your search

@@ -56,6 +56,9 @@ func TestEventInsertsCommitInIDOrder(t *testing.T) {
time.Sleep(10 * time.Millisecond)
}
if visible, err := st.EventsSince(t.Context(), "", 10); err != nil || len(visible) != 0 {
t.Fatalf("EventsSince while the first insert is uncommitted = %d events, %v; want none", len(visible), err)
}
if err := tx.Commit(t.Context()); err != nil {
t.Fatalf("Commit: %v", err)
}
@@ -66,4 +69,11 @@ func TestEventInsertsCommitInIDOrder(t *testing.T) {
if r.id <= firstID {
t.Fatalf("second id %d, want greater than first id %d", r.id, firstID)
}
all, err := st.EventsSince(t.Context(), "", 10)
if err != nil {
t.Fatalf("EventsSince: %v", err)
}
if len(all) != 2 || all[0].Kind != "first" || all[1].Kind != "second" {
t.Fatalf("EventsSince after both commit = %+v, want first then second", all)
}
}
@@ -31,7 +31,7 @@ func AcquireOwnership(ctx context.Context, pool *pgxpool.Pool, name string) (*Ow
return nil, fmt.Errorf("postgres: ownership %s: %w", name, err)
}
var taken bool
if err := conn.QueryRow(ctx, "SELECT pg_try_advisory_lock(hashtextextended('archie.ownership.' || $1, 0))", name).Scan(&taken); err != nil {
if err := conn.QueryRow(ctx, "SELECT pg_try_advisory_lock(hashtextextended('archie.' || $1 || '.owner', 0))", name).Scan(&taken); err != nil {
conn.Release()
return nil, fmt.Errorf("postgres: ownership %s: %w", name, err)
}
@@ -51,7 +51,7 @@ func (o *Ownership) Release(ctx context.Context) error {
}
conn := o.conn
o.conn = nil
if _, err := conn.Exec(ctx, "SELECT pg_advisory_unlock(hashtextextended('archie.ownership.' || $1, 0))", o.name); err != nil {
if _, err := conn.Exec(ctx, "SELECT pg_advisory_unlock(hashtextextended('archie.' || $1 || '.owner', 0))", o.name); err != nil {
return errors.Join(fmt.Errorf("postgres: release ownership %s: %w", o.name, err), conn.Hijack().Close(ctx))
}
conn.Release()
@@ -13,7 +13,7 @@ import (
const insertEvent = `-- name: InsertEvent :one
WITH append_lock AS (
SELECT pg_advisory_xact_lock(hashtextextended('archie.events.append', 0))
SELECT pg_advisory_xact_lock(hashtextextended('archie.events.insert', 0))
)
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
@@ -9,7 +9,7 @@
-- the order they were drawn. The lock is inside the statement so a standalone
-- insert and one inside a longer transaction both take it.
WITH append_lock AS (
SELECT pg_advisory_xact_lock(hashtextextended('archie.events.append', 0))
SELECT pg_advisory_xact_lock(hashtextextended('archie.events.insert', 0))
)
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13