40003
PostgreSQLERRORCommonTransaction RollbackHIGH confidence

statement completion unknown

Production Risk

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.

expected output

ERROR:  statement completion unknown

Fix 1

Query the database to verify whether the statement had an effect

WHEN After receiving 40003.

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 When designing systems that must tolerate uncertain statement completion.

Use idempotent operations to allow safe retries
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

If the original statement committed, a retry causes duplicate data.

Sources
Official documentation ↗

Class 40 — Transaction Rollback

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors