Skip to content

Instantly share code, notes, and snippets.

@danielgustafsson
Created March 29, 2019 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgustafsson/02f32ed8a9cb14a7fcfb56f5d3b3f0fd to your computer and use it in GitHub Desktop.
Save danielgustafsson/02f32ed8a9cb14a7fcfb56f5d3b3f0fd to your computer and use it in GitHub Desktop.
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
index 166078410c..ff3cbb7acb 100644
--- a/doc/src/sgml/ref/create_table.sgml
+++ b/doc/src/sgml/ref/create_table.sgml
@@ -22,7 +22,7 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable> ( [
- { <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ COLLATE <replaceable>collation</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
+ { <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ COLLATE <replaceable>collation</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ] [ COMMENT <replaceable>comment_text</replaceable> ]
| <replaceable>table_constraint</replaceable>
| LIKE <replaceable>source_table</replaceable> [ <replaceable>like_option</replaceable> ... ] }
[, ... ]
@@ -284,6 +284,17 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><literal>COMMENT <replaceable>comment_text</replaceable></literal></term>
+ <listitem>
+ <para>
+ The optional <literal>COMMENT<literal/> clause adds a comment on
+ the column. See <xref linkend="sql-comment"> for more info
+ on comments.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><literal>INHERITS ( <replaceable>parent_table</replaceable> [, ... ] )</literal></term>
<listitem>
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 16492a23c7..f60a013347 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -1084,6 +1084,20 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
*/
relation_close(rel, NoLock);
+ /*
+ * Create any comments associated with the attributes.
+ */
+ attnum = 0;
+ foreach(listptr, stmt->tableElts)
+ {
+ ColumnDef *colDef = lfirst(listptr);
+
+ attnum++;
+ if (colDef->comment)
+ CreateComments(relationId, RelationRelationId, attnum,
+ colDef->comment->comment);
+ }
+
return address;
}
diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index 84f9112add..516018c99c 100644
--- a/src/backend/nodes/copyfuncs.c
+++ b/src/backend/nodes/copyfuncs.c
@@ -2893,6 +2893,7 @@ _copyColumnDef(const ColumnDef *from)
COPY_NODE_FIELD(constraints);
COPY_NODE_FIELD(fdwoptions);
COPY_LOCATION_FIELD(location);
+ COPY_NODE_FIELD(comment);
return newnode;
}
diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c
index 7eb9f1dd92..7530e85e74 100644
--- a/src/backend/nodes/equalfuncs.c
+++ b/src/backend/nodes/equalfuncs.c
@@ -2570,6 +2570,7 @@ _equalColumnDef(const ColumnDef *a, const ColumnDef *b)
COMPARE_NODE_FIELD(constraints);
COMPARE_NODE_FIELD(fdwoptions);
COMPARE_LOCATION_FIELD(location);
+ COMPARE_NODE_FIELD(comment);
return true;
}
diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c
index 7085ed2c4c..a66e151b3f 100644
--- a/src/backend/nodes/makefuncs.c
+++ b/src/backend/nodes/makefuncs.c
@@ -504,6 +504,7 @@ makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
n->constraints = NIL;
n->fdwoptions = NIL;
n->location = -1;
+ n->comment = NULL;
return n;
}
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 28f62de97e..90fc7ae5f3 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -543,6 +543,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
%type <ival> TableLikeOptionList TableLikeOption
%type <list> ColQualList
%type <node> ColConstraint ColConstraintElem ConstraintAttr
+%type <node> OptColComment ColComment
%type <ival> key_actions key_delete key_match key_update key_action
%type <ival> ConstraintAttributeSpec ConstraintAttributeElem
%type <str> ExistingIndex
@@ -3326,7 +3327,7 @@ TypedTableElement:
| TableConstraint { $$ = $1; }
;
-columnDef: ColId Typename create_generic_options ColQualList
+columnDef: ColId Typename OptColComment create_generic_options ColQualList
{
ColumnDef *n = makeNode(ColumnDef);
n->colname = $1;
@@ -3339,10 +3340,11 @@ columnDef: ColId Typename create_generic_options ColQualList
n->raw_default = NULL;
n->cooked_default = NULL;
n->collOid = InvalidOid;
- n->fdwoptions = $3;
- SplitColQualList($4, &n->constraints, &n->collClause,
+ n->fdwoptions = $4;
+ SplitColQualList($5, &n->constraints, &n->collClause,
yyscanner);
n->location = @1;
+ n->comment = (CommentStmt *) $3;
$$ = (Node *)n;
}
;
@@ -3390,6 +3392,21 @@ ColQualList:
| /*EMPTY*/ { $$ = NIL; }
;
+OptColComment:
+ ColComment { $$ = $1; }
+ | /* EMPTY */ { $$ = NULL; }
+ ;
+
+ColComment: COMMENT comment_text
+ {
+ CommentStmt *n = makeNode(CommentStmt);
+ n->objtype = OBJECT_COLUMN;
+ n->object = NULL;
+ n->comment = $2;
+ $$ = (Node *) n;
+ }
+ ;
+
ColConstraint:
CONSTRAINT name ColConstraintElem
{
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index e81c626913..79aae0a6be 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -660,6 +660,7 @@ typedef struct ColumnDef
List *constraints; /* other constraints on column */
List *fdwoptions; /* per-column FDW options */
int location; /* parse location, or -1 if none/unknown */
+ struct CommentStmt *comment; /* column comment if any */
} ColumnDef;
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment