Speed up dropping tables with many partitions.
authorRobert Haas
Fri, 28 Apr 2017 18:00:58 +0000 (14:00 -0400)
committerRobert Haas
Fri, 28 Apr 2017 18:02:24 +0000 (14:02 -0400)
We need to lock the parent, but we don't need a relcache entry
for it.

Gao Zeng Qi, reviewed by Amit Langote

Discussion: http://postgr.es/m/CAFmBtr0ukqJjRJEhPWL5wt4rNMrJUUxggVAGXPR3SyYh3E+HDQ@mail.gmail.com

src/backend/catalog/heap.c

index ece4df02cde98ba626838d9e22461d135c1ef929..ab3d83f29bba7a81a8e77dfbb6a9a2f9e5904274 100644 (file)
@@ -68,6 +68,7 @@
 #include "parser/parse_collate.h"
 #include "parser/parse_expr.h"
 #include "parser/parse_relation.h"
+#include "storage/lmgr.h"
 #include "storage/predicate.h"
 #include "storage/smgr.h"
 #include "utils/acl.h"
@@ -1760,8 +1761,7 @@ heap_drop_with_catalog(Oid relid)
 {
    Relation    rel;
    HeapTuple   tuple;
-   Oid         parentOid;
-   Relation    parent = NULL;
+   Oid         parentOid = InvalidOid;
 
    /*
     * To drop a partition safely, we must grab exclusive lock on its parent,
@@ -1776,7 +1776,7 @@ heap_drop_with_catalog(Oid relid)
    if (((Form_pg_class) GETSTRUCT(tuple))->relispartition)
    {
        parentOid = get_partition_parent(relid);
-       parent = heap_open(parentOid, AccessExclusiveLock);
+       LockRelationOid(parentOid, AccessExclusiveLock);
    }
 
    ReleaseSysCache(tuple);
@@ -1885,14 +1885,14 @@ heap_drop_with_catalog(Oid relid)
     */
    DeleteRelationTuple(relid);
 
-   if (parent)
+   if (OidIsValid(parentOid))
    {
        /*
         * Invalidate the parent's relcache so that the partition is no longer
         * included in its partition descriptor.
         */
-       CacheInvalidateRelcache(parent);
-       heap_close(parent, NoLock);     /* keep the lock */
+       CacheInvalidateRelcacheByRelid(parentOid);
+       /* keep the lock */
    }
 }