Skip to content

Commit 74a1d4f

Browse files
committed
Improve behavior of concurrent rename statements.
Previously, renaming a table, sequence, view, index, foreign table, column, or trigger checked permissions before locking the object, which meant that if permissions were revoked during the lock wait, we would still allow the operation. Similarly, if the original object is dropped and a new one with the same name is created, the operation will be allowed if we had permissions on the old object; the permissions on the new object don't matter. All this is now fixed. Along the way, attempting to rename a trigger on a foreign table now gives the same error message as trying to create one there in the first place (i.e. that it's not a table or view) rather than simply stating that no trigger by that name exists. Patch by me; review by Noah Misch.
1 parent d039fd5 commit 74a1d4f

File tree

7 files changed

+192
-141
lines changed

7 files changed

+192
-141
lines changed

src/backend/commands/alter.c

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -105,62 +105,18 @@ ExecRenameStmt(RenameStmt *stmt)
105105
case OBJECT_SEQUENCE:
106106
case OBJECT_VIEW:
107107
case OBJECT_INDEX:
108+
case OBJECT_FOREIGN_TABLE:
109+
RenameRelation(stmt);
110+
break;
111+
108112
case OBJECT_COLUMN:
109113
case OBJECT_ATTRIBUTE:
114+
renameatt(stmt);
115+
break;
116+
110117
case OBJECT_TRIGGER:
111-
case OBJECT_FOREIGN_TABLE:
112-
{
113-
Oid relid;
114-
115-
CheckRelationOwnership(stmt->relation, true);
116-
117-
/*
118-
* Lock level used here should match what will be taken later,
119-
* in RenameRelation, renameatt, or renametrig.
120-
*/
121-
relid = RangeVarGetRelid(stmt->relation, AccessExclusiveLock,
122-
false);
123-
124-
switch (stmt->renameType)
125-
{
126-
case OBJECT_TABLE:
127-
case OBJECT_SEQUENCE:
128-
case OBJECT_VIEW:
129-
case OBJECT_INDEX:
130-
case OBJECT_FOREIGN_TABLE:
131-
{
132-
/*
133-
* RENAME TABLE requires that we (still) hold
134-
* CREATE rights on the containing namespace, as
135-
* well as ownership of the table.
136-
*/
137-
Oid namespaceId = get_rel_namespace(relid);
138-
AclResult aclresult;
139-
140-
aclresult = pg_namespace_aclcheck(namespaceId,
141-
GetUserId(),
142-
ACL_CREATE);
143-
if (aclresult != ACLCHECK_OK)
144-
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
145-
get_namespace_name(namespaceId));
146-
147-
RenameRelation(relid, stmt->newname, stmt->renameType);
148-
break;
149-
}
150-
case OBJECT_COLUMN:
151-
case OBJECT_ATTRIBUTE:
152-
renameatt(relid, stmt);
153-
break;
154-
case OBJECT_TRIGGER:
155-
renametrig(relid,
156-
stmt->subname, /* old att name */
157-
stmt->newname); /* new att name */
158-
break;
159-
default:
160-
/* can't happen */ ;
161-
}
162-
break;
163-
}
118+
renametrig(stmt);
119+
break;
164120

165121
case OBJECT_TSPARSER:
166122
RenameTSParser(stmt->object, stmt->newname);

src/backend/commands/cluster.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,28 +1474,24 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
14741474
{
14751475
Relation toastrel;
14761476
Oid toastidx;
1477-
Oid toastnamespace;
14781477
char NewToastName[NAMEDATALEN];
14791478

14801479
toastrel = relation_open(newrel->rd_rel->reltoastrelid,
14811480
AccessShareLock);
14821481
toastidx = toastrel->rd_rel->reltoastidxid;
1483-
toastnamespace = toastrel->rd_rel->relnamespace;
14841482
relation_close(toastrel, AccessShareLock);
14851483

14861484
/* rename the toast table ... */
14871485
snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u",
14881486
OIDOldHeap);
14891487
RenameRelationInternal(newrel->rd_rel->reltoastrelid,
1490-
NewToastName,
1491-
toastnamespace);
1488+
NewToastName);
14921489

14931490
/* ... and its index too */
14941491
snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index",
14951492
OIDOldHeap);
14961493
RenameRelationInternal(toastidx,
1497-
NewToastName,
1498-
toastnamespace);
1494+
NewToastName);
14991495
}
15001496
relation_close(newrel, NoLock);
15011497
}

0 commit comments

Comments
 (0)