This is an automated email from the ASF dual-hosted git repository.

reshke pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 386b74fb7d3bfd3f22cf3889f19e14b8983b7122
Author: Huansong Fu <[email protected]>
AuthorDate: Thu Aug 11 12:50:43 2022 -0700

    ALTER TABLE SET ACCESS METHOD: AOCO->Heap support
    
    As part of the ATSETAM support, this commit adds support for
    changing AM of a table from AOCO to heap. E.g.:
    
    ```
    CREATE TABLE foo (appendonly=true, orientation=column);
    ALTER TABLE foo SET ACCESS METHOD heap;
    -- Or:
    ALTER TABLE foo SET WITH (appendonly=false);
    ```
    
    Optionally, user can specify reloptions in a WITH clause too, e.g.:
    
    ```
    ALTER TABLE foo SET ACCESS METHOD heap WITH (fillfactor=70);
    ```
---
 src/backend/catalog/pg_appendonly.c              |  8 +++++---
 src/backend/commands/cluster.c                   | 13 ++++++++++---
 src/test/regress/expected/alter_table_set_am.out | 20 ++++++++++----------
 src/test/regress/sql/alter_table_set_am.sql      |  4 ++--
 4 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/src/backend/catalog/pg_appendonly.c 
b/src/backend/catalog/pg_appendonly.c
index 5b17075eef..48881330bd 100644
--- a/src/backend/catalog/pg_appendonly.c
+++ b/src/backend/catalog/pg_appendonly.c
@@ -31,6 +31,7 @@
 #include "access/table.h"
 #include "catalog/dependency.h"
 #include "catalog/indexing.h"
+#include "catalog/pg_attribute_encoding.h"
 #include "utils/builtins.h"
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
@@ -589,9 +590,10 @@ ATAOEntries(Form_pg_class relform1, Form_pg_class relform2,
                        switch(relform2->relam)
                        {
                                case HEAP_TABLE_AM_OID:
-                                       ereport(ERROR,
-                                                       
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                                               errmsg("alter 
table does not support switch from AOCO to Heap")));
+                                       /* For pg_appendonly entries, it's the 
same as AO->Heap. */
+                                       
TransferAppendonlyEntries(relform1->oid, relform2->oid);
+                                       /* Remove the pg_attribute_encoding 
entries, since heap tables shouldn't have these. */
+                                       
RemoveAttributeEncodingsByRelid(relform1->oid);
                                        break;
                                case AO_ROW_TABLE_AM_OID:
                                        /* For pg_appendonly entries, it's same 
as AO->AO/CO. */
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index 80554d5028..d7ba0c34b8 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -913,9 +913,16 @@ make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, Oid 
NewAccessMethod,
 
        CacheInvalidateRelcacheByRelid(OIDNewHeap);
 
-       cloneAttributeEncoding(OIDOldHeap,
-                                                  OIDNewHeap,
-                                                  
RelationGetNumberOfAttributes(OldHeap));
+       /* 
+        * Copy the pg_attribute_encoding entries over if new table needs them.
+        * Note that in the case of AM change from heap/ao to aoco, we still 
need 
+        * to do this since we created those entries for the heap/ao table at 
the 
+        * phase 2 of ATSETAM (see ATExecCmd).
+        */
+       if (NewAccessMethod == AO_COLUMN_TABLE_AM_OID)
+               cloneAttributeEncoding(OIDOldHeap,
+                                                          OIDNewHeap,
+                                                          
RelationGetNumberOfAttributes(OldHeap));
 
        table_close(OldHeap, NoLock);
 
diff --git a/src/test/regress/expected/alter_table_set_am.out 
b/src/test/regress/expected/alter_table_set_am.out
index 15da82171c..f809c9cb14 100644
--- a/src/test/regress/expected/alter_table_set_am.out
+++ b/src/test/regress/expected/alter_table_set_am.out
@@ -741,10 +741,10 @@ SELECT * FROM gp_toolkit.__gp_aoblkdir('ao2co3');
 SELECT c.relname, a.attnum, a.attoptions FROM pg_attribute_encoding a, 
pg_class c WHERE a.attrelid = c.oid AND c.relname LIKE 'ao2co%';
  relname | attnum |                       attoptions                        
 ---------+--------+---------------------------------------------------------
- ao2co   |      1 | {compresstype=zlib,blocksize=65536,compresslevel=5}
- ao2co   |      2 | {compresstype=zlib,blocksize=65536,compresslevel=5}
- ao2co2  |      1 | {compresstype=zlib,blocksize=65536,compresslevel=5}
- ao2co2  |      2 | {compresstype=zlib,blocksize=65536,compresslevel=5}
+ ao2co   |      1 | {compresstype=zlib,compresslevel=5,blocksize=65536}
+ ao2co   |      2 | {compresstype=zlib,compresslevel=5,blocksize=65536}
+ ao2co2  |      1 | {compresstype=zlib,compresslevel=5,blocksize=65536}
+ ao2co2  |      2 | {compresstype=zlib,compresslevel=5,blocksize=65536}
  ao2co3  |      1 | {blocksize=32768,compresstype=rle_type,compresslevel=3}
  ao2co3  |      2 | {blocksize=32768,compresstype=rle_type,compresslevel=3}
  ao2co4  |      1 | {blocksize=32768,compresstype=rle_type,compresslevel=3}
@@ -788,7 +788,7 @@ INSERT INTO co2heap2 SELECT i,i FROM generate_series(1,5) i;
 INSERT INTO co2heap3 SELECT i,i FROM generate_series(1,5) i;
 INSERT INTO co2heap4 SELECT i,i FROM generate_series(1,5) i;
 -- Prior-ATSETAM checks:
--- Check once that the AO tables have the custom reloptions
+-- Check once that the AO tables have the custom reloptions 
 SELECT relname, reloptions FROM pg_class WHERE relname LIKE 'co2heap%';
  relname  |            reloptions             
 ----------+-----------------------------------
@@ -869,7 +869,7 @@ SELECT count(*) FROM co2heap4;
 (1 row)
 
 -- No AO aux tables should be left.
--- Only testing 2 out of the 4 tables being created, where the tables were 
altered w/wo reloptions.
+-- Only testing 2 out of the 4 tables being created, where the tables were 
altered w/wo reloptions. 
 -- No need to test the other ones created by the alternative syntax SET WITH().
 SELECT * FROM gp_toolkit.__gp_aoseg('co2heap');
 ERROR:  'co2heap' is not an append-only row relation
@@ -1278,10 +1278,10 @@ SELECT count(*) FROM 
gp_toolkit.__gp_aocsseg('heap2co3');
 SELECT c.relname, a.attnum, a.attoptions FROM pg_attribute_encoding a, 
pg_class c WHERE a.attrelid = c.oid AND c.relname LIKE 'heap2co%';
  relname  | attnum |                     attoptions                      
 ----------+--------+-----------------------------------------------------
- heap2co  |      2 | {compresstype=zlib,blocksize=65536,compresslevel=5}
- heap2co  |      1 | {compresstype=zlib,blocksize=65536,compresslevel=5}
- heap2co2 |      2 | {compresstype=zlib,blocksize=65536,compresslevel=5}
- heap2co2 |      1 | {compresstype=zlib,blocksize=65536,compresslevel=5}
+ heap2co  |      2 | {compresstype=zlib,compresslevel=5,blocksize=65536}
+ heap2co  |      1 | {compresstype=zlib,compresslevel=5,blocksize=65536}
+ heap2co2 |      2 | {compresstype=zlib,compresslevel=5,blocksize=65536}
+ heap2co2 |      1 | {compresstype=zlib,compresslevel=5,blocksize=65536}
  heap2co3 |      2 | {blocksize=32768,compresslevel=3,compresstype=zlib}
  heap2co3 |      1 | {blocksize=32768,compresslevel=3,compresstype=zlib}
  heap2co4 |      2 | {blocksize=32768,compresslevel=3,compresstype=zlib}
diff --git a/src/test/regress/sql/alter_table_set_am.sql 
b/src/test/regress/sql/alter_table_set_am.sql
index 9520360981..cc572c2fc1 100644
--- a/src/test/regress/sql/alter_table_set_am.sql
+++ b/src/test/regress/sql/alter_table_set_am.sql
@@ -440,7 +440,7 @@ INSERT INTO co2heap3 SELECT i,i FROM generate_series(1,5) i;
 INSERT INTO co2heap4 SELECT i,i FROM generate_series(1,5) i;
 
 -- Prior-ATSETAM checks:
--- Check once that the AO tables have the custom reloptions
+-- Check once that the AO tables have the custom reloptions 
 SELECT relname, reloptions FROM pg_class WHERE relname LIKE 'co2heap%';
 -- Check once that the AO tables have relfrozenxid = 0
 SELECT relname, relfrozenxid FROM pg_class WHERE relname LIKE 'co2heap%';
@@ -475,7 +475,7 @@ SELECT count(*) FROM co2heap3;
 SELECT count(*) FROM co2heap4;
 
 -- No AO aux tables should be left.
--- Only testing 2 out of the 4 tables being created, where the tables were 
altered w/wo reloptions.
+-- Only testing 2 out of the 4 tables being created, where the tables were 
altered w/wo reloptions. 
 -- No need to test the other ones created by the alternative syntax SET WITH().
 SELECT * FROM gp_toolkit.__gp_aoseg('co2heap');
 SELECT * FROM gp_toolkit.__gp_aovisimap('co2heap');


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to