Skip to content

Commit 1d6fbc3

Browse files
committed
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
1 parent 07a3af0 commit 1d6fbc3

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

src/backend/catalog/objectaddress.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,7 +3508,8 @@ getObjectDescription(const ObjectAddress *object)
35083508
case OCLASS_PUBLICATION:
35093509
{
35103510
appendStringInfo(&buffer, _("publication %s"),
3511-
get_publication_name(object->objectId));
3511+
get_publication_name(object->objectId,
3512+
false));
35123513
break;
35133514
}
35143515

@@ -3526,7 +3527,7 @@ getObjectDescription(const ObjectAddress *object)
35263527
object->objectId);
35273528

35283529
prform = (Form_pg_publication_rel) GETSTRUCT(tup);
3529-
pubname = get_publication_name(prform->prpubid);
3530+
pubname = get_publication_name(prform->prpubid, false);
35303531

35313532
initStringInfo(&rel);
35323533
getRelationDescription(&rel, prform->prrelid);
@@ -3542,7 +3543,8 @@ getObjectDescription(const ObjectAddress *object)
35423543
case OCLASS_SUBSCRIPTION:
35433544
{
35443545
appendStringInfo(&buffer, _("subscription %s"),
3545-
get_subscription_name(object->objectId));
3546+
get_subscription_name(object->objectId,
3547+
false));
35463548
break;
35473549
}
35483550

@@ -5042,7 +5044,7 @@ getObjectIdentityParts(const ObjectAddress *object,
50425044
{
50435045
char *pubname;
50445046

5045-
pubname = get_publication_name(object->objectId);
5047+
pubname = get_publication_name(object->objectId, false);
50465048
appendStringInfoString(&buffer,
50475049
quote_identifier(pubname));
50485050
if (objname)
@@ -5063,7 +5065,7 @@ getObjectIdentityParts(const ObjectAddress *object,
50635065
object->objectId);
50645066

50655067
prform = (Form_pg_publication_rel) GETSTRUCT(tup);
5066-
pubname = get_publication_name(prform->prpubid);
5068+
pubname = get_publication_name(prform->prpubid, false);
50675069

50685070
getRelationIdentity(&buffer, prform->prrelid, objname);
50695071
appendStringInfo(&buffer, " in publication %s", pubname);
@@ -5079,7 +5081,7 @@ getObjectIdentityParts(const ObjectAddress *object,
50795081
{
50805082
char *subname;
50815083

5082-
subname = get_subscription_name(object->objectId);
5084+
subname = get_subscription_name(object->objectId, false);
50835085
appendStringInfoString(&buffer,
50845086
quote_identifier(subname));
50855087
if (objname)

src/backend/catalog/pg_publication.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,12 @@ get_publication_oid(const char *pubname, bool missing_ok)
427427

428428
/*
429429
* get_publication_name - given a publication Oid, look up the name
430+
*
431+
* If missing_ok is false, throw an error if name not found. If true, just
432+
* return NULL.
430433
*/
431434
char *
432-
get_publication_name(Oid pubid)
435+
get_publication_name(Oid pubid, bool missing_ok)
433436
{
434437
HeapTuple tup;
435438
char *pubname;
@@ -438,7 +441,11 @@ get_publication_name(Oid pubid)
438441
tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
439442

440443
if (!HeapTupleIsValid(tup))
441-
elog(ERROR, "cache lookup failed for publication %u", pubid);
444+
{
445+
if (!missing_ok)
446+
elog(ERROR, "cache lookup failed for publication %u", pubid);
447+
return NULL;
448+
}
442449

443450
pubform = (Form_pg_publication) GETSTRUCT(tup);
444451
pubname = pstrdup(NameStr(pubform->pubname));

src/backend/catalog/pg_subscription.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,12 @@ get_subscription_oid(const char *subname, bool missing_ok)
179179

180180
/*
181181
* get_subscription_name - given a subscription OID, look up the name
182+
*
183+
* If missing_ok is false, throw an error if name not found. If true, just
184+
* return NULL.
182185
*/
183186
char *
184-
get_subscription_name(Oid subid)
187+
get_subscription_name(Oid subid, bool missing_ok)
185188
{
186189
HeapTuple tup;
187190
char *subname;
@@ -190,7 +193,11 @@ get_subscription_name(Oid subid)
190193
tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
191194

192195
if (!HeapTupleIsValid(tup))
193-
elog(ERROR, "cache lookup failed for subscription %u", subid);
196+
{
197+
if (!missing_ok)
198+
elog(ERROR, "cache lookup failed for subscription %u", subid);
199+
return NULL;
200+
}
194201

195202
subform = (Form_pg_subscription) GETSTRUCT(tup);
196203
subname = pstrdup(NameStr(subform->subname));

src/include/catalog/pg_publication.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extern ObjectAddress publication_add_relation(Oid pubid, Relation targetrel,
8888
bool if_not_exists);
8989

9090
extern Oid get_publication_oid(const char *pubname, bool missing_ok);
91-
extern char *get_publication_name(Oid pubid);
91+
extern char *get_publication_name(Oid pubid, bool missing_ok);
9292

9393
extern Datum pg_get_publication_tables(PG_FUNCTION_ARGS);
9494

src/include/catalog/pg_subscription.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ typedef struct Subscription
8080
extern Subscription *GetSubscription(Oid subid, bool missing_ok);
8181
extern void FreeSubscription(Subscription *sub);
8282
extern Oid get_subscription_oid(const char *subname, bool missing_ok);
83-
extern char *get_subscription_name(Oid subid);
83+
extern char *get_subscription_name(Oid subid, bool missing_ok);
8484

8585
extern int CountDBSubscriptions(Oid dbid);
8686

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy