Skip to content

Commit 6b24d3f

Browse files
author
Amit Kapila
committed
Move common catalog cache access routines to lsyscache.c
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
1 parent c689baa commit 6b24d3f

File tree

6 files changed

+127
-128
lines changed

6 files changed

+127
-128
lines changed

Diff for: src/backend/catalog/pg_publication.c

+22-73
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ is_publishable_relation(Relation rel)
149149
return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
150150
}
151151

152+
/*
153+
* SQL-callable variant of the above
154+
*
155+
* This returns null when the relation does not exist. This is intended to be
156+
* used for example in psql to avoid gratuitous errors when there are
157+
* concurrent catalog changes.
158+
*/
159+
Datum
160+
pg_relation_is_publishable(PG_FUNCTION_ARGS)
161+
{
162+
Oid relid = PG_GETARG_OID(0);
163+
HeapTuple tuple;
164+
bool result;
165+
166+
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
167+
if (!HeapTupleIsValid(tuple))
168+
PG_RETURN_NULL();
169+
result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple));
170+
ReleaseSysCache(tuple);
171+
PG_RETURN_BOOL(result);
172+
}
173+
152174
/*
153175
* Filter out the partitions whose parent tables were also specified in
154176
* the publication.
@@ -219,28 +241,6 @@ is_schema_publication(Oid pubid)
219241
return result;
220242
}
221243

222-
/*
223-
* SQL-callable variant of the above
224-
*
225-
* This returns null when the relation does not exist. This is intended to be
226-
* used for example in psql to avoid gratuitous errors when there are
227-
* concurrent catalog changes.
228-
*/
229-
Datum
230-
pg_relation_is_publishable(PG_FUNCTION_ARGS)
231-
{
232-
Oid relid = PG_GETARG_OID(0);
233-
HeapTuple tuple;
234-
bool result;
235-
236-
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
237-
if (!HeapTupleIsValid(tuple))
238-
PG_RETURN_NULL();
239-
result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple));
240-
ReleaseSysCache(tuple);
241-
PG_RETURN_BOOL(result);
242-
}
243-
244244
/*
245245
* Gets the relations based on the publication partition option for a specified
246246
* relation.
@@ -1012,7 +1012,6 @@ GetPublication(Oid pubid)
10121012
return pub;
10131013
}
10141014

1015-
10161015
/*
10171016
* Get Publication using name.
10181017
*/
@@ -1026,56 +1025,6 @@ GetPublicationByName(const char *pubname, bool missing_ok)
10261025
return OidIsValid(oid) ? GetPublication(oid) : NULL;
10271026
}
10281027

1029-
/*
1030-
* get_publication_oid - given a publication name, look up the OID
1031-
*
1032-
* If missing_ok is false, throw an error if name not found. If true, just
1033-
* return InvalidOid.
1034-
*/
1035-
Oid
1036-
get_publication_oid(const char *pubname, bool missing_ok)
1037-
{
1038-
Oid oid;
1039-
1040-
oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
1041-
CStringGetDatum(pubname));
1042-
if (!OidIsValid(oid) && !missing_ok)
1043-
ereport(ERROR,
1044-
(errcode(ERRCODE_UNDEFINED_OBJECT),
1045-
errmsg("publication \"%s\" does not exist", pubname)));
1046-
return oid;
1047-
}
1048-
1049-
/*
1050-
* get_publication_name - given a publication Oid, look up the name
1051-
*
1052-
* If missing_ok is false, throw an error if name not found. If true, just
1053-
* return NULL.
1054-
*/
1055-
char *
1056-
get_publication_name(Oid pubid, bool missing_ok)
1057-
{
1058-
HeapTuple tup;
1059-
char *pubname;
1060-
Form_pg_publication pubform;
1061-
1062-
tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
1063-
1064-
if (!HeapTupleIsValid(tup))
1065-
{
1066-
if (!missing_ok)
1067-
elog(ERROR, "cache lookup failed for publication %u", pubid);
1068-
return NULL;
1069-
}
1070-
1071-
pubform = (Form_pg_publication) GETSTRUCT(tup);
1072-
pubname = pstrdup(NameStr(pubform->pubname));
1073-
1074-
ReleaseSysCache(tup);
1075-
1076-
return pubname;
1077-
}
1078-
10791028
/*
10801029
* Returns information of tables in a publication.
10811030
*/

Diff for: src/backend/catalog/pg_subscription.c

-50
Original file line numberDiff line numberDiff line change
@@ -205,56 +205,6 @@ DisableSubscription(Oid subid)
205205
table_close(rel, NoLock);
206206
}
207207

208-
/*
209-
* get_subscription_oid - given a subscription name, look up the OID
210-
*
211-
* If missing_ok is false, throw an error if name not found. If true, just
212-
* return InvalidOid.
213-
*/
214-
Oid
215-
get_subscription_oid(const char *subname, bool missing_ok)
216-
{
217-
Oid oid;
218-
219-
oid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
220-
MyDatabaseId, CStringGetDatum(subname));
221-
if (!OidIsValid(oid) && !missing_ok)
222-
ereport(ERROR,
223-
(errcode(ERRCODE_UNDEFINED_OBJECT),
224-
errmsg("subscription \"%s\" does not exist", subname)));
225-
return oid;
226-
}
227-
228-
/*
229-
* get_subscription_name - given a subscription OID, look up the name
230-
*
231-
* If missing_ok is false, throw an error if name not found. If true, just
232-
* return NULL.
233-
*/
234-
char *
235-
get_subscription_name(Oid subid, bool missing_ok)
236-
{
237-
HeapTuple tup;
238-
char *subname;
239-
Form_pg_subscription subform;
240-
241-
tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
242-
243-
if (!HeapTupleIsValid(tup))
244-
{
245-
if (!missing_ok)
246-
elog(ERROR, "cache lookup failed for subscription %u", subid);
247-
return NULL;
248-
}
249-
250-
subform = (Form_pg_subscription) GETSTRUCT(tup);
251-
subname = pstrdup(NameStr(subform->subname));
252-
253-
ReleaseSysCache(tup);
254-
255-
return subname;
256-
}
257-
258208
/*
259209
* Convert text array to list of strings.
260210
*

Diff for: src/backend/utils/cache/lsyscache.c

+101
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "catalog/pg_proc.h"
3434
#include "catalog/pg_range.h"
3535
#include "catalog/pg_statistic.h"
36+
#include "catalog/pg_subscription.h"
3637
#include "catalog/pg_transform.h"
3738
#include "catalog/pg_type.h"
3839
#include "miscadmin.h"
@@ -3578,3 +3579,103 @@ get_index_isclustered(Oid index_oid)
35783579

35793580
return isclustered;
35803581
}
3582+
3583+
/*
3584+
* get_publication_oid - given a publication name, look up the OID
3585+
*
3586+
* If missing_ok is false, throw an error if name not found. If true, just
3587+
* return InvalidOid.
3588+
*/
3589+
Oid
3590+
get_publication_oid(const char *pubname, bool missing_ok)
3591+
{
3592+
Oid oid;
3593+
3594+
oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
3595+
CStringGetDatum(pubname));
3596+
if (!OidIsValid(oid) && !missing_ok)
3597+
ereport(ERROR,
3598+
(errcode(ERRCODE_UNDEFINED_OBJECT),
3599+
errmsg("publication \"%s\" does not exist", pubname)));
3600+
return oid;
3601+
}
3602+
3603+
/*
3604+
* get_publication_name - given a publication Oid, look up the name
3605+
*
3606+
* If missing_ok is false, throw an error if name not found. If true, just
3607+
* return NULL.
3608+
*/
3609+
char *
3610+
get_publication_name(Oid pubid, bool missing_ok)
3611+
{
3612+
HeapTuple tup;
3613+
char *pubname;
3614+
Form_pg_publication pubform;
3615+
3616+
tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
3617+
3618+
if (!HeapTupleIsValid(tup))
3619+
{
3620+
if (!missing_ok)
3621+
elog(ERROR, "cache lookup failed for publication %u", pubid);
3622+
return NULL;
3623+
}
3624+
3625+
pubform = (Form_pg_publication) GETSTRUCT(tup);
3626+
pubname = pstrdup(NameStr(pubform->pubname));
3627+
3628+
ReleaseSysCache(tup);
3629+
3630+
return pubname;
3631+
}
3632+
3633+
/*
3634+
* get_subscription_oid - given a subscription name, look up the OID
3635+
*
3636+
* If missing_ok is false, throw an error if name not found. If true, just
3637+
* return InvalidOid.
3638+
*/
3639+
Oid
3640+
get_subscription_oid(const char* subname, bool missing_ok)
3641+
{
3642+
Oid oid;
3643+
3644+
oid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid,
3645+
MyDatabaseId, CStringGetDatum(subname));
3646+
if (!OidIsValid(oid) && !missing_ok)
3647+
ereport(ERROR,
3648+
(errcode(ERRCODE_UNDEFINED_OBJECT),
3649+
errmsg("subscription \"%s\" does not exist", subname)));
3650+
return oid;
3651+
}
3652+
3653+
/*
3654+
* get_subscription_name - given a subscription OID, look up the name
3655+
*
3656+
* If missing_ok is false, throw an error if name not found. If true, just
3657+
* return NULL.
3658+
*/
3659+
char *
3660+
get_subscription_name(Oid subid, bool missing_ok)
3661+
{
3662+
HeapTuple tup;
3663+
char* subname;
3664+
Form_pg_subscription subform;
3665+
3666+
tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
3667+
3668+
if (!HeapTupleIsValid(tup))
3669+
{
3670+
if (!missing_ok)
3671+
elog(ERROR, "cache lookup failed for subscription %u", subid);
3672+
return NULL;
3673+
}
3674+
3675+
subform = (Form_pg_subscription) GETSTRUCT(tup);
3676+
subname = pstrdup(NameStr(subform->subname));
3677+
3678+
ReleaseSysCache(tup);
3679+
3680+
return subname;
3681+
}

Diff for: src/include/catalog/pg_publication.h

-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,4 @@ extern ObjectAddress publication_add_schema(Oid pubid, Oid schemaid,
155155
extern Bitmapset *pub_collist_to_bitmapset(Bitmapset *columns, Datum pubcols,
156156
MemoryContext mcxt);
157157

158-
extern Oid get_publication_oid(const char *pubname, bool missing_ok);
159-
extern char *get_publication_name(Oid pubid, bool missing_ok);
160-
161158
#endif /* PG_PUBLICATION_H */

Diff for: src/include/catalog/pg_subscription.h

-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ typedef struct Subscription
140140
extern Subscription *GetSubscription(Oid subid, bool missing_ok);
141141
extern void FreeSubscription(Subscription *sub);
142142
extern void DisableSubscription(Oid subid);
143-
extern Oid get_subscription_oid(const char *subname, bool missing_ok);
144-
extern char *get_subscription_name(Oid subid, bool missing_ok);
145143

146144
extern int CountDBSubscriptions(Oid dbid);
147145

Diff for: src/include/utils/lsyscache.h

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ extern Oid get_index_column_opclass(Oid index_oid, int attno);
198198
extern bool get_index_isreplident(Oid index_oid);
199199
extern bool get_index_isvalid(Oid index_oid);
200200
extern bool get_index_isclustered(Oid index_oid);
201+
extern Oid get_publication_oid(const char *pubname, bool missing_ok);
202+
extern char *get_publication_name(Oid pubid, bool missing_ok);
203+
extern Oid get_subscription_oid(const char *subname, bool missing_ok);
204+
extern char *get_subscription_name(Oid subid, bool missing_ok);
201205

202206
#define type_is_array(typid) (get_element_type(typid) != InvalidOid)
203207
/* type_is_array_domain accepts both plain arrays and domains over arrays */

0 commit comments

Comments
 (0)