Change get_constraint_index() to use pg_constraint.conindid
authorPeter Eisentraut <peter@eisentraut.org>
Wed, 9 Dec 2020 14:12:05 +0000 (15:12 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 9 Dec 2020 14:41:45 +0000 (15:41 +0100)
It was still using a scan of pg_depend instead of using the conindid
column that has been added since.

Since it is now just a catalog lookup wrapper and not related to
pg_depend, move from pg_depend.c to lsyscache.c.

Reviewed-by: Matthias van de Meent <boekewurm+postgres@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://www.postgresql.org/message-id/flat/4688d55c-9a2e-9a5a-d166-5f24fe0bf8db%40enterprisedb.com

src/backend/catalog/pg_depend.c
src/backend/commands/tablecmds.c
src/backend/optimizer/util/plancat.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/cache/lsyscache.c
src/include/catalog/dependency.h
src/include/utils/lsyscache.h

index 25290c821fda3a594b5a9756d557f981497753a2..429791694f0fe3c2ed2b483417d7ce2ed4973102 100644 (file)
@@ -968,75 +968,6 @@ getIdentitySequence(Oid relid, AttrNumber attnum, bool missing_ok)
        return linitial_oid(seqlist);
 }
 
-/*
- * get_constraint_index
- *             Given the OID of a unique, primary-key, or exclusion constraint,
- *             return the OID of the underlying index.
- *
- * Return InvalidOid if the index couldn't be found; this suggests the
- * given OID is bogus, but we leave it to caller to decide what to do.
- */
-Oid
-get_constraint_index(Oid constraintId)
-{
-       Oid                     indexId = InvalidOid;
-       Relation        depRel;
-       ScanKeyData key[3];
-       SysScanDesc scan;
-       HeapTuple       tup;
-
-       /* Search the dependency table for the dependent index */
-       depRel = table_open(DependRelationId, AccessShareLock);
-
-       ScanKeyInit(&key[0],
-                               Anum_pg_depend_refclassid,
-                               BTEqualStrategyNumber, F_OIDEQ,
-                               ObjectIdGetDatum(ConstraintRelationId));
-       ScanKeyInit(&key[1],
-                               Anum_pg_depend_refobjid,
-                               BTEqualStrategyNumber, F_OIDEQ,
-                               ObjectIdGetDatum(constraintId));
-       ScanKeyInit(&key[2],
-                               Anum_pg_depend_refobjsubid,
-                               BTEqualStrategyNumber, F_INT4EQ,
-                               Int32GetDatum(0));
-
-       scan = systable_beginscan(depRel, DependReferenceIndexId, true,
-                                                         NULL, 3, key);
-
-       while (HeapTupleIsValid(tup = systable_getnext(scan)))
-       {
-               Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
-
-               /*
-                * We assume any internal dependency of an index on the constraint
-                * must be what we are looking for.
-                */
-               if (deprec->classid == RelationRelationId &&
-                       deprec->objsubid == 0 &&
-                       deprec->deptype == DEPENDENCY_INTERNAL)
-               {
-                       char            relkind = get_rel_relkind(deprec->objid);
-
-                       /*
-                        * This is pure paranoia; there shouldn't be any other relkinds
-                        * dependent on a constraint.
-                        */
-                       if (relkind != RELKIND_INDEX &&
-                               relkind != RELKIND_PARTITIONED_INDEX)
-                               continue;
-
-                       indexId = deprec->objid;
-                       break;
-               }
-       }
-
-       systable_endscan(scan);
-       table_close(depRel, AccessShareLock);
-
-       return indexId;
-}
-
 /*
  * get_index_constraint
  *             Given the OID of an index, return the OID of the owning unique,
index 46f1637e77424e7819621ee6a20ca37b04514be0..1fa9f19f08cbcfa19a5b89043a5ee3e56bd4a9de 100644 (file)
@@ -26,7 +26,6 @@
 #include "access/xact.h"
 #include "access/xlog.h"
 #include "catalog/catalog.h"
-#include "catalog/dependency.h"
 #include "catalog/heap.h"
 #include "catalog/index.h"
 #include "catalog/namespace.h"
index 3e94256d34a340d6569ffaec5cbe6055fe5330a9..daf17596233f1aff6cdbe06da4d1987c11bcb904 100644 (file)
@@ -26,7 +26,6 @@
 #include "access/transam.h"
 #include "access/xlog.h"
 #include "catalog/catalog.h"
-#include "catalog/dependency.h"
 #include "catalog/heap.h"
 #include "catalog/index.h"
 #include "catalog/pg_am.h"
index c2c6df2a4f7bb787c0f4fd7909d77f67cdf036ec..ad582f99a5a0702fd967335ae649f23b23cbf043 100644 (file)
@@ -24,7 +24,6 @@
 #include "access/relation.h"
 #include "access/sysattr.h"
 #include "access/table.h"
-#include "catalog/dependency.h"
 #include "catalog/pg_aggregate.h"
 #include "catalog/pg_am.h"
 #include "catalog/pg_authid.h"
@@ -2140,7 +2139,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
 
                                appendStringInfoChar(&buf, ')');
 
-                               indexId = get_constraint_index(constraintId);
+                               indexId = conForm->conindid;
 
                                /* Build including column list (from pg_index.indkeys) */
                                indtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexId));
index ae23299162377483b6c3fd835188050ebe78f497..47a83658492496a3a2c646f19648d1693f8bbff3 100644 (file)
@@ -1094,6 +1094,33 @@ get_constraint_name(Oid conoid)
                return NULL;
 }
 
+/*
+ * get_constraint_index
+ *             Given the OID of a unique, primary-key, or exclusion constraint,
+ *             return the OID of the underlying index.
+ *
+ * Return InvalidOid if the index couldn't be found; this suggests the
+ * given OID is bogus, but we leave it to caller to decide what to do.
+ */
+Oid
+get_constraint_index(Oid conoid)
+{
+       HeapTuple       tp;
+
+       tp = SearchSysCache1(CONSTROID, ObjectIdGetDatum(conoid));
+       if (HeapTupleIsValid(tp))
+       {
+               Form_pg_constraint contup = (Form_pg_constraint) GETSTRUCT(tp);
+               Oid                     result;
+
+               result = contup->conindid;
+               ReleaseSysCache(tp);
+               return result;
+       }
+       else
+               return InvalidOid;
+}
+
 /*                             ---------- LANGUAGE CACHE ----------                                     */
 
 char *
index 901d5019cdfd5cb8a743ba69f03df812ac1c37e4..1fcc4a39ec4917de6e3cdb351e78a582a3983f3a 100644 (file)
@@ -235,8 +235,6 @@ extern bool sequenceIsOwned(Oid seqId, char deptype, Oid *tableId, int32 *colId)
 extern List *getOwnedSequences(Oid relid);
 extern Oid     getIdentitySequence(Oid relid, AttrNumber attnum, bool missing_ok);
 
-extern Oid     get_constraint_index(Oid constraintId);
-
 extern Oid     get_index_constraint(Oid indexId);
 
 extern List *get_index_ref_constraints(Oid indexId);
index fecfe1f4f6ef71a14eed1ab16d773250ec19e398..c97e12dde8a315454c1f1fc0716641dafeb83c75 100644 (file)
@@ -96,6 +96,7 @@ extern Oid    get_cast_oid(Oid sourcetypeid, Oid targettypeid, bool missing_ok);
 extern char *get_collation_name(Oid colloid);
 extern bool get_collation_isdeterministic(Oid colloid);
 extern char *get_constraint_name(Oid conoid);
+extern Oid     get_constraint_index(Oid conoid);
 extern char *get_language_name(Oid langoid, bool missing_ok);
 extern Oid     get_opclass_family(Oid opclass);
 extern Oid     get_opclass_input_type(Oid opclass);