]> granicus.if.org Git - postgresql/commitdiff
Refactor routines for subscription and publication lookups
authorMichael Paquier <michael@paquier.xyz>
Tue, 18 Sep 2018 03:00:18 +0000 (12:00 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 18 Sep 2018 03:00:18 +0000 (12:00 +0900)
Those routines gain a missing_ok argument, allowing a caller to get a
NULL result instead of an error if set to true.  This is part of a
larger refactoring effort for objectaddress.c where trying to check for
non-existing objects does not result in cache lookup failures.

Author: Michael Paquier
Reviewed-by: Aleksander Alekseev, Álvaro Herrera
Discussion: https://postgr.es/m/CAB7nPqSZxrSmdHK-rny7z8mi=EAFXJ5J-0RbzDw6aus=wB5azQ@mail.gmail.com

src/backend/catalog/objectaddress.c
src/backend/catalog/pg_publication.c
src/backend/catalog/pg_subscription.c
src/include/catalog/pg_publication.h
src/include/catalog/pg_subscription.h

index 7db942dcbacbed5e3189d814117dd746819b6224..593e6f7022d9047bc6025796753d1839d637938c 100644 (file)
@@ -3508,7 +3508,8 @@ getObjectDescription(const ObjectAddress *object)
                case OCLASS_PUBLICATION:
                        {
                                appendStringInfo(&buffer, _("publication %s"),
-                                                                get_publication_name(object->objectId));
+                                                                get_publication_name(object->objectId,
+                                                                                                         false));
                                break;
                        }
 
@@ -3526,7 +3527,7 @@ getObjectDescription(const ObjectAddress *object)
                                                 object->objectId);
 
                                prform = (Form_pg_publication_rel) GETSTRUCT(tup);
-                               pubname = get_publication_name(prform->prpubid);
+                               pubname = get_publication_name(prform->prpubid, false);
 
                                initStringInfo(&rel);
                                getRelationDescription(&rel, prform->prrelid);
@@ -3542,7 +3543,8 @@ getObjectDescription(const ObjectAddress *object)
                case OCLASS_SUBSCRIPTION:
                        {
                                appendStringInfo(&buffer, _("subscription %s"),
-                                                                get_subscription_name(object->objectId));
+                                                                get_subscription_name(object->objectId,
+                                                                                                          false));
                                break;
                        }
 
@@ -5042,7 +5044,7 @@ getObjectIdentityParts(const ObjectAddress *object,
                        {
                                char       *pubname;
 
-                               pubname = get_publication_name(object->objectId);
+                               pubname = get_publication_name(object->objectId, false);
                                appendStringInfoString(&buffer,
                                                                           quote_identifier(pubname));
                                if (objname)
@@ -5063,7 +5065,7 @@ getObjectIdentityParts(const ObjectAddress *object,
                                                 object->objectId);
 
                                prform = (Form_pg_publication_rel) GETSTRUCT(tup);
-                               pubname = get_publication_name(prform->prpubid);
+                               pubname = get_publication_name(prform->prpubid, false);
 
                                getRelationIdentity(&buffer, prform->prrelid, objname);
                                appendStringInfo(&buffer, " in publication %s", pubname);
@@ -5079,7 +5081,7 @@ getObjectIdentityParts(const ObjectAddress *object,
                        {
                                char       *subname;
 
-                               subname = get_subscription_name(object->objectId);
+                               subname = get_subscription_name(object->objectId, false);
                                appendStringInfoString(&buffer,
                                                                           quote_identifier(subname));
                                if (objname)
index ec3bd1d22d22a399d54001d2a1d7ee3087f13bfc..3ecf6d57bf06e481ff00ce04c86631ec03806405 100644 (file)
@@ -427,9 +427,12 @@ get_publication_oid(const char *pubname, bool missing_ok)
 
 /*
  * 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)
+get_publication_name(Oid pubid, bool missing_ok)
 {
        HeapTuple       tup;
        char       *pubname;
@@ -438,7 +441,11 @@ get_publication_name(Oid pubid)
        tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
 
        if (!HeapTupleIsValid(tup))
-               elog(ERROR, "cache lookup failed for publication %u", pubid);
+       {
+               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));
index 8705d8b1d36d536e3ee33266b504a890f3208896..f891ff8054384672f75e968e2f6ae36ada43f587 100644 (file)
@@ -179,9 +179,12 @@ get_subscription_oid(const char *subname, bool missing_ok)
 
 /*
  * 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)
+get_subscription_name(Oid subid, bool missing_ok)
 {
        HeapTuple       tup;
        char       *subname;
@@ -190,7 +193,11 @@ get_subscription_name(Oid subid)
        tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
 
        if (!HeapTupleIsValid(tup))
-               elog(ERROR, "cache lookup failed for subscription %u", subid);
+       {
+               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));
index 83c59cb10dd2371aef9c3273491ec0cd2d56737f..a5d5570f76ec555ff6d1db51942abd1863252761 100644 (file)
@@ -88,7 +88,7 @@ extern ObjectAddress publication_add_relation(Oid pubid, Relation targetrel,
                                                 bool if_not_exists);
 
 extern Oid     get_publication_oid(const char *pubname, bool missing_ok);
-extern char *get_publication_name(Oid pubid);
+extern char *get_publication_name(Oid pubid, bool missing_ok);
 
 extern Datum pg_get_publication_tables(PG_FUNCTION_ARGS);
 
index 82dd6faf23886fa19330ca3f6bb506bb9bd1558a..e4dc771cf5322e30415a519ec118bbfcf2af1dbc 100644 (file)
@@ -80,7 +80,7 @@ typedef struct Subscription
 extern Subscription *GetSubscription(Oid subid, bool missing_ok);
 extern void FreeSubscription(Subscription *sub);
 extern Oid     get_subscription_oid(const char *subname, bool missing_ok);
-extern char *get_subscription_name(Oid subid);
+extern char *get_subscription_name(Oid subid, bool missing_ok);
 
 extern int     CountDBSubscriptions(Oid dbid);