pg_get_viewdef/pg_dump print a prefix operator on a constant without the constant's cast, so ORDER BY/GROUP BY (- '3'::int4) becomes (- 3), which reparses as a select-list position: the view can't be recreated or silently changes meaning ============================================================================================================================================================================================================================================= AI-generated bug report from the AI Annex Report ID: 01a22dd7-7850-73b1-ab53-6bee871e819b Filed: 2026-11-30 19:03:45 UTC Severity: 30/100 Blamed commit: 3e21ecbbe6b3a9eb4b1f8373b006d21b9634be65 Reported on pgsql-bugs as BUG #19767 (against 18.6; the thread also reports 19rc1, 14.24 and master). pg_get_viewdef() (and therefore pg_dump) prints a prefix operator applied to a constant without the constant's cast. `ORDER BY (- '3'::int4)` and `GROUP BY (- '3'::int4)` are printed as `(- 3)`. When that text is parsed again, the grammar folds `- 3` into the integer constant -3, which ORDER BY, GROUP BY and DISTINCT ON read as a select-list position. The view therefore cannot be recreated from its own definition, and where the folded value is a valid position the recreated view silently changes meaning. CREATE TABLE t (a int); INSERT INTO t VALUES (1), (2); CREATE VIEW o AS SELECT a FROM t ORDER BY (- '3'::int4); CREATE VIEW g AS SELECT count(*) AS n FROM t GROUP BY (- '3'::int4); SELECT pg_get_viewdef('o'::regclass); -- ... ORDER BY (- 3) SELECT pg_get_viewdef('g'::regclass); -- ... GROUP BY (- 3) CREATE VIEW o2 AS SELECT a FROM t ORDER BY (- 3); -- ERROR: ORDER BY position -3 is not in select list CREATE VIEW g2 AS SELECT count(*) AS n FROM t GROUP BY (- 3); -- ERROR: GROUP BY position -3 is not in select list CREATE TABLE t2 (a int, b int); INSERT INTO t2 VALUES (1, 10), (2, 20), (1, 30); CREATE VIEW dv AS SELECT DISTINCT ON (- (- '1'::int4)) a, b FROM t2 ORDER BY - (- '1'::int4), b DESC; SELECT * FROM dv; -- one row: (1, 30) SELECT pg_get_viewdef('dv'::regclass); -- DISTINCT ON ((- (- 1))) ... ORDER BY (- (- 1)), b DESC -- a view recreated from that text returns (1,30) and (2,20) A pg_dump -s of the database restored into an empty database fails with the two position errors, followed by `relation "public.o" does not exist` and `relation "public.g" does not exist`. Cause: get_oper_expr() prints a prefix operator as `(op arg)`, and get_const_expr() prints a non-negative int4 (or float8) constant without a type label. The grammar's doNegate() then folds the negated literal into one constant, and ORDER BY/GROUP BY/DISTINCT ON treat a bare integer constant as a target-list position. A bare top-level constant in these clauses is already printed as a position number, but a constant nested under an operator is not covered. Blame is 3e21ecbbe6b3a9eb4b1f8373b006d21b9634be65 ("Make the rule deparser a little less quote-happy", 1999-10-04), which made get_const_expr() print int4 and float8 values unquoted and unlabeled. Before it, the operand printed as `'3'::"int4"` and round-tripped. This was found by reading the code, because trees that old don't build with current toolchains; the oldest tree built and run, 8097d189ccc2cd55a8bf189bd21cb196e3cfc385 (2017-12-05), reproduces. The ORDER BY form also needs 05e3d0ee8666b74f11ffad16f46e372459d6e53e (2000-10-05), which first deparsed ORDER BY items. A patch, "Fix deparsing of unary operators applied to constants" (v2, Commitfest entry 7500), is in the thread. It is not committed on any branch as of master ce10389d6378c98875fb5b3c8160085c83b64716 and was not tested here. Branches: REL_14 through master (reproduced on the master and REL_19 through REL_14 tips).