Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3ce3fb2

Browse files
committed
Ensure correct lock level is used in ALTER ... RENAME
Commit1b5d797 intended to relax the lock level used to renameindexes, but inadvertently allowed *any* relation to be renamed with alowered lock level, as long as the command is spelled ALTER INDEX.That's undesirable for other relation types, so retry the operation withthe higher lock if the relation turns out not to be an index.After this fix, ALTER INDEX <sometable> RENAME will require accessexclusive lock, which it didn't before.Author: Nathan Bossart <bossartn@amazon.com>Author: Álvaro Herrera <alvherre@alvh.no-ip.org>Reported-by: Onder Kalaci <onderk@microsoft.com>Discussion:https://postgr.es/m/PH0PR21MB1328189E2821CDEC646F8178D8AE9@PH0PR21MB1328.namprd21.prod.outlook.com
1 parent533315b commit3ce3fb2

File tree

3 files changed

+127
-14
lines changed

3 files changed

+127
-14
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,7 @@ RenameConstraint(RenameStmt *stmt)
37333733
ObjectAddress
37343734
RenameRelation(RenameStmt *stmt)
37353735
{
3736-
boolis_index = stmt->renameType == OBJECT_INDEX;
3736+
boolis_index_stmt = stmt->renameType == OBJECT_INDEX;
37373737
Oidrelid;
37383738
ObjectAddress address;
37393739

@@ -3743,24 +3743,48 @@ RenameRelation(RenameStmt *stmt)
37433743
* end of transaction.
37443744
*
37453745
* Lock level used here should match RenameRelationInternal, to avoid lock
3746-
* escalation.
3746+
* escalation. However, because ALTER INDEX can be used with any relation
3747+
* type, we mustn't believe without verification.
37473748
*/
3748-
relid = RangeVarGetRelidExtended(stmt->relation,
3749-
is_index ? ShareUpdateExclusiveLock : AccessExclusiveLock,
3750-
stmt->missing_ok ? RVR_MISSING_OK : 0,
3751-
RangeVarCallbackForAlterRelation,
3752-
(void *) stmt);
3753-
3754-
if (!OidIsValid(relid))
3749+
for (;;)
37553750
{
3756-
ereport(NOTICE,
3757-
(errmsg("relation \"%s\" does not exist, skipping",
3758-
stmt->relation->relname)));
3759-
return InvalidObjectAddress;
3751+
LOCKMODElockmode;
3752+
charrelkind;
3753+
boolobj_is_index;
3754+
3755+
lockmode = is_index_stmt ? ShareUpdateExclusiveLock : AccessExclusiveLock;
3756+
3757+
relid = RangeVarGetRelidExtended(stmt->relation, lockmode,
3758+
stmt->missing_ok ? RVR_MISSING_OK : 0,
3759+
RangeVarCallbackForAlterRelation,
3760+
(void *) stmt);
3761+
3762+
if (!OidIsValid(relid))
3763+
{
3764+
ereport(NOTICE,
3765+
(errmsg("relation \"%s\" does not exist, skipping",
3766+
stmt->relation->relname)));
3767+
return InvalidObjectAddress;
3768+
}
3769+
3770+
/*
3771+
* We allow mismatched statement and object types (e.g., ALTER INDEX
3772+
* to rename a table), but we might've used the wrong lock level. If
3773+
* that happens, retry with the correct lock level. We don't bother
3774+
* if we already acquired AccessExclusiveLock with an index, however.
3775+
*/
3776+
relkind = get_rel_relkind(relid);
3777+
obj_is_index = (relkind == RELKIND_INDEX ||
3778+
relkind == RELKIND_PARTITIONED_INDEX);
3779+
if (obj_is_index || is_index_stmt == obj_is_index)
3780+
break;
3781+
3782+
UnlockRelationOid(relid, lockmode);
3783+
is_index_stmt = obj_is_index;
37603784
}
37613785

37623786
/* Do the work */
3763-
RenameRelationInternal(relid, stmt->newname, false,is_index);
3787+
RenameRelationInternal(relid, stmt->newname, false,is_index_stmt);
37643788

37653789
ObjectAddressSet(address, RelationRelationId, relid);
37663790

@@ -3808,6 +3832,16 @@ RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bo
38083832
errmsg("relation \"%s\" already exists",
38093833
newrelname)));
38103834

3835+
/*
3836+
* RenameRelation is careful not to believe the caller's idea of the
3837+
* relation kind being handled. We don't have to worry about this, but
3838+
* let's not be totally oblivious to it. We can process an index as
3839+
* not-an-index, but not the other way around.
3840+
*/
3841+
Assert(!is_index ||
3842+
is_index == (targetrelation->rd_rel->relkind == RELKIND_INDEX ||
3843+
targetrelation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX));
3844+
38113845
/*
38123846
* Update pg_class tuple with new relname. (Scribbling on reltup is OK
38133847
* because it's a copy...)

‎src/test/regress/expected/alter_table.out

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,54 @@ SET ROLE regress_alter_table_user1;
237237
ALTER INDEX onek_unique1 RENAME TO fail; -- permission denied
238238
ERROR: must be owner of index onek_unique1
239239
RESET ROLE;
240+
-- rename statements with mismatching statement and object types
241+
CREATE TABLE alter_idx_rename_test (a INT);
242+
CREATE INDEX alter_idx_rename_test_idx ON alter_idx_rename_test (a);
243+
CREATE TABLE alter_idx_rename_test_parted (a INT) PARTITION BY LIST (a);
244+
CREATE INDEX alter_idx_rename_test_parted_idx ON alter_idx_rename_test_parted (a);
245+
BEGIN;
246+
ALTER INDEX alter_idx_rename_test RENAME TO alter_idx_rename_test_2;
247+
ALTER INDEX alter_idx_rename_test_parted RENAME TO alter_idx_rename_test_parted_2;
248+
SELECT relation::regclass, mode FROM pg_locks
249+
WHERE pid = pg_backend_pid() AND locktype = 'relation'
250+
AND relation::regclass::text LIKE 'alter\_idx%'
251+
ORDER BY relation::regclass::text;
252+
relation | mode
253+
--------------------------------+---------------------
254+
alter_idx_rename_test_2 | AccessExclusiveLock
255+
alter_idx_rename_test_parted_2 | AccessExclusiveLock
256+
(2 rows)
257+
258+
COMMIT;
259+
BEGIN;
260+
ALTER INDEX alter_idx_rename_test_idx RENAME TO alter_idx_rename_test_idx_2;
261+
ALTER INDEX alter_idx_rename_test_parted_idx RENAME TO alter_idx_rename_test_parted_idx_2;
262+
SELECT relation::regclass, mode FROM pg_locks
263+
WHERE pid = pg_backend_pid() AND locktype = 'relation'
264+
AND relation::regclass::text LIKE 'alter\_idx%'
265+
ORDER BY relation::regclass::text;
266+
relation | mode
267+
------------------------------------+--------------------------
268+
alter_idx_rename_test_idx_2 | ShareUpdateExclusiveLock
269+
alter_idx_rename_test_parted_idx_2 | ShareUpdateExclusiveLock
270+
(2 rows)
271+
272+
COMMIT;
273+
BEGIN;
274+
ALTER TABLE alter_idx_rename_test_idx_2 RENAME TO alter_idx_rename_test_idx_3;
275+
ALTER TABLE alter_idx_rename_test_parted_idx_2 RENAME TO alter_idx_rename_test_parted_idx_3;
276+
SELECT relation::regclass, mode FROM pg_locks
277+
WHERE pid = pg_backend_pid() AND locktype = 'relation'
278+
AND relation::regclass::text LIKE 'alter\_idx%'
279+
ORDER BY relation::regclass::text;
280+
relation | mode
281+
------------------------------------+---------------------
282+
alter_idx_rename_test_idx_3 | AccessExclusiveLock
283+
alter_idx_rename_test_parted_idx_3 | AccessExclusiveLock
284+
(2 rows)
285+
286+
COMMIT;
287+
DROP TABLE alter_idx_rename_test_2;
240288
-- renaming views
241289
CREATE VIEW attmp_view (unique1) AS SELECT unique1 FROM tenk1;
242290
ALTER TABLE attmp_view RENAME TO attmp_view_new;

‎src/test/regress/sql/alter_table.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,37 @@ SET ROLE regress_alter_table_user1;
231231
ALTERINDEX onek_unique1 RENAME TO fail;-- permission denied
232232
RESET ROLE;
233233

234+
-- rename statements with mismatching statement and object types
235+
CREATETABLEalter_idx_rename_test (aINT);
236+
CREATEINDEXalter_idx_rename_test_idxON alter_idx_rename_test (a);
237+
CREATETABLEalter_idx_rename_test_parted (aINT) PARTITION BY LIST (a);
238+
CREATEINDEXalter_idx_rename_test_parted_idxON alter_idx_rename_test_parted (a);
239+
BEGIN;
240+
ALTERINDEX alter_idx_rename_test RENAME TO alter_idx_rename_test_2;
241+
ALTERINDEX alter_idx_rename_test_parted RENAME TO alter_idx_rename_test_parted_2;
242+
SELECT relation::regclass, modeFROM pg_locks
243+
WHERE pid= pg_backend_pid()AND locktype='relation'
244+
AND relation::regclass::textLIKE'alter\_idx%'
245+
ORDER BY relation::regclass::text;
246+
COMMIT;
247+
BEGIN;
248+
ALTERINDEX alter_idx_rename_test_idx RENAME TO alter_idx_rename_test_idx_2;
249+
ALTERINDEX alter_idx_rename_test_parted_idx RENAME TO alter_idx_rename_test_parted_idx_2;
250+
SELECT relation::regclass, modeFROM pg_locks
251+
WHERE pid= pg_backend_pid()AND locktype='relation'
252+
AND relation::regclass::textLIKE'alter\_idx%'
253+
ORDER BY relation::regclass::text;
254+
COMMIT;
255+
BEGIN;
256+
ALTERTABLE alter_idx_rename_test_idx_2 RENAME TO alter_idx_rename_test_idx_3;
257+
ALTERTABLE alter_idx_rename_test_parted_idx_2 RENAME TO alter_idx_rename_test_parted_idx_3;
258+
SELECT relation::regclass, modeFROM pg_locks
259+
WHERE pid= pg_backend_pid()AND locktype='relation'
260+
AND relation::regclass::textLIKE'alter\_idx%'
261+
ORDER BY relation::regclass::text;
262+
COMMIT;
263+
DROPTABLE alter_idx_rename_test_2;
264+
234265
-- renaming views
235266
CREATEVIEWattmp_view (unique1)ASSELECT unique1FROM tenk1;
236267
ALTERTABLE attmp_view RENAME TO attmp_view_new;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp