Re: v17 Possible Union All Bug

David G. Johnston <david.g.johnston@gmail.com>

From: "David G. Johnston" <david.g.johnston@gmail.com>
To: PostgreSQL Bug List <pgsql-bugs@lists.postgresql.org>
Date: 2024-01-26T22:32:56Z
Lists: pgsql-bugs
On Tue, Jan 23, 2024 at 4:51 PM David G. Johnston <
david.g.johnston@gmail.com> wrote:

> I appreciate this is a bit of a messy test case.  I'm willing to work on
> simplifying it further but figured I'd at least get confirmation of
> reproducibility and maybe someone will have an ah-ha! moment.
>
>
Decided to focus on simplifying the query first.  I figured this out:

 WITH cte_role_graph AS (
         SELECT leaf_role.oid,
            leaf_role.role_type,
            leaf_role.rolname,
            leaf_role.rolsuper,
            array_to_string(ARRAY(

                 SELECT 'false' where false
                 UNION ALL

                 SELECT format('%I from %s'::text, 'test',
string_agg('test', '---'::text
                           ORDER BY grant_instance.level,
grant_instance.grantor, grant_instance.grantor_path
                    ))

                   FROM unnest(leaf_role.memberof_groups) other(other)
                     JOIN pg_roles other_role ON other_role.oid =
other.other
                     JOIN rolegraph.role_relationship grant_instance ON
grant_instance.leaf_node = leaf_role.oid AND grant_instance.group_node =
other.other
                     JOIN pg_roles grant_role ON grant_role.oid =
grant_instance.grantor
                  GROUP BY other_role.rolname, grant_instance.via
            ), E'\n'::text) AS administration
           FROM rolegraph.role_graph_detail leaf_role
           where rolname ~ 'u6_green'
        )
select * from cte_role_graph;

Running this query against the previously supplied dump file on HEAD should
produce the broken result.  Simply commenting out the ORDER BY clause in
the string_agg causes the correct result to appear, even with the UNION ALL
present.  Removing the union all and leaving the order by likewise still
produces the correct result.

psql (17devel)
Type "help" for help.

postgres=# \i tmp3.sql
  oid  | role_type |      rolname       | rolsuper | administration
-------+-----------+--------------------+----------+----------------
 16405 | User      | u6_green_leader_su | f        | test from test+
       |           |                    |          | test from test+
       |           |                    |          | test from test+
       |           |                    |          | test from test+
       |           |                    |          | test from test
(1 row)

postgres=# \i tmp3.sql
  oid  | role_type |      rolname       | rolsuper |    administration
-------+-----------+--------------------+----------+-----------------------
 16405 | User      | u6_green_leader_su | f        | test from test       +
       |           |                    |          | test from test---test+
       |           |                    |          | test from test       +
       |           |                    |          | test from test
(1 row)

David J.

Commits

  1. Fix usage of aggregate pathkeys in group_keys_reorder_by_pathkeys()

  2. Move is_valid_ascii() to ascii.h.

  3. Test EXPLAIN (FORMAT JSON) ... XMLTABLE

  4. Add test module injection_points

  5. Fix table name collision in tests in 0452b461bc

  6. Explore alternative orderings of group-by pathkeys during optimization.

  7. Generalize the common code of adding sort before processing of grouping

  8. Reorder actions in ProcArrayApplyRecoveryInfo()

  9. More documentation updates for incremental backup.

  10. Avoid useless ReplicationOriginExitCleanup locking