PG
PRO
40003ERRORTier 1 — Safe✅ HIGH confidence

statement completion unknown

Category: Transaction RollbackVersions: All Postgres versions

🔴 Production Risk Error

Critical: potential for duplicate writes if the statement committed and is retried without verifying state.

What this means

SQLSTATE 40003 is raised when a statement completes but it is not known whether the statement succeeded or failed — typically because the connection broke at the point of command completion. This is a rare but critical production risk.

Why it happens

  1. 1The connection breaks immediately after a statement executes but before the server can confirm the result to the client

How to reproduce

Connection failure at statement completion.

trigger — this will ERROR
ERROR: statement completion unknown

Fix 1: Query the database to verify whether the statement had an effect

After receiving 40003.

fix

Why this works

Reconnect and query for business-observable evidence of the statement. If the effect is present, do not retry. If absent, retry is safe.

Fix 2: Use idempotent operations to allow safe retries

When designing systems that must tolerate uncertain statement completion.

fix
INSERT INTO orders (id, ...) VALUES (:client_uuid, ...)
ON CONFLICT (id) DO NOTHING;

Why this works

Idempotent inserts with a client-generated primary key allow safe retries regardless of whether the original statement committed.

What not to do

Retry blindly after 40003

Why it's wrong: If the original statement committed, a retry causes duplicate data.

Dangerous variant

⚠️ Warning

40003 on a write operation — verify state before retrying

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 40 — Transaction Rollback

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for uncertain statement completion. Analogous to 08007 for full transaction commits.

See also

📄 Reference pages

Idempotent OperationsConnection Handling
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →