Add sanity asserts for index OID and attnums during cache init
authorMichael Paquier <[email protected]>
Thu, 27 Jul 2023 01:55:16 +0000 (10:55 +0900)
committerMichael Paquier <[email protected]>
Thu, 27 Jul 2023 01:55:16 +0000 (10:55 +0900)
There was already a check on the relation OID, but not its index OID and
the attributes that can be used during the syscache lookups.  The two
assertions added by this commit are cheap, and actually useful for
developers to fasten the detection of incorrect data in a new entry
added in the syscache list, as these assertions are triggered during the
initial cache loading (initdb, or just backend startup), not requiring a
syscache that uses the new entry.

While on it, the relation OID check is switched to use OidIsValid().

Author: Aleksander Alekseev
Reviewed-by: Dagfinn Ilmari Mannsåker, Zhang Mingli, Michael Paquier
Discussion: https://postgr.es/m/CAJ7c6TOjUTJ0jxvWY6oJeP2-840OF8ch7qscZQsuVuotXTOS_g@mail.gmail.com

src/backend/utils/cache/catcache.c
src/backend/utils/cache/syscache.c

index 4510031fe69507b7138e481fe601205cb13cc5d6..1aacb736c22327408b0d7d9b6035bac7e6a72e4a 100644 (file)
@@ -825,7 +825,10 @@ InitCatCache(int id,
    cp->cc_nbuckets = nbuckets;
    cp->cc_nkeys = nkeys;
    for (i = 0; i < nkeys; ++i)
+   {
+       Assert(AttributeNumberIsValid(key[i]));
        cp->cc_keyno[i] = key[i];
+   }
 
    /*
     * new cache is initialized as far as we can go for now. print some
index 4e4a34bde800340ad75d28b5f11b6b732f2899ed..8dbda0024f90b6aa7fb421ea08365b3fe9cf0c50 100644 (file)
@@ -720,7 +720,9 @@ InitCatalogCache(void)
         * Assert that every enumeration value defined in syscache.h has been
         * populated in the cacheinfo array.
         */
-       Assert(cacheinfo[cacheId].reloid != 0);
+       Assert(OidIsValid(cacheinfo[cacheId].reloid));
+       Assert(OidIsValid(cacheinfo[cacheId].indoid));
+       /* .nbuckets and .key[] are checked by InitCatCache() */
 
        SysCache[cacheId] = InitCatCache(cacheId,
                                         cacheinfo[cacheId].reloid,