2F002ERRORTier 2 — Caution✅ HIGH confidencemodifying SQL data not permitted
Category: SQL Routine ExceptionVersions: All Postgres versions
What this means
SQLSTATE 2F002 is raised when a SQL function declared as CONTAINS SQL or READS SQL DATA attempts to modify data (INSERT, UPDATE, DELETE), which is not permitted by its declared data access category.
Why it happens
- 1A SQL function with CONTAINS SQL or READS SQL DATA attribute attempts to execute a DML statement that modifies data
How to reproduce
SQL function with READS SQL DATA attempting a write.
trigger — this will ERROR
CREATE FUNCTION read_only_fn() RETURNS VOID
LANGUAGE SQL
READS SQL DATA
AS $ INSERT INTO log (msg) VALUES ('test'); $; -- modifies dataERROR: modifying SQL-data not permitted
Fix 1: Change the function to MODIFIES SQL DATA if data modification is intentional
When the function should be allowed to write data.
fix
CREATE OR REPLACE FUNCTION my_fn() RETURNS VOID
LANGUAGE SQL
MODIFIES SQL DATA
AS $ INSERT INTO log (msg) VALUES ('test'); $;Why this works
MODIFIES SQL DATA declares that the function may change data, allowing DML statements.
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
📄 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 →