From e76a5f93280ca26dc92ea8230c48d894569850b3 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Fri, 4 Aug 2017 11:16:01 +0200
Subject: [PATCH 3/4] Refactor routines for subscription and publication
 lookups

Those routines gain a missing_ok argument, allowing a caller to get a
NULL result instead of an error if set to true.
---
 src/backend/catalog/objectaddress.c   | 13 +++++++------
 src/backend/catalog/pg_publication.c  |  6 +++++-
 src/backend/catalog/pg_subscription.c |  6 +++++-
 src/include/catalog/pg_publication.h  |  2 +-
 src/include/catalog/pg_subscription.h |  2 +-
 5 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c
index 98fa02aa02..c4f993b8f7 100644
--- a/src/backend/catalog/objectaddress.c
+++ b/src/backend/catalog/objectaddress.c
@@ -3367,7 +3367,8 @@ getObjectDescription(const ObjectAddress *object)
 		case OCLASS_PUBLICATION:
 			{
 				appendStringInfo(&buffer, _("publication %s"),
-								 get_publication_name(object->objectId));
+								 get_publication_name(object->objectId,
+													  false));
 				break;
 			}
 
@@ -3384,7 +3385,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);
 
 				appendStringInfo(&buffer, _("publication table %s in publication %s"),
 								 get_rel_name(prform->prrelid), pubname);
@@ -3395,7 +3396,7 @@ getObjectDescription(const ObjectAddress *object)
 		case OCLASS_SUBSCRIPTION:
 			{
 				appendStringInfo(&buffer, _("subscription %s"),
-								 get_subscription_name(object->objectId));
+								 get_subscription_name(object->objectId, false));
 				break;
 			}
 
@@ -4890,7 +4891,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)
@@ -4911,7 +4912,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);
 
 				appendStringInfo(&buffer, _("%s in publication %s"),
 								 get_rel_name(prform->prrelid), pubname);
@@ -4930,7 +4931,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)
diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index 3ef7ba8cd5..4f74c47e60 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -419,7 +419,7 @@ get_publication_oid(const char *pubname, bool missing_ok)
  * get_publication_name - given a publication Oid, look up the name
  */
 char *
-get_publication_name(Oid pubid)
+get_publication_name(Oid pubid, bool missing_ok)
 {
 	HeapTuple	tup;
 	char	   *pubname;
@@ -428,7 +428,11 @@ get_publication_name(Oid pubid)
 	tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
 
 	if (!HeapTupleIsValid(tup))
+	{
+		if (missing_ok)
+			return NULL;
 		elog(ERROR, "cache lookup failed for publication %u", pubid);
+	}
 
 	pubform = (Form_pg_publication) GETSTRUCT(tup);
 	pubname = pstrdup(NameStr(pubform->pubname));
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index fb53d71cd6..7c96859d0a 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -181,7 +181,7 @@ get_subscription_oid(const char *subname, bool missing_ok)
  * get_subscription_name - given a subscription OID, look up the name
  */
 char *
-get_subscription_name(Oid subid)
+get_subscription_name(Oid subid, bool missing_ok)
 {
 	HeapTuple	tup;
 	char	   *subname;
@@ -190,7 +190,11 @@ get_subscription_name(Oid subid)
 	tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
 
 	if (!HeapTupleIsValid(tup))
+	{
+		if (missing_ok)
+			return NULL;
 		elog(ERROR, "cache lookup failed for subscription %u", subid);
+	}
 
 	subform = (Form_pg_subscription) GETSTRUCT(tup);
 	subname = pstrdup(NameStr(subform->subname));
diff --git a/src/include/catalog/pg_publication.h b/src/include/catalog/pg_publication.h
index aa148960cd..64a8cffef0 100644
--- a/src/include/catalog/pg_publication.h
+++ b/src/include/catalog/pg_publication.h
@@ -97,7 +97,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);
 
diff --git a/src/include/catalog/pg_subscription.h b/src/include/catalog/pg_subscription.h
index 274ff6bc42..7b8eb789c4 100644
--- a/src/include/catalog/pg_subscription.h
+++ b/src/include/catalog/pg_subscription.h
@@ -89,7 +89,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);
 
-- 
2.13.4

