postgresql/src/backend
Tom Lane 55dc86eca7 Fix pull_varnos' miscomputation of relids set for a PlaceHolderVar.
Previously, pull_varnos() took the relids of a PlaceHolderVar as being
equal to the relids in its contents, but that fails to account for the
possibility that we have to postpone evaluation of the PHV due to outer
joins.  This could result in a malformed plan.  The known cases end up
triggering the "failed to assign all NestLoopParams to plan nodes"
sanity check in createplan.c, but other symptoms may be possible.

The right value to use is the join level we actually intend to evaluate
the PHV at.  We can get that from the ph_eval_at field of the associated
PlaceHolderInfo.  However, there are some places that call pull_varnos()
before the PlaceHolderInfos have been created; in that case, fall back
to the conservative assumption that the PHV will be evaluated at its
syntactic level.  (In principle this might result in missing some legal
optimization, but I'm not aware of any cases where it's an issue in
practice.)  Things are also a bit ticklish for calls occurring during
deconstruct_jointree(), but AFAICS the ph_eval_at fields should have
reached their final values by the time we need them.

The main problem in making this work is that pull_varnos() has no
way to get at the PlaceHolderInfos.  We can fix that easily, if a
bit tediously, in HEAD by passing it the planner "root" pointer.
In the back branches that'd cause an unacceptable API/ABI break for
extensions, so leave the existing entry points alone and add new ones
with the additional parameter.  (If an old entry point is called and
encounters a PHV, it'll fall back to using the syntactic level,
again possibly missing some valid optimization.)

Back-patch to v12.  The computation is surely also wrong before that,
but it appears that we cannot reach a bad plan thanks to join order
restrictions imposed on the subquery that the PlaceHolderVar came from.
The error only became reachable when commit 4be058fe9 allowed trivial
subqueries to be collapsed out completely, eliminating their join order
restrictions.

Per report from Stephan Springl.

Discussion: https://postgr.es/m/171041.1610849523@sss.pgh.pa.us
2021-01-21 15:37:23 -05:00
..
access Fix bug in detecting concurrent page splits in GiST insert 2021-01-20 11:58:03 +02:00
bootstrap Update copyright for 2021 2021-01-02 13:06:25 -05:00
catalog Fix ALTER DEFAULT PRIVILEGES with duplicated objects 2021-01-20 11:38:17 +09:00
commands Refactor option handling of CLUSTER, REINDEX and VACUUM 2021-01-18 14:03:10 +09:00
executor Fix initialization of FDW batching in ExecInitModifyTable 2021-01-21 03:34:32 +01:00
foreign Update copyright for 2021 2021-01-02 13:06:25 -05:00
jit Update copyright for 2021 2021-01-02 13:06:25 -05:00
lib Update copyright for 2021 2021-01-02 13:06:25 -05:00
libpq Update copyright for 2021 2021-01-02 13:06:25 -05:00
main Update copyright for 2021 2021-01-02 13:06:25 -05:00
nodes Implement support for bulk inserts in postgres_fdw 2021-01-20 23:57:27 +01:00
optimizer Fix pull_varnos' miscomputation of relids set for a PlaceHolderVar. 2021-01-21 15:37:23 -05:00
parser Re-implement pl/pgsql's expression and assignment parsing. 2021-01-04 11:52:00 -05:00
partitioning Update copyright for 2021 2021-01-02 13:06:25 -05:00
po Translation updates 2020-05-18 12:49:30 +02:00
port Update copyright for 2021 2021-01-02 13:06:25 -05:00
postmaster Add pg_stat_database counters for sessions and session time 2021-01-17 13:52:31 +01:00
regex Fix ancient bug in parsing of BRE-mode regular expressions. 2021-01-08 12:16:00 -05:00
replication pgindent worker.c. 2021-01-19 08:10:13 +05:30
rewrite Update copyright for 2021 2021-01-02 13:06:25 -05:00
snowball Update copyright for 2021 2021-01-02 13:06:25 -05:00
statistics Update copyright for 2021 2021-01-02 13:06:25 -05:00
storage Allow for error or refusal while absorbing a ProcSignalBarrier. 2021-01-18 12:09:52 -05:00
tcop Refactor option handling of CLUSTER, REINDEX and VACUUM 2021-01-18 14:03:10 +09:00
tsearch Update copyright for 2021 2021-01-02 13:06:25 -05:00
utils Fix pull_varnos' miscomputation of relids set for a PlaceHolderVar. 2021-01-21 15:37:23 -05:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
Makefile Update copyright for 2021 2021-01-02 13:06:25 -05:00
common.mk Remove PARTIAL_LINKING build mode. 2018-03-30 17:33:04 -07:00
nls.mk Add missing gettext triggers 2020-04-28 13:35:40 +02:00