PostgreSQL Error 38001: SQL Not Permitted Fix
In this article
PostgreSQL error 38001 means a function you are calling is declared with EXECUTE ... AS restrictions that forbid the SQL statement you passed to it. The fix is to stop passing arbitrary SQL through that function and instead call the underlying operation directly, or to have the function's owner change its security context. This error does not come from the SQL you typed in your client; it comes from inside a function or procedure that is executing your SQL on your behalf. You will need the name of the function in the error message, access to the database as the function owner (or a superuser), and a way to inspect the function definition.
What You'll Need
- The full error text, including the function name and line number PostgreSQL reports.
- A connection to the database with permission to read
pg_proc(any normal role can usually do this). - For the permanent fix: the function owner's role or a superuser account, because changing a function's security attributes requires ownership.
- A SQL client such as psql or a GUI tool that shows the full error detail.
Step-by-Step
- Read the whole error message. Error 38001 is usually reported as
ERROR: 38001: SQL not permitted in a function with EXECUTE ... ASor similar, and it names the function and the line inside it. Note the function name and schema. If your client truncates the message, run the failing statement in psql so you see the full detail and theCONTEXTline. - Confirm which function is at fault. The error is raised by the function, not by your top-level statement. If you called a wrapper function, the failing function may be one level down. Check the
CONTEXTlines in the error output to see the call stack. - Inspect the function definition. Run
SELECT pg_get_functiondef('schema.function_name'::regproc);(or::regprocedureif it is overloaded) to see the source. Look for a clause likeEXECUTE ... ASon theCREATE FUNCTIONstatement. That clause tells PostgreSQL which SQL statements the function is allowed to run. If your statement is not on the permitted list, the error is expected. - Stop passing SQL through the function. The most common cause is calling a function that is designed to run only a fixed set of statements, such as an extension helper or a security-definer wrapper, with your own SQL string. Call the underlying operation directly instead. For example, if a wrapper function is meant to run only
SELECTstatements and you passed anUPDATE, run theUPDATEyourself with the appropriate privileges. - Check whether the function is security definer. Run
SELECT prosecdef FROM pg_proc WHERE proname = 'function_name';. If it returns true, the function runs with the owner's privileges, and theEXECUTE ... ASrestriction is a deliberate guard. Do not try to work around it by granting yourself more privileges; fix the call instead. - If you own the function, adjust the permitted statements. As the owner, recreate or alter the function so the
EXECUTE ... ASlist includes the statement type you need, or remove the restriction if that is appropriate for your security model. UseCREATE OR REPLACE FUNCTIONwith the corrected clause. Test in a development database first, because loosening this restriction can widen what callers can do through the function. - If you do not own the function, contact the owner. Only the owner or a superuser can change the function's definition. Send them the exact error, the function name, and the statement you were trying to run. Ask whether the function is meant to support that operation or whether you should use a different API.
- Re-run your statement and verify. After the call is changed or the function is updated, run the original statement again. If the error persists, return to step 1: a different function in the call stack may be raising it.
Common Mistakes
- Blaming your own SQL. Error 38001 is raised inside a function. Rewriting your top-level query will not help until you find the function that is rejecting it.
- Granting more privileges to the caller. The restriction is enforced by the function's
EXECUTE ... ASclause, not by table grants. Adding privileges does not bypass it and may be a security problem. - Editing a function you do not own.
CREATE OR REPLACE FUNCTIONon someone else's function fails with a permission error. Confirm ownership first. - Removing the restriction without understanding it. The
EXECUTE ... ASclause exists to limit what a security-definer function can do. Removing it can turn a safe helper into a privilege escalation path. - Ignoring overloads. If the function name is overloaded,
pg_get_functiondefneeds the full argument signature, or you may inspect the wrong version.
Tips
- Use
\errverbosein psql after an error to print the full message with all fields, including the function and line number. - Search the function source for
EXECUTEandAStogether; the restriction is easy to miss in a long definition. - Keep a development copy of any function you plan to alter, so you can compare behaviour before and after.
- If the function comes from an extension, do not edit it directly. Check the extension's documentation for the supported way to run the operation you need.
- When reporting the problem to a colleague or the function owner, include the exact statement, the full error text, and the output of
pg_get_functiondef.
Troubleshooting
If the error still appears after you change your call, the function raising it may be nested deeper than you think. Re-read the CONTEXT lines and inspect each function in the stack. If pg_get_functiondef returns nothing, the function name or schema is wrong, or you lack permission to see it; try SELECT proname, pronamespace::regnamespace FROM pg_proc WHERE proname ILIKE '%name%'; to find the exact identifier. If you cannot alter the function because you are not the owner, the only correct path is to ask the owner or use a different interface. If the function is part of an extension, reinstalling or upgrading the extension is not a fix for 38001; the restriction is intentional, and the supported answer is to call the documented operation instead. If you are a superuser and decide to change the function, test the change on a copy of the database first and confirm that the new permitted statement list is no broader than necessary.
Frequently Asked Questions
What does PostgreSQL error 38001 mean exactly?
It means a function declared with an EXECUTE ... AS clause was asked to run a SQL statement that is not on its permitted list. The function refuses the statement rather than running it.
Is error 38001 caused by my query?
Not directly. Your query triggers the function call, but the rejection happens inside the function. The fix is in how the function is called or how it is defined.
Can I fix it by granting more permissions?
No. The restriction is part of the function definition, not a table or schema privilege. Only the function owner or a superuser can change it.
Why does the error name a function I did not call?
You probably called a wrapper function that calls the restricted function internally. The CONTEXT lines in the error output show the call stack.
Should I remove the EXECUTE ... AS clause?
Only if you own the function and understand the security implications. The clause limits what a security-definer function can do, so removing it can widen access.
Does this error occur in all PostgreSQL versions?
The EXECUTE ... AS restriction and its error code are long-standing PostgreSQL behaviour. Exact message wording can differ slightly between versions, but the cause and fix are the same.