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 crash when pg_class is used in MERGE #7853

Merged
merged 5 commits into from
Jan 21, 2025

Conversation

m3hm3t
Copy link
Contributor

@m3hm3t m3hm3t commented Jan 20, 2025

This pull request addresses Issue #7846, where specific MERGE queries on non-distributed and distributed tables can result in crashes in certain scenarios. The issue stems from the usage of pg_class catalog table, and the FilterShardsFromPgclass function in Citus. This function goes through the query's jointree to hide the shards. However, in PG17, MERGE's join quals are in a separate structure called mergeJoinCondition. Therefore FilterShardsFromPgclass was not filtering correctly in a MERGE command that involves pg_class. To fix the issue, we handle mergeJoinCondition separately in PG17.

Relevant PG commit:
postgres/postgres@0294df2

Non-Distributed Tables:
A MERGE query involving a non-distributed table using pg_catalog.pg_class as the source may execute successfully but needs testing to ensure stability.

Distributed Tables:
Performing a MERGE on a distributed table using pg_catalog.pg_class as the source raises an error:
ERROR: MERGE INTO a distributed table from Postgres table is not yet supported
However, in some cases, this can lead to a server crash if the unsupported operation is not properly handled.

This is the test output from the same test conducted prior to the code changes being implemented.

-- Issue #7846: Test crash scenarios with MERGE on non-distributed and distributed tables
-- Step 1: Connect to a worker node to verify shard visibility
\c postgresql://postgres@localhost::worker_1_port/regression?application_name=psql
SET search_path TO pg17;
-- Step 2: Create and test a non-distributed table
CREATE TABLE non_dist_table_12345 (id INTEGER);
-- Test MERGE on the non-distributed table
MERGE INTO non_dist_table_12345 AS target_0
USING pg_catalog.pg_class AS ref_0
ON target_0.id = ref_0.relpages
WHEN NOT MATCHED THEN DO NOTHING;
SSL SYSCALL error: EOF detected
connection to server was lost

@m3hm3t m3hm3t self-assigned this Jan 20, 2025
Copy link

codecov bot commented Jan 20, 2025

Codecov Report

Attention: Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.48%. Comparing base (c55bc8c) to head (e04858f).
Report is 2 commits behind head on release-13.0.

Additional details and impacted files
@@              Coverage Diff              @@
##           release-13.0    #7853   +/-   ##
=============================================
  Coverage         89.48%   89.48%           
=============================================
  Files               276      276           
  Lines             60063    60066    +3     
  Branches           7524     7524           
=============================================
+ Hits              53747    53752    +5     
+ Misses             4166     4165    -1     
+ Partials           2150     2149    -1     

@m3hm3t m3hm3t requested a review from naisila January 21, 2025 08:36
@m3hm3t m3hm3t marked this pull request as ready for review January 21, 2025 08:36
@naisila naisila changed the title Issue 7846 PG17 Compatibility - Fix crash when pg_class is used in MERGE Jan 21, 2025
@m3hm3t m3hm3t merged commit ab7c3b7 into release-13.0 Jan 21, 2025
153 checks passed
@m3hm3t m3hm3t deleted the issue_7846 branch January 21, 2025 14:48
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.

PG17 - SQLSmith - Server Crash Due to MERGE Command on Distributed Table with Schema-Based Sharding
2 participants