<para>
Acquired by <command>VACUUM</command> (without <option>FULL</option>),
<command>ANALYZE</command>, <command>CREATE INDEX CONCURRENTLY</command>,
- <command>CREATE STATISTICS</command> and
- <command>ALTER TABLE VALIDATE</command> and other
- <command>ALTER TABLE</command> variants (for full details see
- <xref linkend="sql-altertable"/>).
+ <command>CREATE STATISTICS</command>, and certain <command>ALTER
+ INDEX</command> and <command>ALTER TABLE</command> variants (for full
+ details see <xref linkend="sql-alterindex"/> and <xref
+ linkend="sql-altertable"/>).
</para>
</listitem>
</varlistentry>
</para>
<para>
- Acquired by <command>CREATE TRIGGER</command> and many forms of
+ Acquired by <command>CREATE TRIGGER</command> and some forms of
<command>ALTER TABLE</command> (see <xref linkend="sql-altertable"/>).
</para>
</listitem>
<command>CLUSTER</command>, <command>VACUUM FULL</command>,
and <command>REFRESH MATERIALIZED VIEW</command> (without
<option>CONCURRENTLY</option>)
- commands. Many forms of <command>ALTER TABLE</command> also acquire
+ commands. Many forms of <command>ALTER INDEX</command> and <command>ALTER TABLE</command> also acquire
a lock at this level. This is also the default lock mode for
<command>LOCK TABLE</command> statements that do not specify
a mode explicitly.
snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u",
OIDOldHeap);
RenameRelationInternal(newrel->rd_rel->reltoastrelid,
- NewToastName, true);
+ NewToastName, true, false);
/* ... and its valid index too. */
snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u_index",
OIDOldHeap);
RenameRelationInternal(toastidx,
- NewToastName, true);
+ NewToastName, true, true);
}
relation_close(newrel, NoLock);
}
|| con->contype == CONSTRAINT_UNIQUE
|| con->contype == CONSTRAINT_EXCLUSION))
/* rename the index; this renames the constraint as well */
- RenameRelationInternal(con->conindid, newconname, false);
+ RenameRelationInternal(con->conindid, newconname, false, true);
else
RenameConstraintById(constraintOid, newconname);
ObjectAddress
RenameRelation(RenameStmt *stmt)
{
+ bool is_index = stmt->renameType == OBJECT_INDEX;
Oid relid;
ObjectAddress address;
* Lock level used here should match RenameRelationInternal, to avoid lock
* escalation.
*/
- relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
+ relid = RangeVarGetRelidExtended(stmt->relation,
+ is_index ? ShareUpdateExclusiveLock : AccessExclusiveLock,
stmt->missing_ok ? RVR_MISSING_OK : 0,
RangeVarCallbackForAlterRelation,
(void *) stmt);
}
/* Do the work */
- RenameRelationInternal(relid, stmt->newname, false);
+ RenameRelationInternal(relid, stmt->newname, false, is_index);
ObjectAddressSet(address, RelationRelationId, relid);
* RenameRelationInternal - change the name of a relation
*/
void
-RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal)
+RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bool is_index)
{
Relation targetrelation;
Relation relrelation; /* for RELATION relation */
Oid namespaceId;
/*
- * Grab an exclusive lock on the target table, index, sequence, view,
- * materialized view, or foreign table, which we will NOT release until
- * end of transaction.
+ * Grab a lock on the target relation, which we will NOT release until end
+ * of transaction. We need at least a self-exclusive lock so that
+ * concurrent DDL doesn't overwrite the rename if they start updating
+ * while still seeing the old version. The lock also guards against
+ * triggering relcache reloads in concurrent sessions, which might not
+ * handle this information changing under them. For indexes, we can use a
+ * reduced lock level because RelationReloadIndexInfo() handles indexes
+ * specially.
*/
- targetrelation = relation_open(myrelid, AccessExclusiveLock);
+ targetrelation = relation_open(myrelid, is_index ? ShareUpdateExclusiveLock : AccessExclusiveLock);
namespaceId = RelationGetNamespace(targetrelation);
/*
}
/*
- * Close rel, but keep exclusive lock!
+ * Close rel, but keep lock!
*/
relation_close(targetrelation, NoLock);
}
ereport(NOTICE,
(errmsg("ALTER TABLE / ADD CONSTRAINT USING INDEX will rename index \"%s\" to \"%s\"",
indexName, constraintName)));
- RenameRelationInternal(index_oid, constraintName, false);
+ RenameRelationInternal(index_oid, constraintName, false, true);
}
/* Extra checks needed if making primary key */