Skip to content

Commit 06ae362

Browse files
dahliaclaude
andcommitted
Ignore duplicate_table error in concurrent init
When multiple PostgresMessageQueue instances share the same table name but use different Sql connections (e.g., mq1 and mq2 in tests), their concurrent CREATE TABLE IF NOT EXISTS statements can race. Despite the IF NOT EXISTS clause, PostgreSQL can raise error 42P07 (duplicate_table) under concurrent DDL due to a known race condition in catalog lookups. Previously, only the pg_type_typname_nsp_index constraint violation was suppressed. Now the 42P07 error code is also ignored, since it simply means the table already exists — exactly the condition that IF NOT EXISTS is meant to handle. This surfaced in CI as an uncaught promise rejection when two listeners started concurrently on separate connections. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 22a610d commit 06ae362

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

  • packages/postgres/src

packages/postgres/src/mq.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,13 @@ export class PostgresMessageQueue implements MessageQueue {
361361
} catch (error) {
362362
if (
363363
!(error instanceof postgres.PostgresError &&
364-
error.constraint_name === "pg_type_typname_nsp_index")
364+
(error.constraint_name === "pg_type_typname_nsp_index" ||
365+
// When multiple PostgresMessageQueue instances sharing the same
366+
// table name initialize concurrently (e.g., mq1 and mq2 in
367+
// tests), the concurrent CREATE TABLE IF NOT EXISTS statements
368+
// can race and one may fail with 42P07 (duplicate_table).
369+
// This is safe to ignore because the table already exists.
370+
error.code === "42P07"))
365371
) {
366372
logger.error("Failed to initialize the message queue table: {error}", {
367373
error,

0 commit comments

Comments
 (0)