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.  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
This commit is contained in:
Michael Paquier 2018-09-18 12:00:18 +09:00
parent 07a3af0ff8
commit 1d6fbc38d9
5 changed files with 28 additions and 12 deletions

View File

@ -3508,7 +3508,8 @@ getObjectDescription(const ObjectAddress *object)
case OCLASS_PUBLICATION: case OCLASS_PUBLICATION:
{ {
appendStringInfo(&buffer, _("publication %s"), appendStringInfo(&buffer, _("publication %s"),
get_publication_name(object->objectId)); get_publication_name(object->objectId,
false));
break; break;
} }
@ -3526,7 +3527,7 @@ getObjectDescription(const ObjectAddress *object)
object->objectId); object->objectId);
prform = (Form_pg_publication_rel) GETSTRUCT(tup); prform = (Form_pg_publication_rel) GETSTRUCT(tup);
pubname = get_publication_name(prform->prpubid); pubname = get_publication_name(prform->prpubid, false);
initStringInfo(&rel); initStringInfo(&rel);
getRelationDescription(&rel, prform->prrelid); getRelationDescription(&rel, prform->prrelid);
@ -3542,7 +3543,8 @@ getObjectDescription(const ObjectAddress *object)
case OCLASS_SUBSCRIPTION: case OCLASS_SUBSCRIPTION:
{ {
appendStringInfo(&buffer, _("subscription %s"), appendStringInfo(&buffer, _("subscription %s"),
get_subscription_name(object->objectId)); get_subscription_name(object->objectId,
false));
break; break;
} }
@ -5042,7 +5044,7 @@ getObjectIdentityParts(const ObjectAddress *object,
{ {
char *pubname; char *pubname;
pubname = get_publication_name(object->objectId); pubname = get_publication_name(object->objectId, false);
appendStringInfoString(&buffer, appendStringInfoString(&buffer,
quote_identifier(pubname)); quote_identifier(pubname));
if (objname) if (objname)
@ -5063,7 +5065,7 @@ getObjectIdentityParts(const ObjectAddress *object,
object->objectId); object->objectId);
prform = (Form_pg_publication_rel) GETSTRUCT(tup); 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); getRelationIdentity(&buffer, prform->prrelid, objname);
appendStringInfo(&buffer, " in publication %s", pubname); appendStringInfo(&buffer, " in publication %s", pubname);
@ -5079,7 +5081,7 @@ getObjectIdentityParts(const ObjectAddress *object,
{ {
char *subname; char *subname;
subname = get_subscription_name(object->objectId); subname = get_subscription_name(object->objectId, false);
appendStringInfoString(&buffer, appendStringInfoString(&buffer,
quote_identifier(subname)); quote_identifier(subname));
if (objname) if (objname)

View 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 * 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 * char *
get_publication_name(Oid pubid) get_publication_name(Oid pubid, bool missing_ok)
{ {
HeapTuple tup; HeapTuple tup;
char *pubname; char *pubname;
@ -438,7 +441,11 @@ get_publication_name(Oid pubid)
tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid)); tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
if (!HeapTupleIsValid(tup)) 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); pubform = (Form_pg_publication) GETSTRUCT(tup);
pubname = pstrdup(NameStr(pubform->pubname)); pubname = pstrdup(NameStr(pubform->pubname));

View 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 * 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 * char *
get_subscription_name(Oid subid) get_subscription_name(Oid subid, bool missing_ok)
{ {
HeapTuple tup; HeapTuple tup;
char *subname; char *subname;
@ -190,7 +193,11 @@ get_subscription_name(Oid subid)
tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid)); tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
if (!HeapTupleIsValid(tup)) 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); subform = (Form_pg_subscription) GETSTRUCT(tup);
subname = pstrdup(NameStr(subform->subname)); subname = pstrdup(NameStr(subform->subname));

View File

@ -88,7 +88,7 @@ extern ObjectAddress publication_add_relation(Oid pubid, Relation targetrel,
bool if_not_exists); bool if_not_exists);
extern Oid get_publication_oid(const char *pubname, bool missing_ok); 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); extern Datum pg_get_publication_tables(PG_FUNCTION_ARGS);

View File

@ -80,7 +80,7 @@ typedef struct Subscription
extern Subscription *GetSubscription(Oid subid, bool missing_ok); extern Subscription *GetSubscription(Oid subid, bool missing_ok);
extern void FreeSubscription(Subscription *sub); extern void FreeSubscription(Subscription *sub);
extern Oid get_subscription_oid(const char *subname, bool missing_ok); 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); extern int CountDBSubscriptions(Oid dbid);