Skip to content

Support distribution key update in MERGE for partitioned tables#1730

Open
lss602726449 wants to merge 1 commit into
apache:cbdb-postgres-mergefrom
lss602726449:fix_merge
Open

Support distribution key update in MERGE for partitioned tables#1730
lss602726449 wants to merge 1 commit into
apache:cbdb-postgres-mergefrom
lss602726449:fix_merge

Conversation

@lss602726449
Copy link
Copy Markdown
Contributor

  • Add SplitMerge node to handle MERGE statements that modify distribution key columns, splitting UPDATE into DELETE + INSERT with correct segment routing
  • Always use root table action lists (not per-partition adjusted lists) so hashAttnos match root attribute numbers for correct hash computation
  • Consolidate MERGE CMD_MERGE blocks in preptlist.c and simplify check_splitupdate condition
  • Refactor nodeSplitMerge.c: extract computeTargetSegment(), SwitchResultRelForPartition(), BuildRootUpdateTupleDesc(), define SPLITMERGE_ACTION_PASSTHROUGH constant, remove dead code

Fixes #ISSUE_Number

What does this PR do?

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context

CI Skip Instructions


@lss602726449 lss602726449 force-pushed the fix_merge branch 9 times, most recently from 945b1ac to 38ad5ba Compare May 11, 2026 07:12
Previously, MERGE ... WHEN MATCHED THEN UPDATE SET <dist_key> = ...
was rejected with "cannot update column in merge with distributed
column". This commit adds support by extending the SplitMerge node
to handle distribution key updates via DELETE + INSERT routing.

Key changes:

Planner (preptlist.c, createplan.c, setrefs.c, cdbpath.c):
- Detect distribution key modification in MERGE UPDATE actions and
  set merge_need_split_update flag
- Add all target table columns to subplan targetlist so SplitMerge
  can project complete rows for INSERT
- Expand UPDATE action targetlists to include all columns (not just
  SET columns) using expand_insert_targetlist
- Build SplitMerge targetlist in N+M+1 format: N target table columns
  + M subplan columns + 1 DMLAction column
- Always use root table action lists (not per-partition adjusted lists)
  to ensure hashAttnos match root attribute numbers
- Add set_splitmerge_tlist_references for proper OUTER_VAR conversion

Executor (nodeSplitMerge.c, nodeModifyTable.c):
- SplitMerge splits MATCHED UPDATE into DELETE + INSERT tuple pair,
  each routed to the correct segment via hash computation
- NOT MATCHED rows get PASSTHROUGH action for normal ExecMerge processing
- ModifyTable handles DMLAction-tagged tuples from SplitMerge output
- Support lazy partition routing for CMD_MERGE DML_INSERT

Refactoring (nodeSplitMerge.c):
- Extract computeTargetSegment(), SwitchResultRelForPartition(),
  BuildRootUpdateTupleDesc() helper functions
- Define SPLITMERGE_ACTION_PASSTHROUGH constant
- Remove dead code and consolidate duplicated logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant