PG
PRO
38004ERRORTier 2 — Caution✅ HIGH confidence

reading SQL data not permitted

Category: External Routine ExceptionVersions: All Postgres versions

What this means

SQLSTATE 38004 is raised when an external routine declared as NO SQL attempts to read data with a SELECT statement.

Why it happens

  1. 1An external function declared NO SQL executes a SELECT or other read statement

How to reproduce

NO SQL external function executing a SELECT.

trigger — this will ERROR
ERROR: reading SQL-data not permitted

Fix 1: Change the function attribute to READS SQL DATA

When the function needs to read data.

fix
CREATE FUNCTION my_fn() RETURNS INT
AS 'my_ext', 'my_fn'
LANGUAGE C
READS SQL DATA;

Why this works

READS SQL DATA permits SELECT statements in the function.

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. Stable across versions.

See also

📄 Reference pages

External Language FunctionsCREATE FUNCTION
⚙️ 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 →