diff --git a/src/common/backend/utils/misc/guc.cpp b/src/common/backend/utils/misc/guc.cpp index 73608efd2e742a0a17a65fceea550e6c4fb0e213..d9e6e9139792f10d3972aa84b9a3f638e76f6412 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 9d564502710b932112692dcf3984a8ebe9f7c190..936b4fd8de8afa17e6e75e319f602f007d96c78e 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 f7de700db3f4e3c909f1f95d9d7b1cf11741aee5..73d652ddef7d915b8bb3d17abae4a9543e35ac52 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 68fad62e8cac9ab2cce98c0c83404684988e0faa..281274a023e8340f883d7ae0d8c08b15e5a1ec2b 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/gausskernel/storage/smgr/cfs/cfs_md.cpp b/src/gausskernel/storage/smgr/cfs/cfs_md.cpp index 6fd99a521d2d2486fdbf7b3aadb8f150f80a1e24..78998e1af327c9df6a319c76f5b6e9151adcea78 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 e9201f3ce235758f9d2c1669a0c305b9be0301be..9f177526b597cdc77400ba987d97baaabb122deb 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 { diff --git a/src/include/access/reloptions.h b/src/include/access/reloptions.h index b701b91d516d828a60597ab75a13155067783587..c39f3f73bccb80ed92f5eefae95f98b47c3d05bf 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 0b482311b5efbf653092c43d74ba07c5831de676..e068094489248ea29a75058c005589dc1f099fa6 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;