Fix EXPLAIN's column alias output for mismatched child tables.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 3 Dec 2019 00:08:10 +0000 (19:08 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 3 Dec 2019 00:08:10 +0000 (19:08 -0500)
commit55a1954da16e041f895e5c3a6abff13c5e3a4a2f
treea7259305e0cd81599279d92d2f41c05c89b8bd51
parentce76c0ba53e4bd0daf3db7a703671b27797b7244
Fix EXPLAIN's column alias output for mismatched child tables.

If an inheritance/partitioning parent table is assigned some column
alias names in the query, EXPLAIN mapped those aliases onto the
child tables' columns by physical position, resulting in bogus output
if a child table's columns aren't one-for-one with the parent's.

To fix, make expand_single_inheritance_child() generate a correctly
re-mapped column alias list, rather than just copying the parent
RTE's alias node.  (We have to fill the alias field, not just
adjust the eref field, because ruleutils.c will ignore eref in
favor of looking at the real column names.)

This means that child tables will now always have alias fields in
plan rtables, where before they might not have.  That results in
a rather substantial set of regression test output changes:
EXPLAIN will now always show child tables with aliases that match
the parent table (usually with "_N" appended for uniqueness).
But that seems like a net positive for understandability, since
the parent alias corresponds to something that actually appeared
in the original query, while the child table names didn't.
(Note that this does not change anything for cases where an explicit
table alias was written in the query for the parent table; it
just makes cases without such aliases behave similarly to that.)
Hence, while we could avoid these subsidiary changes if we made
inherit.c more complicated, we choose not to.

Discussion: https://postgr.es/m/12424.1575168015@sss.pgh.pa.us
16 files changed:
contrib/postgres_fdw/expected/postgres_fdw.out
src/backend/optimizer/util/inherit.c
src/test/regress/expected/aggregates.out
src/test/regress/expected/alter_table.out
src/test/regress/expected/inherit.out
src/test/regress/expected/partition_aggregate.out
src/test/regress/expected/partition_join.out
src/test/regress/expected/partition_prune.out
src/test/regress/expected/rowsecurity.out
src/test/regress/expected/select_parallel.out
src/test/regress/expected/tablesample.out
src/test/regress/expected/union.out
src/test/regress/expected/updatable_views.out
src/test/regress/expected/update.out
src/test/regress/expected/with.out
src/test/regress/sql/partition_prune.sql