Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PG17 compatibility: Fix Test Failure in local_table_join #7732

Open
wants to merge 10 commits into
base: naisila/pg17_support
Choose a base branch
from

Conversation

m3hm3t
Copy link
Contributor

@m3hm3t m3hm3t commented Nov 11, 2024

PostgreSQL 17 seems to have introduced improvements in how correlated subqueries are handled during plan generation. Instead of generating a trivial subplan with WHERE true, it now applies more specific filtering (WHERE (key = 5)), which makes the execution plan more efficient.

postgres/postgres@b262ad44

diff -dU10 -w /__w/citus/citus/src/test/regress/expected/local_table_join.out /__w/citus/citus/src/test/regress/results/local_table_join.out
--- /__w/citus/citus/src/test/regress/expected/local_table_join.out.modified	2024-11-05 09:53:50.423970699 +0000
+++ /__w/citus/citus/src/test/regress/results/local_table_join.out.modified	2024-11-05 09:53:50.463971296 +0000
@@ -1420,32 +1420,32 @@
   ) as subq_1
 ) as subq_2;
 DEBUG:  Wrapping relation "custom_pg_type" to a subquery
 DEBUG:  generating subplan 204_1 for subquery SELECT typdefault FROM local_table_join.custom_pg_type WHERE true
 ERROR:  direct joins between distributed and local tables are not supported
 HINT:  Use CTE's or subqueries to select from local tables and use them in joins
 -- correlated sublinks are not yet supported because of #4470, unless we convert not-correlated table
 SELECT COUNT(*) FROM distributed_table d1 JOIN postgres_table using(key)
 WHERE d1.key IN (SELECT key FROM distributed_table WHERE d1.key = key and key = 5);
 DEBUG:  Wrapping relation "postgres_table" to a subquery
-DEBUG:  generating subplan XXX_1 for subquery SELECT key FROM local_table_join.postgres_table WHERE true
+DEBUG:  generating subplan 206_1 for subquery SELECT key FROM local_table_join.postgres_table WHERE (key OPERATOR(pg_catalog.=) 5)

naisila and others added 9 commits November 11, 2024 11:57
(cherry picked from commit ae3ed7d)
(cherry picked from commit 76f60a7)
(cherry picked from commit df9c7b4)
In PG17, the outer loop in acquire_sample_rows() changed
from
while (BlockSampler_HasMore(&bs))
to
while (table_scan_analyze_next_block(scan, stream))

Relevant PG commit:
041b96802efa33d2bc9456f2ad946976b92b5ae1
postgres/postgres@041b968

It is expected that the scan_analyze_next_block function will
check if there are any blocks left. So we add that check in
columnar_scan_analyze_next_block

(cherry picked from commit 7eb0ad5)
PG 17 added support for DEFAULT in ALTER TABLE .. SET ACCESS METHOD

Relevant PG commit:
d61a6cad6418f643a5773352038d0dfe5d3535b8
postgres/postgres@d61a6ca

In that case, name in AlterTableCmd would be null.
Add a null check here to avoid crash.

(cherry picked from commit 71b9974)
…tests

Fix pg15 pg16 multi_mx_create_table multi_schema_support

Relevant PG commit:
postgres/postgres@f696c0c
f696c0cd5f299f1b51e214efc55a22a782cc175d

(cherry picked from commit 17a2ed0)
Relevant PG commit:
f69319f2f1fb16eda4b535bcccec90dff3a6795e
postgres/postgres@f69319f

(cherry picked from commit 6c12b10)
This PR addresses a regression test failure in the multi-mx feature of
Citus with the new PostgreSQL 17 version. The regression was identified
during the execution of multi-node tests, specifically targeting
compatibility issues introduced with PostgreSQL 17.

---------

Co-authored-by: Mehmet YILMAZ <[email protected]>
(cherry picked from commit 70cf729)
This reverts commit e4040dd.

Reverting for now as this commit is fixing more than one thing
at once at multi_extension.out file

Its a harmless revert for testing purposes
@m3hm3t m3hm3t self-assigned this Nov 11, 2024
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.61%. Comparing base (b29c332) to head (7ba57a6).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           naisila/pg17_support    #7732      +/-   ##
========================================================
- Coverage                 89.61%   89.61%   -0.01%     
========================================================
  Files                       274      274              
  Lines                     59689    59689              
  Branches                   7446     7446              
========================================================
- Hits                      53490    53488       -2     
  Misses                     4069     4069              
- Partials                   2130     2132       +2     

@m3hm3t m3hm3t marked this pull request as ready for review November 11, 2024 20:47
@@ -1438,7 +1442,7 @@ set citus.local_table_join_policy to 'prefer-distributed';
SELECT COUNT(*) FROM distributed_table d1 JOIN postgres_table using(key)
WHERE d1.key IN (SELECT key FROM distributed_table WHERE d1.key = key and key = 5);
DEBUG: Wrapping relation "distributed_table" "d1" to a subquery
DEBUG: generating subplan XXX_1 for subquery SELECT key FROM local_table_join.distributed_table d1 WHERE true
DEBUG: generating subplan XXX_1 for subquery SELECT key FROM local_table_join.distributed_table d1 WHERE (key OPERATOR(pg_catalog.=) 5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have just two lines with the same change (key OPERATOR(pg_catalog.=) 5)
How about a normalized line, we can change the table names such that the normalization rule detects the line easily.
e.g.
postgres_table_key5_filtering
distributed_table_key5_filtering

Just an idea, could be better. But I am reluctant of merging a new file with 1659 new lines just for 2 lines change in the test output. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the solution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, these changes make sense. Based on your solution, I think we can leverage renaming the table even further by using a table alias, this will make changes even more minimal. What I mean by this:
In order to include everything with one normalize line, and avoid creating a new table, we can make use of aliases. For example, table_diff_filtering alias. We can include this alias in local_dist_join_mixed line difference as well. And we can change the output to WHERE true
(in local_table_join, WHERE true is in PG16; in local_dist_join_mixed, WHERE true is in PG17, but we don't need to document this part)

I did a sketch commit and it seems to work. https://github.com/citusdata/citus/tree/naisila/try_filtering_diff
Could you please update this PR accordingly?

(PS: We had used this table alias trick in PG16 support as well, I believe it deserves to be added to the documentation.)

@naisila naisila changed the title Fix Test Failure in local_table_join in PG17 PG17 compatibility: Fix Test Failure in local_table_join Nov 12, 2024
@m3hm3t m3hm3t marked this pull request as draft November 14, 2024 10:01
@m3hm3t m3hm3t marked this pull request as ready for review November 15, 2024 16:34
@m3hm3t m3hm3t requested a review from naisila November 15, 2024 16:34
update

update

.

update
@naisila naisila force-pushed the naisila/pg17_support branch 4 times, most recently from 46dc966 to 6d036b0 Compare November 20, 2024 11:54
Copy link
Member

@naisila naisila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Mehmet, after your remarks and @onurctirtir's ones, I re-checked this test. You are right that the filter change is not trivial.

However, the filter is not necessary to what the test is about: the test is about correlated sublinks not being yet supported. If we remove "key = 5" from the query, we still have a correlated sublink. So, we can remove "key = 5" from the queries and the result will be the same, and this will get rid of the test difference between versions.

On the other hand, we can add the test with "key = 5" to pg17.sql to see the improvement of Postgres applied in Citus.

@onurctirtir
Copy link
Member

onurctirtir commented Nov 21, 2024

Hey Mehmet, after your remarks and @onurctirtir's ones, I re-checked this test. You are right that the filter change is not trivial.

However, the filter is not necessary to what the test is about: the test is about correlated sublinks not being yet supported. If we remove "key = 5" from the query, we still have a correlated sublink. So, we can remove "key = 5" from the queries and the result will be the same, and this will get rid of the test difference between versions.

On the other hand, we can add the test with "key = 5" to pg17.sql to see the improvement of Postgres applied in Citus.


So, we can remove "key = 5" from the queries and the result will be the same, and this will get rid of the test difference between versions.

On the other hand, we can add the test with "key = 5" to pg17.sql to see the improvement of Postgres applied in Citus.

All makes sense to me, let's do both.

@naisila naisila force-pushed the naisila/pg17_support branch 6 times, most recently from e108bb8 to c396ce6 Compare November 22, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants