Thread: pgsql: Fix set_subquery_pathlist() to copy the RTE's subquery before it

pgsql: Fix set_subquery_pathlist() to copy the RTE's subquery before it

From
tgl@postgresql.org (Tom Lane)
Date:
Log Message:
-----------
Fix set_subquery_pathlist() to copy the RTE's subquery before it gets mangled
by the planning process.  This prevents the "failed to locate grouping columns"
error recently reported by Dickson Guedes.  That happens because planning
replaces SubLinks by SubPlans in the subquery's targetlist, and exprTypmod()
is smarter about the former than the latter, causing the apparent type of
the subquery's output columns to change.  This seems to be a deficiency we
should fix in exprTypmod(), but that will be a much more invasive patch
with possible side-effects elsewhere, so I'll do that only in HEAD.

Back-patch to 8.3.  Arguably the lack of a copying step is broken/dangerous
all the way back, but in the absence of known problems I'll refrain from
making the older branches pay the extra cost.  (The reason this particular
symptom didn't appear before is that exprTypmod() wasn't smart about SubLinks
either, until 8.3.)

Modified Files:
--------------
    pgsql/src/backend/optimizer/path:
        allpaths.c (r1.180 -> r1.181)
        (http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/path/allpaths.c?r1=1.180&r2=1.181)
    pgsql/src/test/regress/expected:
        subselect.out (r1.21 -> r1.22)
        (http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/test/regress/expected/subselect.out?r1=1.21&r2=1.22)
    pgsql/src/test/regress/sql:
        subselect.sql (r1.13 -> r1.14)
        (http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/test/regress/sql/subselect.sql?r1=1.13&r2=1.14)