PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 22004: query string argument of EXECUTE is null

A NULL reached a place in PL/pgSQL that does not accept NULL: the EXECUTE string, a format() identifier, or a NOT NULL variable.

PostgreSQL 17.12
22004: query string argument of EXECUTE is null

Jump to a cause

  1. 01Dynamic SQL built with || and a NULL value
  2. 02format('%I', NULL) for a table or column name
  3. 03SELECT INTO a NOT NULL variable that finds no row

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
Setuprun as lab_a
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 $$;
OUTPUT
CREATE TABLE
INSERT 0 2
CREATE TABLE
INSERT 0 1
CREATE FUNCTION
Reproducerun as lab_a
SELECT count_rows('sale');
✕ ERROR
ERROR:  22004: query string argument of EXECUTE is null
CONTEXT:  PL/pgSQL function count_rows(text) line 5 at EXECUTE
Checkrun as lab_a
SELECT report, table_name FROM report_config;
OUTPUT
 report | table_name
--------+------------
 sales  | sales_2026
(1 row)
Fixrun as lab_a
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 $$;
OUTPUT
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.

After the fixrun as lab_a
SELECT count_rows('sales');
✓ SUCCESS
 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
Setuprun as lab_a
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 $$;
OUTPUT
CREATE TABLE
INSERT 0 1
CREATE FUNCTION
Reproducerun as lab_a
SELECT set_status('orders', NULL, 'DONE');
✕ ERROR
ERROR:  22004: null values cannot be formatted as an SQL identifier
CONTEXT:  PL/pgSQL function set_status(text,text,text) line 3 at EXECUTE
Checkrun as lab_a
SELECT column_name FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position;
OUTPUT
 column_name
-------------
 id
 status
(2 rows)
After the fixrun as lab_a
SELECT set_status('orders', 'status', 'DONE');
SELECT id, status FROM orders;
✓ SUCCESS
 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
Setuprun as lab_a
CREATE TABLE products (code text PRIMARY KEY, stock int);
INSERT INTO products VALUES ('A100', 5);
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
DO $$
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
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 statement
Checkrun as lab_a
SELECT code, stock FROM products;
OUTPUT
 code | stock
------+-------
 A100 |     5
(1 row)
After the fixrun as lab_a
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 $$;
✓ SUCCESS
NOTICE:  00000: stock=0
DO

FAQ

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