pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

SQL function created before `ADD COLUMN` it depends on (42703 on apply)

Open martinbaste opened this issue 1 month ago • 0 comments

Version: v1.13.1 (also v1.13.0)

When one apply both adds a column to an existing table and creates a LANGUAGE sql function whose body reads that column, the plan emits CREATE FUNCTION before ALTER TABLE ... ADD COLUMN. Postgres validates SQL function bodies at creation (check_function_bodies is on by default), so the apply fails.

Current state:

CREATE TABLE entry (id int PRIMARY KEY);

Desired state:

CREATE TABLE entry (
    id int PRIMARY KEY,
    request_id int
);

CREATE FUNCTION open_requests() RETURNS bigint LANGUAGE sql STABLE AS $$
    SELECT count(*) FROM entry WHERE entry.request_id IS NOT NULL;
$$;

Actual:

Error: failed to execute concatenated statements in group 1: ERROR: column entry.request_id does not exist (SQLSTATE 42703)

Expected: ALTER TABLE entry ADD COLUMN request_id runs before CREATE FUNCTION open_requests().

Applying to an empty database works, because the table is created with the column, so this only appears when upgrading an existing database. It looks like the function-body counterpart of #414 (the same problem for views), and related to the ordering fixes in #480, #530 and #616. We're working around it with check_function_bodies=off for the apply session.

martinbaste avatar Oct 06 '26 12:10 martinbaste