Never distribute a CTAS table by a resjunk column - #404
Open
Alena0704 wants to merge 1 commit into
Open
Conversation
Alena0704
marked this pull request as ready for review
July 28, 2026 15:36
Alena0704
marked this pull request as draft
July 28, 2026 15:48
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
force-pushed
the
ctas-resjunk-distkey-fix
branch
from
July 29, 2026 09:56
00456bc to
8d16566
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.