Assertion failure if a local partitioned table is modified where a CTE contains a distributed table
- Dominant language
- C
- Stars
- 12.8k
- Forks
- 794
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 31
Description
We've an outdated logic to get the first distributed relation in the query tree (`ExtractFirstDistributedTableId()`), which fails with more complex SQL that we support.
Steps to reproduce
```SQL
CREATE TABLE local_partitioned_table (
key bigint,
collection_id integer
) PARTITION BY LIST (collection_id );
CREATE TABLE local_partitioned_table_0
PARTITION OF local_partitioned_table (key, collection_id)
FOR VALUES IN ( 0 );
CREATE TABLE distributed_table (a int, b int);
SELECT create_distributed_table('distributed_table', 'a');
WITH t1 AS (SELECT * FROM distributed_table) update local_partitioned_table set collection_id = 15;
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
Time: 6.248 ms
!>
```
bt:
```
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
* frame #0: 0x00007fff672662c6 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x00007fff67321bf1 libsystem_pthread.dylib`pthread_kill + 284
frame #2: 0x00007fff671d06a6 libsystem_c.dylib`abort + 127
frame #3: 0x000000010271a82e postgres`ExceptionalCondition(conditionName=, errorType=, fileName=, lineNumber=) at assert.c:54:2
frame #4: 0x000000010267102c postgres`dopr(target=0x00007fff672d7245, format=, args=0x00007ffeed99a000) at snprintf.c:442:4
frame #5: 0x00000001026707cf postgres`pg_vsnprintf(str=, count=, fmt=, args=) at snprintf.c:195:2
frame #6: 0x0000000102677802 postgres`pvsnprintf(buf=, len=1024, fmt="relation %s is not distributed", args=) at psprintf.c:110:13
frame #7: 0x000000010242af76 postgres`appendStringInfoVA(str=0x00007ffeed999fd0, fmt=, args=) at stringinfo.c:136:13
frame #8: 0x0000000102632f9d postgres`errmsg(fmt="relation %s is not distributed") at elog.c:794:2
frame #9: 0x0000000102fc6591 citus.so`DistributedTableCacheEntry.cold.1 + 65
frame #10: 0x0000000102fa4826 citus.so`DistributedTableCacheEntry(distributedRelationId=) at metadata_cache.c:810:24
frame #11: 0x0000000102f7ca09 citus.so`DistPartitionKey(relationId=) at multi_join_order.c:1233:40
frame #12: 0x0000000102f7c9ed citus.so`PartitionColumn(relationId=, rangeTableId=1) at multi_join_order.c:1204:22
frame #13: 0x0000000102f89c41 citus.so`ModifyQuerySupported(queryTree=0x00007fd3810031e0, originalQuery=0x00007fd3810030c8, multiShardQuery=, plannerRestrictionContext=0x00007fd38104aaf8) at multi_router_planner.c:564:25
frame #14: 0x0000000102f89b4b citus.so`CreateModifyPlan(originalQuery=0x00007fd3810030c8, query=0x00007fd3810031e0, plannerRestrictionContext=0x00007fd38104aaf8) at multi_router_planner.c:207:35
frame #15: 0x0000000102f7874d citus.so`CreateDistributedPlan(planId=1, originalQuery=0x00007fd3810030c8, q
```
Contributor guide
Research direction
Start with ExtractFirstDistributedTableId() and the planner paths shown in multi_router_planner.c and multi_join_order.c; use the provided SQL reproduction to follow how the CTE relation is selected. Check metadata_cache.c where the assertion reports an undistributed relation. Done means the reproduction no longer aborts and the local partitioned-table update completes successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, postgresql, sql
- Domain
- databases, distributed-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100