From 1e619005f9523ae7a8ba5d246ea74afc66bc03fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=B2=91=E7=82=AF?= <834727721@qq.com> Date: Mon, 24 Mar 2025 23:13:58 +0800 Subject: [PATCH 1/2] add default compress params for test only --- src/common/backend/utils/misc/guc.cpp | 39 +++++++++++++++++++ .../optimizer/commands/indexcmds.cpp | 25 +++++++++++- .../optimizer/commands/tablecmds.cpp | 28 ++++++++++++- .../storage/access/common/reloptions.cpp | 5 ++- src/include/access/reloptions.h | 2 +- .../knl/knl_guc/knl_session_attr_sql.h | 3 ++ 6 files changed, 96 insertions(+), 6 deletions(-) diff --git a/src/common/backend/utils/misc/guc.cpp b/src/common/backend/utils/misc/guc.cpp index 73608efd2e..d9e6e91397 100755 --- a/src/common/backend/utils/misc/guc.cpp +++ b/src/common/backend/utils/misc/guc.cpp @@ -3286,6 +3286,45 @@ static void InitConfigureNamesInt() NULL, NULL, NULL}, + {{"default_compress_type", + PGC_USERSET, + NODE_SINGLENODE, + QUERY_TUNING_METHOD, + gettext_noop("default compress type"), + NULL}, + &u_sess->attr.attr_sql.default_compress_type, + 0, + 0, + 4, + NULL, + NULL, + NULL}, + {{"default_compress_chunk_size", + PGC_USERSET, + NODE_SINGLENODE, + QUERY_TUNING_METHOD, + gettext_noop("default compress chunk size"), + NULL}, + &u_sess->attr.attr_sql.default_compress_chunk_size, + 0, + 0, + 4096, + NULL, + NULL, + NULL}, + {{"default_compress_level", + PGC_USERSET, + NODE_SINGLENODE, + QUERY_TUNING_METHOD, + gettext_noop("default compress level"), + NULL}, + &u_sess->attr.attr_sql.default_compress_level, + 0, + 0, + 31, + NULL, + NULL, + NULL}, /* End-of-list marker */ {{NULL, (GucContext)0, diff --git a/src/gausskernel/optimizer/commands/indexcmds.cpp b/src/gausskernel/optimizer/commands/indexcmds.cpp index 9d56450271..936b4fd8de 100644 --- a/src/gausskernel/optimizer/commands/indexcmds.cpp +++ b/src/gausskernel/optimizer/commands/indexcmds.cpp @@ -804,17 +804,38 @@ ObjectAddress DefineIndex(Oid relationId, IndexStmt* stmt, Oid indexRelationId, rel = heap_open(relationId, lockmode); bool segment = get_rel_segment(rel); - TableCreateSupport indexCreateSupport{(int)COMPRESS_TYPE_NONE, false, false, false, false, false, true, false}; + TableCreateSupport indexCreateSupport{u_sess->attr.attr_sql.default_compress_type, false, false, false, false, false, true, false}; + bool hasCompressType = false; ListCell *cell = NULL; foreach (cell, stmt->options) { DefElem *defElem = (DefElem *)lfirst(cell); - SetOneOfCompressOption(defElem, &indexCreateSupport); + SetOneOfCompressOption(defElem, &indexCreateSupport, &hasCompressType); if (pg_strcasecmp(defElem->defname, "deduplication") == 0){ has_dedup_opt = true; } } + + if (indexCreateSupport.compressType != COMPRESS_TYPE_NONE) { + if (!hasCompressType) { + DefElem* def = makeDefElem("compresstype", (Node *)makeInteger(indexCreateSupport.compressType)); + stmt->options = lappend(stmt->options, def); + } + if (!indexCreateSupport.compressLevel && u_sess->attr.attr_sql.default_compress_level != 0) { + DefElem* def = makeDefElem("compress_level", + (Node*)makeInteger(u_sess->attr.attr_sql.default_compress_level)); + stmt->options = lappend(stmt->options, def); + indexCreateSupport.compressLevel = true; + } + if (!indexCreateSupport.compressChunkSize && u_sess->attr.attr_sql.default_compress_chunk_size != 0) { + DefElem* def = makeDefElem("compress_chunk_size", + (Node*)makeInteger(u_sess->attr.attr_sql.default_compress_chunk_size)); + stmt->options = lappend(stmt->options, def); + indexCreateSupport.compressChunkSize = true; + } + } + CheckCompressOption(&indexCreateSupport); /* do not suppport to create compressed index for temp table. */ if ((indexCreateSupport.compressType != (int)COMPRESS_TYPE_NONE) && diff --git a/src/gausskernel/optimizer/commands/tablecmds.cpp b/src/gausskernel/optimizer/commands/tablecmds.cpp index f7de700db3..73d652ddef 100755 --- a/src/gausskernel/optimizer/commands/tablecmds.cpp +++ b/src/gausskernel/optimizer/commands/tablecmds.cpp @@ -1211,7 +1211,8 @@ static List* AddDefaultOptionsIfNeed(List* options, const char relkind, CreateSt bool isUstore = false; bool assignedStorageType = false; bool segment = false; - TableCreateSupport tableCreateSupport{(int)COMPRESS_TYPE_NONE, false, false, false, false, false, true, false}; + TableCreateSupport tableCreateSupport{u_sess->attr.attr_sql.default_compress_type, false, false, false, false, false, true, false}; + bool hasCompressType = false; bool hasOids = false; (void)isOrientationSet(options, NULL, false); foreach (cell, options) { @@ -1248,7 +1249,7 @@ static List* AddDefaultOptionsIfNeed(List* options, const char relkind, CreateSt } else if (pg_strcasecmp(def->defname, "segment") == 0) { segment = ReadBoolFromDefElem(def); } else { - SetOneOfCompressOption(def, &tableCreateSupport); + SetOneOfCompressOption(def, &tableCreateSupport, &hasCompressType); } if (pg_strcasecmp(def->defname, "orientation") == 0 && pg_strcasecmp(defGetString(def), ORIENTATION_ORC) == 0) { @@ -1276,6 +1277,29 @@ static List* AddDefaultOptionsIfNeed(List* options, const char relkind, CreateSt } } + if (tableCreateSupport.compressType != COMPRESS_TYPE_NONE) { + if (!hasCompressType) { + DefElem* def = makeDefElem("compresstype", (Node *)makeInteger(tableCreateSupport.compressType)); + res = lappend(res, def); + } + if (!tableCreateSupport.compressLevel && u_sess->attr.attr_sql.default_compress_level != 0) { + DefElem* def = makeDefElem("compress_level", + (Node*)makeInteger(u_sess->attr.attr_sql.default_compress_level)); + res = lappend(res, def); + tableCreateSupport.compressLevel = true; + } + if (!tableCreateSupport.compressChunkSize && u_sess->attr.attr_sql.default_compress_chunk_size != 0) { + DefElem* def = makeDefElem("compress_chunk_size", + (Node*)makeInteger(u_sess->attr.attr_sql.default_compress_chunk_size)); + res = lappend(res, def); + tableCreateSupport.compressChunkSize = true; + } + + if (options == NULL) { + options = res; + } + } + if (isUstore == true && hasOids == true) { ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("OIDS option is not supported for ustore table"))); } diff --git a/src/gausskernel/storage/access/common/reloptions.cpp b/src/gausskernel/storage/access/common/reloptions.cpp index 68fad62e8c..281274a023 100644 --- a/src/gausskernel/storage/access/common/reloptions.cpp +++ b/src/gausskernel/storage/access/common/reloptions.cpp @@ -3088,12 +3088,15 @@ bool ReadBoolFromDefElem(DefElem* defElem) return result; } -void SetOneOfCompressOption(DefElem* defElem, TableCreateSupport* tableCreateSupport) +void SetOneOfCompressOption(DefElem* defElem, TableCreateSupport* tableCreateSupport, bool *hasCompressType) { auto defname = defElem->defname; if (pg_strcasecmp(defname, "compresstype") == 0) { /* compresstype must be a valid number type */ tableCreateSupport->compressType = (int)strtol(defGetString(defElem), NULL, RS_CUSTOM_VALUE_TEN); + if (hasCompressType != NULL) { + *hasCompressType = true; + } } else if (pg_strcasecmp(defname, "compress_chunk_size") == 0) { tableCreateSupport->compressChunkSize = true; } else if (pg_strcasecmp(defname, "compress_prealloc_chunks") == 0) { diff --git a/src/include/access/reloptions.h b/src/include/access/reloptions.h index b701b91d51..c39f3f73bc 100644 --- a/src/include/access/reloptions.h +++ b/src/include/access/reloptions.h @@ -306,7 +306,7 @@ extern List* RemoveRelOption(List* options, const char* optName, bool* removed); void RowTblCheckCompressionOption(List *options, int8 rowCompress = REL_CMPRS_PAGE_PLAIN); void RowTblCheckHashBucketOption(List* options, StdRdOptions* std_opt); void ForbidUserToSetCompressedOptions(List *options); -void SetOneOfCompressOption(DefElem* defElem, TableCreateSupport *tableCreateSupport); +void SetOneOfCompressOption(DefElem* defElem, TableCreateSupport *tableCreateSupport, bool *hasCompressType = NULL); bool ReadBoolFromDefElem(DefElem* defElem); void CheckCompressOption(TableCreateSupport *tableCreateSupport); bool CheckSegmentStorageOption(List *options); diff --git a/src/include/knl/knl_guc/knl_session_attr_sql.h b/src/include/knl/knl_guc/knl_session_attr_sql.h index 0b482311b5..e068094489 100644 --- a/src/include/knl/knl_guc/knl_session_attr_sql.h +++ b/src/include/knl/knl_guc/knl_session_attr_sql.h @@ -247,6 +247,9 @@ typedef struct knl_session_attr_sql { bool mot_allow_index_on_nullable_column; bool enable_default_ustore_table; + int default_compress_type; + int default_compress_chunk_size; + int default_compress_level; char* ustore_attr; #ifdef ENABLE_UT char* ustore_unit_test; -- Gitee From 4778f274931a62496ef2a2413bbefe6fecb1cef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=B2=91=E7=82=AF?= <834727721@qq.com> Date: Wed, 26 Mar 2025 17:15:29 +0800 Subject: [PATCH 2/2] handle cases where relation has been removed during compressed table accessing --- src/gausskernel/storage/smgr/cfs/cfs_md.cpp | 14 ++++++++++++++ src/gausskernel/storage/smgr/md.cpp | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/gausskernel/storage/smgr/cfs/cfs_md.cpp b/src/gausskernel/storage/smgr/cfs/cfs_md.cpp index 6fd99a521d..78998e1af3 100644 --- a/src/gausskernel/storage/smgr/cfs/cfs_md.cpp +++ b/src/gausskernel/storage/smgr/cfs/cfs_md.cpp @@ -1070,6 +1070,10 @@ void CfsHeaderPageCheckAndRepair(SMgrRelation reln, BlockNumber logicBlockNumber { errno_t rc = 0; int fd = CfsGetFd(reln, MAIN_FORKNUM, logicBlockNumber, true, EXTENT_OPEN_FILE); + /* The relation has been removed, nothing to do here. */ + if (fd < 0) { + return; + } ExtentLocation location = g_location_convert[COMMON_STORAGE](reln, reln->smgr_rnode.node, fd, CFS_EXTENT_SIZE, MAIN_FORKNUM, logicBlockNumber); @@ -1430,6 +1434,10 @@ void MdRecoveryPcaPage(SMgrRelation reln, ForkNumber forknum, BlockNumber blockn CfsExtentAddress *extAddr = NULL; int fd = CfsGetFd(reln, forknum, blocknum, skipFsync, WRITE_BACK_OPEN_FILE); + /* The relation has been removed, nothing to do here. */ + if (fd < 0) { + return; + } /* buffer init is ahead of dw, buffer can be used for reading pca */ ExtentLocation location = StorageConvert(reln, reln->smgr_rnode.node, fd, CFS_EXTENT_SIZE, forknum, blocknum); @@ -1475,6 +1483,12 @@ void MdAssistFileProcess(SMgrRelation relation, const char *assistInfo, int assi int fd = CfsGetFd(relation, extInfo->forknum, extInfo->extentNumber * CFS_LOGIC_BLOCKS_PER_EXTENT, false, WRITE_BACK_OPEN_FILE); + + /* The relation has been removed, nothing to do here. */ + if (fd < 0) { + return; + } + ExtentLocation location = StorageConvert(relation, relation->smgr_rnode.node, fd, CFS_EXTENT_SIZE, extInfo->forknum, extInfo->extentNumber * CFS_LOGIC_BLOCKS_PER_EXTENT); diff --git a/src/gausskernel/storage/smgr/md.cpp b/src/gausskernel/storage/smgr/md.cpp index e9201f3ce2..9f177526b5 100644 --- a/src/gausskernel/storage/smgr/md.cpp +++ b/src/gausskernel/storage/smgr/md.cpp @@ -891,6 +891,10 @@ void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, while (nblocks > 0) { if (IS_COMPRESSED_MAINFORK(reln, forknum)) { int fd = CfsGetFd(reln, MAIN_FORKNUM, blocknum, true, WRITE_BACK_OPEN_FILE); + /* The relation has been removed, nothing to do here. */ + if (fd < 0) { + return; + } auto nflushed = CfsWriteBack(reln, relNode, fd, CFS_LOGIC_BLOCKS_PER_EXTENT, forknum, blocknum, nblocks, COMMON_STORAGE); if (nflushed == InvalidBlockNumber) { @@ -1468,6 +1472,10 @@ void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const bool compressed = IS_COMPRESSED_MAINFORK(reln, forknum); if (compressed) { int fd = CfsGetFd(reln, forknum, blocknum, skipFsync, EXTENT_OPEN_FILE); + /* The relation has been removed, nothing to do here. */ + if (fd < 0) { + return; + } nbytes = (int)CfsWritePage(reln, reln->smgr_rnode.node, fd, CFS_LOGIC_BLOCKS_PER_EXTENT, forknum, blocknum, buffer, false, COMMON_STORAGE); } else { -- Gitee