[PATCH v1] Preserve implicit composite casts in ROW deparsing

Поиск
Список
Период
Сортировка
Искать

[PATCH v1] Preserve implicit composite casts in ROW deparsing

От:
Sho Ito <i.sho.0628@gmail.com>
Дата:
Вложения:
Hi hackers,

An implicitly coerced composite argument can lose its type when a stored
expression is deparsed. After another function overload is added,
recreating a view can fail with an ambiguous-function error or silently
call a different function.

For example, in a fresh database:

```sql
CREATE TYPE pair AS (a int, b int);
CREATE FUNCTION pick(pair) RETURNS int
  LANGUAGE sql AS 'SELECT 1';
CREATE VIEW v AS SELECT pick(ROW(1, 2)) AS r;

CREATE FUNCTION pick(record) RETURNS int
  LANGUAGE plpgsql AS 'BEGIN RETURN 2; END';

SELECT r FROM v; -- 1
SELECT 'CREATE VIEW v_copy AS ' || pg_get_viewdef('v') \gexec
SELECT r FROM v_copy; -- 2 before the fix, 1 after
```

The stored call correctly refers to `pick(pair)`, but `pg_get_viewdef()`
prints `pick(ROW(1, 2))` without `::pair`. On reparse, the `record`
overload wins.

`get_func_expr()` requests `showimplicit = true` for its arguments, but
the `RowExpr` printer did not honour that flag for implicitly coerced
named composites. The attached v1 prints the named composite type when
`showimplicit` is true, leaving anonymous `record` output unchanged.

The patch also updates existing `EXPLAIN` expectations that now show
named-type casts.

I think this merits backpatching because it can silently change results
after restore. I have only tested master so far, but I'm happy to help
prepare and test back-branch versions if appropriate.

Regards,
Sho

--
=======================================
Sho Ito (Sean)

=======================================

Re: [PATCH v1] Preserve implicit composite casts in ROW deparsing

От:
Henson Choi <assam258@gmail.com>
Дата:
Hi Sho,

> The stored call correctly refers to `pick(pair)`, but `pg_get_viewdef()`
> prints `pick(ROW(1, 2))` without `::pair`. On reparse, the `record`
> overload wins.

Thanks for the patch.  It reproduces on master: the copied view
returns 2.  With v1 it prints pick(ROW(1, 2)::pair) and returns 1,
and make check and the pg_upgrade test pass.  Other implicit
coercions of function arguments are printed under showimplicit too,
so this looks like the right place for the fix to me; RowExpr was
the only one ignoring that flag.

I used AI to generate a range of cases, and the fix reaches further
than the examples in your mail.  Named arguments, operators, aggregate
and window function arguments, GROUP BY and HAVING, whole-row
references to a subquery passed as a composite argument, BEGIN ATOMIC
bodies, and index and CHECK expressions all change results or fail to
restore on master, and are all right with v1.  The strongest case is
VARIADIC, where a dump from master does not restore even without
adding an overload later:

  CREATE TYPE pair AS (a int, b int);
  CREATE FUNCTION pv(VARIADIC pair[]) RETURNS int
    LANGUAGE sql AS 'SELECT 1';
  CREATE VIEW vv AS SELECT pv(ROW(1, 2), ROW(3, 4)) AS r;
  -- master: pv(VARIADIC ARRAY[ROW(1, 2), ROW(3, 4)]) AS r,
  --         restore fails: function pv(record[]) does not exist
  -- v1:     pv(VARIADIC ARRAY[ROW(1, 2)::pair, ROW(3, 4)::pair]) AS r

It may be worth a test.

> The patch also updates existing `EXPLAIN` expectations that now show
> named-type casts.

The EXPLAIN hunks in the patch come from argument positions:

  - the two in alter_table from get_func_expr(), which prints
    function arguments with showimplicit = true
  - the one in updatable_views from get_oper_expr(), which does the
    same for operator arguments

To see whether they could be avoided, I passed false at those two
places only while deparsing a plan.  The patch's hunks go away, but
the other implicit casts there are hidden too, so other plans change:

  - function arguments: 19 lines in six other core regression files
    and 3 in postgres_fdw, e.g. sin((two)::double precision) becomes
    sin(two)
  - operator arguments: 152 lines in 15 files and 13 in postgres_fdw,
    e.g. (tenthous)::numeric = '1'::numeric becomes
    tenthous = '1'::numeric

So the patch's EXPLAIN changes look like the smaller cost.

> I think this merits backpatching because it can silently change results
> after restore.

Agreed.  I tried 15-19: there is only an easily fixed conflict, and no
code changes are needed:

  - 19: applies as it is.
  - 15-18: only the updatable_views.out hunk fails, as that test (ON
    CONFLICT DO SELECT) exists from 19; dropping it is enough, and the
    other expected output still matches.
  - make check passes on all five branches, and the example from your
    mail reproduces on all of them.
  - EXPLAIN VERBOSE and pg_dump output change on the back branches
    too.

In the tests, all the new cases are function arguments, so dropping
the showimplicit check still passes rowtypes.  The case where
showimplicit is false would be pinned by a test like this:

  CREATE TABLE rowtypes_deparse_def
    (c rowtypes_deparse_a DEFAULT ROW(1, 2));
  SELECT pg_get_expr(adbin, adrelid) FROM pg_attrdef
    WHERE adrelid = 'rowtypes_deparse_def'::regclass;
  -- ROW(1, 2), before and after the patch

The patch covers one case of CF 7452 [1]: a whole-row reference
coerced to an operator's composite input type.  7452 handles it in
the parser, but this patch fixes it in deparse, also for objects
already stored, so once this patch is committed I will drop that
part from 7452.

[1] https://www.postgresql.org/message-id/flat/CAAAe_zAppw9B_PjFepFkUf9F1_hs1SpS%2ByizJuRPDRSyw3RkHQ%40mail.gmail.com

Best regards,
Henson

Re: [PATCH v1] Preserve implicit composite casts in ROW deparsing

От:
Sho Ito <i.sho.0628@gmail.com>
Дата:
Вложения:
Hi Henson,

Thanks for the review and for testing the patch.
 
The strongest case is VARIADIC, where a dump from master does not restore even without
adding an overload later:

Attached v2 adds the `VARIADIC` case, including view recreation with
pretty-printing both enabled and disabled, and the column-default test
to cover `showimplicit = false`. The default still deparses as
`ROW(1, 2)`. The implementation is unchanged from v1. 
 
Agreed.  I tried 15-19: there is only an easily fixed conflict, and no
code changes are needed:
 
As you noted, 19 takes the patch unchanged; on 15-18 I omitted only the `updatable_views.out` hunk.

The attachments are alternatives: the main patch is for master and 19;
the `nocfbot-` patch is for 15-18. They should be applied separately,
not as a series.

I agree about keeping the `EXPLAIN` changes. Thanks also for clarifying
the overlap with #7452 [1].

Regards,
Sho

[1] https://commitfest.postgresql.org/patch/7452/

--
=======================================
Sho Ito (Sean)

=======================================

Re: [PATCH v1] Preserve implicit composite casts in ROW deparsing

От:
Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Дата:
Hello,

At Thu, 19 Nov 2026 23:41:09 -0500, Sho Ito  wrote in 
> Attached v2 adds the `VARIADIC` case, including view recreation with
> pretty-printing both enabled and disabled, and the column-default test
> to cover `showimplicit = false`. The default still deparses as
> `ROW(1, 2)`. The implementation is unchanged from v1.

The change itself looks good to me, and the tests seem sufficient.

However, I think the comment might be more useful if it focused on
explaining why RECORDOID can be excluded even when showimplicit is
true.

For example:

> A bare ROW() expression has type RECORD, so omitting the cast to
> RECORD even when showimplicit is true does not affect overload
> resolution on reparse.

Regards,

-- 
Kyotaro Horiguchi
NTT Open Source Software Center


Re: [PATCH v1] Preserve implicit composite casts in ROW deparsing

От:
Sho Ito <i.sho.0628@gmail.com>
Дата:
Вложения:
Hi Horiguchi san,
 
However, I think the comment might be more useful if it focused on
explaining why RECORDOID can be excluded even when showimplicit is
true.

Thanks for reviewing. Attached v3 revises the comment to explain that
a bare `ROW()` already has type `RECORD`, so omitting an implicit
cast to `RECORD` does not affect overload resolution on reparse.

There are no code or test changes from v2. I also added author and
reviewer credits and the discussion link to the commit messages.
The main attachment is for master and 19; the `nocfbot-` attachment
is the alternative for 15-18.

Thanks,
Sho

--
=======================================
Sho Ito (Sean)

=======================================

Re: [PATCH v1] Preserve implicit composite casts in ROW deparsing

От:
Henson Choi <assam258@gmail.com>
Дата:
Hi Sho,

> > However, I think the comment might be more useful if it focused on
> > explaining why RECORDOID can be excluded even when showimplicit is
> > true.
>
> Thanks for reviewing. Attached v3 revises the comment to explain that
> a bare `ROW()` already has type `RECORD`, so omitting an implicit
> cast to `RECORD` does not affect overload resolution on reparse.

I agree with Horiguchi-san: leaving out RECORDOID is the part of the
condition that is not obvious, so that is what the comment should
explain.  The v3 wording reads well to me.

> There are no code or test changes from v2.

I tested v3 and found no problems.  The added VARIADIC and column
default test cases look good to me.

Best regards,
Henson
FAQ