Re: BUG #19767: pg_get_viewdef prints ORDER BY (- '3'::int4) as (- 3), which reparses as column position -3

From: Nitin Motiani <nitinmotiani(at)google(dot)com>
To: alpkeles99(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19767: pg_get_viewdef prints ORDER BY (- '3'::int4) as (- 3), which reparses as column position -3
Date: 2026-10-19 11:29:58
Message-ID: CAH5HC97hx9ZNRdQrj3SL3wRQVn+1dkvDDb4xPd4+LaO=a+HD_g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

On Fri, Oct 16, 2026 at 9:46 PM PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
>
> pg_get_viewdef() prints that term as "(- 3)". When that text is parsed
> again, the grammar folds "- 3" into the integer constant -3 (doNegate),
> which ORDER BY then reads as a column position. So the printed
> definition of a valid view is not a valid view, and pg_dump, which
> prints views the same way, writes a dump that does not restore.
>
> ```
> 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 * FROM o; -- 1, 2
> SELECT * FROM g; -- 2
>

I'm not sure if this kind of use case is common enough to warrant
concern. But I looked into a possible fix. The fix is small where
during deparsing, get_oper_expr checks if the argument to a prefix
operator is a constant and forces adding '::type' to it (so it would
create 3::int4 in this case). It might lead to some extra '::type'
prints in other cases unaffected by the bug but that seems fine to me.

I'm attaching the patch with the fix and a couple of tests. Please take a look.

Thanks & Regards,
Nitin Motiani
Google

Attachment Content-Type Size
v1-0001-Fix-deparsing-of-unary-operators-applied-to-const.patch application/x-patch 6.1 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Narayanan Venkateswaran 2026-10-19 11:33:18 Re: BUG #19737: Empty `JSON_OBJECT` cannot use documented `ON NULL` or unique-key clauses
Previous Message Fujii Masao 2026-10-19 10:09:57 Re: Inconsistency in psql output with \d+x PATTERN