Move common catalog cache access routines to lsyscache.c
authorAmit Kapila <akapila@postgresql.org>
Tue, 2 Aug 2022 05:17:22 +0000 (10:47 +0530)
committerAmit Kapila <akapila@postgresql.org>
Tue, 2 Aug 2022 05:17:22 +0000 (10:47 +0530)
In passing, move pg_relation_is_publishable next to similar functions.

Suggested-by: Alvaro Herrera
Author: Amit Kapila
Reviewed-by: Hou Zhijie
Discussion: https://postgr.es/m/CAHut+PupQ5UW9A9ut0Yjt21J9tHhx958z5L0k8-9hTYf_NYqxA@mail.gmail.com

src/backend/catalog/pg_publication.c
src/backend/catalog/pg_subscription.c
src/backend/utils/cache/lsyscache.c
src/include/catalog/pg_publication.h
src/include/catalog/pg_subscription.h
src/include/utils/lsyscache.h

index e294bea60c56fed3ac6e122796cb76348f392103..6af357000512204dae9543ad6ac984a35cb79809 100644 (file)
@@ -149,6 +149,28 @@ is_publishable_relation(Relation rel)
    return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
 }
 
+/*
+ * SQL-callable variant of the above
+ *
+ * This returns null when the relation does not exist.  This is intended to be
+ * used for example in psql to avoid gratuitous errors when there are
+ * concurrent catalog changes.
+ */
+Datum
+pg_relation_is_publishable(PG_FUNCTION_ARGS)
+{
+   Oid         relid = PG_GETARG_OID(0);
+   HeapTuple   tuple;
+   bool        result;
+
+   tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
+   if (!HeapTupleIsValid(tuple))
+       PG_RETURN_NULL();
+   result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple));
+   ReleaseSysCache(tuple);
+   PG_RETURN_BOOL(result);
+}
+
 /*
  * Filter out the partitions whose parent tables were also specified in
  * the publication.
@@ -219,28 +241,6 @@ is_schema_publication(Oid pubid)
    return result;
 }
 
-/*
- * SQL-callable variant of the above
- *
- * This returns null when the relation does not exist.  This is intended to be
- * used for example in psql to avoid gratuitous errors when there are
- * concurrent catalog changes.
- */
-Datum
-pg_relation_is_publishable(PG_FUNCTION_ARGS)
-{
-   Oid         relid = PG_GETARG_OID(0);
-   HeapTuple   tuple;
-   bool        result;
-
-   tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
-   if (!HeapTupleIsValid(tuple))
-       PG_RETURN_NULL();
-   result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple));
-   ReleaseSysCache(tuple);
-   PG_RETURN_BOOL(result);
-}
-
 /*
  * Gets the relations based on the publication partition option for a specified
  * relation.
@@ -1012,7 +1012,6 @@ GetPublication(Oid pubid)
    return pub;
 }
 
-
 /*
  * Get Publication using name.
  */
@@ -1026,56 +1025,6 @@ GetPublicationByName(const char *pubname, bool missing_ok)
    return OidIsValid(oid) ? GetPublication(oid) : NULL;
 }
 
-/*
- * get_publication_oid - given a publication name, look up the OID
- *
- * If missing_ok is false, throw an error if name not found.  If true, just
- * return InvalidOid.
- */
-Oid
-get_publication_oid(const char *pubname, bool missing_ok)
-{
-   Oid         oid;
-
-   oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
-                         CStringGetDatum(pubname));
-   if (!OidIsValid(oid) && !missing_ok)
-       ereport(ERROR,
-               (errcode(ERRCODE_UNDEFINED_OBJECT),
-                errmsg("publication \"%s\" does not exist", pubname)));
-   return oid;
-}
-
-/*
- * get_publication_name - given a publication Oid, look up the name
- *
- * If missing_ok is false, throw an error if name not found.  If true, just
- * return NULL.
- */
-char *
-get_publication_name(Oid pubid, bool missing_ok)
-{
-   HeapTuple   tup;
-   char       *pubname;
-   Form_pg_publication pubform;
-
-   tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
-
-   if (!HeapTupleIsValid(tup))
-   {
-       if (!missing_ok)
-           elog(ERROR, "cache lookup failed for publication %u", pubid);
-       return NULL;
-   }
-
-   pubform = (Form_pg_publication) GETSTRUCT(tup);
-   pubname = pstrdup(NameStr(pubform->pubname));
-
-   ReleaseSysCache(tup);
-
-   return pubname;
-}
-
 /*
  * Returns information of tables in a publication.
  */
index c7d2537fb5dadef4f7a529c28b0fd6ac043eb2e5..a506fc3ec83df2508f7a9c5cbd41ed261718df2c 100644 (file)
@@ -205,56 +205,6 @@ DisableSubscription(Oid subid)
    table_close(rel, NoLock);
 }
 
-/*
- * get_subscription_oid - given a subscription name, look up the OID
- *
- * If missing_ok is false, throw an error if name not found.  If true, just
- * return InvalidOid.
- */
-Oid
-get_subscription_oid(const char *subname, bool missing_ok)
-{
-   Oid         oid;
-
-   oid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
-                         MyDatabaseId, CStringGetDatum(subname));
-   if (!OidIsValid(oid) && !missing_ok)
-       ereport(ERROR,
-               (errcode(ERRCODE_UNDEFINED_OBJECT),
-                errmsg("subscription \"%s\" does not exist", subname)));
-   return oid;
-}
-
-/*
- * get_subscription_name - given a subscription OID, look up the name
- *
- * If missing_ok is false, throw an error if name not found.  If true, just
- * return NULL.
- */
-char *
-get_subscription_name(Oid subid, bool missing_ok)
-{
-   HeapTuple   tup;
-   char       *subname;
-   Form_pg_subscription subform;
-
-   tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
-
-   if (!HeapTupleIsValid(tup))
-   {
-       if (!missing_ok)
-           elog(ERROR, "cache lookup failed for subscription %u", subid);
-       return NULL;
-   }
-
-   subform = (Form_pg_subscription) GETSTRUCT(tup);
-   subname = pstrdup(NameStr(subform->subname));
-
-   ReleaseSysCache(tup);
-
-   return subname;
-}
-
 /*
  * Convert text array to list of strings.
  *
index 1b7e11b93e015547b3ec036c8053ea454a5677de..a16a63f4957b525cfa9c1d0d694e91401ae9c100 100644 (file)
@@ -33,6 +33,7 @@
 #include "catalog/pg_proc.h"
 #include "catalog/pg_range.h"
 #include "catalog/pg_statistic.h"
+#include "catalog/pg_subscription.h"
 #include "catalog/pg_transform.h"
 #include "catalog/pg_type.h"
 #include "miscadmin.h"
@@ -3578,3 +3579,103 @@ get_index_isclustered(Oid index_oid)
 
    return isclustered;
 }
+
+/*
+ * get_publication_oid - given a publication name, look up the OID
+ *
+ * If missing_ok is false, throw an error if name not found.  If true, just
+ * return InvalidOid.
+ */
+Oid
+get_publication_oid(const char *pubname, bool missing_ok)
+{
+   Oid         oid;
+
+   oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
+                         CStringGetDatum(pubname));
+   if (!OidIsValid(oid) && !missing_ok)
+       ereport(ERROR,
+               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                errmsg("publication \"%s\" does not exist", pubname)));
+   return oid;
+}
+
+/*
+ * get_publication_name - given a publication Oid, look up the name
+ *
+ * If missing_ok is false, throw an error if name not found.  If true, just
+ * return NULL.
+ */
+char *
+get_publication_name(Oid pubid, bool missing_ok)
+{
+   HeapTuple   tup;
+   char    *pubname;
+   Form_pg_publication pubform;
+
+   tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
+
+   if (!HeapTupleIsValid(tup))
+   {
+       if (!missing_ok)
+           elog(ERROR, "cache lookup failed for publication %u", pubid);
+       return NULL;
+   }
+
+   pubform = (Form_pg_publication) GETSTRUCT(tup);
+   pubname = pstrdup(NameStr(pubform->pubname));
+
+   ReleaseSysCache(tup);
+
+   return pubname;
+}
+
+/*
+ * get_subscription_oid - given a subscription name, look up the OID
+ *
+ * If missing_ok is false, throw an error if name not found.  If true, just
+ * return InvalidOid.
+ */
+Oid
+get_subscription_oid(const char* subname, bool missing_ok)
+{
+   Oid         oid;
+
+   oid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
+                          MyDatabaseId, CStringGetDatum(subname));
+   if (!OidIsValid(oid) && !missing_ok)
+       ereport(ERROR,
+           (errcode(ERRCODE_UNDEFINED_OBJECT),
+            errmsg("subscription \"%s\" does not exist", subname)));
+   return oid;
+}
+
+/*
+ * get_subscription_name - given a subscription OID, look up the name
+ *
+ * If missing_ok is false, throw an error if name not found.  If true, just
+ * return NULL.
+ */
+char *
+get_subscription_name(Oid subid, bool missing_ok)
+{
+   HeapTuple   tup;
+   char* subname;
+   Form_pg_subscription subform;
+
+   tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
+
+   if (!HeapTupleIsValid(tup))
+   {
+       if (!missing_ok)
+           elog(ERROR, "cache lookup failed for subscription %u", subid);
+       return NULL;
+   }
+
+   subform = (Form_pg_subscription) GETSTRUCT(tup);
+   subname = pstrdup(NameStr(subform->subname));
+
+   ReleaseSysCache(tup);
+
+   return subname;
+}
index 48205ba42930f513c866369847cb5993a0f722cf..c298327f5e5c9dd180f5db251e58ab5238f4617a 100644 (file)
@@ -155,7 +155,4 @@ extern ObjectAddress publication_add_schema(Oid pubid, Oid schemaid,
 extern Bitmapset *pub_collist_to_bitmapset(Bitmapset *columns, Datum pubcols,
                                           MemoryContext mcxt);
 
-extern Oid get_publication_oid(const char *pubname, bool missing_ok);
-extern char *get_publication_name(Oid pubid, bool missing_ok);
-
 #endif                         /* PG_PUBLICATION_H */
index c9a3026b2832032f5ccd5ad7be4b786b57050674..7b98714f3085959aa9125211fa11f4a75ff2842d 100644 (file)
@@ -140,8 +140,6 @@ typedef struct Subscription
 extern Subscription *GetSubscription(Oid subid, bool missing_ok);
 extern void FreeSubscription(Subscription *sub);
 extern void DisableSubscription(Oid subid);
-extern Oid get_subscription_oid(const char *subname, bool missing_ok);
-extern char *get_subscription_name(Oid subid, bool missing_ok);
 
 extern int CountDBSubscriptions(Oid dbid);
 
index b8dd27d4a96085b9aa5746f0102c4ebe493055ab..50f028830525044a5f84965d6cadf7a9acd4fe7a 100644 (file)
@@ -198,6 +198,10 @@ extern Oid get_index_column_opclass(Oid index_oid, int attno);
 extern bool get_index_isreplident(Oid index_oid);
 extern bool get_index_isvalid(Oid index_oid);
 extern bool get_index_isclustered(Oid index_oid);
+extern Oid get_publication_oid(const char *pubname, bool missing_ok);
+extern char *get_publication_name(Oid pubid, bool missing_ok);
+extern Oid get_subscription_oid(const char *subname, bool missing_ok);
+extern char *get_subscription_name(Oid subid, bool missing_ok);
 
 #define type_is_array(typid)  (get_element_type(typid) != InvalidOid)
 /* type_is_array_domain accepts both plain arrays and domains over arrays */