citus/src/backend/distributed/worker
Mehmet YILMAZ 1bb6c7e95f PG17 Compatibility - Fix crash when pg_class is used in MERGE (#7853)
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:

0294df2f1f

**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
```
2025-03-12 12:43:01 +03:00
..
task_tracker_protocol.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
worker_create_or_replace.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
worker_data_fetch_protocol.c PG17 compatibility: Resolve compilation issues (#7699) 2025-03-12 11:01:49 +03:00
worker_drop_protocol.c Rename foreach_ macros to foreach_declared_ macros (#7700) 2025-03-12 11:01:49 +03:00
worker_partition_protocol.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00
worker_shard_visibility.c PG17 Compatibility - Fix crash when pg_class is used in MERGE (#7853) 2025-03-12 12:43:01 +03:00
worker_sql_task_protocol.c Create directories and files with pg_file_create_mode and pg_dir_create_mode permissions (#7479) 2024-02-07 12:48:31 +01:00
worker_truncate_trigger_protocol.c Sort includes (#7326) 2023-11-23 18:19:54 +01:00