Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
SQLSTATE 22004 (null_value_not_allowed) mostly comes from PL/pgSQL. The message depends on where the NULL arrived: query string argument of EXECUTE is null, null values cannot be formatted as an SQL identifier, or null value cannot be assigned to variable "v_stock" declared NOT NULL. The CONTEXT line shows the function and line number.
Below are three cases reproduced on PostgreSQL 17 and the fixes that were run.
01Dynamic SQL built with || and a NULL value
count_rows() read a table name from report_config and ran EXECUTE 'SELECT count(*) FROM ' || v_tab. It was called with 'sale', but the config only has sales. SELECT ... INTO found no row and left v_tab NULL; '...' || NULL is NULL, so EXECUTE failed at line 5.
The function was rewritten with SELECT ... INTO STRICT (no row raises an error instead of continuing with NULL) and quote_ident(v_tab). Called with 'sales', it returned 2.
Setup
CREATE TABLE sales_2026 (id int, amount int);
INSERT INTO sales_2026 VALUES (1, 100), (2, 250);
CREATE TABLE report_config (report text, table_name text);
INSERT INTO report_config VALUES ('sales', 'sales_2026');
CREATE FUNCTION count_rows(p_report text) RETURNS bigint LANGUAGE plpgsql AS $$
DECLARE v_tab text; v_cnt bigint;
BEGIN
SELECT table_name INTO v_tab FROM report_config WHERE report = p_report;
EXECUTE 'SELECT count(*) FROM ' || v_tab INTO v_cnt;
RETURN v_cnt;
END $$;CREATE TABLE
INSERT 0 2
CREATE TABLE
INSERT 0 1
CREATE FUNCTIONSELECT count_rows('sale');ERROR: 22004: query string argument of EXECUTE is null
CONTEXT: PL/pgSQL function count_rows(text) line 5 at EXECUTESELECT report, table_name FROM report_config; report | table_name
--------+------------
sales | sales_2026
(1 row)CREATE OR REPLACE FUNCTION count_rows(p_report text) RETURNS bigint LANGUAGE plpgsql AS $$
DECLARE v_tab text; v_cnt bigint;
BEGIN
SELECT table_name INTO STRICT v_tab FROM report_config WHERE report = p_report;
EXECUTE 'SELECT count(*) FROM ' || quote_ident(v_tab) INTO v_cnt;
RETURN v_cnt;
EXCEPTION WHEN no_data_found THEN
RAISE EXCEPTION 'unknown report: %', p_report;
END $$;CREATE FUNCTION|| turns the whole string into NULL if any part is NULL. Check the value before EXECUTE, or make the lookup fail loudly with INTO STRICT.
SELECT count_rows('sales'); count_rows
------------
2
(1 row)02format('%I', NULL) for a table or column name
set_status() built format('UPDATE %I SET %I = %L', ...). It was called with NULL as the column name, and format() stopped with null values cannot be formatted as an SQL identifier.
%L accepts NULL (it writes NULL), but %I does not. The column list showed status; calling set_status('orders', 'status', 'DONE') updated the row to DONE.
Setup
CREATE TABLE orders (id int, status text);
INSERT INTO orders VALUES (1, 'NEW');
CREATE FUNCTION set_status(p_table text, p_col text, p_val text) RETURNS void LANGUAGE plpgsql AS $$
BEGIN
EXECUTE format('UPDATE %I SET %I = %L', p_table, p_col, p_val);
END $$;CREATE TABLE
INSERT 0 1
CREATE FUNCTIONSELECT set_status('orders', NULL, 'DONE');ERROR: 22004: null values cannot be formatted as an SQL identifier
CONTEXT: PL/pgSQL function set_status(text,text,text) line 3 at EXECUTESELECT column_name FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position; column_name
-------------
id
status
(2 rows)SELECT set_status('orders', 'status', 'DONE');
SELECT id, status FROM orders; set_status
------------
(1 row)
id | status
----+--------
1 | DONE
(1 row)03SELECT INTO a NOT NULL variable that finds no row
v_stock int NOT NULL := 0 received SELECT stock INTO v_stock ... WHERE code = 'B200'. There is no B200 product (only A100), so the SELECT put NULL into the variable and it failed with 22004.
With SELECT coalesce(max(stock), 0) INTO v_stock, the query always returns one non-NULL value. The block finished with NOTICE: stock=0.
Setup
CREATE TABLE products (code text PRIMARY KEY, stock int);
INSERT INTO products VALUES ('A100', 5);CREATE TABLE
INSERT 0 1DO $$
DECLARE v_stock int NOT NULL := 0;
BEGIN
SELECT stock INTO v_stock FROM products WHERE code = 'B200';
RAISE NOTICE 'stock=%', v_stock;
END $$;ERROR: 22004: null value cannot be assigned to variable "v_stock" declared NOT NULL
CONTEXT: PL/pgSQL function inline_code_block line 4 at SQL statementSELECT code, stock FROM products; code | stock
------+-------
A100 | 5
(1 row)DO $$
DECLARE v_stock int NOT NULL := 0;
BEGIN
SELECT coalesce(max(stock), 0) INTO v_stock FROM products WHERE code = 'B200';
RAISE NOTICE 'stock=%', v_stock;
END $$;NOTICE: 00000: stock=0
DOFAQ
What does query string argument of EXECUTE is null mean?
The string given to EXECUTE in PL/pgSQL is NULL, usually because one piece concatenated with || was NULL. Look at the line in CONTEXT and check each variable used to build the string.
Why does format %I fail with null values cannot be formatted as an SQL identifier?
%I (identifier) does not accept NULL. %L (literal) turns NULL into the word NULL, and %s turns it into an empty string. Pass a real table or column name.
Does SELECT INTO with no rows raise an error in PL/pgSQL?
Not by default: the variable becomes NULL. With a NOT NULL variable that NULL raises 22004; with INTO STRICT no row raises an error instead.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12