PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_publication.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/tableam.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/objectaddress.h"
#include "catalog/partition.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_publication.h"
#include "catalog/pg_publication_namespace.h"
#include "catalog/pg_publication_rel.h"
#include "catalog/pg_type.h"
#include "commands/publicationcmds.h"
#include "funcapi.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for pg_publication.c:

Go to the source code of this file.

Data Structures

struct  published_rel
 

Macros

#define NUM_PUBLICATION_TABLES_ELEM   4
 

Functions

static void check_publication_add_relation (PublicationRelInfo *pri)
 
static void check_publication_add_schema (Oid schemaid)
 
static bool is_publishable_class (Oid relid, Form_pg_class reltuple)
 
bool is_publishable_relation (Relation rel)
 
static bool is_publishable_table (Oid tableoid)
 
Datum pg_relation_is_publishable (PG_FUNCTION_ARGS)
 
static bool is_ancestor_member_tableinfos (Oid ancestor, List *table_infos)
 
static void filter_partitions (List *table_infos)
 
bool is_schema_publication (Oid pubid)
 
bool is_table_publication (Oid pubid)
 
bool check_and_fetch_column_list (Publication *pub, Oid relid, MemoryContext mcxt, Bitmapset **cols)
 
ListGetPubPartitionOptionRelations (List *result, PublicationPartOpt pub_partopt, Oid relid)
 
Oid GetTopMostAncestorInPublication (Oid puboid, List *ancestors, int *ancestor_level)
 
static int2vectorattnumstoint2vector (Bitmapset *attrs)
 
ObjectAddress publication_add_relation (Oid pubid, PublicationRelInfo *pri, bool if_not_exists, AlterPublicationStmt *alter_stmt)
 
Bitmapsetpub_collist_validate (Relation targetrel, List *columns)
 
Bitmapsetpub_collist_to_bitmapset (Bitmapset *columns, Datum pubcols, MemoryContext mcxt)
 
Bitmapsetpub_form_cols_map (Relation relation, PublishGencolsType include_gencols_type)
 
ObjectAddress publication_add_schema (Oid pubid, Oid schemaid, bool if_not_exists)
 
static Listget_relation_publications (Oid relid, bool except_flag)
 
ListGetRelationIncludedPublications (Oid relid)
 
ListGetRelationExcludedPublications (Oid relid)
 
static Listget_publication_relations (Oid pubid, PublicationPartOpt pub_partopt, bool except_flag)
 
ListGetIncludedPublicationRelations (Oid pubid, PublicationPartOpt pub_partopt)
 
ListGetExcludedPublicationTables (Oid pubid, PublicationPartOpt pub_partopt)
 
ListGetAllTablesPublications (void)
 
ListGetAllPublicationRelations (Oid pubid, char relkind, bool pubviaroot)
 
ListGetPublicationSchemas (Oid pubid)
 
ListGetSchemaPublications (Oid schemaid)
 
ListGetSchemaPublicationRelations (Oid schemaid, PublicationPartOpt pub_partopt)
 
ListGetAllSchemaPublicationRelations (Oid pubid, PublicationPartOpt pub_partopt)
 
PublicationGetPublication (Oid pubid)
 
PublicationGetPublicationByName (const char *pubname, bool missing_ok)
 
static bool is_table_publishable_in_publication (Oid relid, Publication *pub)
 
static Datum pg_get_publication_tables (FunctionCallInfo fcinfo, ArrayType *pubnames, Oid target_relid, bool filter_by_relid, bool pub_missing_ok)
 
Datum pg_get_publication_tables_a (PG_FUNCTION_ARGS)
 
Datum pg_get_publication_tables_b (PG_FUNCTION_ARGS)
 
Datum pg_get_publication_sequences (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ NUM_PUBLICATION_TABLES_ELEM

#define NUM_PUBLICATION_TABLES_ELEM   4

Function Documentation

◆ attnumstoint2vector()

static int2vector * attnumstoint2vector ( Bitmapset attrs)
static

Definition at line 500 of file pg_publication.c.

501{
503 int n = bms_num_members(attrs);
504 int i = -1;
505 int j = 0;
506
508
509 while ((i = bms_next_member(attrs, i)) >= 0)
510 {
512
513 result->values[j++] = (int16) i;
514 }
515
516 return result;
517}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:744
#define Assert(condition)
Definition c.h:943
int16_t int16
Definition c.h:619
#define PG_INT16_MAX
Definition c.h:670
uint32 result
int2vector * buildint2vector(const int16 *int2s, int n)
Definition int.c:114
int j
Definition isn.c:78
int i
Definition isn.c:77
static int fb(int x)

References Assert, bms_next_member(), bms_num_members(), buildint2vector(), fb(), i, j, PG_INT16_MAX, and result.

Referenced by publication_add_relation().

◆ check_and_fetch_column_list()

bool check_and_fetch_column_list ( Publication pub,
Oid  relid,
MemoryContext  mcxt,
Bitmapset **  cols 
)

Definition at line 363 of file pg_publication.c.

365{
367 bool found = false;
368
369 if (pub->alltables)
370 return false;
371
373 ObjectIdGetDatum(relid),
374 ObjectIdGetDatum(pub->oid));
376 {
378 bool isnull;
379
380 /* Lookup the column list attribute. */
383
384 /* Was a column list found? */
385 if (!isnull)
386 {
387 /* Build the column list bitmap in the given memory context. */
388 if (cols)
389 *cols = pub_collist_to_bitmapset(*cols, cfdatum, mcxt);
390
391 found = true;
392 }
393
395 }
396
397 return found;
398}
#define HeapTupleIsValid(tuple)
Definition htup.h:78
Bitmapset * pub_collist_to_bitmapset(Bitmapset *columns, Datum pubcols, MemoryContext mcxt)
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:231
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596

References Publication::alltables, fb(), HeapTupleIsValid, ObjectIdGetDatum(), Publication::oid, pub_collist_to_bitmapset(), ReleaseSysCache(), SearchSysCache2(), and SysCacheGetAttr().

Referenced by check_and_init_gencol(), pgoutput_column_list_init(), and pub_contains_invalid_column().

◆ check_publication_add_relation()

static void check_publication_add_relation ( PublicationRelInfo pri)
static

Definition at line 56 of file pg_publication.c.

57{
58 Relation targetrel = pri->relation;
59 const char *relname;
60 const char *errormsg;
61
62 if (pri->except)
63 {
65 errormsg = gettext_noop("cannot specify relation \"%s\" in the publication EXCEPT clause");
66 }
67 else
68 {
70 errormsg = gettext_noop("cannot add relation \"%s\" to publication");
71 }
72
73 /* If in EXCEPT clause, must be root partitioned table */
74 if (pri->except && targetrel->rd_rel->relispartition)
77 errmsg(errormsg, relname),
78 errdetail("This operation is not supported for individual partitions.")));
79
80 /* Must be a regular or partitioned table */
85 errmsg(errormsg, relname),
87
88 /* Can't be system table */
92 errmsg(errormsg, relname),
93 errdetail("This operation is not supported for system tables.")));
94
95 /* UNLOGGED and TEMP relations cannot be part of publication. */
96 if (targetrel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
99 errmsg(errormsg, relname),
100 errdetail("This operation is not supported for temporary tables.")));
101 else if (targetrel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
104 errmsg(errormsg, relname),
105 errdetail("This operation is not supported for unlogged tables.")));
106}
#define gettext_noop(x)
Definition c.h:1285
bool IsCatalogRelation(Relation relation)
Definition catalog.c:104
int errcode(int sqlerrcode)
Definition elog.c:875
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
int errdetail_relkind_not_supported(char relkind)
Definition pg_class.c:24
NameData relname
Definition pg_class.h:40
#define RelationGetForm(relation)
Definition rel.h:510
#define RelationGetRelationName(relation)
Definition rel.h:550
char * RelationGetQualifiedRelationName(Relation rel)
Definition relcache.c:2142

References ereport, errcode(), errdetail(), errdetail_relkind_not_supported(), errmsg, ERROR, fb(), gettext_noop, IsCatalogRelation(), RelationGetForm, RelationGetQualifiedRelationName(), RelationGetRelationName, and relname.

Referenced by publication_add_relation().

◆ check_publication_add_schema()

static void check_publication_add_schema ( Oid  schemaid)
static

Definition at line 113 of file pg_publication.c.

114{
115 /* Can't be system namespace */
119 errmsg("cannot add schema \"%s\" to publication",
121 errdetail("This operation is not supported for system schemas.")));
122
123 /* Can't be temporary namespace */
127 errmsg("cannot add schema \"%s\" to publication",
129 errdetail("Temporary schemas cannot be replicated.")));
130}
bool IsToastNamespace(Oid namespaceId)
Definition catalog.c:261
bool IsCatalogNamespace(Oid namespaceId)
Definition catalog.c:243
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3599
bool isAnyTempNamespace(Oid namespaceId)
Definition namespace.c:3759

References ereport, errcode(), errdetail(), errmsg, ERROR, fb(), get_namespace_name(), isAnyTempNamespace(), IsCatalogNamespace(), and IsToastNamespace().

Referenced by publication_add_schema().

◆ filter_partitions()

static void filter_partitions ( List table_infos)
static

Definition at line 250 of file pg_publication.c.

251{
252 ListCell *lc;
253
254 foreach(lc, table_infos)
255 {
256 bool skip = false;
257 List *ancestors = NIL;
258 ListCell *lc2;
260
262 ancestors = get_partition_ancestors(table_info->relid);
263
264 foreach(lc2, ancestors)
265 {
267
269 {
270 skip = true;
271 break;
272 }
273 }
274
275 if (skip)
277 }
278}
bool get_rel_relispartition(Oid relid)
Definition lsyscache.c:2258
List * get_partition_ancestors(Oid relid)
Definition partition.c:134
static const struct exclude_list_item skip[]
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:423
#define lfirst_oid(lc)
Definition pg_list.h:174
static bool is_ancestor_member_tableinfos(Oid ancestor, List *table_infos)
unsigned int Oid
Definition pg_list.h:54

References fb(), foreach_delete_current, get_partition_ancestors(), get_rel_relispartition(), is_ancestor_member_tableinfos(), lfirst, lfirst_oid, NIL, and skip.

Referenced by pg_get_publication_tables().

◆ get_publication_relations()

static List * get_publication_relations ( Oid  pubid,
PublicationPartOpt  pub_partopt,
bool  except_flag 
)
static

Definition at line 925 of file pg_publication.c.

927{
928 List *result;
931 SysScanDesc scan;
933
934 /* Find all relations associated with the publication. */
936
940 ObjectIdGetDatum(pubid));
941
943 true, NULL, 1, &scankey);
944
945 result = NIL;
946 while (HeapTupleIsValid(tup = systable_getnext(scan)))
947 {
949
951
952 if (except_flag == pubrel->prexcept)
954 pubrel->prrelid);
955 }
956
957 systable_endscan(scan);
959
960 /* Now sort and de-duplicate the result list */
963
964 return result;
965}
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:612
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:523
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
static void * GETSTRUCT(const HeapTupleData *tuple)
void list_sort(List *list, list_sort_comparator cmp)
Definition list.c:1674
void list_deduplicate_oid(List *list)
Definition list.c:1495
int list_oid_cmp(const ListCell *p1, const ListCell *p2)
Definition list.c:1703
#define AccessShareLock
Definition lockdefs.h:36
List * GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt, Oid relid)
END_CATALOG_STRUCT typedef FormData_pg_publication_rel * Form_pg_publication_rel
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40

References AccessShareLock, BTEqualStrategyNumber, fb(), Form_pg_publication_rel, GetPubPartitionOptionRelations(), GETSTRUCT(), HeapTupleIsValid, list_deduplicate_oid(), list_oid_cmp(), list_sort(), NIL, ObjectIdGetDatum(), result, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by GetExcludedPublicationTables(), and GetIncludedPublicationRelations().

◆ get_relation_publications()

static List * get_relation_publications ( Oid  relid,
bool  except_flag 
)
static

Definition at line 876 of file pg_publication.c.

877{
878 List *result = NIL;
880
881 /* Find all publications associated with the relation. */
883 ObjectIdGetDatum(relid));
884 for (int i = 0; i < pubrellist->n_members; i++)
885 {
886 HeapTuple tup = &pubrellist->members[i]->tuple;
888 Oid pubid = pubrel->prpubid;
889
890 if (pubrel->prexcept == except_flag)
891 result = lappend_oid(result, pubid);
892 }
893
895
896 return result;
897}
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
#define ReleaseSysCacheList(x)
Definition syscache.h:134
#define SearchSysCacheList1(cacheId, key1)
Definition syscache.h:127

References fb(), Form_pg_publication_rel, GETSTRUCT(), i, lappend_oid(), NIL, ObjectIdGetDatum(), ReleaseSysCacheList, result, and SearchSysCacheList1.

Referenced by GetRelationExcludedPublications(), and GetRelationIncludedPublications().

◆ GetAllPublicationRelations()

List * GetAllPublicationRelations ( Oid  pubid,
char  relkind,
bool  pubviaroot 
)

Definition at line 1045 of file pg_publication.c.

1046{
1048 ScanKeyData key[1];
1049 TableScanDesc scan;
1050 HeapTuple tuple;
1051 List *result = NIL;
1052 List *exceptlist = NIL;
1053
1054 Assert(!(relkind == RELKIND_SEQUENCE && pubviaroot));
1055
1056 /* EXCEPT filtering applies only to relations, not sequences */
1057 if (relkind == RELKIND_RELATION)
1058 exceptlist = GetExcludedPublicationTables(pubid, pubviaroot ?
1061
1063
1064 ScanKeyInit(&key[0],
1067 CharGetDatum(relkind));
1068
1069 scan = table_beginscan_catalog(classRel, 1, key);
1070
1071 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1072 {
1074 Oid relid = relForm->oid;
1075
1076 if (is_publishable_class(relid, relForm) &&
1077 !(relForm->relispartition && pubviaroot) &&
1078 !list_member_oid(exceptlist, relid))
1079 result = lappend_oid(result, relid);
1080 }
1081
1082 table_endscan(scan);
1083
1084 if (pubviaroot)
1085 {
1086 ScanKeyInit(&key[0],
1090
1091 scan = table_beginscan_catalog(classRel, 1, key);
1092
1093 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1094 {
1096 Oid relid = relForm->oid;
1097
1098 if (is_publishable_class(relid, relForm) &&
1099 !relForm->relispartition &&
1100 !list_member_oid(exceptlist, relid))
1101 result = lappend_oid(result, relid);
1102 }
1103
1104 table_endscan(scan);
1105 }
1106
1108 return result;
1109}
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition heapam.c:1435
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
static bool is_publishable_class(Oid relid, Form_pg_class reltuple)
List * GetExcludedPublicationTables(Oid pubid, PublicationPartOpt pub_partopt)
@ PUBLICATION_PART_LEAF
@ PUBLICATION_PART_ROOT
static Datum CharGetDatum(char X)
Definition postgres.h:132
@ ForwardScanDirection
Definition sdir.h:28
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKeyData *key)
Definition tableam.c:113
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1061

References AccessShareLock, Assert, BTEqualStrategyNumber, CharGetDatum(), fb(), ForwardScanDirection, GetExcludedPublicationTables(), GETSTRUCT(), heap_getnext(), is_publishable_class(), lappend_oid(), list_member_oid(), NIL, PUBLICATION_PART_LEAF, PUBLICATION_PART_ROOT, result, ScanKeyInit(), table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by pg_get_publication_sequences(), and pg_get_publication_tables().

◆ GetAllSchemaPublicationRelations()

List * GetAllSchemaPublicationRelations ( Oid  pubid,
PublicationPartOpt  pub_partopt 
)

Definition at line 1238 of file pg_publication.c.

1239{
1240 List *result = NIL;
1242 ListCell *cell;
1243
1244 foreach(cell, pubschemalist)
1245 {
1246 Oid schemaid = lfirst_oid(cell);
1247 List *schemaRels = NIL;
1248
1251 }
1252
1253 return result;
1254}
List * list_concat(List *list1, const List *list2)
Definition list.c:561
List * GetPublicationSchemas(Oid pubid)
List * GetSchemaPublicationRelations(Oid schemaid, PublicationPartOpt pub_partopt)

References fb(), GetPublicationSchemas(), GetSchemaPublicationRelations(), lfirst_oid, list_concat(), NIL, and result.

Referenced by AlterPublicationOptions(), InvalidatePubRelSyncCache(), and pg_get_publication_tables().

◆ GetAllTablesPublications()

List * GetAllTablesPublications ( void  )

Definition at line 999 of file pg_publication.c.

1000{
1001 List *result;
1002 Relation rel;
1004 SysScanDesc scan;
1005 HeapTuple tup;
1006
1007 /* Find all publications that are marked as for all tables. */
1009
1013 BoolGetDatum(true));
1014
1015 scan = systable_beginscan(rel, InvalidOid, false,
1016 NULL, 1, &scankey);
1017
1018 result = NIL;
1019 while (HeapTupleIsValid(tup = systable_getnext(scan)))
1020 {
1021 Oid oid = ((Form_pg_publication) GETSTRUCT(tup))->oid;
1022
1023 result = lappend_oid(result, oid);
1024 }
1025
1026 systable_endscan(scan);
1028
1029 return result;
1030}
END_CATALOG_STRUCT typedef FormData_pg_publication * Form_pg_publication
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
#define InvalidOid

References AccessShareLock, BoolGetDatum(), BTEqualStrategyNumber, fb(), Form_pg_publication, GETSTRUCT(), HeapTupleIsValid, InvalidOid, lappend_oid(), NIL, result, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by RelationBuildPublicationDesc().

◆ GetExcludedPublicationTables()

List * GetExcludedPublicationTables ( Oid  pubid,
PublicationPartOpt  pub_partopt 
)

Definition at line 988 of file pg_publication.c.

989{
990 Assert(GetPublication(pubid)->alltables);
991
992 return get_publication_relations(pubid, pub_partopt, true);
993}
static List * get_publication_relations(Oid pubid, PublicationPartOpt pub_partopt, bool except_flag)
Publication * GetPublication(Oid pubid)

References Assert, fb(), get_publication_relations(), and GetPublication().

Referenced by AlterPublicationTables(), and GetAllPublicationRelations().

◆ GetIncludedPublicationRelations()

List * GetIncludedPublicationRelations ( Oid  pubid,
PublicationPartOpt  pub_partopt 
)

◆ GetPublication()

Publication * GetPublication ( Oid  pubid)

Definition at line 1262 of file pg_publication.c.

1263{
1264 HeapTuple tup;
1265 Publication *pub;
1267
1269 if (!HeapTupleIsValid(tup))
1270 elog(ERROR, "cache lookup failed for publication %u", pubid);
1271
1273
1275 pub->oid = pubid;
1276 pub->name = pstrdup(NameStr(pubform->pubname));
1277 pub->alltables = pubform->puballtables;
1278 pub->allsequences = pubform->puballsequences;
1279 pub->pubactions.pubinsert = pubform->pubinsert;
1280 pub->pubactions.pubupdate = pubform->pubupdate;
1281 pub->pubactions.pubdelete = pubform->pubdelete;
1282 pub->pubactions.pubtruncate = pubform->pubtruncate;
1283 pub->pubviaroot = pubform->pubviaroot;
1284 pub->pubgencols_type = pubform->pubgencols;
1285
1287
1288 return pub;
1289}
#define NameStr(name)
Definition c.h:835
#define elog(elevel,...)
Definition elog.h:228
#define palloc_object(type)
Definition fe_memutils.h:89
char * pstrdup(const char *in)
Definition mcxt.c:1910
PublishGencolsType pubgencols_type
PublicationActions pubactions
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221

References Publication::allsequences, Publication::alltables, elog, ERROR, fb(), Form_pg_publication, GETSTRUCT(), HeapTupleIsValid, Publication::name, NameStr, ObjectIdGetDatum(), Publication::oid, palloc_object, pstrdup(), Publication::pubactions, PublicationActions::pubdelete, Publication::pubgencols_type, PublicationActions::pubinsert, PublicationActions::pubtruncate, PublicationActions::pubupdate, Publication::pubviaroot, ReleaseSysCache(), and SearchSysCache1().

Referenced by GetExcludedPublicationTables(), GetIncludedPublicationRelations(), GetPublicationByName(), pg_get_publication_tables(), pub_contains_invalid_column(), publication_add_relation(), and publication_add_schema().

◆ GetPublicationByName()

Publication * GetPublicationByName ( const char pubname,
bool  missing_ok 
)

Definition at line 1295 of file pg_publication.c.

1296{
1297 Oid oid;
1298
1299 oid = get_publication_oid(pubname, missing_ok);
1300
1301 return OidIsValid(oid) ? GetPublication(oid) : NULL;
1302}
#define OidIsValid(objectId)
Definition c.h:858
Oid get_publication_oid(const char *pubname, bool missing_ok)
Definition lsyscache.c:3903

References fb(), get_publication_oid(), GetPublication(), and OidIsValid.

Referenced by get_object_address_publication_rel(), get_object_address_publication_schema(), LoadPublications(), pg_get_publication_sequences(), and pg_get_publication_tables().

◆ GetPublicationSchemas()

List * GetPublicationSchemas ( Oid  pubid)

◆ GetPubPartitionOptionRelations()

List * GetPubPartitionOptionRelations ( List result,
PublicationPartOpt  pub_partopt,
Oid  relid 
)

Definition at line 405 of file pg_publication.c.

407{
410 {
412 NULL);
413
417 {
418 ListCell *lc;
419
420 foreach(lc, all_parts)
421 {
423
426 }
427 }
428 else
429 Assert(false);
430 }
431 else
432 result = lappend_oid(result, relid);
433
434 return result;
435}
#define NoLock
Definition lockdefs.h:34
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2234
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
@ PUBLICATION_PART_ALL

References Assert, fb(), find_all_inheritors(), get_rel_relkind(), lappend_oid(), lfirst_oid, list_concat(), NoLock, PUBLICATION_PART_ALL, PUBLICATION_PART_LEAF, PUBLICATION_PART_ROOT, and result.

Referenced by AlterPublicationOptions(), get_publication_relations(), GetSchemaPublicationRelations(), publication_add_relation(), and RemovePublicationRelById().

◆ GetRelationExcludedPublications()

List * GetRelationExcludedPublications ( Oid  relid)

Definition at line 912 of file pg_publication.c.

913{
914 return get_relation_publications(relid, true);
915}
static List * get_relation_publications(Oid relid, bool except_flag)

References get_relation_publications().

Referenced by ATExecAttachPartition(), get_rel_sync_entry(), and RelationBuildPublicationDesc().

◆ GetRelationIncludedPublications()

List * GetRelationIncludedPublications ( Oid  relid)

◆ GetSchemaPublicationRelations()

List * GetSchemaPublicationRelations ( Oid  schemaid,
PublicationPartOpt  pub_partopt 
)

Definition at line 1181 of file pg_publication.c.

1182{
1184 ScanKeyData key[1];
1185 TableScanDesc scan;
1186 HeapTuple tuple;
1187 List *result = NIL;
1188
1190
1192
1193 ScanKeyInit(&key[0],
1197
1198 /* get all the relations present in the specified schema */
1199 scan = table_beginscan_catalog(classRel, 1, key);
1200 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1201 {
1203 Oid relid = relForm->oid;
1204 char relkind;
1205
1206 if (!is_publishable_class(relid, relForm))
1207 continue;
1208
1209 relkind = get_rel_relkind(relid);
1210 if (relkind == RELKIND_RELATION)
1211 result = lappend_oid(result, relid);
1212 else if (relkind == RELKIND_PARTITIONED_TABLE)
1213 {
1215
1216 /*
1217 * It is quite possible that some of the partitions are in a
1218 * different schema than the parent table, so we need to get such
1219 * partitions separately.
1220 */
1223 relForm->oid);
1225 }
1226 }
1227
1228 table_endscan(scan);
1230 return result;
1231}
List * list_concat_unique_oid(List *list1, const List *list2)
Definition list.c:1469

References AccessShareLock, Assert, BTEqualStrategyNumber, fb(), ForwardScanDirection, get_rel_relkind(), GetPubPartitionOptionRelations(), GETSTRUCT(), heap_getnext(), is_publishable_class(), lappend_oid(), list_concat_unique_oid(), NIL, ObjectIdGetDatum(), OidIsValid, result, ScanKeyInit(), table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by GetAllSchemaPublicationRelations(), publication_add_schema(), and RemovePublicationSchemaById().

◆ GetSchemaPublications()

List * GetSchemaPublications ( Oid  schemaid)

Definition at line 1155 of file pg_publication.c.

1156{
1157 List *result = NIL;
1159 int i;
1160
1161 /* Find all publications associated with the schema */
1164 for (i = 0; i < pubschlist->n_members; i++)
1165 {
1166 HeapTuple tup = &pubschlist->members[i]->tuple;
1168
1169 result = lappend_oid(result, pubid);
1170 }
1171
1173
1174 return result;
1175}

References fb(), Form_pg_publication_namespace, GETSTRUCT(), i, lappend_oid(), NIL, ObjectIdGetDatum(), ReleaseSysCacheList, result, and SearchSysCacheList1.

Referenced by get_rel_sync_entry(), GetTopMostAncestorInPublication(), and RelationBuildPublicationDesc().

◆ GetTopMostAncestorInPublication()

Oid GetTopMostAncestorInPublication ( Oid  puboid,
List ancestors,
int ancestor_level 
)

Definition at line 449 of file pg_publication.c.

450{
451 ListCell *lc;
453 int level = 0;
454
455 /*
456 * Find the "topmost" ancestor that is in this publication.
457 */
458 foreach(lc, ancestors)
459 {
463
464 level++;
465
467 {
469
470 if (ancestor_level)
471 *ancestor_level = level;
472 }
473 else
474 {
477 {
479
480 if (ancestor_level)
481 *ancestor_level = level;
482 }
483 }
484
487 }
488
489 return topmost_relid;
490}
void list_free(List *list)
Definition list.c:1546
Oid get_rel_namespace(Oid relid)
Definition lsyscache.c:2183
List * GetRelationIncludedPublications(Oid relid)
List * GetSchemaPublications(Oid schemaid)

References fb(), get_rel_namespace(), GetRelationIncludedPublications(), GetSchemaPublications(), InvalidOid, lfirst_oid, list_free(), list_member_oid(), and NIL.

Referenced by get_rel_sync_entry(), is_table_publishable_in_publication(), pub_contains_invalid_column(), and pub_rf_contains_invalid_column().

◆ is_ancestor_member_tableinfos()

static bool is_ancestor_member_tableinfos ( Oid  ancestor,
List table_infos 
)
static

Definition at line 231 of file pg_publication.c.

232{
233 ListCell *lc;
234
235 foreach(lc, table_infos)
236 {
237 Oid relid = ((published_rel *) lfirst(lc))->relid;
238
239 if (relid == ancestor)
240 return true;
241 }
242
243 return false;
244}

References fb(), and lfirst.

Referenced by filter_partitions().

◆ is_publishable_class()

static bool is_publishable_class ( Oid  relid,
Form_pg_class  reltuple 
)
static

Definition at line 154 of file pg_publication.c.

155{
156 return (reltuple->relkind == RELKIND_RELATION ||
158 reltuple->relkind == RELKIND_SEQUENCE) &&
159 !IsCatalogRelationOid(relid) &&
160 reltuple->relpersistence == RELPERSISTENCE_PERMANENT &&
161 relid >= FirstNormalObjectId;
162}
bool IsCatalogRelationOid(Oid relid)
Definition catalog.c:121
#define FirstNormalObjectId
Definition transam.h:197

References fb(), FirstNormalObjectId, and IsCatalogRelationOid().

Referenced by GetAllPublicationRelations(), GetSchemaPublicationRelations(), is_publishable_relation(), is_publishable_table(), and pg_relation_is_publishable().

◆ is_publishable_relation()

bool is_publishable_relation ( Relation  rel)

Definition at line 168 of file pg_publication.c.

169{
171}
#define RelationGetRelid(relation)
Definition rel.h:516
Form_pg_class rd_rel
Definition rel.h:111

References is_publishable_class(), RelationData::rd_rel, and RelationGetRelid.

Referenced by pgoutput_change(), pgoutput_truncate(), and RelationBuildPublicationDesc().

◆ is_publishable_table()

static bool is_publishable_table ( Oid  tableoid)
static

Definition at line 178 of file pg_publication.c.

179{
180 HeapTuple tuple;
182
183 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(tableoid));
184 if (!HeapTupleIsValid(tuple))
185 return false;
186
188
189 /*
190 * is_publishable_class() includes sequences, so we need to explicitly
191 * check the relkind to filter them out here.
192 */
193 if (relform->relkind != RELKIND_SEQUENCE &&
194 is_publishable_class(tableoid, relform))
195 {
196 ReleaseSysCache(tuple);
197 return true;
198 }
199
200 ReleaseSysCache(tuple);
201 return false;
202}

References fb(), GETSTRUCT(), HeapTupleIsValid, is_publishable_class(), ObjectIdGetDatum(), ReleaseSysCache(), and SearchSysCache1().

Referenced by pg_get_publication_tables().

◆ is_schema_publication()

◆ is_table_publication()

bool is_table_publication ( Oid  pubid)

Definition at line 316 of file pg_publication.c.

317{
320 SysScanDesc scan;
322 bool result = false;
323
328 ObjectIdGetDatum(pubid));
329
332 true, NULL, 1, &scankey);
333 tup = systable_getnext(scan);
335 {
337
339
340 /*
341 * For any publication, pg_publication_rel contains either only EXCEPT
342 * entries or only explicitly included tables. Therefore, examining
343 * the first tuple is sufficient to determine table inclusion.
344 */
345 result = !pubrel->prexcept;
346 }
347
348 systable_endscan(scan);
350
351 return result;
352}

References AccessShareLock, BTEqualStrategyNumber, fb(), Form_pg_publication_rel, GETSTRUCT(), HeapTupleIsValid, ObjectIdGetDatum(), result, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by CheckAlterPublication().

◆ is_table_publishable_in_publication()

static bool is_table_publishable_in_publication ( Oid  relid,
Publication pub 
)
static

Definition at line 1323 of file pg_publication.c.

1324{
1325 bool relispartition;
1326 List *ancestors = NIL;
1327
1328 /*
1329 * For non-pubviaroot publications, a partitioned table is never the
1330 * effective published OID; only its leaf partitions can be.
1331 */
1333 return false;
1334
1336
1337 if (relispartition)
1338 ancestors = get_partition_ancestors(relid);
1339
1340 if (pub->alltables)
1341 {
1342 /*
1343 * ALL TABLES with pubviaroot includes only regular tables or top-most
1344 * partitioned tables -- never child partitions.
1345 */
1346 if (pub->pubviaroot && relispartition)
1347 return false;
1348
1349 /*
1350 * For ALL TABLES publications, the table is published unless it
1351 * appears in the EXCEPT clause. Only the top-most can appear in the
1352 * EXCEPT clause, so exclusion must be evaluated at the top-most
1353 * ancestor if it has. These publications store only EXCEPT'ed tables
1354 * in pg_publication_rel, so checking existence is sufficient.
1355 *
1356 * Note that this existence check below would incorrectly return true
1357 * (published) for partitions when pubviaroot is enabled; however,
1358 * that case is already caught and returned false by the above check.
1359 */
1361 ObjectIdGetDatum(ancestors
1362 ? llast_oid(ancestors) : relid),
1363 ObjectIdGetDatum(pub->oid));
1364 }
1365
1366 /*
1367 * Non-ALL-TABLE publication cases.
1368 *
1369 * A table is published if it (or a containing schema) was explicitly
1370 * added, or if it is a partition whose ancestor was added.
1371 */
1372
1373 /*
1374 * If an ancestor is published, the partition's status depends on
1375 * publish_via_partition_root value.
1376 *
1377 * If it's true, the ancestor's relation OID is the effective published
1378 * OID, so the partition itself should be excluded (return false).
1379 *
1380 * If it's false, the partition is covered by its ancestor's presence in
1381 * the publication, it should be included (return true).
1382 */
1383 if (relispartition &&
1385 return !pub->pubviaroot;
1386
1387 /*
1388 * Check whether the table is explicitly published via pg_publication_rel
1389 * or pg_publication_namespace.
1390 */
1392 ObjectIdGetDatum(relid),
1393 ObjectIdGetDatum(pub->oid)) ||
1396 ObjectIdGetDatum(pub->oid)));
1397}
#define llast_oid(l)
Definition pg_list.h:200
Oid GetTopMostAncestorInPublication(Oid puboid, List *ancestors, int *ancestor_level)
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition syscache.h:102

References Publication::alltables, fb(), get_partition_ancestors(), get_rel_namespace(), get_rel_relispartition(), get_rel_relkind(), GetTopMostAncestorInPublication(), llast_oid, NIL, ObjectIdGetDatum(), Publication::oid, OidIsValid, Publication::pubviaroot, and SearchSysCacheExists2.

Referenced by pg_get_publication_tables().

◆ pg_get_publication_sequences()

Datum pg_get_publication_sequences ( PG_FUNCTION_ARGS  )

Definition at line 1686 of file pg_publication.c.

1687{
1689 List *sequences = NIL;
1690
1691 /* stuff done only on the first call of the function */
1692 if (SRF_IS_FIRSTCALL())
1693 {
1694 char *pubname = text_to_cstring(PG_GETARG_TEXT_PP(0));
1695 Publication *publication;
1696 MemoryContext oldcontext;
1697
1698 /* create a function context for cross-call persistence */
1700
1701 /* switch to memory context appropriate for multiple function calls */
1702 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1703
1704 publication = GetPublicationByName(pubname, false);
1705
1706 if (publication->allsequences)
1709 false);
1710
1711 funcctx->user_fctx = sequences;
1712
1713 MemoryContextSwitchTo(oldcontext);
1714 }
1715
1716 /* stuff done on every call of the function */
1718 sequences = (List *) funcctx->user_fctx;
1719
1720 if (funcctx->call_cntr < list_length(sequences))
1721 {
1722 Oid relid = list_nth_oid(sequences, funcctx->call_cntr);
1723
1725 }
1726
1728}
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define SRF_IS_FIRSTCALL()
Definition funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition funcapi.h:328
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
static SequenceItem * sequences
Definition pg_dump.c:214
static int list_length(const List *l)
Definition pg_list.h:152
static Oid list_nth_oid(const List *list, int n)
Definition pg_list.h:353
Publication * GetPublicationByName(const char *pubname, bool missing_ok)
List * GetAllPublicationRelations(Oid pubid, char relkind, bool pubviaroot)
char * text_to_cstring(const text *t)
Definition varlena.c:217

References Publication::allsequences, fb(), GetAllPublicationRelations(), GetPublicationByName(), list_length(), list_nth_oid(), MemoryContextSwitchTo(), NIL, ObjectIdGetDatum(), Publication::oid, PG_GETARG_TEXT_PP, sequences, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, and text_to_cstring().

◆ pg_get_publication_tables()

static Datum pg_get_publication_tables ( FunctionCallInfo  fcinfo,
ArrayType pubnames,
Oid  target_relid,
bool  filter_by_relid,
bool  pub_missing_ok 
)
static

Definition at line 1412 of file pg_publication.c.

1415{
1416#define NUM_PUBLICATION_TABLES_ELEM 4
1418 List *table_infos = NIL;
1419
1420 /* stuff done only on the first call of the function */
1421 if (SRF_IS_FIRSTCALL())
1422 {
1423 TupleDesc tupdesc;
1424 MemoryContext oldcontext;
1425 Datum *elems;
1426 int nelems,
1427 i;
1428 bool viaroot = false;
1429
1430 /* create a function context for cross-call persistence */
1432
1433 /*
1434 * Preliminary check if the specified table can be published in the
1435 * first place. If not, we can return early without checking the given
1436 * publications and the table.
1437 */
1440
1441 /* switch to memory context appropriate for multiple function calls */
1442 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1443
1444 /*
1445 * Deconstruct the parameter into elements where each element is a
1446 * publication name.
1447 */
1448 deconstruct_array_builtin(pubnames, TEXTOID, &elems, NULL, &nelems);
1449
1450 /* Get Oids of tables from each publication. */
1451 for (i = 0; i < nelems; i++)
1452 {
1455 ListCell *lc;
1456
1459
1460 if (pub_elem == NULL)
1461 continue;
1462
1463 if (filter_by_relid)
1464 {
1465 /* Check if the given table is published for the publication */
1467 {
1469 }
1470 }
1471 else
1472 {
1473 /*
1474 * Publications support partitioned tables. If
1475 * publish_via_partition_root is false, all changes are
1476 * replicated using leaf partition identity and schema, so we
1477 * only need those. Otherwise, get the partitioned table
1478 * itself.
1479 */
1480 if (pub_elem->alltables)
1483 pub_elem->pubviaroot);
1484 else
1485 {
1486 List *relids,
1487 *schemarelids;
1488
1490 pub_elem->pubviaroot ?
1494 pub_elem->pubviaroot ?
1498 }
1499 }
1500
1501 /*
1502 * Record the published table and the corresponding publication so
1503 * that we can get row filters and column lists later.
1504 *
1505 * When a table is published by multiple publications, to obtain
1506 * all row filters and column lists, the structure related to this
1507 * table will be recorded multiple times.
1508 */
1509 foreach(lc, pub_elem_tables)
1510 {
1512
1513 table_info->relid = lfirst_oid(lc);
1514 table_info->pubid = pub_elem->oid;
1516 }
1517
1518 /* At least one publication is using publish_via_partition_root. */
1519 if (pub_elem->pubviaroot)
1520 viaroot = true;
1521 }
1522
1523 /*
1524 * If the publication publishes partition changes via their respective
1525 * root partitioned tables, we must exclude partitions in favor of
1526 * including the root partitioned tables. Otherwise, the function
1527 * could return both the child and parent tables which could cause
1528 * data of the child table to be double-published on the subscriber
1529 * side.
1530 */
1531 if (viaroot)
1533
1534 /* Construct a tuple descriptor for the result rows. */
1536 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pubid",
1537 OIDOID, -1, 0);
1538 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "relid",
1539 OIDOID, -1, 0);
1540 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "attrs",
1541 INT2VECTOROID, -1, 0);
1542 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "qual",
1543 PG_NODE_TREEOID, -1, 0);
1544
1545 TupleDescFinalize(tupdesc);
1546 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
1547 funcctx->user_fctx = table_infos;
1548
1549 MemoryContextSwitchTo(oldcontext);
1550 }
1551
1552 /* stuff done on every call of the function */
1554 table_infos = (List *) funcctx->user_fctx;
1555
1556 if (funcctx->call_cntr < list_length(table_infos))
1557 {
1560 Publication *pub;
1562 Oid relid = table_info->relid;
1565 bool nulls[NUM_PUBLICATION_TABLES_ELEM] = {0};
1566
1567 /*
1568 * Form tuple with appropriate data.
1569 */
1570
1571 pub = GetPublication(table_info->pubid);
1572
1573 values[0] = ObjectIdGetDatum(pub->oid);
1574 values[1] = ObjectIdGetDatum(relid);
1575
1576 /*
1577 * We don't consider row filters or column lists for FOR ALL TABLES or
1578 * FOR TABLES IN SCHEMA publications.
1579 */
1580 if (!pub->alltables &&
1583 ObjectIdGetDatum(pub->oid)))
1585 ObjectIdGetDatum(relid),
1586 ObjectIdGetDatum(pub->oid));
1587
1589 {
1590 /* Lookup the column list attribute. */
1593 &(nulls[2]));
1594
1595 /* Null indicates no filter. */
1598 &(nulls[3]));
1599 }
1600 else
1601 {
1602 nulls[2] = true;
1603 nulls[3] = true;
1604 }
1605
1606 /* Show all columns when the column list is not specified. */
1607 if (nulls[2])
1608 {
1609 Relation rel = table_open(relid, AccessShareLock);
1610 int nattnums = 0;
1611 int16 *attnums;
1612 TupleDesc desc = RelationGetDescr(rel);
1613 int i;
1614
1615 attnums = palloc_array(int16, desc->natts);
1616
1617 for (i = 0; i < desc->natts; i++)
1618 {
1619 Form_pg_attribute att = TupleDescAttr(desc, i);
1620
1621 if (att->attisdropped)
1622 continue;
1623
1624 if (att->attgenerated)
1625 {
1626 /* We only support replication of STORED generated cols. */
1627 if (att->attgenerated != ATTRIBUTE_GENERATED_STORED)
1628 continue;
1629
1630 /*
1631 * User hasn't requested to replicate STORED generated
1632 * cols.
1633 */
1635 continue;
1636 }
1637
1638 attnums[nattnums++] = att->attnum;
1639 }
1640
1641 if (nattnums > 0)
1642 {
1643 values[2] = PointerGetDatum(buildint2vector(attnums, nattnums));
1644 nulls[2] = false;
1645 }
1646
1648 }
1649
1650 rettuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
1651
1653 }
1654
1656}
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
int16 AttrNumber
Definition attnum.h:21
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define TextDatumGetCString(d)
Definition builtins.h:99
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
#define palloc_array(type, count)
Definition fe_memutils.h:91
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1025
List * lappend(List *list, void *datum)
Definition list.c:339
FormData_pg_attribute * Form_pg_attribute
#define list_make1_oid(x1)
Definition pg_list.h:274
static void * list_nth(const List *list, int n)
Definition pg_list.h:331
List * GetAllSchemaPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt)
static void filter_partitions(List *table_infos)
List * GetIncludedPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt)
static bool is_publishable_table(Oid tableoid)
#define NUM_PUBLICATION_TABLES_ELEM
static bool is_table_publishable_in_publication(Oid relid, Publication *pub)
#define PointerGetDatum(X)
Definition postgres.h:354
#define RelationGetDescr(relation)
Definition rel.h:542
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition syscache.h:93
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:511
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:900
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

References AccessShareLock, Publication::alltables, BlessTupleDesc(), buildint2vector(), CreateTemplateTupleDesc(), deconstruct_array_builtin(), fb(), filter_partitions(), get_rel_namespace(), GetAllPublicationRelations(), GetAllSchemaPublicationRelations(), GetIncludedPublicationRelations(), GetPublication(), GetPublicationByName(), heap_form_tuple(), HeapTupleGetDatum(), HeapTupleIsValid, i, is_publishable_table(), is_table_publishable_in_publication(), lappend(), lfirst_oid, list_concat_unique_oid(), list_length(), list_make1_oid, list_nth(), MemoryContextSwitchTo(), TupleDescData::natts, NIL, NUM_PUBLICATION_TABLES_ELEM, ObjectIdGetDatum(), Publication::oid, palloc_array, palloc_object, PointerGetDatum, Publication::pubgencols_type, PUBLICATION_PART_LEAF, PUBLICATION_PART_ROOT, RelationGetDescr, published_rel::relid, SearchSysCacheCopy2, SearchSysCacheExists2, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, SysCacheGetAttr(), table_close(), table_open(), TextDatumGetCString, TupleDescAttr(), TupleDescFinalize(), TupleDescInitEntry(), and values.

Referenced by pg_get_publication_tables_a(), and pg_get_publication_tables_b().

◆ pg_get_publication_tables_a()

Datum pg_get_publication_tables_a ( PG_FUNCTION_ARGS  )

Definition at line 1659 of file pg_publication.c.

1660{
1661 /*
1662 * Get information for all tables in the given publications.
1663 * filter_by_relid is false so all tables are returned; pub_missing_ok is
1664 * false for backward compatibility.
1665 */
1667 InvalidOid, false, false);
1668}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
static Datum pg_get_publication_tables(FunctionCallInfo fcinfo, ArrayType *pubnames, Oid target_relid, bool filter_by_relid, bool pub_missing_ok)

References InvalidOid, pg_get_publication_tables(), and PG_GETARG_ARRAYTYPE_P.

◆ pg_get_publication_tables_b()

Datum pg_get_publication_tables_b ( PG_FUNCTION_ARGS  )

Definition at line 1671 of file pg_publication.c.

1672{
1673 /*
1674 * Get information for the specified table in the given publications. The
1675 * SQL-level function is declared STRICT, so target_relid is guaranteed to
1676 * be non-NULL here.
1677 */
1679 PG_GETARG_OID(1), true, true);
1680}
#define PG_GETARG_OID(n)
Definition fmgr.h:275

References pg_get_publication_tables(), PG_GETARG_ARRAYTYPE_P, and PG_GETARG_OID.

◆ pg_relation_is_publishable()

Datum pg_relation_is_publishable ( PG_FUNCTION_ARGS  )

Definition at line 212 of file pg_publication.c.

213{
214 Oid relid = PG_GETARG_OID(0);
215 HeapTuple tuple;
216 bool result;
217
218 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
219 if (!HeapTupleIsValid(tuple))
222 ReleaseSysCache(tuple);
224}
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360

References fb(), GETSTRUCT(), HeapTupleIsValid, is_publishable_class(), ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_BOOL, PG_RETURN_NULL, ReleaseSysCache(), result, and SearchSysCache1().

◆ pub_collist_to_bitmapset()

Bitmapset * pub_collist_to_bitmapset ( Bitmapset columns,
Datum  pubcols,
MemoryContext  mcxt 
)

Definition at line 724 of file pg_publication.c.

725{
726 Bitmapset *result = columns;
727 ArrayType *arr;
728 int nelems;
729 int16 *elems;
731
733 nelems = ARR_DIMS(arr)[0];
734 elems = (int16 *) ARR_DATA_PTR(arr);
735
736 /* If a memory context was specified, switch to it. */
737 if (mcxt)
739
740 for (int i = 0; i < nelems; i++)
741 result = bms_add_member(result, elems[i]);
742
743 if (mcxt)
745
746 return result;
747}
#define ARR_DATA_PTR(a)
Definition array.h:322
#define DatumGetArrayTypeP(X)
Definition array.h:261
#define ARR_DIMS(a)
Definition array.h:294
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799

References ARR_DATA_PTR, ARR_DIMS, bms_add_member(), DatumGetArrayTypeP, fb(), i, MemoryContextSwitchTo(), and result.

Referenced by AlterPublicationTables(), and check_and_fetch_column_list().

◆ pub_collist_validate()

Bitmapset * pub_collist_validate ( Relation  targetrel,
List columns 
)

Definition at line 675 of file pg_publication.c.

676{
677 Bitmapset *set = NULL;
678 ListCell *lc;
680
681 foreach(lc, columns)
682 {
683 char *colname = strVal(lfirst(lc));
685
689 errmsg("column \"%s\" of relation \"%s\" does not exist",
691
695 errmsg("cannot use system column \"%s\" in publication column list",
696 colname));
697
698 if (TupleDescAttr(tupdesc, attnum - 1)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
701 errmsg("cannot use virtual generated column \"%s\" in publication column list",
702 colname));
703
704 if (bms_is_member(attnum, set))
707 errmsg("duplicate column \"%s\" in publication column list",
708 colname));
709
710 set = bms_add_member(set, attnum);
711 }
712
713 return set;
714}
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition attnum.h:41
#define InvalidAttrNumber
Definition attnum.h:23
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:1015
int16 attnum
#define ERRCODE_DUPLICATE_OBJECT
Definition streamutil.c:30
#define strVal(v)
Definition value.h:82

References attnum, AttrNumberIsForUserDefinedAttr, bms_add_member(), bms_is_member(), ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg, ERROR, fb(), get_attnum(), InvalidAttrNumber, lfirst, RelationGetDescr, RelationGetRelationName, RelationGetRelid, strVal, and TupleDescAttr().

Referenced by AlterPublicationTables(), and publication_add_relation().

◆ pub_form_cols_map()

Bitmapset * pub_form_cols_map ( Relation  relation,
PublishGencolsType  include_gencols_type 
)

Definition at line 756 of file pg_publication.c.

757{
759 TupleDesc desc = RelationGetDescr(relation);
760
761 for (int i = 0; i < desc->natts; i++)
762 {
763 Form_pg_attribute att = TupleDescAttr(desc, i);
764
765 if (att->attisdropped)
766 continue;
767
768 if (att->attgenerated)
769 {
770 /* We only support replication of STORED generated cols. */
771 if (att->attgenerated != ATTRIBUTE_GENERATED_STORED)
772 continue;
773
774 /* User hasn't requested to replicate STORED generated cols. */
775 if (include_gencols_type != PUBLISH_GENCOLS_STORED)
776 continue;
777 }
778
779 result = bms_add_member(result, att->attnum);
780 }
781
782 return result;
783}

References bms_add_member(), fb(), i, TupleDescData::natts, RelationGetDescr, result, and TupleDescAttr().

Referenced by pgoutput_column_list_init().

◆ publication_add_relation()

ObjectAddress publication_add_relation ( Oid  pubid,
PublicationRelInfo pri,
bool  if_not_exists,
AlterPublicationStmt alter_stmt 
)

Definition at line 523 of file pg_publication.c.

525{
526 Relation rel;
529 bool nulls[Natts_pg_publication_rel];
530 Relation targetrel = pri->relation;
533 Bitmapset *attnums;
534 Publication *pub = GetPublication(pubid);
537 List *relids = NIL;
538 int i;
540
542
543 /*
544 * Check for duplicates. Note that this does not really prevent
545 * duplicates, it's here just to provide nicer error message in common
546 * case. The real protection is the unique key on the catalog.
547 */
549 ObjectIdGetDatum(pubid)))
550 {
552
553 if (if_not_exists)
555
558 errmsg("relation \"%s\" is already member of publication \"%s\"",
560 }
561
563
564 /* Validate and translate column names into a Bitmapset of attnums. */
565 attnums = pub_collist_validate(pri->relation, pri->columns);
566
567 /* Form a tuple. */
568 memset(values, 0, sizeof(values));
569 memset(nulls, false, sizeof(nulls));
570
575 ObjectIdGetDatum(pubid);
577 ObjectIdGetDatum(relid);
579 BoolGetDatum(pri->except);
580
581 /* Add qualifications, if available */
582 if (pri->whereClause != NULL)
584 else
585 nulls[Anum_pg_publication_rel_prqual - 1] = true;
586
587 /* Add column list, if available */
588 if (pri->columns)
590 else
591 nulls[Anum_pg_publication_rel_prattrs - 1] = true;
592
594
595 /* Insert tuple into catalog. */
598
599 /* Register dependencies as needed */
601
602 /* Add dependency on the publication */
605
606 /* Add dependency on the relation */
609
610 /* Add dependency on the objects mentioned in the qualifications */
611 if (pri->whereClause)
612 recordDependencyOnSingleRelExpr(&myself, pri->whereClause, relid,
614 false);
615
616 /* Add dependency on the columns, if any are listed */
617 i = -1;
618 while ((i = bms_next_member(attnums, i)) >= 0)
619 {
622 }
623
624 /* Close the table. */
626
627 /*
628 * Determine whether EXCEPT tables require explicit relcache invalidation.
629 *
630 * For CREATE PUBLICATION with EXCEPT tables, invalidation is skipped
631 * here, as CreatePublication() function invalidates all relations as part
632 * of defining a FOR ALL TABLES publication.
633 *
634 * For ALTER PUBLICATION, invalidation is needed only when adding an
635 * EXCEPT table to a publication already marked as ALL TABLES. For
636 * publications that were originally empty or defined as ALL SEQUENCES and
637 * are being converted to ALL TABLES, invalidation is skipped here, as
638 * AlterPublicationAllFlags() function invalidates all relations while
639 * marking the publication as ALL TABLES publication.
640 */
642 (alter_stmt->for_all_tables && pri->except);
643
644 if (!pri->except || inval_except_table)
645 {
646 /*
647 * Invalidate relcache so that publication info is rebuilt.
648 *
649 * For the partitioned tables, we must invalidate all partitions
650 * contained in the respective partition hierarchies, not just the one
651 * explicitly mentioned in the publication. This is required because
652 * we implicitly publish the child tables when the parent table is
653 * published.
654 */
656 relid);
657
659 }
660
661 return myself;
662}
#define CStringGetTextDatum(s)
Definition builtins.h:98
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition catalog.c:448
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
@ DEPENDENCY_AUTO
Definition dependency.h:34
@ DEPENDENCY_NORMAL
Definition dependency.h:33
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
#define RowExclusiveLock
Definition lockdefs.h:38
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
char * nodeToString(const void *obj)
Definition outfuncs.c:811
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition pg_depend.c:47
static int2vector * attnumstoint2vector(Bitmapset *attrs)
Bitmapset * pub_collist_validate(Relation targetrel, List *columns)
static void check_publication_add_relation(PublicationRelInfo *pri)
void InvalidatePublicationRels(List *relids)

References Publication::alltables, attnumstoint2vector(), bms_next_member(), BoolGetDatum(), CatalogTupleInsert(), check_publication_add_relation(), CStringGetTextDatum, DEPENDENCY_AUTO, DEPENDENCY_NORMAL, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg, ERROR, fb(), GetNewOidWithIndex(), GetPublication(), GetPubPartitionOptionRelations(), heap_form_tuple(), heap_freetuple(), i, InvalidatePublicationRels(), InvalidObjectAddress, Publication::name, NIL, nodeToString(), ObjectAddressSet, ObjectAddressSubSet, ObjectIdGetDatum(), PointerGetDatum, pub_collist_validate(), PUBLICATION_PART_ALL, recordDependencyOn(), recordDependencyOnSingleRelExpr(), RelationGetDescr, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, SearchSysCacheExists2, table_close(), table_open(), and values.

Referenced by PublicationAddTables().

◆ publication_add_schema()

ObjectAddress publication_add_schema ( Oid  pubid,
Oid  schemaid,
bool  if_not_exists 
)

Definition at line 789 of file pg_publication.c.

790{
791 Relation rel;
795 Oid psschid;
796 Publication *pub = GetPublication(pubid);
800
802
803 /*
804 * Check for duplicates. Note that this does not really prevent
805 * duplicates, it's here just to provide nicer error message in common
806 * case. The real protection is the unique key on the catalog.
807 */
810 ObjectIdGetDatum(pubid)))
811 {
813
814 if (if_not_exists)
816
819 errmsg("schema \"%s\" is already member of publication \"%s\"",
821 }
822
824
825 /* Form a tuple */
826 memset(values, 0, sizeof(values));
827 memset(nulls, false, sizeof(nulls));
828
833 ObjectIdGetDatum(pubid);
836
838
839 /* Insert tuple into catalog */
842
844
845 /* Add dependency on the publication */
848
849 /* Add dependency on the schema */
852
853 /* Close the table */
855
856 /*
857 * Invalidate relcache so that publication info is rebuilt. See
858 * publication_add_relation for why we need to consider all the
859 * partitions.
860 */
864
865 return myself;
866}
static void check_publication_add_schema(Oid schemaid)

References CatalogTupleInsert(), check_publication_add_schema(), DEPENDENCY_AUTO, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg, ERROR, fb(), get_namespace_name(), GetNewOidWithIndex(), GetPublication(), GetSchemaPublicationRelations(), heap_form_tuple(), heap_freetuple(), InvalidatePublicationRels(), InvalidObjectAddress, Publication::name, NIL, ObjectAddressSet, ObjectIdGetDatum(), PUBLICATION_PART_ALL, recordDependencyOn(), RelationGetDescr, RowExclusiveLock, SearchSysCacheExists2, table_close(), table_open(), and values.

Referenced by PublicationAddSchemas().