2F004ERRORTier 2 — Caution✅ HIGH confidencereading SQL data not permitted
Category: SQL Routine ExceptionVersions: All Postgres versions
What this means
SQLSTATE 2F004 is raised when a SQL function declared as NO SQL or CONTAINS SQL (without read data permission) attempts to execute a SELECT or other read statement.
Why it happens
- 1A SQL function with NO SQL or inappropriate data access attribute attempts to read data with a SELECT
How to reproduce
Function with NO SQL attempting a SELECT.
trigger — this will ERROR
ERROR: reading SQL-data not permitted
Fix 1: Change the function attribute to READS SQL DATA or CONTAINS SQL
When the function needs to read data.
fix
CREATE OR REPLACE FUNCTION my_fn() RETURNS INT
LANGUAGE SQL
READS SQL DATA
AS $ SELECT COUNT(*) FROM orders; $;Why this works
READS SQL DATA permits SELECT statements in the function body.
Sources
📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html
🔧 Source ref: Class 2F — SQL Routine Exception
Confidence assessment
✅ HIGH confidence
Standard SQLSTATE. Stable across versions.
See also
🔗 Related errors
📄 Reference pages
CREATE FUNCTIONSQL 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 →