pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Bug: Interval type cast stripped from function parameter defaults in migration SQL

Open francisdupuis opened this issue 11 months ago • 1 comments

Bug: Interval type cast stripped from function parameter defaults in migration SQL

pgschema version

1.5.1

PostgreSQL version

14.17

Description

When generating migration SQL for functions that have interval type casts in parameter defaults, pgschema strips the ::interval cast, producing invalid SQL that fails with ERROR: operator is not unique: date + unknown.

Steps to reproduce

  1. Create a function with an interval default parameter:
CREATE OR REPLACE FUNCTION test_interval_bug(
    p_tenant_id uuid,
    p_end_date date DEFAULT (CURRENT_DATE + INTERVAL '1 year')
)
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
    -- do nothing
END;
$$;
  1. Create a schema file with the same function (or slightly different to trigger a modification):
-- schema/functions/test_interval_bug.sql
CREATE OR REPLACE FUNCTION test_interval_bug(
    p_tenant_id uuid,
    p_end_date date DEFAULT (CURRENT_DATE + INTERVAL '1 year')
)
RETURNS void
LANGUAGE plpgsql
VOLATILE  -- added to trigger diff
AS $$
BEGIN
    -- do nothing
END;
$$;
  1. Generate a plan:
pgschema plan --host localhost --db mydb --user postgres --file schema/main.sql --output-sql migration.sql
  1. Inspect the generated migration.sql:
-- ACTUAL (incorrect):
p_end_date date DEFAULT (CURRENT_DATE + '1 year'),

-- EXPECTED (correct):
p_end_date date DEFAULT (CURRENT_DATE + INTERVAL '1 year'),
  1. Apply fails:
pgschema apply --host localhost --db mydb --user postgres --file schema/main.sql --auto-approve
# ERROR: operator is not unique: date + unknown (SQLSTATE 42725)

Root cause

When pgschema reads function definitions from pg_proc, PostgreSQL stores the default as (CURRENT_DATE + '1 year'::interval). When serializing this back to SQL, pgschema outputs '1 year' without preserving the ::interval type cast.

PostgreSQL accepts both forms when creating functions:

  • INTERVAL '1 year' (prefix notation)
  • '1 year'::interval (cast notation)

But '1 year' alone (without any interval indication) is ambiguous and causes the operator is not unique error.

Expected behavior

pgschema should preserve interval type information when serializing function parameter defaults, outputting either:

  • INTERVAL '1 year' (prefix notation), or
  • '1 year'::interval (cast notation)

Impact

This bug makes pgschema apply unusable for any codebase containing functions with interval-typed default parameters. The workaround is to apply functions directly with psql -f, bypassing pgschema entirely.

Workaround

Apply function files directly instead of using pgschema apply:

for f in schema/functions/*.sql schema/procedures/*.sql; do
    psql -h localhost -U postgres -d mydb -f "$f"
done

but this unfortunately prevents us from using pgschema in our ci/cd workflow

francisdupuis avatar Dec 27 '25 13:12 francisdupuis

Thanks for the detailed report. Fixed in the head.

tianzhou avatar Dec 29 '25 10:12 tianzhou