From 4240e429d0c2d889d0cda23c618f94e12c13ade7 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Fri, 8 Jul 2011 22:19:30 -0400 Subject: Try to acquire relation locks in RangeVarGetRelid. In the previous coding, we would look up a relation in RangeVarGetRelid, lock the resulting OID, and then AcceptInvalidationMessages(). While this was sufficient to ensure that we noticed any changes to the relation definition before building the relcache entry, it didn't handle the possibility that the name we looked up no longer referenced the same OID. This was particularly problematic in the case where a table had been dropped and recreated: we'd latch on to the entry for the old relation and fail later on. Now, we acquire the relation lock inside RangeVarGetRelid, and retry the name lookup if we notice that invalidation messages have been processed meanwhile. Many operations that would previously have failed with an error in the presence of concurrent DDL will now succeed. There is a good deal of work remaining to be done here: many callers of RangeVarGetRelid still pass NoLock for one reason or another. In addition, nothing in this patch guards against the possibility that the meaning of an unqualified name might change due to the creation of a relation in a schema earlier in the user's search path than the one where it was previously found. Furthermore, there's nothing at all here to guard against similar race conditions for non-relations. For all that, it's a start. Noah Misch and Robert Haas --- src/backend/commands/indexcmds.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/backend/commands/indexcmds.c') diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index b7c021d943a..50248540816 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -1531,9 +1531,15 @@ ReindexIndex(RangeVar *indexRelation) Oid indOid; HeapTuple tuple; - indOid = RangeVarGetRelid(indexRelation, false); + /* + * XXX: This is not safe in the presence of concurrent DDL. We should + * take AccessExclusiveLock here, but that would violate the rule that + * indexes should only be locked after their parent tables. For now, + * we live with it. + */ + indOid = RangeVarGetRelid(indexRelation, NoLock, false, false); tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(indOid)); - if (!HeapTupleIsValid(tuple)) /* shouldn't happen */ + if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for relation %u", indOid); if (((Form_pg_class) GETSTRUCT(tuple))->relkind != RELKIND_INDEX) @@ -1562,7 +1568,8 @@ ReindexTable(RangeVar *relation) Oid heapOid; HeapTuple tuple; - heapOid = RangeVarGetRelid(relation, false); + /* The lock level used here should match reindex_relation(). */ + heapOid = RangeVarGetRelid(relation, ShareLock, false, false); tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(heapOid)); if (!HeapTupleIsValid(tuple)) /* shouldn't happen */ elog(ERROR, "cache lookup failed for relation %u", heapOid); -- cgit v1.2.3