PostgreSQL Source Code git master
Loading...
Searching...
No Matches
comment.h File Reference
Include dependency graph for comment.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress CommentObject (CommentStmt *stmt)
 
void DeleteComments (Oid oid, Oid classoid, int32 subid)
 
void CreateComments (Oid oid, Oid classoid, int32 subid, const char *comment)
 
void DeleteSharedComments (Oid oid, Oid classoid)
 
void CreateSharedComments (Oid oid, Oid classoid, const char *comment)
 
charGetComment (Oid oid, Oid classoid, int32 subid)
 

Function Documentation

◆ CommentObject()

ObjectAddress CommentObject ( CommentStmt stmt)
extern

Definition at line 40 of file comment.c.

41{
42 Relation relation;
44 bool missing_ok;
45
46 /*
47 * When loading a dump, we may see a COMMENT ON DATABASE for the old name
48 * of the database. Erroring out would prevent pg_restore from completing
49 * (which is really pg_restore's fault, but for now we will work around
50 * the problem here). Consensus is that the best fix is to treat wrong
51 * database name as a WARNING not an ERROR; hence, the following special
52 * case.
53 */
54 if (stmt->objtype == OBJECT_DATABASE)
55 {
56 char *database = strVal(stmt->object);
57
58 if (!OidIsValid(get_database_oid(database, true)))
59 {
62 errmsg("database \"%s\" does not exist", database)));
63 return address;
64 }
65 }
66
67 /*
68 * During binary upgrade, allow nonexistent large objects so that we don't
69 * have to create them during schema restoration. pg_upgrade will
70 * transfer the contents of pg_largeobject_metadata via COPY or by
71 * copying/linking its files from the old cluster later on.
72 */
73 missing_ok = IsBinaryUpgrade && stmt->objtype == OBJECT_LARGEOBJECT;
74
75 /*
76 * Translate the parser representation that identifies this object into an
77 * ObjectAddress. get_object_address() will throw an error if the object
78 * does not exist, and will also acquire a lock on the target to guard
79 * against concurrent DROP operations.
80 */
81 address = get_object_address(stmt->objtype, stmt->object,
82 &relation, ShareUpdateExclusiveLock,
83 missing_ok);
84
85 /* Require ownership of the target object. */
86 check_object_ownership(GetUserId(), stmt->objtype, address,
87 stmt->object, relation);
88
89 /* Perform other integrity checks as needed. */
90 switch (stmt->objtype)
91 {
92 case OBJECT_COLUMN:
93
94 /*
95 * Allow comments only on columns of tables, views, materialized
96 * views, composite types, and foreign tables (which are the only
97 * relkinds for which pg_dump will dump per-column comments). In
98 * particular we wish to disallow comments on index columns,
99 * because the naming of an index's columns may change across PG
100 * versions, so dumping per-column comments could create reload
101 * failures.
102 */
103 if (relation->rd_rel->relkind != RELKIND_RELATION &&
104 relation->rd_rel->relkind != RELKIND_VIEW &&
105 relation->rd_rel->relkind != RELKIND_MATVIEW &&
106 relation->rd_rel->relkind != RELKIND_COMPOSITE_TYPE &&
107 relation->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
108 relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
111 errmsg("cannot set comment on relation \"%s\"",
112 RelationGetRelationName(relation)),
113 errdetail_relkind_not_supported(relation->rd_rel->relkind)));
114 break;
115 default:
116 break;
117 }
118
119 /*
120 * Databases, tablespaces, and roles are cluster-wide objects, so any
121 * comments on those objects are recorded in the shared pg_shdescription
122 * catalog. Comments on all other objects are recorded in pg_description.
123 */
124 if (stmt->objtype == OBJECT_DATABASE || stmt->objtype == OBJECT_TABLESPACE
125 || stmt->objtype == OBJECT_ROLE)
126 CreateSharedComments(address.objectId, address.classId, stmt->comment);
127 else
128 CreateComments(address.objectId, address.classId, address.objectSubId,
129 stmt->comment);
130
131 /*
132 * If get_object_address() opened the relation for us, we close it to keep
133 * the reference count correct - but we retain any locks acquired by
134 * get_object_address() until commit time, to guard against concurrent
135 * activity.
136 */
137 if (relation != NULL)
138 relation_close(relation, NoLock);
139
140 return address;
141}
#define OidIsValid(objectId)
Definition c.h:800
void CreateSharedComments(Oid oid, Oid classoid, const char *comment)
Definition comment.c:248
void CreateComments(Oid oid, Oid classoid, int32 subid, const char *comment)
Definition comment.c:153
Oid get_database_oid(const char *dbname, bool missing_ok)
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
#define WARNING
Definition elog.h:36
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
bool IsBinaryUpgrade
Definition globals.c:121
#define stmt
#define NoLock
Definition lockdefs.h:34
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
Oid GetUserId(void)
Definition miscinit.c:469
void check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address, Node *object, Relation relation)
const ObjectAddress InvalidObjectAddress
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
@ OBJECT_COLUMN
@ OBJECT_TABLESPACE
@ OBJECT_ROLE
@ OBJECT_LARGEOBJECT
@ OBJECT_DATABASE
int errdetail_relkind_not_supported(char relkind)
Definition pg_class.c:24
static int fb(int x)
#define RelationGetRelationName(relation)
Definition rel.h:548
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Form_pg_class rd_rel
Definition rel.h:111
#define strVal(v)
Definition value.h:82

References check_object_ownership(), ObjectAddress::classId, CreateComments(), CreateSharedComments(), ereport, errcode(), errdetail_relkind_not_supported(), errmsg(), ERROR, fb(), get_database_oid(), get_object_address(), GetUserId(), InvalidObjectAddress, IsBinaryUpgrade, NoLock, OBJECT_COLUMN, OBJECT_DATABASE, OBJECT_LARGEOBJECT, OBJECT_ROLE, OBJECT_TABLESPACE, ObjectAddress::objectId, ObjectAddress::objectSubId, OidIsValid, RelationData::rd_rel, relation_close(), RelationGetRelationName, ShareUpdateExclusiveLock, stmt, strVal, and WARNING.

Referenced by ATExecCmd(), ProcessUtilitySlow(), and standard_ProcessUtility().

◆ CreateComments()

void CreateComments ( Oid  oid,
Oid  classoid,
int32  subid,
const char comment 
)
extern

Definition at line 153 of file comment.c.

154{
156 ScanKeyData skey[3];
158 HeapTuple oldtuple;
159 HeapTuple newtuple = NULL;
161 bool nulls[Natts_pg_description];
163 int i;
164
165 /* Reduce empty-string to NULL case */
166 if (comment != NULL && strlen(comment) == 0)
167 comment = NULL;
168
169 /* Prepare to form or update a tuple, if necessary */
170 if (comment != NULL)
171 {
172 for (i = 0; i < Natts_pg_description; i++)
173 {
174 nulls[i] = false;
175 replaces[i] = true;
176 }
181 }
182
183 /* Use the index to search for a matching old tuple */
184
185 ScanKeyInit(&skey[0],
188 ObjectIdGetDatum(oid));
189 ScanKeyInit(&skey[1],
192 ObjectIdGetDatum(classoid));
193 ScanKeyInit(&skey[2],
196 Int32GetDatum(subid));
197
199
201 NULL, 3, skey);
202
203 while ((oldtuple = systable_getnext(sd)) != NULL)
204 {
205 /* Found the old tuple, so delete or update it */
206
207 if (comment == NULL)
209 else
210 {
212 nulls, replaces);
213 CatalogTupleUpdate(description, &oldtuple->t_self, newtuple);
214 }
215
216 break; /* Assume there can be only one match */
217 }
218
220
221 /* If we didn't find an old tuple, insert a new one */
222
223 if (newtuple == NULL && comment != NULL)
224 {
226 values, nulls);
228 }
229
230 if (newtuple != NULL)
231 heap_freetuple(newtuple);
232
233 /* Done */
234
236}
static Datum values[MAXATTR]
Definition bootstrap.c:147
#define CStringGetTextDatum(s)
Definition builtins.h:98
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
#define comment
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
int i
Definition isn.c:77
#define RowExclusiveLock
Definition lockdefs.h:38
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
#define RelationGetDescr(relation)
Definition rel.h:540
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31
ItemPointerData t_self
Definition htup.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
const char * description

References BTEqualStrategyNumber, CatalogTupleDelete(), CatalogTupleInsert(), CatalogTupleUpdate(), comment, CStringGetTextDatum, description, fb(), heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), i, Int32GetDatum(), NoLock, ObjectIdGetDatum(), RelationGetDescr, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), table_open(), and values.

Referenced by CommentObject(), CreateExtensionInternal(), CreateStatistics(), DefineIndex(), and pg_import_system_collations().

◆ CreateSharedComments()

void CreateSharedComments ( Oid  oid,
Oid  classoid,
const char comment 
)
extern

Definition at line 248 of file comment.c.

249{
251 ScanKeyData skey[2];
253 HeapTuple oldtuple;
254 HeapTuple newtuple = NULL;
256 bool nulls[Natts_pg_shdescription];
258 int i;
259
260 /* Reduce empty-string to NULL case */
261 if (comment != NULL && strlen(comment) == 0)
262 comment = NULL;
263
264 /* Prepare to form or update a tuple, if necessary */
265 if (comment != NULL)
266 {
267 for (i = 0; i < Natts_pg_shdescription; i++)
268 {
269 nulls[i] = false;
270 replaces[i] = true;
271 }
275 }
276
277 /* Use the index to search for a matching old tuple */
278
279 ScanKeyInit(&skey[0],
282 ObjectIdGetDatum(oid));
283 ScanKeyInit(&skey[1],
286 ObjectIdGetDatum(classoid));
287
289
291 NULL, 2, skey);
292
293 while ((oldtuple = systable_getnext(sd)) != NULL)
294 {
295 /* Found the old tuple, so delete or update it */
296
297 if (comment == NULL)
299 else
300 {
302 values, nulls, replaces);
303 CatalogTupleUpdate(shdescription, &oldtuple->t_self, newtuple);
304 }
305
306 break; /* Assume there can be only one match */
307 }
308
310
311 /* If we didn't find an old tuple, insert a new one */
312
313 if (newtuple == NULL && comment != NULL)
314 {
316 values, nulls);
318 }
319
320 if (newtuple != NULL)
321 heap_freetuple(newtuple);
322
323 /* Done */
324
326}

References BTEqualStrategyNumber, CatalogTupleDelete(), CatalogTupleInsert(), CatalogTupleUpdate(), comment, CStringGetTextDatum, fb(), heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), i, NoLock, ObjectIdGetDatum(), RelationGetDescr, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), table_open(), and values.

Referenced by CommentObject().

◆ DeleteComments()

void DeleteComments ( Oid  oid,
Oid  classoid,
int32  subid 
)
extern

Definition at line 336 of file comment.c.

337{
339 ScanKeyData skey[3];
340 int nkeys;
342 HeapTuple oldtuple;
343
344 /* Use the index to search for all matching old tuples */
345
346 ScanKeyInit(&skey[0],
349 ObjectIdGetDatum(oid));
350 ScanKeyInit(&skey[1],
353 ObjectIdGetDatum(classoid));
354
355 if (subid != 0)
356 {
357 ScanKeyInit(&skey[2],
360 Int32GetDatum(subid));
361 nkeys = 3;
362 }
363 else
364 nkeys = 2;
365
367
369 NULL, nkeys, skey);
370
371 while ((oldtuple = systable_getnext(sd)) != NULL)
373
374 /* Done */
375
378}

References BTEqualStrategyNumber, CatalogTupleDelete(), description, fb(), Int32GetDatum(), ObjectIdGetDatum(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by deleteOneObject().

◆ DeleteSharedComments()

void DeleteSharedComments ( Oid  oid,
Oid  classoid 
)
extern

◆ GetComment()

char * GetComment ( Oid  oid,
Oid  classoid,
int32  subid 
)
extern

Definition at line 420 of file comment.c.

421{
423 ScanKeyData skey[3];
425 TupleDesc tupdesc;
426 HeapTuple tuple;
427 char *comment;
428
429 /* Use the index to search for a matching old tuple */
430
431 ScanKeyInit(&skey[0],
434 ObjectIdGetDatum(oid));
435 ScanKeyInit(&skey[1],
438 ObjectIdGetDatum(classoid));
439 ScanKeyInit(&skey[2],
442 Int32GetDatum(subid));
443
445 tupdesc = RelationGetDescr(description);
446
448 NULL, 3, skey);
449
450 comment = NULL;
451 while ((tuple = systable_getnext(sd)) != NULL)
452 {
453 Datum value;
454 bool isnull;
455
456 /* Found the tuple, get description field */
457 value = heap_getattr(tuple, Anum_pg_description_description, tupdesc, &isnull);
458 if (!isnull)
460 break; /* Assume there can be only one match */
461 }
462
464
465 /* Done */
467
468 return comment;
469}
#define TextDatumGetCString(d)
Definition builtins.h:99
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
static struct @174 value
#define AccessShareLock
Definition lockdefs.h:36

References AccessShareLock, BTEqualStrategyNumber, comment, description, fb(), heap_getattr(), Int32GetDatum(), ObjectIdGetDatum(), RelationGetDescr, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), TextDatumGetCString, and value.

Referenced by ATPostAlterTypeParse(), expandTableLikeClause(), RebuildConstraintComment(), and transformTableLikeClause().