pgsql: Fix the issue that SJE mistakenly omits qual clauses

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix the issue that SJE mistakenly omits qual clauses
Date: 2024-01-06 12:10:09
Message-ID: E1rM5VB-0009wL-B7@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix the issue that SJE mistakenly omits qual clauses

When the SJE code handles the transfer of qual clauses from the removed
relation to the remaining one, it replaces the Vars of the removed
relation with the Vars of the remaining relation for each clause, and
then reintegrates these clauses into the appropriate restriction or join
clause lists, while attempting to avoid duplicates.

However, the code compares RestrictInfo->clause to determine if two
clauses are duplicates. This is just flat wrong. Two RestrictInfos
with the same clause can have different required_relids,
incompatible_relids, is_pushed_down, and so on. This can cause qual
clauses to be mistakenly omitted, leading to wrong results.

This patch fixes it by comparing the entire RestrictInfos not just their
clauses ignoring 'rinfo_serial' field (otherwise almost all RestrictInfos will
be unique). Making 'rinfo_serial' equal_ignore would break other code. This
is why this commit implements our own comparison function for checking the
equality of RestrictInfos.

Reported-by: Zuming Jiang
Bug: #18261
Discussion: https://postgr.es/m/18261-2a75d748c928609b%40postgresql.org
Author: Richard Guo

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/5ef34a8fc3899a306fbc907a762fee0ba3782462

Modified Files
--------------
src/backend/optimizer/plan/analyzejoins.c | 26 ++++++++++++++++++++++++--
src/test/regress/expected/join.out | 26 +++++++++++++++++++++++++-
src/test/regress/sql/join.sql | 17 ++++++++++++++++-
3 files changed, 65 insertions(+), 4 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alexander Korotkov 2024-01-07 07:29:11 pgsql: Fix oversized memory allocation in Parallel Hash Join
Previous Message Tom Lane 2024-01-05 19:32:42 pgsql: Clean up some edge cases in plpgsql's %TYPE parsing.