Skip to content

Never distribute a CTAS table by a resjunk column - #404

Open
Alena0704 wants to merge 1 commit into
OPENGPDB_STABLEfrom
ctas-resjunk-distkey-fix
Open

Never distribute a CTAS table by a resjunk column#404
Alena0704 wants to merge 1 commit into
OPENGPDB_STABLEfrom
ctas-resjunk-distkey-fix

Conversation

@Alena0704

@Alena0704 Alena0704 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Never distribute a CTAS table by a resjunk column

Some queries need auxiliary resjunk columns in the targetlist, an ORDER BY expression that is not in the select list for instance. They are needed to execute the query but are not columns of the resulting relation.

When CREATE TABLE AS has no explicit DISTRIBUTED BY clause and the distribution key cannot be deduced from the flow, the policy falls back to hashing on the first hashable column of the plan targetlist. That scan did not skip resjunk entries, so if every visible column was of a type with no hash operator class while a resjunk one was hashable, the resjunk column became the distribution key. Its resno then pointed past the last attribute of the new table, and reading the table crashed the backend:

create table resjunk_test as
with cte as (select point(1, 2) as a, random() as b)
select a from cte order by b;
select * from resjunk_test; -- segmentation fault

Skip resjunk entries, as get_partitioned_policy_from_flow() already does, so that such a table ends up randomly distributed instead.

Adapted from GreengageDB/greengage#328.

@Alena0704
Alena0704 marked this pull request as ready for review July 28, 2026 15:36
@Alena0704
Alena0704 marked this pull request as draft July 28, 2026 15:48
@Alena0704
Alena0704 marked this pull request as ready for review July 28, 2026 16:31
Some queries need auxiliary resjunk columns in the targetlist, an ORDER
BY expression that is not in the select list for instance.  They are
needed to execute the query but are not columns of the resulting
relation.

When CREATE TABLE AS has no explicit DISTRIBUTED BY clause and the
distribution key cannot be deduced from the flow, the policy falls back
to hashing on the first hashable column of the plan targetlist.  That
scan did not skip resjunk entries, so if every visible column was of a
type with no hash operator class while a resjunk one was hashable, the
resjunk column became the distribution key.  Its resno then pointed past
the last attribute of the new table, and reading the table crashed the
backend:

  create table resjunk_test as
      with cte as (select point(1, 2) as a, random() as b)
      select a from cte order by b;
  select * from resjunk_test;   -- segmentation fault

Skip resjunk entries, as get_partitioned_policy_from_flow() already
does, so that such a table ends up randomly distributed instead.

Adapted from GreengageDB/greengage#328.
@Alena0704
Alena0704 force-pushed the ctas-resjunk-distkey-fix branch from 00456bc to 8d16566 Compare July 29, 2026 09:56
@Alena0704
Alena0704 requested a review from andr-sokolov July 29, 2026 12:03
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