Refactor cluster.c to use new routine get_index_isclustered()
authorMichael Paquier <[email protected]>
Mon, 6 Apr 2020 02:44:23 +0000 (11:44 +0900)
committerMichael Paquier <[email protected]>
Mon, 6 Apr 2020 02:44:23 +0000 (11:44 +0900)
This new cache lookup routine has been introduced in a40caf5, and more
code paths can directly use it.

Note that in cluster_rel(), the code was returning immediately if the
tuple's entry in pg_index for the clustered index was not valid.  This
commit changes the code so as a lookup error is raised instead,
something that could not happen from the start as we check for the
existence of the index beforehand, while holding an exclusive lock on
the parent table.

Author: Justin Pryzby
Reviewed-by: Álvaro Herrera, Michael Paquier
Discussion: https://postgr.es/m/20200202161718[email protected]

src/backend/commands/cluster.c

index ccd0c9b286e67060a09c3938615c15293e7ecc38..04d12a7ece69855b12c86c468d17706b08795edf 100644 (file)
@@ -139,21 +139,9 @@ cluster(ClusterStmt *stmt, bool isTopLevel)
            /* We need to find the index that has indisclustered set. */
            foreach(index, RelationGetIndexList(rel))
            {
-               HeapTuple   idxtuple;
-               Form_pg_index indexForm;
-
                indexOid = lfirst_oid(index);
-               idxtuple = SearchSysCache1(INDEXRELID,
-                                          ObjectIdGetDatum(indexOid));
-               if (!HeapTupleIsValid(idxtuple))
-                   elog(ERROR, "cache lookup failed for index %u", indexOid);
-               indexForm = (Form_pg_index) GETSTRUCT(idxtuple);
-               if (indexForm->indisclustered)
-               {
-                   ReleaseSysCache(idxtuple);
+               if (get_index_isclustered(indexOid))
                    break;
-               }
-               ReleaseSysCache(idxtuple);
                indexOid = InvalidOid;
            }
 
@@ -304,9 +292,6 @@ cluster_rel(Oid tableOid, Oid indexOid, int options)
     */
    if (recheck)
    {
-       HeapTuple   tuple;
-       Form_pg_index indexForm;
-
        /* Check that the user still owns the relation */
        if (!pg_class_ownercheck(tableOid, GetUserId()))
        {
@@ -345,22 +330,12 @@ cluster_rel(Oid tableOid, Oid indexOid, int options)
            /*
             * Check that the index is still the one with indisclustered set.
             */
-           tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexOid));
-           if (!HeapTupleIsValid(tuple))   /* probably can't happen */
-           {
-               relation_close(OldHeap, AccessExclusiveLock);
-               pgstat_progress_end_command();
-               return;
-           }
-           indexForm = (Form_pg_index) GETSTRUCT(tuple);
-           if (!indexForm->indisclustered)
+           if (!get_index_isclustered(indexOid))
            {
-               ReleaseSysCache(tuple);
                relation_close(OldHeap, AccessExclusiveLock);
                pgstat_progress_end_command();
                return;
            }
-           ReleaseSysCache(tuple);
        }
    }
 
@@ -519,18 +494,8 @@ mark_index_clustered(Relation rel, Oid indexOid, bool is_internal)
     */
    if (OidIsValid(indexOid))
    {
-       indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexOid));
-       if (!HeapTupleIsValid(indexTuple))
-           elog(ERROR, "cache lookup failed for index %u", indexOid);
-       indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
-
-       if (indexForm->indisclustered)
-       {
-           ReleaseSysCache(indexTuple);
+       if (get_index_isclustered(indexOid))
            return;
-       }
-
-       ReleaseSysCache(indexTuple);
    }
 
    /*