[Bug] ORCA returns wrong system column values through a partially pruned CTE consumer

オープン
#2,458 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@yjhjstz がすでに取り組んでいます。

2026年12月1日 から。

  • #2465 @yjhjstz による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c, postgresql
領域
backend, databases

調査の方向性

Start by tracing CPhysicalCTEConsumer's fallback when the producer has no used mask, then inspect how the producer's Shared Scan output columns are ordered. The reproduction SQL and EXPLAIN (verbose, costs off) in the issue show the mismatch: the consumer requests gp_segment_id but reads ctid by position. Done when ORCA returns the same system column values as the Postgres planner for the supplied queries.

索引モデルが issue の本文から書いたものです。

説明

type: Bug

Bug reported with the assistance of Claude Code

Apache Cloudberry version

main (reproduced at e8113db9679)

What happened

With ORCA on, selecting a system column (gp_segment_id, tableoid, ...) through a CTE consumer that keeps only some of its producer's columns returns wrong values. One way to hit it is the full join expansion (optimizer_expand_fulljoin = on): wa.gp_segment_id comes back as 0 on every row, wa.tableoid::regclass as -, and when wa.ctid is selected too, gp_segment_id becomes garbage such as 939.

EXPLAIN VERBOSE labels the consumer's output as share0_ref2.ctid although the query asked for gp_segment_id:

->  Shared Scan (share slice:id 1:0)
      Output: share0_ref1.a, share0_ref1.ctid, share0_ref1.xmin, share0_ref1.cmin, share0_ref1.xmax, share0_ref1.cmax, share0_ref1.tableoid, share0_ref1.gp_segment_id, share0_ref1.gp_foreign_server
      ->  Seq Scan on public.wa
...
      ->  Hash Left Join
            Output: share0_ref2.a, share0_ref2.ctid, share1_ref2.b
            ->  Shared Scan (share slice:id 1:0)
                  Output: share0_ref2.a, share0_ref2.ctid
What you think should happen instead

ORCA should return the same values as the Postgres planner:

 a  | b  | gp_segment_id
----+----+---------------
 -1 | -1 |             2
  0 |  0 |             1
  1 |  1 |             1
    |  2 |

(ORCA currently returns 0 for the first three rows.)

Cause: a CTE created by an xform such as the full join expansion is not pruned, so its producer has no used mask and its Shared Scan emits every column of the scan, system columns included. In CPhysicalCTEConsumer, the fallback for a producer without a used mask keeps only the consumer's used columns (a, gp_segment_id). ShareInputScan does not project; it reads the shared tuple by position. So the consumer's second column is the producer's second column, ctid, not gp_segment_id.

How to reproduce

On a 3-segment demo cluster:

create table wa (a int, x int, y text) distributed by (a);
create table wb (b int, z int, w text) distributed by (b);
insert into wa select i, i, 'x' from generate_series(-1,1) i;
insert into wb select i, i, 'y' from generate_series(-1,2) i;
analyze wa; analyze wb;

set optimizer = on;
set optimizer_expand_fulljoin = on;
select disable_xform('CXformFullOuterJoin2HashJoin');
select disable_xform('CXformImplementFullOuterMergeJoin');

SELECT a, b, wa.gp_segment_id FROM wa FULL JOIN wb ON a = b order by 1,2;          -- ORCA: 0,0,0; planner: 2,1,1
SELECT a, b, wa.tableoid::regclass FROM wa FULL JOIN wb ON a = b order by 1,2;     -- ORCA: '-'; planner: wa
SELECT a, b, wa.gp_segment_id, wa.ctid FROM wa FULL JOIN wb ON a = b order by 1,2; -- gp_segment_id is garbage
EXPLAIN (verbose, costs off) SELECT a, b, wa.gp_segment_id FROM wa FULL JOIN wb ON a = b order by 1,2;

Compare with set optimizer = off.

Operating System

Rocky Linux 9 (apache/incubator-cloudberry:cbdb-build-rocky9-latest)

Anything else

Happens every time. Queries that only use a leading prefix of the producer's columns are not affected, which is the common case, so it only surfaces when a system column (or any non-prefix subset) is referenced through such a consumer.

Are you willing to submit PR?
  • Yes, I am willing to submit a PR!
Code of Conduct
主要言語
C
スター
1.5k
フォーク
280
平均マージ
4日 21時間
マージ済み PR(30日)
122

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/cloudberry のほかの issue

apache/cloudberry の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。