Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e468ec0

Browse files
committed
Add an assertion in get_object_address()
Some places declared a Relation before calling get_object_address() only to assert that the relation is NULL after the call. The new assertion allows passing NULL as the relation argument at those places making the code cleaner and easier to understand. Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://www.postgresql.org/message-id/ZzG34eNrT83W/Orz@ip-10-97-1-34.eu-west-3.compute.internal
1 parent 818119a commit e468ec0

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/backend/catalog/objectaddress.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,8 @@ static void getRelationIdentity(StringInfo buffer, Oid relid, List **object,
896896
*
897897
* If the object is a relation or a child object of a relation (e.g. an
898898
* attribute or constraint), the relation is also opened and *relp receives
899-
* the open relcache entry pointer; otherwise, *relp is set to NULL. This
899+
* the open relcache entry pointer; otherwise, *relp is set to NULL.
900+
* (relp can be NULL if the caller never passes a relation-related object.) This
900901
* is a bit grotty but it makes life simpler, since the caller will
901902
* typically need the relcache entry too. Caller must close the relcache
902903
* entry when done with it. The relation is locked with the specified lockmode
@@ -1204,8 +1205,12 @@ get_object_address(ObjectType objtype, Node *object,
12041205
old_address = address;
12051206
}
12061207

1208+
/* relp must be given if it's a relation */
1209+
Assert(!relation || relp);
1210+
12071211
/* Return the object address and the relation. */
1208-
*relp = relation;
1212+
if (relp)
1213+
*relp = relation;
12091214
return address;
12101215
}
12111216

src/backend/commands/alter.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,11 @@ ExecRenameStmt(RenameStmt *stmt)
421421
{
422422
ObjectAddress address;
423423
Relation catalog;
424-
Relation relation;
425424

426425
address = get_object_address(stmt->renameType,
427426
stmt->object,
428-
&relation,
427+
NULL,
429428
AccessExclusiveLock, false);
430-
Assert(relation == NULL);
431429

432430
catalog = table_open(address.classId, RowExclusiveLock);
433431
AlterObjectRename_internal(catalog,
@@ -482,8 +480,7 @@ ExecAlterObjectDependsStmt(AlterObjectDependsStmt *stmt, ObjectAddress *refAddre
482480
table_close(rel, NoLock);
483481

484482
refAddr = get_object_address(OBJECT_EXTENSION, (Node *) stmt->extname,
485-
&rel, AccessExclusiveLock, false);
486-
Assert(rel == NULL);
483+
NULL, AccessExclusiveLock, false);
487484
if (refAddress)
488485
*refAddress = refAddr;
489486

@@ -563,16 +560,14 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt,
563560
case OBJECT_TSTEMPLATE:
564561
{
565562
Relation catalog;
566-
Relation relation;
567563
Oid classId;
568564
Oid nspOid;
569565

570566
address = get_object_address(stmt->objectType,
571567
stmt->object,
572-
&relation,
568+
NULL,
573569
AccessExclusiveLock,
574570
false);
575-
Assert(relation == NULL);
576571
classId = address.classId;
577572
catalog = table_open(classId, RowExclusiveLock);
578573
nspOid = LookupCreationNamespace(stmt->newschema);
@@ -876,15 +871,13 @@ ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
876871
case OBJECT_TSDICTIONARY:
877872
case OBJECT_TSCONFIGURATION:
878873
{
879-
Relation relation;
880874
ObjectAddress address;
881875

882876
address = get_object_address(stmt->objectType,
883877
stmt->object,
884-
&relation,
878+
NULL,
885879
AccessExclusiveLock,
886880
false);
887-
Assert(relation == NULL);
888881

889882
AlterObjectOwner_internal(address.classId, address.objectId,
890883
newowner);

0 commit comments

Comments
 (0)