From 08ac904e7bf56bd6a33dea2429baf2610008f994 Mon Sep 17 00:00:00 2001 From: copetol Date: Wed, 27 Mar 2024 18:18:54 +0300 Subject: [PATCH 1/5] recreate table and column comments on undistribute_table --- .../distributed/commands/alter_table.c | 22 ++++++++++ src/backend/distributed/commands/comment.c | 27 ++++++++++-- .../deparser/deparse_comment_stmts.c | 2 + src/include/distributed/comment.h | 9 ++-- .../expected/comment_on_table_and_column.out | 41 +++++++++++++++++++ src/test/regress/multi_schedule | 1 + .../regress/sql/comment_on_table_column.sql | 15 +++++++ 7 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 src/test/regress/expected/comment_on_table_and_column.out create mode 100644 src/test/regress/sql/comment_on_table_column.sql diff --git a/src/backend/distributed/commands/alter_table.c b/src/backend/distributed/commands/alter_table.c index 030dbbe78..f3332fb98 100644 --- a/src/backend/distributed/commands/alter_table.c +++ b/src/backend/distributed/commands/alter_table.c @@ -69,6 +69,7 @@ #include "distributed/tenant_schema_metadata.h" #include "distributed/worker_protocol.h" #include "distributed/worker_transaction.h" +#include "distributed/comment.h" /* Table Conversion Types */ @@ -753,6 +754,27 @@ ConvertTableInternal(TableConversionState *con) postLoadCommands = list_concat(postLoadCommands, WrapTableDDLCommands(alterPublicationCommands)); + if (con->conversionType == UNDISTRIBUTE_TABLE) + { + List *commentDDLCommandsTable = GetCommentPropagationCommandsX( + con->relationId, RelationRelationId, con->relationName, OBJECT_TABLE, + NULL, 0); + postLoadCommands = list_concat(postLoadCommands, + WrapTableDDLCommands(commentDDLCommandsTable)); + List *nonStoredColumnNameList = GetNonGeneratedStoredColumnNameList( + con->relationId); + char *columnName = NULL; + int columnCount = 0; + foreach_ptr(columnName, nonStoredColumnNameList) + { + List *commentDDLCommandsColumn = GetCommentPropagationCommandsX( + con->relationId, RelationRelationId, columnName , OBJECT_COLUMN, + con->relationName, ++columnCount); + postLoadCommands = list_concat(postLoadCommands, + WrapTableDDLCommands(commentDDLCommandsColumn)); + } + } + List *foreignKeyCommands = NIL; if (con->conversionType == ALTER_DISTRIBUTED_TABLE) { diff --git a/src/backend/distributed/commands/comment.c b/src/backend/distributed/commands/comment.c index e18a5c5cc..08da7a2b0 100644 --- a/src/backend/distributed/commands/comment.c +++ b/src/backend/distributed/commands/comment.c @@ -20,28 +20,47 @@ #include "utils/fmgroids.h" #include "utils/rel.h" +#include "commands/comment.h" #include "distributed/comment.h" static char * GetCommentForObject(Oid classOid, Oid objectOid); -List * +inline List * GetCommentPropagationCommands(Oid classOid, Oid objOoid, char *objectName, ObjectType objectType) +{ + return GetCommentPropagationCommandsX(classOid, objOoid, objectName, objectType, NULL, 0); +} + +List * +GetCommentPropagationCommandsX(Oid classOid, Oid objOoid, char *objectName, ObjectType + objectType, char *qualifier, int32 subid) { List *commands = NIL; StringInfo commentStmt = makeStringInfo(); - /* Get the comment for the database */ - char *comment = GetCommentForObject(classOid, objOoid); + char *comment = NULL; + + if((objectType == OBJECT_DATABASE) || (objectType == OBJECT_ROLE) || (objectType == + OBJECT_TABLESPACE)) + { + /* Get the comment for the shared object */ + comment = GetCommentForObject(classOid, objOoid); + } + else + { + comment = GetComment(classOid, objOoid, subid); + } + char const *commentObjectType = ObjectTypeNames[objectType]; /* Create the SQL command to propagate the comment to other nodes */ if (comment != NULL) { appendStringInfo(commentStmt, "COMMENT ON %s %s IS %s;", commentObjectType, - quote_identifier(objectName), + quote_qualified_identifier(qualifier, objectName), quote_literal_cstr(comment)); } diff --git a/src/backend/distributed/deparser/deparse_comment_stmts.c b/src/backend/distributed/deparser/deparse_comment_stmts.c index 36a63c97b..263c9a594 100644 --- a/src/backend/distributed/deparser/deparse_comment_stmts.c +++ b/src/backend/distributed/deparser/deparse_comment_stmts.c @@ -35,6 +35,8 @@ const char *ObjectTypeNames[] = [OBJECT_ROLE] = "ROLE", [OBJECT_TSCONFIGURATION] = "TEXT SEARCH CONFIGURATION", [OBJECT_TSDICTIONARY] = "TEXT SEARCH DICTIONARY", + [OBJECT_TABLE] = "TABLE", + [OBJECT_COLUMN] = "COLUMN", /* When support for propagating comments to new objects is introduced, an entry for each * statement type should be added to this list. The first element in each entry is the 'object_type' keyword diff --git a/src/include/distributed/comment.h b/src/include/distributed/comment.h index bef216ae4..3cd3c8c04 100644 --- a/src/include/distributed/comment.h +++ b/src/include/distributed/comment.h @@ -8,8 +8,8 @@ *------------------------------------------------------------------------- */ -#ifndef COMMENT_H -#define COMMENT_H +#ifndef CITUS_COMMENT_H +#define CITUS_COMMENT_H #include "postgres.h" @@ -21,6 +21,9 @@ extern const char *ObjectTypeNames[]; extern List * GetCommentPropagationCommands(Oid classOid, Oid oid, char *objectName, ObjectType objectType); +extern List * GetCommentPropagationCommandsX(Oid classOid, Oid oid, char *objectName, + ObjectType objectType, char *qualifier, + int32 subid); extern List * CommentObjectAddress(Node *node, bool missing_ok, bool isPostprocess); -# endif /* COMMENT_H */ +# endif /* CITUS_COMMENT_H */ diff --git a/src/test/regress/expected/comment_on_table_and_column.out b/src/test/regress/expected/comment_on_table_and_column.out new file mode 100644 index 000000000..a3de9aae8 --- /dev/null +++ b/src/test/regress/expected/comment_on_table_and_column.out @@ -0,0 +1,41 @@ +CREATE SCHEMA comment_on_table_and_column; +SET search_path TO comment_on_table_and_column; +create table tbl (a int, b text); +comment on table tbl is 'table comment'; +comment on column tbl.b is 'column b comment'; +select col_description('tbl'::regclass,0) as table_comment, col_description('tbl'::regclass,1) as column1_comment, col_description('tbl'::regclass,2) as column2_comment; + table_comment | column1_comment | column2_comment +--------------------------------------------------------------------- + table comment | | column b comment +(1 row) + +select create_distributed_table('tbl','a'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +select col_description('tbl'::regclass,0) as table_comment, col_description('tbl'::regclass,1) as column1_comment, col_description('tbl'::regclass,2) as column2_comment; + table_comment | column1_comment | column2_comment +--------------------------------------------------------------------- + table comment | | column b comment +(1 row) + +select undistribute_table('tbl'); +NOTICE: creating a new table for comment_on_table_and_column.tbl +NOTICE: moving the data of comment_on_table_and_column.tbl +NOTICE: dropping the old comment_on_table_and_column.tbl +NOTICE: renaming the new table to comment_on_table_and_column.tbl + undistribute_table +--------------------------------------------------------------------- + +(1 row) + +select col_description('tbl'::regclass,0) as table_comment, col_description('tbl'::regclass,1) as column1_comment, col_description('tbl'::regclass,2) as column2_comment; + table_comment | column1_comment | column2_comment +--------------------------------------------------------------------- + table comment | | column b comment +(1 row) + +DROP SCHEMA comment_on_table_and_column CASCADE; +NOTICE: drop cascades to table tbl diff --git a/src/test/regress/multi_schedule b/src/test/regress/multi_schedule index af5921e60..7739ec33e 100644 --- a/src/test/regress/multi_schedule +++ b/src/test/regress/multi_schedule @@ -111,6 +111,7 @@ test: background_task_queue_monitor test: other_databases grant_role_from_non_maindb role_operations_from_non_maindb seclabel_non_maindb test: citus_internal_access test: function_with_case_when +test: comment_on_table_column # Causal clock test test: clock diff --git a/src/test/regress/sql/comment_on_table_column.sql b/src/test/regress/sql/comment_on_table_column.sql new file mode 100644 index 000000000..3dc4c698d --- /dev/null +++ b/src/test/regress/sql/comment_on_table_column.sql @@ -0,0 +1,15 @@ +CREATE SCHEMA comment_on_table_and_column; +SET search_path TO comment_on_table_and_column; + +create table tbl (a int, b text); +comment on table tbl is 'table comment'; +comment on column tbl.b is 'column b comment'; +select col_description('tbl'::regclass,0) as table_comment, col_description('tbl'::regclass,1) as column1_comment, col_description('tbl'::regclass,2) as column2_comment; + +select create_distributed_table('tbl','a'); +select col_description('tbl'::regclass,0) as table_comment, col_description('tbl'::regclass,1) as column1_comment, col_description('tbl'::regclass,2) as column2_comment; + +select undistribute_table('tbl'); +select col_description('tbl'::regclass,0) as table_comment, col_description('tbl'::regclass,1) as column1_comment, col_description('tbl'::regclass,2) as column2_comment; + +DROP SCHEMA comment_on_table_and_column CASCADE; From 64a8d71739545eab98fd41b22146e23e41e3e68b Mon Sep 17 00:00:00 2001 From: copetol Date: Wed, 27 Mar 2024 19:19:08 +0300 Subject: [PATCH 2/5] reindent c files --- src/backend/distributed/commands/alter_table.c | 7 ++++--- src/backend/distributed/commands/comment.c | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/backend/distributed/commands/alter_table.c b/src/backend/distributed/commands/alter_table.c index f3332fb98..df1989cd2 100644 --- a/src/backend/distributed/commands/alter_table.c +++ b/src/backend/distributed/commands/alter_table.c @@ -762,16 +762,17 @@ ConvertTableInternal(TableConversionState *con) postLoadCommands = list_concat(postLoadCommands, WrapTableDDLCommands(commentDDLCommandsTable)); List *nonStoredColumnNameList = GetNonGeneratedStoredColumnNameList( - con->relationId); + con->relationId); char *columnName = NULL; int columnCount = 0; foreach_ptr(columnName, nonStoredColumnNameList) { List *commentDDLCommandsColumn = GetCommentPropagationCommandsX( - con->relationId, RelationRelationId, columnName , OBJECT_COLUMN, + con->relationId, RelationRelationId, columnName, OBJECT_COLUMN, con->relationName, ++columnCount); postLoadCommands = list_concat(postLoadCommands, - WrapTableDDLCommands(commentDDLCommandsColumn)); + WrapTableDDLCommands( + commentDDLCommandsColumn)); } } diff --git a/src/backend/distributed/commands/comment.c b/src/backend/distributed/commands/comment.c index 08da7a2b0..6c8194c2b 100644 --- a/src/backend/distributed/commands/comment.c +++ b/src/backend/distributed/commands/comment.c @@ -30,9 +30,11 @@ inline List * GetCommentPropagationCommands(Oid classOid, Oid objOoid, char *objectName, ObjectType objectType) { - return GetCommentPropagationCommandsX(classOid, objOoid, objectName, objectType, NULL, 0); + return GetCommentPropagationCommandsX(classOid, objOoid, objectName, objectType, NULL, + 0); } + List * GetCommentPropagationCommandsX(Oid classOid, Oid objOoid, char *objectName, ObjectType objectType, char *qualifier, int32 subid) @@ -43,8 +45,8 @@ GetCommentPropagationCommandsX(Oid classOid, Oid objOoid, char *objectName, Obje char *comment = NULL; - if((objectType == OBJECT_DATABASE) || (objectType == OBJECT_ROLE) || (objectType == - OBJECT_TABLESPACE)) + if ((objectType == OBJECT_DATABASE) || (objectType == OBJECT_ROLE) || (objectType == + OBJECT_TABLESPACE)) { /* Get the comment for the shared object */ comment = GetCommentForObject(classOid, objOoid); From 742abb1aff5075634c92b963a663650826fd8697 Mon Sep 17 00:00:00 2001 From: copetol Date: Wed, 27 Mar 2024 19:50:07 +0300 Subject: [PATCH 3/5] remove usless space in comment.h --- src/include/distributed/comment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/distributed/comment.h b/src/include/distributed/comment.h index 3cd3c8c04..c5f6e28db 100644 --- a/src/include/distributed/comment.h +++ b/src/include/distributed/comment.h @@ -22,7 +22,7 @@ extern const char *ObjectTypeNames[]; extern List * GetCommentPropagationCommands(Oid classOid, Oid oid, char *objectName, ObjectType objectType); extern List * GetCommentPropagationCommandsX(Oid classOid, Oid oid, char *objectName, - ObjectType objectType, char *qualifier, + ObjectType objectType, char *qualifier, int32 subid); extern List * CommentObjectAddress(Node *node, bool missing_ok, bool isPostprocess); From 8e04036643c92f6174d0e68b7764e8f77c6e4315 Mon Sep 17 00:00:00 2001 From: copetol Date: Wed, 27 Mar 2024 20:14:34 +0300 Subject: [PATCH 4/5] sort includes --- src/backend/distributed/commands/alter_table.c | 2 +- src/backend/distributed/commands/comment.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/commands/alter_table.c b/src/backend/distributed/commands/alter_table.c index df1989cd2..d3485ddd0 100644 --- a/src/backend/distributed/commands/alter_table.c +++ b/src/backend/distributed/commands/alter_table.c @@ -47,6 +47,7 @@ #include "distributed/colocation_utils.h" #include "distributed/commands.h" #include "distributed/commands/utility_hook.h" +#include "distributed/comment.h" #include "distributed/coordinator_protocol.h" #include "distributed/deparser.h" #include "distributed/distribution_column.h" @@ -69,7 +70,6 @@ #include "distributed/tenant_schema_metadata.h" #include "distributed/worker_protocol.h" #include "distributed/worker_transaction.h" -#include "distributed/comment.h" /* Table Conversion Types */ diff --git a/src/backend/distributed/commands/comment.c b/src/backend/distributed/commands/comment.c index 6c8194c2b..90be5617f 100644 --- a/src/backend/distributed/commands/comment.c +++ b/src/backend/distributed/commands/comment.c @@ -15,12 +15,12 @@ #include "access/htup_details.h" #include "access/table.h" #include "catalog/pg_shdescription.h" +#include "commands/comment.h" #include "nodes/parsenodes.h" #include "utils/builtins.h" #include "utils/fmgroids.h" #include "utils/rel.h" -#include "commands/comment.h" #include "distributed/comment.h" static char * GetCommentForObject(Oid classOid, Oid objectOid); From d9c245194d69d642da14d3636eaee9ffebf76245 Mon Sep 17 00:00:00 2001 From: copetol Date: Wed, 27 Mar 2024 22:10:09 +0300 Subject: [PATCH 5/5] fix test result filename --- ...omment_on_table_and_column.out => comment_on_table_column.out} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/test/regress/expected/{comment_on_table_and_column.out => comment_on_table_column.out} (100%) diff --git a/src/test/regress/expected/comment_on_table_and_column.out b/src/test/regress/expected/comment_on_table_column.out similarity index 100% rename from src/test/regress/expected/comment_on_table_and_column.out rename to src/test/regress/expected/comment_on_table_column.out