· 6 years ago · Apr 08, 2019, 10:10 AM
1diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
2index 166078410c..ff3cbb7acb 100644
3--- a/doc/src/sgml/ref/create_table.sgml
4+++ b/doc/src/sgml/ref/create_table.sgml
5@@ -22,7 +22,7 @@ PostgreSQL documentation
6 <refsynopsisdiv>
7 <synopsis>
8 CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable> ( [
9- { <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ COLLATE <replaceable>collation</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
10+ { <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> ]
11 | <replaceable>table_constraint</replaceable>
12 | LIKE <replaceable>source_table</replaceable> [ <replaceable>like_option</replaceable> ... ] }
13 [, ... ]
14@@ -284,6 +284,17 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
15 </listitem>
16 </varlistentry>
17
18+ <varlistentry>
19+ <term><literal>COMMENT <replaceable>comment_text</replaceable></literal></term>
20+ <listitem>
21+ <para>
22+ The optional <literal>COMMENT<literal/> clause adds a comment on
23+ the column. See <xref linkend="sql-comment"> for more info
24+ on comments.
25+ </para>
26+ </listitem>
27+ </varlistentry>
28+
29 <varlistentry>
30 <term><literal>INHERITS ( <replaceable>parent_table</replaceable> [, ... ] )</literal></term>
31 <listitem>
32diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
33index 16492a23c7..f60a013347 100644
34--- a/src/backend/commands/tablecmds.c
35+++ b/src/backend/commands/tablecmds.c
36@@ -1084,6 +1084,20 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
37 */
38 relation_close(rel, NoLock);
39
40+ /*
41+ * Create any comments associated with the attributes.
42+ */
43+ attnum = 0;
44+ foreach(listptr, stmt->tableElts)
45+ {
46+ ColumnDef *colDef = lfirst(listptr);
47+
48+ attnum++;
49+ if (colDef->comment)
50+ CreateComments(relationId, RelationRelationId, attnum,
51+ colDef->comment->comment);
52+ }
53+
54 return address;
55 }
56
57diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
58index 84f9112add..516018c99c 100644
59--- a/src/backend/nodes/copyfuncs.c
60+++ b/src/backend/nodes/copyfuncs.c
61@@ -2893,6 +2893,7 @@ _copyColumnDef(const ColumnDef *from)
62 COPY_NODE_FIELD(constraints);
63 COPY_NODE_FIELD(fdwoptions);
64 COPY_LOCATION_FIELD(location);
65+ COPY_NODE_FIELD(comment);
66
67 return newnode;
68 }
69diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c
70index 7eb9f1dd92..7530e85e74 100644
71--- a/src/backend/nodes/equalfuncs.c
72+++ b/src/backend/nodes/equalfuncs.c
73@@ -2570,6 +2570,7 @@ _equalColumnDef(const ColumnDef *a, const ColumnDef *b)
74 COMPARE_NODE_FIELD(constraints);
75 COMPARE_NODE_FIELD(fdwoptions);
76 COMPARE_LOCATION_FIELD(location);
77+ COMPARE_NODE_FIELD(comment);
78
79 return true;
80 }
81diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c
82index 7085ed2c4c..a66e151b3f 100644
83--- a/src/backend/nodes/makefuncs.c
84+++ b/src/backend/nodes/makefuncs.c
85@@ -504,6 +504,7 @@ makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
86 n->constraints = NIL;
87 n->fdwoptions = NIL;
88 n->location = -1;
89+ n->comment = NULL;
90
91 return n;
92 }
93diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
94index 28f62de97e..90fc7ae5f3 100644
95--- a/src/backend/parser/gram.y
96+++ b/src/backend/parser/gram.y
97@@ -543,6 +543,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
98 %type <ival> TableLikeOptionList TableLikeOption
99 %type <list> ColQualList
100 %type <node> ColConstraint ColConstraintElem ConstraintAttr
101+%type <node> OptColComment ColComment
102 %type <ival> key_actions key_delete key_match key_update key_action
103 %type <ival> ConstraintAttributeSpec ConstraintAttributeElem
104 %type <str> ExistingIndex
105@@ -3326,7 +3327,7 @@ TypedTableElement:
106 | TableConstraint { $$ = $1; }
107 ;
108
109-columnDef: ColId Typename create_generic_options ColQualList
110+columnDef: ColId Typename OptColComment create_generic_options ColQualList
111 {
112 ColumnDef *n = makeNode(ColumnDef);
113 n->colname = $1;
114@@ -3339,10 +3340,11 @@ columnDef: ColId Typename create_generic_options ColQualList
115 n->raw_default = NULL;
116 n->cooked_default = NULL;
117 n->collOid = InvalidOid;
118- n->fdwoptions = $3;
119- SplitColQualList($4, &n->constraints, &n->collClause,
120+ n->fdwoptions = $4;
121+ SplitColQualList($5, &n->constraints, &n->collClause,
122 yyscanner);
123 n->location = @1;
124+ n->comment = (CommentStmt *) $3;
125 $$ = (Node *)n;
126 }
127 ;
128@@ -3390,6 +3392,21 @@ ColQualList:
129 | /*EMPTY*/ { $$ = NIL; }
130 ;
131
132+OptColComment:
133+ ColComment { $$ = $1; }
134+ | /* EMPTY */ { $$ = NULL; }
135+ ;
136+
137+ColComment: COMMENT comment_text
138+ {
139+ CommentStmt *n = makeNode(CommentStmt);
140+ n->objtype = OBJECT_COLUMN;
141+ n->object = NULL;
142+ n->comment = $2;
143+ $$ = (Node *) n;
144+ }
145+ ;
146+
147 ColConstraint:
148 CONSTRAINT name ColConstraintElem
149 {
150diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
151index e81c626913..79aae0a6be 100644
152--- a/src/include/nodes/parsenodes.h
153+++ b/src/include/nodes/parsenodes.h
154@@ -660,6 +660,7 @@ typedef struct ColumnDef
155 List *constraints; /* other constraints on column */
156 List *fdwoptions; /* per-column FDW options */
157 int location; /* parse location, or -1 if none/unknown */
158+ struct CommentStmt *comment; /* column comment if any */
159 } ColumnDef;
160
161 /*