pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

apply fails on mutually recursive SQL functions while check_function_bodies is on

Open Goldziher opened this issue 1 month ago • 0 comments

Summary

pgschema apply fails on a schema containing mutually recursive SQL-language functions, because the generated plan creates them one after another while check_function_bodies is on, and no order can satisfy both bodies.

Version

pgschema 1.13.3, PostgreSQL 18.

Repro

CREATE FUNCTION a(n int) RETURNS int LANGUAGE sql AS $$ SELECT CASE WHEN n <= 0 THEN 0 ELSE b(n - 1) END $$;
CREATE FUNCTION b(n int) RETURNS int LANGUAGE sql AS $$ SELECT CASE WHEN n <= 0 THEN 1 ELSE a(n - 1) END $$;

pgschema apply --file schema.sql --auto-approve against an empty PostgreSQL 18 database:

ERROR: failed to execute concatenated statements in group 1: ERROR: function b(integer) does not exist (SQLSTATE 42883)

With PGOPTIONS='-c check_function_bodies=off' the same apply succeeds and SELECT a(3) returns 1.

Expected

The plan applies. pg_dump handles this by emitting SET check_function_bodies = false; before creating functions. pgschema could do the same within the apply session (and optionally re-validate bodies afterwards), or create such cycles with a placeholder body first and replace it.

Workaround

PGOPTIONS='-c check_function_bodies=off' pgschema apply ..., then re-run CREATE OR REPLACE for the affected functions (or any check that executes them) to validate the bodies.

Related: #246, #569, #624.

Goldziher avatar Oct 27 '26 14:10 Goldziher