pgsql: Fix some issues with wrong placement of pseudo-constant quals.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix some issues with wrong placement of pseudo-constant quals.
Date: 2023-02-22 17:39:22
Message-ID: E1pUt5N-000Ag4-So@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix some issues with wrong placement of pseudo-constant quals.

initsplan.c figured that it could push Var-free qual clauses to
the top of the current JoinDomain, which is okay in the abstract.
But if the current domain is inside some outer join, and we later
commute an inside-the-domain outer join with one outside it,
we end up placing the pushed-up qual clause incorrectly.

In distribute_qual_to_rels, avoid this by using the syntactic scope
of the qual clause; with the exception that if we're in the top-level
join domain we can still use the full query relid set, ensuring the
resulting gating Result node goes to the top of the plan. (This is
approximately as smart as the pre-v16 code was. Perhaps we can do
better later, but it's not clear that such cases are worth a lot of
sweat.)

In process_implied_equality, we don't have a clear notion of syntactic
scope, but we do have the results of SpecialJoinInfo construction.
Thumb through those and remove any lower outer joins that might get
commuted to above the join domain. Again, we can make an exception
for the top-level join domain. It'd be possible to work harder here
(for example, by keeping outer joins that aren't shown as potentially
commutable), but I'm going to stop here for the moment. This issue
has convinced me that the current representation of join domains
probably needs further refinement, so I'm disinclined to write
inessential dependent logic just yet.

In passing, tighten the qualscope passed to process_implied_equality
by generate_base_implied_equalities_no_const; there's no need for
it to be larger than the rel we are currently considering.

Tom Lane and Richard Guo, per report from Tender Wang.

Discussion: https://postgr.es/m/CAHewXNk9eJ35ru5xATWioTV4+xZPHptjy9etdcNPjUfY9RQ+uQ@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a75ff55c83342656266deb1890bcdd27466333ea

Modified Files
--------------
src/backend/optimizer/path/equivclass.c | 4 +-
src/backend/optimizer/plan/initsplan.c | 74 +++++++++++++++++++++++++++++----
src/test/regress/expected/join.out | 61 +++++++++++++++++++++++++++
src/test/regress/sql/join.sql | 25 +++++++++++
4 files changed, 155 insertions(+), 9 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Heikki Linnakangas 2023-02-22 19:42:44 pgsql: Check for unbounded authentication exchanges in libpq.
Previous Message Tomas Vondra 2023-02-22 16:35:36 pgsql: Fix snapshot handling in logicalmsg_decode