PG
PRO
38001ERRORTier 2 — Caution✅ HIGH confidence

containing SQL not permitted

Category: External Routine ExceptionVersions: All Postgres versions

What this means

SQLSTATE 38001 is raised when an external routine (e.g., a C function) attempts to execute SQL in a context where SQL execution is not permitted.

Why it happens

  1. 1An external language function attempts to execute SQL when it is not allowed to do so in the current calling context

How to reproduce

External function executing SQL in a forbidden context.

trigger — this will ERROR
ERROR: containing SQL not permitted

Fix 1: Restructure the function to execute SQL in a permitted context

When this error appears from an external routine.

fix

Why this works

Review where the function is called from and whether SQL execution is permitted in that context. Functions called during certain operator or aggregate processing may not be allowed to execute SQL.

Sources

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

🔧 Source ref: Class 38 — External Routine Exception

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE. Rarely encountered in practice.

See also

📄 Reference pages

C ExtensionsExternal Language Functions
⚙️ 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 →