pg_catalog.pg_get_viewdef pretty-print removes important parentheses

From: Duncan Sands <duncan(dot)sands(at)deepbluecap(dot)com>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: pg_catalog.pg_get_viewdef pretty-print removes important parentheses
Date: 2022-12-01 14:12:09
Message-ID: f41566aa-a057-6628-4b7c-b48770ecb84a@deepbluecap.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

When pg_catalog.pg_get_viewdef is called with pretty-print enabled, it removes
parentheses around (DATE + INTERVAL) in the construct "(DATE + INTERVAL) AT TIME
ZONE TZ", outputting SQL that fails to parse. Here is an example, showing that
the SQL output when pretty printing is off does parse, but does not parse when
pretty printing is on.

-- EXAMPLE BEGINS

$ psql
Pager usage is off.
SET
psql (15.1 (Ubuntu 15.1-1.pgdg22.04+1))
Type "help" for help.

duncan=> CREATE VIEW pg_temp.v AS SELECT ('2022-12-01'::date + INTERVAL '1 day')
AT TIME ZONE 'Europe/Paris';
CREATE VIEW
duncan=> SELECT pg_catalog.pg_get_viewdef('pg_temp.v'::regclass, false);
pg_get_viewdef

---------------------------------------------------------------------------------------------------
SELECT (('2022-12-01'::date + '1 day'::interval) AT TIME ZONE
'Europe/Paris'::text) AS timezone;
(1 row)

duncan=> SELECT (('2022-12-01'::date + '1 day'::interval) AT TIME ZONE
'Europe/Paris'::text) AS timezone;
timezone
------------------------
2022-12-02 00:00:00+01
(1 row)

duncan=> SELECT pg_catalog.pg_get_viewdef('pg_temp.v'::regclass, true);
pg_get_viewdef

-------------------------------------------------------------------------------------------------
SELECT ('2022-12-01'::date + '1 day'::interval AT TIME ZONE
'Europe/Paris'::text) AS timezone;
(1 row)

duncan=> SELECT ('2022-12-01'::date + '1 day'::interval AT TIME ZONE
'Europe/Paris'::text) AS timezone;
ERROR: function pg_catalog.timezone(text, interval) does not exist
LINE 1: SELECT ('2022-12-01'::date + '1 day'::interval AT TIME ZONE ...
^
HINT: No function matches the given name and argument types. You might need to
add explicit type casts.

-- EXAMPLE ENDS

This is annoying because psql's \ev pops up the pretty-printed version, and you
have to add the parentheses back in by hand in order to have the view definition
be accepted.

Best wishes, Duncan.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2022-12-01 15:37:05 Re: pg_catalog.pg_get_viewdef pretty-print removes important parentheses
Previous Message David G. Johnston 2022-12-01 13:24:23 Re: BUG #17703: Recursive query early terminate when subquery result of the recursive term is NULL