Skip to content

Commit 976e4c8

Browse files
authored
Fix crash that partition table has no encoding attributes for new columns (apache#820)
The encoding attributes are not added for new columns added by alter table. The parent hasn't any information on the new columns. Its encoding is determined by the storage type, or default encoding.
1 parent e24b737 commit 976e4c8

File tree

7 files changed

+88
-8
lines changed

7 files changed

+88
-8
lines changed

src/backend/access/aocs/aocsam_handler.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,7 @@ aoco_transform_column_encoding_clauses(Relation rel, List *aocoColumnEncoding,
23422342
bool foundCompressTypeNone = false;
23432343
char *cmplevel = NULL;
23442344
bool foundBlockSize = false;
2345+
bool hasAttrs;
23452346
char *arg;
23462347
List *retList = list_copy(aocoColumnEncoding);
23472348
DefElem *el;
@@ -2352,6 +2353,14 @@ aoco_transform_column_encoding_clauses(Relation rel, List *aocoColumnEncoding,
23522353
char *compresstype = NULL;
23532354
NameData compresstype_nd;
23542355

2356+
/*
2357+
* The relam of partition table may be ao table, but partition table
2358+
* has no entry in pg_appendonly. It shouldn't fetch encoding options
2359+
* from here for partition tables. See details in function
2360+
* transformColumnEncoding.
2361+
*/
2362+
hasAttrs = rel && rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE;
2363+
23552364
foreach(lc, aocoColumnEncoding)
23562365
{
23572366
dl = (DefElem *) lfirst(lc);
@@ -2388,7 +2397,7 @@ aoco_transform_column_encoding_clauses(Relation rel, List *aocoColumnEncoding,
23882397
* table setting in pg_appendonly is preferred over default
23892398
* options in GUC gp_default_storage_option.
23902399
*/
2391-
if (rel)
2400+
if (hasAttrs)
23922401
{
23932402
GetAppendOnlyEntryAttributes(RelationGetRelid(rel),
23942403
&blocksize,
@@ -2399,7 +2408,7 @@ aoco_transform_column_encoding_clauses(Relation rel, List *aocoColumnEncoding,
23992408
compresstype = NameStr(compresstype_nd);
24002409
}
24012410

2402-
if (!foundCompressType && rel && compresstype[0])
2411+
if (!foundCompressType && hasAttrs && compresstype[0])
24032412
{
24042413
el = makeDefElem("compresstype", (Node *) makeString(pstrdup(compresstype)), -1);
24052414
retList = lappend(retList, el);

src/backend/access/common/reloptions_gp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,9 +1689,10 @@ transformColumnEncoding(const TableAmRoutine *tam, Relation rel, List *colDefs,
16891689
encoding = copyObject(deflt->encoding);
16901690
else if (!explicitOnly)
16911691
{
1692-
if (parentenc)
1692+
ColumnReferenceStorageDirective *parent_col_encoding;
1693+
parent_col_encoding = find_crsd(d->colname, parentenc);
1694+
if (parent_col_encoding)
16931695
{
1694-
ColumnReferenceStorageDirective *parent_col_encoding = find_crsd(d->colname, parentenc);
16951696
encoding = transformStorageEncodingClause(parent_col_encoding->encoding, true);
16961697
}
16971698
else if (d->typeName) {
@@ -1701,7 +1702,6 @@ transformColumnEncoding(const TableAmRoutine *tam, Relation rel, List *colDefs,
17011702
encoding = tam->transform_column_encoding_clauses(rel, encoding, true, true);
17021703
}
17031704
if (!encoding && createDefaultOne) {
1704-
Assert(tam == GetTableAmRoutineByAmId(rel->rd_rel->relam));
17051705
encoding = default_column_encoding_clause(rel);
17061706
}
17071707
}

src/backend/commands/tablecmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8579,7 +8579,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
85798579
add_column_datatype_dependency(myrelid, newattnum, attribute.atttypid);
85808580
add_column_collation_dependency(myrelid, newattnum, attribute.attcollation);
85818581

8582-
if (OidIsValid(rel->rd_rel->relam) && rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
8582+
if (OidIsValid(rel->rd_rel->relam))
85838583
{
85848584
List *enc;
85858585
const TableAmRoutine *tam = GetTableAmRoutineByAmId(rel->rd_rel->relam);

src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4042,7 +4042,7 @@ CTranslatorDXLToPlStmt::TranslateDXLSequence(
40424042
Plan *
40434043
CTranslatorDXLToPlStmt::TranslateDXLDynTblScan(
40444044
const CDXLNode *dyn_tbl_scan_dxlnode, CDXLTranslateContext *output_context,
4045-
CDXLTranslationContextArray *ctxt_translation_prev_siblings)
4045+
CDXLTranslationContextArray * /*ctxt_translation_prev_siblings*/)
40464046
{
40474047
// translate table descriptor into a range table entry
40484048
CDXLPhysicalDynamicTableScan *dyn_tbl_scan_dxlop =

src/test/regress/expected/uao_ddl/gp_partition_tables_column.out

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,58 @@ select relname, relkind, amname, reloptions from pg_class c left join pg_am am o
177177
pt2_ao_column_1_prt_11 | r | ao_column |
178178
(1 row)
179179

180+
create table gppt_ao_column.alter_part_tab1 (id SERIAL, a1 int, a2 char(5), a3 text)
181+
WITH (appendonly=true, orientation=column, compresstype=zlib)
182+
partition by list(a2) subpartition by range(a1)
183+
subpartition template (
184+
default subpartition subothers,
185+
subpartition sp1 start(1) end(9) with(appendonly=true,orientation=column,compresstype=rle_type),
186+
subpartition sp2 start(11) end(20) with(appendonly=true,orientation=column,compresstype=zstd))
187+
(partition p1 values('val1') , partition p2 values('val2'));
188+
create index on gppt_ao_column.alter_part_tab1(a1);
189+
create index on gppt_ao_column.alter_part_tab1 using bitmap(a3);
190+
alter table gppt_ao_column.alter_part_tab1 add column a4 numeric default 5.5;
191+
update gppt_ao_column.alter_part_tab1 set a4 = a1 % 2;
192+
ALTER TABLE gppt_ao_column.alter_part_tab1 ADD partition p31 values(1);
193+
\d+ gppt_ao_column.alter_part_tab1
194+
Partitioned table "gppt_ao_column.alter_part_tab1"
195+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Compression Type | Compression Level | Block Size | Description
196+
--------+--------------+-----------+----------+---------------------------------------------+----------+--------------+------------------+-------------------+------------+-------------
197+
id | integer | | not null | nextval('alter_part_tab1_id_seq'::regclass) | plain | | zlib | 1 | 32768 |
198+
a1 | integer | | | | plain | | zlib | 1 | 32768 |
199+
a2 | character(5) | | | | extended | | zlib | 1 | 32768 |
200+
a3 | text | | | | extended | | zlib | 1 | 32768 |
201+
a4 | numeric | | | 5.5 | main | | none | 0 | 32768 |
202+
Partition key: LIST (a2)
203+
Indexes:
204+
"alter_part_tab1_a1_idx" btree (a1)
205+
"alter_part_tab1_a3_idx" bitmap (a3)
206+
Partitions: alter_part_tab1_1_prt_p1 FOR VALUES IN ('val1 '), PARTITIONED,
207+
alter_part_tab1_1_prt_p2 FOR VALUES IN ('val2 '), PARTITIONED,
208+
alter_part_tab1_1_prt_p31 FOR VALUES IN ('1 '), PARTITIONED
209+
Distributed by: (id)
210+
Options: compresstype=zlib
211+
212+
\d+ gppt_ao_column.alter_part_tab1_1_prt_p31
213+
Partitioned table "gppt_ao_column.alter_part_tab1_1_prt_p31"
214+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Compression Type | Compression Level | Block Size | Description
215+
--------+--------------+-----------+----------+---------------------------------------------+----------+--------------+------------------+-------------------+------------+-------------
216+
id | integer | | not null | nextval('alter_part_tab1_id_seq'::regclass) | plain | | zlib | 1 | 32768 |
217+
a1 | integer | | | | plain | | zlib | 1 | 32768 |
218+
a2 | character(5) | | | | extended | | zlib | 1 | 32768 |
219+
a3 | text | | | | extended | | zlib | 1 | 32768 |
220+
a4 | numeric | | | 5.5 | main | | none | 0 | 32768 |
221+
Partition of: alter_part_tab1 FOR VALUES IN ('1 ')
222+
Partition constraint: ((a2 IS NOT NULL) AND (a2 = '1 '::character(5)))
223+
Partition key: RANGE (a1)
224+
Indexes:
225+
"alter_part_tab1_1_prt_p31_a1_idx" btree (a1)
226+
"alter_part_tab1_1_prt_p31_a3_idx" bitmap (a3)
227+
Partitions: alter_part_tab1_1_prt_p31_2_prt_sp1 FOR VALUES FROM (1) TO (9),
228+
alter_part_tab1_1_prt_p31_2_prt_sp2 FOR VALUES FROM (11) TO (20),
229+
alter_part_tab1_1_prt_p31_2_prt_subothers DEFAULT
230+
Distributed by: (id)
231+
Options: compresstype=zlib
232+
180233
reset default_table_access_method;
181234
reset search_path;

src/test/regress/greenplum_schedule

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ test: uao_ddl/alter_ao_table_setstorage_column uao_ddl/alter_ao_table_col_ddl_ro
230230
test: uao_ddl/alter_ao_part_exch_column uao_ddl/alter_ao_part_tables_row uao_ddl/create_ao_table_500cols_column uao_ddl/alter_ao_part_tables_column
231231

232232
test: uao_ddl/alter_drop_allcol_row uao_ddl/alter_drop_allcol_column uao_ddl/alter_rollback_row uao_ddl/alter_rollback_column uao_ddl/uao_allalter_row uao_ddl/uao_allalter_column
233-
233+
test: uao_ddl/gp_partition_tables_column uao_ddl/gp_partition_tables_row
234234
# These tests use gp_select_invisible and VACUUM, and will get confused if there are
235235
# concurrent transactions holding back the global xmin.
236236

src/test/regress/sql/uao_ddl/gp_partition_tables_column.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,23 @@ ALTER TABLE gppt_ao_column.pt2_ao_column ADD PARTITION START ('2027-01-01') INCL
5858
\d+ gppt_ao_column.pt2_ao_column_1_prt_11
5959
select relname, relkind, amname, reloptions from pg_class c left join pg_am am on c.relam=am.oid where relname='pt2_ao_column_1_prt_11';
6060

61+
create table gppt_ao_column.alter_part_tab1 (id SERIAL, a1 int, a2 char(5), a3 text)
62+
WITH (appendonly=true, orientation=column, compresstype=zlib)
63+
partition by list(a2) subpartition by range(a1)
64+
subpartition template (
65+
default subpartition subothers,
66+
subpartition sp1 start(1) end(9) with(appendonly=true,orientation=column,compresstype=rle_type),
67+
subpartition sp2 start(11) end(20) with(appendonly=true,orientation=column,compresstype=zstd))
68+
(partition p1 values('val1') , partition p2 values('val2'));
69+
70+
create index on gppt_ao_column.alter_part_tab1(a1);
71+
create index on gppt_ao_column.alter_part_tab1 using bitmap(a3);
72+
alter table gppt_ao_column.alter_part_tab1 add column a4 numeric default 5.5;
73+
update gppt_ao_column.alter_part_tab1 set a4 = a1 % 2;
74+
ALTER TABLE gppt_ao_column.alter_part_tab1 ADD partition p31 values(1);
75+
\d+ gppt_ao_column.alter_part_tab1
76+
\d+ gppt_ao_column.alter_part_tab1_1_prt_p31
77+
78+
6179
reset default_table_access_method;
6280
reset search_path;

0 commit comments

Comments
 (0)