diff --git a/cstore_fdw.c b/cstore_fdw.c index 33a29ad32..2790efaca 100644 --- a/cstore_fdw.c +++ b/cstore_fdw.c @@ -825,6 +825,7 @@ FdwNewRelFileNode(Relation relation) if (OidIsValid(relation->rd_rel->relfilenode)) { RelationDropStorage(relation); + DeleteDataFileMetadataRowIfExists(relation->rd_rel->relfilenode); } if (OidIsValid(relation->rd_rel->reltablespace)) diff --git a/cstore_tableam.c b/cstore_tableam.c index 4e7f22c31..2744a9a76 100644 --- a/cstore_tableam.c +++ b/cstore_tableam.c @@ -445,6 +445,9 @@ cstore_relation_set_new_filenode(Relation rel, SMgrRelation srel; CStoreOptions *options = CStoreTableAMGetOptions(); + /* delete old relfilenode metadata */ + DeleteDataFileMetadataRowIfExists(rel->rd_node.relNode); + Assert(persistence == RELPERSISTENCE_PERMANENT); *freezeXid = RecentXmin; *minmulti = GetOldestMultiXactId(); diff --git a/expected/am_truncate.out b/expected/am_truncate.out index 99db7fe72..951a77f04 100644 --- a/expected/am_truncate.out +++ b/expected/am_truncate.out @@ -15,6 +15,7 @@ CREATE TABLE cstore_truncate_test_second (a int, b int) USING cstore_tableam; -- COMPRESSED CREATE TABLE cstore_truncate_test_compressed (a int, b int) USING cstore_tableam; CREATE TABLE cstore_truncate_test_regular (a int, b int); +SELECT count(*) AS cstore_data_files_before_truncate FROM cstore.cstore_data_files \gset INSERT INTO cstore_truncate_test select a, a from generate_series(1, 10) a; set cstore.compression = 'pglz'; INSERT INTO cstore_truncate_test_compressed select a, a from generate_series(1, 10) a; @@ -145,6 +146,13 @@ SELECT * from cstore_truncate_test; ---+--- (0 rows) +-- make sure TRUNATE deletes metadata for old relfilenode +SELECT :cstore_data_files_before_truncate - count(*) FROM cstore.cstore_data_files; + ?column? +---------- + 0 +(1 row) + -- test if a cached truncate from a pl/pgsql function works CREATE FUNCTION cstore_truncate_test_regular_func() RETURNS void AS $$ BEGIN diff --git a/expected/fdw_truncate.out b/expected/fdw_truncate.out index c92c15559..f357c6358 100644 --- a/expected/fdw_truncate.out +++ b/expected/fdw_truncate.out @@ -14,6 +14,7 @@ CREATE FOREIGN TABLE cstore_truncate_test (a int, b int) SERVER cstore_server; CREATE FOREIGN TABLE cstore_truncate_test_second (a int, b int) SERVER cstore_server; CREATE FOREIGN TABLE cstore_truncate_test_compressed (a int, b int) SERVER cstore_server OPTIONS (compression 'pglz'); CREATE TABLE cstore_truncate_test_regular (a int, b int); +SELECT count(*) AS cstore_data_files_before_truncate FROM cstore.cstore_data_files \gset INSERT INTO cstore_truncate_test select a, a from generate_series(1, 10) a; INSERT INTO cstore_truncate_test_compressed select a, a from generate_series(1, 10) a; INSERT INTO cstore_truncate_test_compressed select a, a from generate_series(1, 10) a; @@ -142,6 +143,13 @@ SELECT * from cstore_truncate_test; ---+--- (0 rows) +-- make sure TRUNATE deletes metadata for old relfilenode +SELECT :cstore_data_files_before_truncate - count(*) FROM cstore.cstore_data_files; + ?column? +---------- + 0 +(1 row) + -- test if a cached truncate from a pl/pgsql function works CREATE FUNCTION cstore_truncate_test_regular_func() RETURNS void AS $$ BEGIN diff --git a/sql/am_truncate.sql b/sql/am_truncate.sql index 3fdce1d82..ae86098ee 100644 --- a/sql/am_truncate.sql +++ b/sql/am_truncate.sql @@ -13,6 +13,8 @@ CREATE TABLE cstore_truncate_test_second (a int, b int) USING cstore_tableam; CREATE TABLE cstore_truncate_test_compressed (a int, b int) USING cstore_tableam; CREATE TABLE cstore_truncate_test_regular (a int, b int); +SELECT count(*) AS cstore_data_files_before_truncate FROM cstore.cstore_data_files \gset + INSERT INTO cstore_truncate_test select a, a from generate_series(1, 10) a; set cstore.compression = 'pglz'; @@ -60,6 +62,9 @@ SELECT * from cstore_truncate_test_regular; TRUNCATE TABLE cstore_truncate_test; SELECT * from cstore_truncate_test; +-- make sure TRUNATE deletes metadata for old relfilenode +SELECT :cstore_data_files_before_truncate - count(*) FROM cstore.cstore_data_files; + -- test if a cached truncate from a pl/pgsql function works CREATE FUNCTION cstore_truncate_test_regular_func() RETURNS void AS $$ BEGIN diff --git a/sql/fdw_truncate.sql b/sql/fdw_truncate.sql index a1849045e..b82e7fc7b 100644 --- a/sql/fdw_truncate.sql +++ b/sql/fdw_truncate.sql @@ -12,6 +12,8 @@ CREATE FOREIGN TABLE cstore_truncate_test_second (a int, b int) SERVER cstore_se CREATE FOREIGN TABLE cstore_truncate_test_compressed (a int, b int) SERVER cstore_server OPTIONS (compression 'pglz'); CREATE TABLE cstore_truncate_test_regular (a int, b int); +SELECT count(*) AS cstore_data_files_before_truncate FROM cstore.cstore_data_files \gset + INSERT INTO cstore_truncate_test select a, a from generate_series(1, 10) a; INSERT INTO cstore_truncate_test_compressed select a, a from generate_series(1, 10) a; @@ -57,6 +59,9 @@ SELECT * from cstore_truncate_test_regular; TRUNCATE TABLE cstore_truncate_test; SELECT * from cstore_truncate_test; +-- make sure TRUNATE deletes metadata for old relfilenode +SELECT :cstore_data_files_before_truncate - count(*) FROM cstore.cstore_data_files; + -- test if a cached truncate from a pl/pgsql function works CREATE FUNCTION cstore_truncate_test_regular_func() RETURNS void AS $$ BEGIN