PostgreSQL Error 38003: Prohibited SQL Statement Fix
In this article
PostgreSQL error 38003 means the server rejected your statement because it is not allowed in the current context—most often a transaction that has been marked for rollback, a read-only standby, a restricted function, or a security policy. The fix is to identify which restriction applies, then change the statement or the session so the operation is permitted.
What You'll Need
- Access to the PostgreSQL server as a user with permission to read logs and inspect settings (typically a superuser or a role with
pg_read_all_settings). - The exact SQL statement that produced the error.
- The application or client code that issued it, so you can see whether it runs inside an explicit transaction.
- Access to the server log or
pg_stat_activityto confirm the session state.
Step-by-Step
- Reproduce the error and capture the full message. Run the statement in
psqlor your client and copy the entire error text, including the SQLSTATE38003and any detail or hint lines. The hint often names the restriction directly. - Check whether you are inside a failed transaction. In
psql, runSELECT txid_current_if_assigned();and look at the prompt: if it shows!or the session reports “current transaction is aborted”, every statement exceptROLLBACKwill fail. IssueROLLBACK;and retry the statement. In application code, make sure the transaction is rolled back before reusing the connection. - Check the server's read-only state. Run
SHOW transaction_read_only;andSELECT pg_is_in_recovery();. If the first returnsonor the second returnst, you are connected to a standby or a read-only session. Send writes to the primary server, or ask the administrator to promote the standby if that is intended. - Inspect the session's default transaction mode. Run
SHOW default_transaction_read_only;. If it ison, the role or database is configured for read-only work. A superuser can change it withALTER ROLE your_role SET default_transaction_read_only = off;orALTER DATABASE your_db SET default_transaction_read_only = off;. For a single session, useSET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE;. - Check for a security policy or restricted function. If the statement runs inside a
SECURITY DEFINERfunction or a row-level security policy, the function may be declared withSET transaction_read_only = onor may execute with reduced privileges. Review the function definition with\df+ function_nameinpsqland confirm the owner andproconfigsettings. Remove the read-only setting only if the function is meant to write. - Confirm the statement type is allowed. Some statements are prohibited in specific contexts regardless of settings—for example,
VACUUMcannot run inside a transaction block, andCREATE INDEX CONCURRENTLYcannot run inside one either. If the error appears for one of these, run it outside any explicit transaction (autocommit mode) and retry. - Review connection pooler settings. If you connect through PgBouncer or another pooler in transaction mode, a session may inherit a read-only state from a previous borrower. Set
server_reset_queryor the pooler's reset behavior so sessions return to read-write, or use session pooling for statements that must write. - Retry and verify. After making the change, reconnect if needed, run the statement again, and confirm with
SELECT txid_current();that a new transaction ID is assigned for writes. Check the server log for any remaining 38003 entries.
Common Mistakes
- Ignoring the aborted-transaction state and repeatedly resending the statement; the connection must be rolled back first.
- Changing
default_transaction_read_onlyglobally when only one role or database needs it, which weakens read-only protection elsewhere. - Assuming the error is a syntax problem; 38003 is a permission/context error, not a parser error.
- Running
VACUUMorCREATE INDEX CONCURRENTLYinside a transaction block and expecting it to work. - Forgetting that a pooler can hand a session to the next client with the previous client's transaction state.
Tips
- Use
\set ON_ERROR_STOP onin scripts so a failed statement stops execution instead of cascading. - Log the SQLSTATE in application error handlers; 38003 is distinct from 25006 (read-only SQL transaction) and 42501 (insufficient privilege), and each needs a different response.
- Keep write workloads on the primary and route reporting queries to a standby deliberately.
- When testing, wrap statements in an explicit
BEGIN; ... ROLLBACK;block to see whether the error is transaction-related.
Troubleshooting
If the error persists after a rollback, check pg_stat_activity for the session's state and backend_xid; a session stuck in idle in transaction (aborted) will reject all statements until it is terminated or rolled back. If you are on a managed service, confirm whether the instance is a read replica and whether failover has occurred. If the statement runs from a function, test the same statement directly in a read-write session to isolate whether the function's configuration is the cause. If the error appears only through an application, inspect the connection string for options=-c default_transaction_read_only=on or a similar parameter. When none of these apply, capture the full error with VERBOSITY verbose in psql and review the server log around the timestamp; the log line usually names the rule or setting that blocked the statement.
Frequently Asked Questions
Is error 38003 the same as a read-only transaction error?
No. 38003 is a general “prohibited SQL statement” condition; a read-only transaction typically raises SQLSTATE 25006. The two can appear together when a write is attempted on a standby, so check both codes.
Can I fix 38003 without superuser rights?
Sometimes. Rolling back an aborted transaction and running the statement outside a transaction block need no special rights. Changing role or database defaults, or altering a function's configuration, requires ownership or superuser privileges.
Why does the error appear only in my application, not in psql?
The application is likely reusing a connection that is in an aborted transaction or has a read-only session setting applied by the driver or pooler. Check the connection initialization and reset behavior.
Does this error mean my data is corrupted?
No. It is a statement-context rejection, not a storage or consistency error. The data is untouched because the statement did not execute.
Can a standby be made writable?
A standby in recovery is read-only by design. Writes must go to the primary; promoting the standby is an administrative action that changes the cluster's role and should be done deliberately.