Re: Fix variadic argument types for pg_get_xxx_ddl() functions

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

Re: Fix variadic argument types for pg_get_xxx_ddl() functions

От:
Chao Li <li.evan.chao@gmail.com>
Дата:
Вложения:


> On Jun 23, 2026, at 10:11, Chao Li  wrote:
> 
> Hi,
> 
> While testing "[76e514ebb] Add pg_get_role_ddl() function" as well as the other pg_get_xxx_ddl() functions, I noticed that their pg_proc.dat definitions use "text" rather than "_text" for variadic argument types. For example:
> ```
> { oid => '6501', descr => 'get DDL to recreate a role',
> proname => 'pg_get_role_ddl', prorows => '10', provariadic => 'text',
> proisstrict => 'f', proretset => 't', provolatile => 's',
> pronargdefaults => '1', prorettype => 'text', proargtypes => 'regrole text',
> proallargtypes => '{regrole,text}', proargmodes => '{i,v}',
> proargdefaults => '{NULL}', prosrc => 'pg_get_role_ddl' },
> ```
> 
> I spotted this problem some time ago, but I wasn’t sure whether it was a real issue worth fixing. When I saw a new patch [1] for pg_get_table_ddl() that had the same problem, I asked about it there. Akshay confirmed the problem and pointed me to the opr_sanity check. Thank you very much, Akshay.
> 
> Then I checked why the sanity check didn’t report the issue. I found that the check used != for the comparison. When the final argument type is not an array type, the subquery returns NULL, and NULL != provariadic evaluates to NULL rather than true. I fixed the check to use IS DISTINCT FROM. After that, it reported the mismatch:
> ```
> % make -C src/test/regress check-tests TESTS='test_setup opr_sanity'
> # +++ regress check in src/test/regress +++
> # initializing database system by copying initdb template
> # using temp instance on port 58928 with PID 36865
> ok 1         - test_setup                                243 ms
> # diff -U3 /Users/chaol/Documents/code/postgresql/src/test/regress/expected/opr_sanity.out /Users/chaol/Documents/code/postgresql/src/test/regress/results/opr_sanity.out
> # --- /Users/chaol/Documents/code/postgresql/src/test/regress/expected/opr_sanity.out   2026-06-23 07:58:52
> # +++ /Users/chaol/Documents/code/postgresql/src/test/regress/results/opr_sanity.out    2026-06-23 07:59:41
> # @@ -488,9 +488,13 @@
> #                 FROM pg_type t
> #                 WHERE t.typarray = proargtypes[array_length(proargtypes, 1)-1])
> #       END IS DISTINCT FROM provariadic;
> # - oid | provariadic | proargtypes
> # ------+-------------+-------------
> # -(0 rows)
> # +                  oid                  | provariadic |       proargtypes
> # +---------------------------------------+-------------+--------------------------
> # + pg_get_role_ddl(regrole,text)         | text        | [0:1]={regrole,text}
> # + pg_get_tablespace_ddl(oid,text)       | text        | [0:1]={oid,text}
> # + pg_get_tablespace_ddl(name,text)      | text        | [0:1]={name,text}
> # + pg_get_database_ddl(regdatabase,text) | text        | [0:1]={regdatabase,text}
> # +(4 rows)
> #
> #  -- Check that all and only those functions with a variadic type have
> #  -- a variadic argument.
> not ok 2     - opr_sanity                               8938 ms
> 1..2
> # 1 of 2 tests failed.
> ```
> 
> After updating pg_proc.dat to change those "text" entries to "_text", the test passed.
> 
> See the attached patch for details.
> 
> [1] https://postgr.es/m/CANxoLDe_GOXyyZs7GN3VUJoT+o1DwqZGhid-TWWvS0D8d5ggYw@mail.gmail.com
> 
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
> 
> 
> 
> 

Oops! Missed the attachment.

--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/




Re: Fix variadic argument types for pg_get_xxx_ddl() functions

От:
Akshay Joshi <akshay.joshi@enterprisedb.com>
Дата:
Hi Chao,

Patch looks good to me. I have added the following test cases in my local version in "src/test/modules/test_misc/t/012_ddlutils.pl". If possible add those and resend the patch

+# Options passed via VARIADIC ARRAY
+$result = $node->safe_psql('postgres',
+       q{SELECT * FROM pg_get_role_ddl('regress_role_ddl_test2',
+           VARIADIC ARRAY['pretty', 'false'])});
+unlike($result, qr/\n\s+SUPERUSER/,
+       'role VARIADIC ARRAY pretty=false suppresses indentation');
+like($result, qr/SUPERUSER/, 'role VARIADIC ARRAY pretty=false still emits attributes');


+# Options passed via VARIADIC ARRAY
+$result = ddl_filter(
+       $node->safe_psql(
+               'postgres',
+               q{SELECT pg_get_database_ddl
+         FROM pg_get_database_ddl('regression_ddlutils_test',
+           VARIADIC ARRAY['pretty', 'false'])}));
+unlike($result, qr/\n\s+WITH TEMPLATE/,
+       'database VARIADIC ARRAY pretty=false suppresses indentation');
+like(
+       $result,
+       qr/CREATE DATABASE regression_ddlutils_test/,
+       'database VARIADIC ARRAY still emits CREATE');


+# Options passed via VARIADIC ARRAY (name variant)
+$result = $node->safe_psql(
+       'postgres',
+       q{SELECT * FROM pg_get_tablespace_ddl('regress_allopt_tblsp',
+         VARIADIC ARRAY['pretty', 'false'])});
+unlike($result, qr/\n\s+OWNER/,
+       'tablespace VARIADIC ARRAY pretty=false suppresses indentation (name)');
+like(
+       $result,
+       qr/OWNER regress_role_ddl_test1/,
+       'tablespace VARIADIC ARRAY still emits OWNER (name)');
+
+# Options passed via VARIADIC ARRAY (oid variant)
+$result = $node->safe_psql(
+       'postgres', q{
+       SELECT * FROM pg_get_tablespace_ddl(
+         (SELECT oid FROM pg_tablespace
+          WHERE spcname = 'regress_allopt_tblsp'),
+         VARIADIC ARRAY['pretty', 'false'])});
+unlike($result, qr/\n\s+OWNER/,
+       'tablespace VARIADIC ARRAY pretty=false suppresses indentation (oid)');
+like(
+       $result,
+       qr/CREATE TABLESPACE regress_allopt_tblsp/,
+       'tablespace VARIADIC ARRAY still emits CREATE (oid)');
+

On Tue, Jun 23, 2026 at 1:50 PM Chao Li <li.evan.chao@gmail.com> wrote:


> On Jun 23, 2026, at 10:11, Chao Li <li.evan.chao@gmail.com> wrote:
>
> Hi,
>
> While testing "[76e514ebb] Add pg_get_role_ddl() function" as well as the other pg_get_xxx_ddl() functions, I noticed that their pg_proc.dat definitions use "text" rather than "_text" for variadic argument types. For example:
> ```
> { oid => '6501', descr => 'get DDL to recreate a role',
> proname => 'pg_get_role_ddl', prorows => '10', provariadic => 'text',
> proisstrict => 'f', proretset => 't', provolatile => 's',
> pronargdefaults => '1', prorettype => 'text', proargtypes => 'regrole text',
> proallargtypes => '{regrole,text}', proargmodes => '{i,v}',
> proargdefaults => '{NULL}', prosrc => 'pg_get_role_ddl' },
> ```
>
> I spotted this problem some time ago, but I wasn’t sure whether it was a real issue worth fixing. When I saw a new patch [1] for pg_get_table_ddl() that had the same problem, I asked about it there. Akshay confirmed the problem and pointed me to the opr_sanity check. Thank you very much, Akshay.
>
> Then I checked why the sanity check didn’t report the issue. I found that the check used != for the comparison. When the final argument type is not an array type, the subquery returns NULL, and NULL != provariadic evaluates to NULL rather than true. I fixed the check to use IS DISTINCT FROM. After that, it reported the mismatch:
> ```
> % make -C src/test/regress check-tests TESTS='test_setup opr_sanity'
> # +++ regress check in src/test/regress +++
> # initializing database system by copying initdb template
> # using temp instance on port 58928 with PID 36865
> ok 1         - test_setup                                243 ms
> # diff -U3 /Users/chaol/Documents/code/postgresql/src/test/regress/expected/opr_sanity.out /Users/chaol/Documents/code/postgresql/src/test/regress/results/opr_sanity.out
> # --- /Users/chaol/Documents/code/postgresql/src/test/regress/expected/opr_sanity.out   2026-06-23 07:58:52
> # +++ /Users/chaol/Documents/code/postgresql/src/test/regress/results/opr_sanity.out    2026-06-23 07:59:41
> # @@ -488,9 +488,13 @@
> #                 FROM pg_type t
> #                 WHERE t.typarray = proargtypes[array_length(proargtypes, 1)-1])
> #       END IS DISTINCT FROM provariadic;
> # - oid | provariadic | proargtypes
> # ------+-------------+-------------
> # -(0 rows)
> # +                  oid                  | provariadic |       proargtypes
> # +---------------------------------------+-------------+--------------------------
> # + pg_get_role_ddl(regrole,text)         | text        | [0:1]={regrole,text}
> # + pg_get_tablespace_ddl(oid,text)       | text        | [0:1]={oid,text}
> # + pg_get_tablespace_ddl(name,text)      | text        | [0:1]={name,text}
> # + pg_get_database_ddl(regdatabase,text) | text        | [0:1]={regdatabase,text}
> # +(4 rows)
> #
> #  -- Check that all and only those functions with a variadic type have
> #  -- a variadic argument.
> not ok 2     - opr_sanity                               8938 ms
> 1..2
> # 1 of 2 tests failed.
> ```
>
> After updating pg_proc.dat to change those "text" entries to "_text", the test passed.
>
> See the attached patch for details.
>
> [1] https://postgr.es/m/CANxoLDe_GOXyyZs7GN3VUJoT+o1DwqZGhid-TWWvS0D8d5ggYw@mail.gmail.com
>
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
>
>
>
>

Oops! Missed the attachment.

--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/




Re: Fix variadic argument types for pg_get_xxx_ddl() functions

От:
Andrew Dunstan <andrew@dunslane.net>
Дата:


On Tue, Jun 23, 2026 at 3:07 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
I wrote:
> Ouch.  Yes, that regression test is clearly one brick shy of a load.
> We need to update the documentation for these functions too, and maybe
> some other places.

After looking around, I couldn't find anything but the documentation
that needed to be fixed, so done.


Thanks for fixing.

cheers

andrew 

Test tidscan,sql is not immune to autovacuum in v14

От:
Alexander Lakhin <exclusion@gmail.com>
Дата:
Вложения:
Hello hackers,

I spotted a very rare test failure (a single one in two years, at least)
generated by basilisk:
      tidscan                      ... FAILED       34 ms
...
diff -U3 /mnt/build/REL_14_STABLE/pgsql.build/../pgsql/src/test/regress/expected/tidscan.out 
/mnt/build/REL_14_STABLE/pgsql.build/src/test/regress/results/tidscan.out
--- /mnt/build/REL_14_STABLE/pgsql.build/../pgsql/src/test/regress/expected/tidscan.out
+++ /mnt/build/REL_14_STABLE/pgsql.build/src/test/regress/results/tidscan.out
@@ -242,10 +242,10 @@
  ----------------------------------------
   Aggregate
     ->  Hash Join
-         Hash Cond: (t1.ctid = t2.ctid)
-         ->  Seq Scan on tenk1 t1
+         Hash Cond: (t2.ctid = t1.ctid)
+         ->  Seq Scan on tenk1 t2
           ->  Hash
-               ->  Seq Scan on tenk1 t2
+               ->  Seq Scan on tenk1 t1
  (6 rows)

  SELECT count(*) FROM tenk1 t1 JOIN tenk1 t2 ON t1.ctid = t2.ctid;

and was intrigued enough to find a reliable reproducer for it.
With the following modification:
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -188,6 +188,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
                 estimate_rel_size(relation, rel->attr_widths - rel->min_attr,
&rel->pages, &rel->tuples, &rel->allvisfrac);

+char *name = get_rel_name(relationObjectId); if (name != NULL && strcmp(name, "tenk1") == 0) pg_usleep(100000);
         /* Retrieve the parallel_workers reloption, or -1 if not set. */
         rel->rel_parallel_workers = RelationGetParallelWorkers(relation, -1);

and
test: tidscan analyze
in parallel_schedule (the full patch is attached), I get:
parallel group (2 tests):  analyze tidscan
      tidscan                      ... FAILED      900 ms
      analyze                      ... ok           63 ms

(The basilisk's log contain no autovacuum messages, but I guess it was
automatic analyze that processed tenk1 in the very lucky moment.)

Given the current statistics, we won't see failures of this ilk anymore,
because it is not reproduced in REL_15_STABLE..master, due to 74388a1ac +
4496020e6, which resulted in a different reltuples value returned for tenk1
during sanity_check/VACUUM and that indirectly affected the plan change.

[1] https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=basilisk&dt=2026-06-15%2006%3A07%3A09

Best regards,
Alexander

Re: Test tidscan,sql is not immune to autovacuum in v14

От:
David Rowley <dgrowleyml@gmail.com>
Дата:
Вложения:
On Thu, 20 Aug 2026 at 08:00, Alexander Lakhin  wrote:
> I spotted a very rare test failure (a single one in two years, at least)
> generated by basilisk:
>       tidscan                      ... FAILED       34 ms
> ...
> diff -U3 /mnt/build/REL_14_STABLE/pgsql.build/../pgsql/src/test/regress/expected/tidscan.out
> /mnt/build/REL_14_STABLE/pgsql.build/src/test/regress/results/tidscan.out
> --- /mnt/build/REL_14_STABLE/pgsql.build/../pgsql/src/test/regress/expected/tidscan.out
> +++ /mnt/build/REL_14_STABLE/pgsql.build/src/test/regress/results/tidscan.out
> @@ -242,10 +242,10 @@
>   ----------------------------------------
>    Aggregate
>      ->  Hash Join
> -         Hash Cond: (t1.ctid = t2.ctid)
> -         ->  Seq Scan on tenk1 t1
> +         Hash Cond: (t2.ctid = t1.ctid)
> +         ->  Seq Scan on tenk1 t2
>            ->  Hash
> -               ->  Seq Scan on tenk1 t2
> +               ->  Seq Scan on tenk1 t1
>   (6 rows)
>
>   SELECT count(*) FROM tenk1 t1 JOIN tenk1 t2 ON t1.ctid = t2.ctid;

I experimented, and I see the costs come out quite different if that
were changed to:

SELECT count(*) FROM tenk1 WHERE ctid IN (SELECT DISTINCT ctid FROM tenk1);

The winning plan is;

                                        QUERY PLAN
-------------------------------------------------------------------------------------------
 Aggregate  (cost=1191.26..1191.27 rows=1 width=8)
   ->  Hash Join  (cost=695.00..1166.26 rows=10000 width=0)
         Hash Cond: (tenk1.ctid = tenk1_1.ctid)
         ->  Seq Scan on tenk1  (cost=0.00..445.00 rows=10000 width=6)
         ->  Hash  (cost=570.00..570.00 rows=10000 width=6)
               ->  HashAggregate  (cost=470.00..570.00 rows=10000 width=6)
                     Group Key: tenk1_1.ctid
                     ->  Seq Scan on tenk1 tenk1_1  (cost=0.00..445.00
rows=10000 width=6)
(8 rows)

and if I force the other Hash Join option via the debugger, I get:

                                     QUERY PLAN
-------------------------------------------------------------------------------------
 Aggregate  (cost=1302.50..1302.51 rows=1 width=8)
   ->  Hash Join  (cost=1040.00..1277.50 rows=10000 width=0)
         Hash Cond: (tenk1_1.ctid = tenk1.ctid)
         ->  HashAggregate  (cost=470.00..570.00 rows=10000 width=6)
               Group Key: tenk1_1.ctid
               ->  Seq Scan on tenk1 tenk1_1  (cost=0.00..445.00
rows=10000 width=6)
         ->  Hash  (cost=445.00..445.00 rows=10000 width=6)
               ->  Seq Scan on tenk1  (cost=0.00..445.00 rows=10000 width=6)
(8 rows)

There's probably also some argument to the distinct semi-join query
giving the code a bit more exercise due to the Hash Agg.

Naturally, the current INNER JOIN query produces the same cost for
each join order, under normal circumstances.

This makes me wonder how significant the estimates varied in the two
calls to estimate_rel_size() in the problem case you saw?

I tried to figure this out by recreating this for myself using your
code on v14, but didn't manage to get the correct timing for a failure
to occur. I can get the plan to flip by adjusting the rel->tuples
after the estimate_rel_size() call by adding 1.0 to rel on the hash
side of the join. That's due to the two Paths comparing fuzzily the
same and the tie-break ending up deferring to:

else if (new_path->rows < old_path->rows)
    remove_old = true; /* new dominates old */
else if (new_path->rows > old_path->rows)
    accept_new = false; /* old dominates new */

So, I suspect you're seeing a very small variation in tuple estimates
and the DISTINCT semi-join query I proposed above would be enough to
solve it.

> Given the current statistics, we won't see failures of this ilk anymore,
> because it is not reproduced in REL_15_STABLE..master, due to 74388a1ac +
> 4496020e6, which resulted in a different reltuples value returned for tenk1
> during sanity_check/VACUUM and that indirectly affected the plan change.

I had also thought that it might not be worth troubling over given
that v14 has less than 3 months to live, but I believe it's generally
bad practice to have queries in tests where multiple plans are so
close together in cost. They're just too prone to very subtle changes
that can result in rare plan changes (as per what you're reporting).

David


Re: Test tidscan,sql is not immune to autovacuum in v14

От:
David Rowley <dgrowleyml@gmail.com>
Дата:
Вложения:
On Thu, 20 Aug 2026 at 14:50, Tom Lane  wrote:
> I don't really like this proposal.  I think the only reason these
> test cases exist at all is to remind us that we aren't very smart
> about joins on ctid.  In the event that somebody fixes that, these
> plans would change --- except that it's really unlikely that
> such an optimization could "see through" the DISTINCT you propose
> to add.  So I think this change basically makes these tests unfit
> for any purpose.

This surprised me. When I looked at the commit message for 0a6ea4001,
I expected that you added these tests to exercise the newly added
hashtid() functions. It makes sense to me that you added the query
with and without EXPLAIN as you wanted to verify all 10k rows were
found and also that the chosen plan was a Hash Join.  If the plan had
flipped to a Merge Join or Nested Loop, then it wouldn't be doing a
very good job of exercising hashtid().

Are you sure that's not the reason?

> I think the reasonable choices are
>
> (1) Remove these test cases completely in all branches.  The
> argument for this is that we're expending test cycles for pretty
> nearly no benefit, and if someone did fix the case then the
> tests could be put back with new expected output.
>
> (2) Fix only in v14, to ensure that we don't see another of
> these failures.
>
> (3) Do nothing, reasoning that we probably won't see another
> such failure in v14's remaining lifespan anyway.
>
> I'm kind of leaning to (1), but (3) is plausible too.
> I'm not seeing a lot of point in (2).

I'm not for #2 either. IMO, #1 reduces the coverage for hashing of
tids, though we probably don't need that done on a 10k-row table. I
thought about #3, but it still leaves a query in our tests that we're
looking at the EXPLAIN for that has two equally cheap lowest-cost
plans. I thought it was best to do something before another reason
appears that introduces something new to make that fail randomly.

Another way to make the alternative join order less favourable would
be with something like:

select count(*) from tenk1 t1 inner join tenk1 t2 on t1.ctid = t2.ctid
where t2.ten = 0;

I make that:

->  Hash Join  (cost=482.50..975.00 rows=1000 width=0)

and with the join order swapped to put the filtering on the probe
side, the costs are:

->  Hash Join  (cost=570.00..1053.75 rows=1000 width=0)

Which is likely a large enough difference to stop any future random failures.

David


Re: Test tidscan,sql is not immune to autovacuum in v14

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
David Rowley  writes:
> Another way to make the alternative join order less favourable would
> be with something like:

> select count(*) from tenk1 t1 inner join tenk1 t2 on t1.ctid = t2.ctid
> where t2.ten = 0;

Sure, that'd work for me.

			regards, tom lane


FAQ