PostgreSQL Source Code git master
pg_publication.h File Reference
#include "catalog/genbki.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_publication_d.h"
Include dependency graph for pg_publication.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PublicationActions
 
struct  PublicationDesc
 
struct  Publication
 
struct  PublicationRelInfo
 

Typedefs

typedef FormData_pg_publicationForm_pg_publication
 
typedef struct PublicationActions PublicationActions
 
typedef struct PublicationDesc PublicationDesc
 
typedef struct Publication Publication
 
typedef struct PublicationRelInfo PublicationRelInfo
 
typedef enum PublicationPartOpt PublicationPartOpt
 

Enumerations

enum  PublicationPartOpt { PUBLICATION_PART_ROOT , PUBLICATION_PART_LEAF , PUBLICATION_PART_ALL }
 

Functions

 CATALOG (pg_publication, 6104, PublicationRelationId)
 
 DECLARE_UNIQUE_INDEX_PKEY (pg_publication_oid_index, 6110, PublicationObjectIndexId, pg_publication, btree(oid oid_ops))
 
 DECLARE_UNIQUE_INDEX (pg_publication_pubname_index, 6111, PublicationNameIndexId, pg_publication, btree(pubname name_ops))
 
 MAKE_SYSCACHE (PUBLICATIONOID, pg_publication_oid_index, 8)
 
 MAKE_SYSCACHE (PUBLICATIONNAME, pg_publication_pubname_index, 8)
 
PublicationGetPublication (Oid pubid)
 
PublicationGetPublicationByName (const char *pubname, bool missing_ok)
 
ListGetRelationPublications (Oid relid)
 
ListGetPublicationRelations (Oid pubid, PublicationPartOpt pub_partopt)
 
ListGetAllTablesPublications (void)
 
ListGetAllTablesPublicationRelations (bool pubviaroot)
 
ListGetPublicationSchemas (Oid pubid)
 
ListGetSchemaPublications (Oid schemaid)
 
ListGetSchemaPublicationRelations (Oid schemaid, PublicationPartOpt pub_partopt)
 
ListGetAllSchemaPublicationRelations (Oid pubid, PublicationPartOpt pub_partopt)
 
ListGetPubPartitionOptionRelations (List *result, PublicationPartOpt pub_partopt, Oid relid)
 
Oid GetTopMostAncestorInPublication (Oid puboid, List *ancestors, int *ancestor_level)
 
bool is_publishable_relation (Relation rel)
 
bool is_schema_publication (Oid pubid)
 
bool check_and_fetch_column_list (Publication *pub, Oid relid, MemoryContext mcxt, Bitmapset **cols)
 
ObjectAddress publication_add_relation (Oid pubid, PublicationRelInfo *pri, bool if_not_exists)
 
Bitmapsetpub_collist_validate (Relation targetrel, List *columns)
 
ObjectAddress publication_add_schema (Oid pubid, Oid schemaid, bool if_not_exists)
 
Bitmapsetpub_collist_to_bitmapset (Bitmapset *columns, Datum pubcols, MemoryContext mcxt)
 
Bitmapsetpub_form_cols_map (Relation relation, bool include_gencols)
 

Variables

 FormData_pg_publication
 

Typedef Documentation

◆ Form_pg_publication

Definition at line 67 of file pg_publication.h.

◆ Publication

typedef struct Publication Publication

◆ PublicationActions

◆ PublicationDesc

◆ PublicationPartOpt

◆ PublicationRelInfo

Enumeration Type Documentation

◆ PublicationPartOpt

Enumerator
PUBLICATION_PART_ROOT 
PUBLICATION_PART_LEAF 
PUBLICATION_PART_ALL 

Definition at line 140 of file pg_publication.h.

141{
PublicationPartOpt
@ PUBLICATION_PART_LEAF
@ PUBLICATION_PART_ROOT
@ PUBLICATION_PART_ALL

Function Documentation

◆ CATALOG()

CATALOG ( pg_publication  ,
6104  ,
PublicationRelationId   
)

Definition at line 29 of file pg_publication.h.

30{
31 Oid oid; /* oid */
32
33 NameData pubname; /* name of the publication */
34
35 Oid pubowner BKI_LOOKUP(pg_authid); /* publication owner */
36
37 /*
38 * indicates that this is special publication which should encompass all
39 * tables in the database (except for the unlogged and temp ones)
40 */
41 bool puballtables;
42
43 /* true if inserts are published */
44 bool pubinsert;
45
46 /* true if updates are published */
47 bool pubupdate;
48
49 /* true if deletes are published */
50 bool pubdelete;
51
52 /* true if truncates are published */
53 bool pubtruncate;
54
55 /* true if partition changes are published using root schema */
56 bool pubviaroot;
57
58 /* true if generated columns data should be published */
59 bool pubgencols;
#define BKI_LOOKUP(catalog)
Definition: genbki.h:46
FormData_pg_publication
unsigned int Oid
Definition: postgres_ext.h:32
Definition: c.h:698

References BKI_LOOKUP.

◆ check_and_fetch_column_list()

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

Definition at line 268 of file pg_publication.c.

270{
271 HeapTuple cftuple;
272 bool found = false;
273
274 if (pub->alltables)
275 return false;
276
277 cftuple = SearchSysCache2(PUBLICATIONRELMAP,
278 ObjectIdGetDatum(relid),
279 ObjectIdGetDatum(pub->oid));
280 if (HeapTupleIsValid(cftuple))
281 {
282 Datum cfdatum;
283 bool isnull;
284
285 /* Lookup the column list attribute. */
286 cfdatum = SysCacheGetAttr(PUBLICATIONRELMAP, cftuple,
287 Anum_pg_publication_rel_prattrs, &isnull);
288
289 /* Was a column list found? */
290 if (!isnull)
291 {
292 /* Build the column list bitmap in the given memory context. */
293 if (cols)
294 *cols = pub_collist_to_bitmapset(*cols, cfdatum, mcxt);
295
296 found = true;
297 }
298
299 ReleaseSysCache(cftuple);
300 }
301
302 return found;
303}
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Bitmapset * pub_collist_to_bitmapset(Bitmapset *columns, Datum pubcols, MemoryContext mcxt)
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:232

References Publication::alltables, 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().

◆ DECLARE_UNIQUE_INDEX()

DECLARE_UNIQUE_INDEX ( pg_publication_pubname_index  ,
6111  ,
PublicationNameIndexId  ,
pg_publication  ,
btree(pubname name_ops)   
)

◆ DECLARE_UNIQUE_INDEX_PKEY()

DECLARE_UNIQUE_INDEX_PKEY ( pg_publication_oid_index  ,
6110  ,
PublicationObjectIndexId  ,
pg_publication  ,
btree(oid oid_ops)   
)

◆ GetAllSchemaPublicationRelations()

List * GetAllSchemaPublicationRelations ( Oid  pubid,
PublicationPartOpt  pub_partopt 
)

Definition at line 1026 of file pg_publication.c.

1027{
1028 List *result = NIL;
1029 List *pubschemalist = GetPublicationSchemas(pubid);
1030 ListCell *cell;
1031
1032 foreach(cell, pubschemalist)
1033 {
1034 Oid schemaid = lfirst_oid(cell);
1035 List *schemaRels = NIL;
1036
1037 schemaRels = GetSchemaPublicationRelations(schemaid, pub_partopt);
1038 result = list_concat(result, schemaRels);
1039 }
1040
1041 return result;
1042}
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define NIL
Definition: pg_list.h:68
#define lfirst_oid(lc)
Definition: pg_list.h:174
List * GetPublicationSchemas(Oid pubid)
List * GetSchemaPublicationRelations(Oid schemaid, PublicationPartOpt pub_partopt)
Definition: pg_list.h:54

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

Referenced by AlterPublicationOptions(), and pg_get_publication_tables().

◆ GetAllTablesPublicationRelations()

List * GetAllTablesPublicationRelations ( bool  pubviaroot)

Definition at line 844 of file pg_publication.c.

845{
846 Relation classRel;
847 ScanKeyData key[1];
848 TableScanDesc scan;
849 HeapTuple tuple;
850 List *result = NIL;
851
852 classRel = table_open(RelationRelationId, AccessShareLock);
853
854 ScanKeyInit(&key[0],
855 Anum_pg_class_relkind,
856 BTEqualStrategyNumber, F_CHAREQ,
857 CharGetDatum(RELKIND_RELATION));
858
859 scan = table_beginscan_catalog(classRel, 1, key);
860
861 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
862 {
863 Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
864 Oid relid = relForm->oid;
865
866 if (is_publishable_class(relid, relForm) &&
867 !(relForm->relispartition && pubviaroot))
868 result = lappend_oid(result, relid);
869 }
870
871 table_endscan(scan);
872
873 if (pubviaroot)
874 {
875 ScanKeyInit(&key[0],
876 Anum_pg_class_relkind,
877 BTEqualStrategyNumber, F_CHAREQ,
878 CharGetDatum(RELKIND_PARTITIONED_TABLE));
879
880 scan = table_beginscan_catalog(classRel, 1, key);
881
882 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
883 {
884 Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
885 Oid relid = relForm->oid;
886
887 if (is_publishable_class(relid, relForm) &&
888 !relForm->relispartition)
889 result = lappend_oid(result, relid);
890 }
891
892 table_endscan(scan);
893 }
894
895 table_close(classRel, AccessShareLock);
896 return result;
897}
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1264
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
#define AccessShareLock
Definition: lockdefs.h:36
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
static bool is_publishable_class(Oid relid, Form_pg_class reltuple)
static Datum CharGetDatum(char X)
Definition: postgres.h:127
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
@ ForwardScanDirection
Definition: sdir.h:28
#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
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1024

References AccessShareLock, BTEqualStrategyNumber, CharGetDatum(), ForwardScanDirection, GETSTRUCT, heap_getnext(), is_publishable_class(), sort-test::key, lappend_oid(), NIL, ScanKeyInit(), table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by pg_get_publication_tables().

◆ GetAllTablesPublications()

List * GetAllTablesPublications ( void  )

Definition at line 803 of file pg_publication.c.

804{
805 List *result;
806 Relation rel;
807 ScanKeyData scankey;
808 SysScanDesc scan;
809 HeapTuple tup;
810
811 /* Find all publications that are marked as for all tables. */
812 rel = table_open(PublicationRelationId, AccessShareLock);
813
814 ScanKeyInit(&scankey,
815 Anum_pg_publication_puballtables,
816 BTEqualStrategyNumber, F_BOOLEQ,
817 BoolGetDatum(true));
818
819 scan = systable_beginscan(rel, InvalidOid, false,
820 NULL, 1, &scankey);
821
822 result = NIL;
823 while (HeapTupleIsValid(tup = systable_getnext(scan)))
824 {
825 Oid oid = ((Form_pg_publication) GETSTRUCT(tup))->oid;
826
827 result = lappend_oid(result, oid);
828 }
829
830 systable_endscan(scan);
832
833 return result;
834}
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:606
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:513
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:387
FormData_pg_publication * Form_pg_publication
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
#define InvalidOid
Definition: postgres_ext.h:37

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

Referenced by RelationBuildPublicationDesc().

◆ GetPublication()

Publication * GetPublication ( Oid  pubid)

Definition at line 1050 of file pg_publication.c.

1051{
1052 HeapTuple tup;
1053 Publication *pub;
1054 Form_pg_publication pubform;
1055
1056 tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
1057 if (!HeapTupleIsValid(tup))
1058 elog(ERROR, "cache lookup failed for publication %u", pubid);
1059
1060 pubform = (Form_pg_publication) GETSTRUCT(tup);
1061
1062 pub = (Publication *) palloc(sizeof(Publication));
1063 pub->oid = pubid;
1064 pub->name = pstrdup(NameStr(pubform->pubname));
1065 pub->alltables = pubform->puballtables;
1066 pub->pubactions.pubinsert = pubform->pubinsert;
1067 pub->pubactions.pubupdate = pubform->pubupdate;
1068 pub->pubactions.pubdelete = pubform->pubdelete;
1069 pub->pubactions.pubtruncate = pubform->pubtruncate;
1070 pub->pubviaroot = pubform->pubviaroot;
1071 pub->pubgencols = pubform->pubgencols;
1072
1073 ReleaseSysCache(tup);
1074
1075 return pub;
1076}
#define NameStr(name)
Definition: c.h:703
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void * palloc(Size size)
Definition: mcxt.c:1317
PublicationActions pubactions
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221

References Publication::alltables, elog, ERROR, GETSTRUCT, HeapTupleIsValid, Publication::name, NameStr, ObjectIdGetDatum(), Publication::oid, palloc(), pstrdup(), Publication::pubactions, PublicationActions::pubdelete, Publication::pubgencols, PublicationActions::pubinsert, PublicationActions::pubtruncate, PublicationActions::pubupdate, Publication::pubviaroot, ReleaseSysCache(), and SearchSysCache1().

Referenced by 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 1082 of file pg_publication.c.

1083{
1084 Oid oid;
1085
1086 oid = get_publication_oid(pubname, missing_ok);
1087
1088 return OidIsValid(oid) ? GetPublication(oid) : NULL;
1089}
#define OidIsValid(objectId)
Definition: c.h:732
Oid get_publication_oid(const char *pubname, bool missing_ok)
Definition: lsyscache.c:3625
Publication * GetPublication(Oid pubid)

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

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

◆ GetPublicationRelations()

List * GetPublicationRelations ( Oid  pubid,
PublicationPartOpt  pub_partopt 
)

Definition at line 760 of file pg_publication.c.

761{
762 List *result;
763 Relation pubrelsrel;
764 ScanKeyData scankey;
765 SysScanDesc scan;
766 HeapTuple tup;
767
768 /* Find all publications associated with the relation. */
769 pubrelsrel = table_open(PublicationRelRelationId, AccessShareLock);
770
771 ScanKeyInit(&scankey,
772 Anum_pg_publication_rel_prpubid,
773 BTEqualStrategyNumber, F_OIDEQ,
774 ObjectIdGetDatum(pubid));
775
776 scan = systable_beginscan(pubrelsrel, PublicationRelPrpubidIndexId,
777 true, NULL, 1, &scankey);
778
779 result = NIL;
780 while (HeapTupleIsValid(tup = systable_getnext(scan)))
781 {
783
784 pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
785 result = GetPubPartitionOptionRelations(result, pub_partopt,
786 pubrel->prrelid);
787 }
788
789 systable_endscan(scan);
790 table_close(pubrelsrel, AccessShareLock);
791
792 /* Now sort and de-duplicate the result list */
793 list_sort(result, list_oid_cmp);
794 list_deduplicate_oid(result);
795
796 return result;
797}
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
List * GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt, Oid relid)
FormData_pg_publication_rel * Form_pg_publication_rel

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

Referenced by AlterPublicationOptions(), AlterPublicationSchemas(), AlterPublicationTables(), and pg_get_publication_tables().

◆ GetPublicationSchemas()

List * GetPublicationSchemas ( Oid  pubid)

Definition at line 905 of file pg_publication.c.

906{
907 List *result = NIL;
908 Relation pubschsrel;
909 ScanKeyData scankey;
910 SysScanDesc scan;
911 HeapTuple tup;
912
913 /* Find all schemas associated with the publication */
914 pubschsrel = table_open(PublicationNamespaceRelationId, AccessShareLock);
915
916 ScanKeyInit(&scankey,
917 Anum_pg_publication_namespace_pnpubid,
918 BTEqualStrategyNumber, F_OIDEQ,
919 ObjectIdGetDatum(pubid));
920
921 scan = systable_beginscan(pubschsrel,
922 PublicationNamespacePnnspidPnpubidIndexId,
923 true, NULL, 1, &scankey);
924 while (HeapTupleIsValid(tup = systable_getnext(scan)))
925 {
927
929
930 result = lappend_oid(result, pubsch->pnnspid);
931 }
932
933 systable_endscan(scan);
934 table_close(pubschsrel, AccessShareLock);
935
936 return result;
937}
FormData_pg_publication_namespace * Form_pg_publication_namespace

References AccessShareLock, BTEqualStrategyNumber, GETSTRUCT, HeapTupleIsValid, lappend_oid(), NIL, ObjectIdGetDatum(), ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by AlterPublicationSchemas(), and GetAllSchemaPublicationRelations().

◆ GetPubPartitionOptionRelations()

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

Definition at line 310 of file pg_publication.c.

312{
313 if (get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE &&
314 pub_partopt != PUBLICATION_PART_ROOT)
315 {
316 List *all_parts = find_all_inheritors(relid, NoLock,
317 NULL);
318
319 if (pub_partopt == PUBLICATION_PART_ALL)
320 result = list_concat(result, all_parts);
321 else if (pub_partopt == PUBLICATION_PART_LEAF)
322 {
323 ListCell *lc;
324
325 foreach(lc, all_parts)
326 {
327 Oid partOid = lfirst_oid(lc);
328
329 if (get_rel_relkind(partOid) != RELKIND_PARTITIONED_TABLE)
330 result = lappend_oid(result, partOid);
331 }
332 }
333 else
334 Assert(false);
335 }
336 else
337 result = lappend_oid(result, relid);
338
339 return result;
340}
#define Assert(condition)
Definition: c.h:815
#define NoLock
Definition: lockdefs.h:34
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2003
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:255

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

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

◆ GetRelationPublications()

List * GetRelationPublications ( Oid  relid)

Definition at line 731 of file pg_publication.c.

732{
733 List *result = NIL;
734 CatCList *pubrellist;
735 int i;
736
737 /* Find all publications associated with the relation. */
738 pubrellist = SearchSysCacheList1(PUBLICATIONRELMAP,
739 ObjectIdGetDatum(relid));
740 for (i = 0; i < pubrellist->n_members; i++)
741 {
742 HeapTuple tup = &pubrellist->members[i]->tuple;
743 Oid pubid = ((Form_pg_publication_rel) GETSTRUCT(tup))->prpubid;
744
745 result = lappend_oid(result, pubid);
746 }
747
748 ReleaseSysCacheList(pubrellist);
749
750 return result;
751}
int i
Definition: isn.c:72
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:180
int n_members
Definition: catcache.h:178
HeapTupleData tuple
Definition: catcache.h:123
#define ReleaseSysCacheList(x)
Definition: syscache.h:134
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:127

References GETSTRUCT, i, lappend_oid(), catclist::members, catclist::n_members, NIL, ObjectIdGetDatum(), ReleaseSysCacheList, SearchSysCacheList1, and catctup::tuple.

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

◆ GetSchemaPublicationRelations()

List * GetSchemaPublicationRelations ( Oid  schemaid,
PublicationPartOpt  pub_partopt 
)

Definition at line 969 of file pg_publication.c.

970{
971 Relation classRel;
972 ScanKeyData key[1];
973 TableScanDesc scan;
974 HeapTuple tuple;
975 List *result = NIL;
976
977 Assert(OidIsValid(schemaid));
978
979 classRel = table_open(RelationRelationId, AccessShareLock);
980
981 ScanKeyInit(&key[0],
982 Anum_pg_class_relnamespace,
983 BTEqualStrategyNumber, F_OIDEQ,
984 schemaid);
985
986 /* get all the relations present in the specified schema */
987 scan = table_beginscan_catalog(classRel, 1, key);
988 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
989 {
990 Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
991 Oid relid = relForm->oid;
992 char relkind;
993
994 if (!is_publishable_class(relid, relForm))
995 continue;
996
997 relkind = get_rel_relkind(relid);
998 if (relkind == RELKIND_RELATION)
999 result = lappend_oid(result, relid);
1000 else if (relkind == RELKIND_PARTITIONED_TABLE)
1001 {
1002 List *partitionrels = NIL;
1003
1004 /*
1005 * It is quite possible that some of the partitions are in a
1006 * different schema than the parent table, so we need to get such
1007 * partitions separately.
1008 */
1009 partitionrels = GetPubPartitionOptionRelations(partitionrels,
1010 pub_partopt,
1011 relForm->oid);
1012 result = list_concat_unique_oid(result, partitionrels);
1013 }
1014 }
1015
1016 table_endscan(scan);
1017 table_close(classRel, AccessShareLock);
1018 return result;
1019}
List * list_concat_unique_oid(List *list1, const List *list2)
Definition: list.c:1469

References AccessShareLock, Assert, BTEqualStrategyNumber, ForwardScanDirection, get_rel_relkind(), GetPubPartitionOptionRelations(), GETSTRUCT, heap_getnext(), is_publishable_class(), sort-test::key, lappend_oid(), list_concat_unique_oid(), NIL, OidIsValid, 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 943 of file pg_publication.c.

944{
945 List *result = NIL;
946 CatCList *pubschlist;
947 int i;
948
949 /* Find all publications associated with the schema */
950 pubschlist = SearchSysCacheList1(PUBLICATIONNAMESPACEMAP,
951 ObjectIdGetDatum(schemaid));
952 for (i = 0; i < pubschlist->n_members; i++)
953 {
954 HeapTuple tup = &pubschlist->members[i]->tuple;
955 Oid pubid = ((Form_pg_publication_namespace) GETSTRUCT(tup))->pnpubid;
956
957 result = lappend_oid(result, pubid);
958 }
959
960 ReleaseSysCacheList(pubschlist);
961
962 return result;
963}

References GETSTRUCT, i, lappend_oid(), catclist::members, catclist::n_members, NIL, ObjectIdGetDatum(), ReleaseSysCacheList, SearchSysCacheList1, and catctup::tuple.

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

◆ GetTopMostAncestorInPublication()

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

Definition at line 354 of file pg_publication.c.

355{
356 ListCell *lc;
357 Oid topmost_relid = InvalidOid;
358 int level = 0;
359
360 /*
361 * Find the "topmost" ancestor that is in this publication.
362 */
363 foreach(lc, ancestors)
364 {
365 Oid ancestor = lfirst_oid(lc);
366 List *apubids = GetRelationPublications(ancestor);
367 List *aschemaPubids = NIL;
368
369 level++;
370
371 if (list_member_oid(apubids, puboid))
372 {
373 topmost_relid = ancestor;
374
375 if (ancestor_level)
376 *ancestor_level = level;
377 }
378 else
379 {
380 aschemaPubids = GetSchemaPublications(get_rel_namespace(ancestor));
381 if (list_member_oid(aschemaPubids, puboid))
382 {
383 topmost_relid = ancestor;
384
385 if (ancestor_level)
386 *ancestor_level = level;
387 }
388 }
389
390 list_free(apubids);
391 list_free(aschemaPubids);
392 }
393
394 return topmost_relid;
395}
void list_free(List *list)
Definition: list.c:1546
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1952
List * GetRelationPublications(Oid relid)
List * GetSchemaPublications(Oid schemaid)

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

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

◆ is_publishable_relation()

bool is_publishable_relation ( Relation  rel)

Definition at line 147 of file pg_publication.c.

148{
150}
#define RelationGetRelid(relation)
Definition: rel.h:505
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_schema_publication()

bool is_schema_publication ( Oid  pubid)

Definition at line 233 of file pg_publication.c.

234{
235 Relation pubschsrel;
236 ScanKeyData scankey;
237 SysScanDesc scan;
238 HeapTuple tup;
239 bool result = false;
240
241 pubschsrel = table_open(PublicationNamespaceRelationId, AccessShareLock);
242 ScanKeyInit(&scankey,
243 Anum_pg_publication_namespace_pnpubid,
244 BTEqualStrategyNumber, F_OIDEQ,
245 ObjectIdGetDatum(pubid));
246
247 scan = systable_beginscan(pubschsrel,
248 PublicationNamespacePnnspidPnpubidIndexId,
249 true, NULL, 1, &scankey);
250 tup = systable_getnext(scan);
251 result = HeapTupleIsValid(tup);
252
253 systable_endscan(scan);
254 table_close(pubschsrel, AccessShareLock);
255
256 return result;
257}

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

Referenced by AlterPublicationOwner_internal(), and AlterPublicationTables().

◆ MAKE_SYSCACHE() [1/2]

MAKE_SYSCACHE ( PUBLICATIONNAME  ,
pg_publication_pubname_index  ,
 
)

◆ MAKE_SYSCACHE() [2/2]

MAKE_SYSCACHE ( PUBLICATIONOID  ,
pg_publication_oid_index  ,
 
)

◆ pub_collist_to_bitmapset()

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

Definition at line 597 of file pg_publication.c.

598{
599 Bitmapset *result = columns;
600 ArrayType *arr;
601 int nelems;
602 int16 *elems;
603 MemoryContext oldcxt = NULL;
604
605 arr = DatumGetArrayTypeP(pubcols);
606 nelems = ARR_DIMS(arr)[0];
607 elems = (int16 *) ARR_DATA_PTR(arr);
608
609 /* If a memory context was specified, switch to it. */
610 if (mcxt)
611 oldcxt = MemoryContextSwitchTo(mcxt);
612
613 for (int i = 0; i < nelems; i++)
614 result = bms_add_member(result, elems[i]);
615
616 if (mcxt)
617 MemoryContextSwitchTo(oldcxt);
618
619 return result;
620}
#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:815
int16_t int16
Definition: c.h:483
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124

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

Referenced by AlterPublicationTables(), and check_and_fetch_column_list().

◆ pub_collist_validate()

Bitmapset * pub_collist_validate ( Relation  targetrel,
List columns 
)

Definition at line 555 of file pg_publication.c.

556{
557 Bitmapset *set = NULL;
558 ListCell *lc;
559
560 foreach(lc, columns)
561 {
562 char *colname = strVal(lfirst(lc));
563 AttrNumber attnum = get_attnum(RelationGetRelid(targetrel), colname);
564
567 errcode(ERRCODE_UNDEFINED_COLUMN),
568 errmsg("column \"%s\" of relation \"%s\" does not exist",
569 colname, RelationGetRelationName(targetrel)));
570
573 errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
574 errmsg("cannot use system column \"%s\" in publication column list",
575 colname));
576
577 if (bms_is_member(attnum, set))
580 errmsg("duplicate column \"%s\" in publication column list",
581 colname));
582
583 set = bms_add_member(set, attnum);
584 }
585
586 return set;
587}
int16 AttrNumber
Definition: attnum.h:21
#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
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:858
int16 attnum
Definition: pg_attribute.h:74
#define lfirst(lc)
Definition: pg_list.h:172
#define RelationGetRelationName(relation)
Definition: rel.h:539
#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, get_attnum(), InvalidAttrNumber, lfirst, RelationGetRelationName, RelationGetRelid, and strVal.

Referenced by AlterPublicationTables(), and publication_add_relation().

◆ pub_form_cols_map()

Bitmapset * pub_form_cols_map ( Relation  relation,
bool  include_gencols 
)

Definition at line 628 of file pg_publication.c.

629{
630 Bitmapset *result = NULL;
631 TupleDesc desc = RelationGetDescr(relation);
632
633 for (int i = 0; i < desc->natts; i++)
634 {
635 Form_pg_attribute att = TupleDescAttr(desc, i);
636
637 if (att->attisdropped || (att->attgenerated && !include_gencols))
638 continue;
639
640 result = bms_add_member(result, att->attnum);
641 }
642
643 return result;
644}
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define RelationGetDescr(relation)
Definition: rel.h:531
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:153

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

Referenced by pgoutput_column_list_init().

◆ publication_add_relation()

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

Definition at line 428 of file pg_publication.c.

430{
431 Relation rel;
432 HeapTuple tup;
433 Datum values[Natts_pg_publication_rel];
434 bool nulls[Natts_pg_publication_rel];
435 Relation targetrel = pri->relation;
436 Oid relid = RelationGetRelid(targetrel);
437 Oid pubreloid;
438 Bitmapset *attnums;
439 Publication *pub = GetPublication(pubid);
440 ObjectAddress myself,
441 referenced;
442 List *relids = NIL;
443 int i;
444
445 rel = table_open(PublicationRelRelationId, RowExclusiveLock);
446
447 /*
448 * Check for duplicates. Note that this does not really prevent
449 * duplicates, it's here just to provide nicer error message in common
450 * case. The real protection is the unique key on the catalog.
451 */
452 if (SearchSysCacheExists2(PUBLICATIONRELMAP, ObjectIdGetDatum(relid),
453 ObjectIdGetDatum(pubid)))
454 {
456
457 if (if_not_exists)
459
462 errmsg("relation \"%s\" is already member of publication \"%s\"",
463 RelationGetRelationName(targetrel), pub->name)));
464 }
465
467
468 /* Validate and translate column names into a Bitmapset of attnums. */
469 attnums = pub_collist_validate(pri->relation, pri->columns);
470
471 /* Form a tuple. */
472 memset(values, 0, sizeof(values));
473 memset(nulls, false, sizeof(nulls));
474
475 pubreloid = GetNewOidWithIndex(rel, PublicationRelObjectIndexId,
476 Anum_pg_publication_rel_oid);
477 values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(pubreloid);
478 values[Anum_pg_publication_rel_prpubid - 1] =
479 ObjectIdGetDatum(pubid);
480 values[Anum_pg_publication_rel_prrelid - 1] =
481 ObjectIdGetDatum(relid);
482
483 /* Add qualifications, if available */
484 if (pri->whereClause != NULL)
485 values[Anum_pg_publication_rel_prqual - 1] = CStringGetTextDatum(nodeToString(pri->whereClause));
486 else
487 nulls[Anum_pg_publication_rel_prqual - 1] = true;
488
489 /* Add column list, if available */
490 if (pri->columns)
491 values[Anum_pg_publication_rel_prattrs - 1] = PointerGetDatum(attnumstoint2vector(attnums));
492 else
493 nulls[Anum_pg_publication_rel_prattrs - 1] = true;
494
495 tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
496
497 /* Insert tuple into catalog. */
498 CatalogTupleInsert(rel, tup);
499 heap_freetuple(tup);
500
501 /* Register dependencies as needed */
502 ObjectAddressSet(myself, PublicationRelRelationId, pubreloid);
503
504 /* Add dependency on the publication */
505 ObjectAddressSet(referenced, PublicationRelationId, pubid);
506 recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
507
508 /* Add dependency on the relation */
509 ObjectAddressSet(referenced, RelationRelationId, relid);
510 recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
511
512 /* Add dependency on the objects mentioned in the qualifications */
513 if (pri->whereClause)
514 recordDependencyOnSingleRelExpr(&myself, pri->whereClause, relid,
516 false);
517
518 /* Add dependency on the columns, if any are listed */
519 i = -1;
520 while ((i = bms_next_member(attnums, i)) >= 0)
521 {
522 ObjectAddressSubSet(referenced, RelationRelationId, relid, i);
523 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
524 }
525
526 /* Close the table. */
528
529 /*
530 * Invalidate relcache so that publication info is rebuilt.
531 *
532 * For the partitioned tables, we must invalidate all partitions contained
533 * in the respective partition hierarchies, not just the one explicitly
534 * mentioned in the publication. This is required because we implicitly
535 * publish the child tables when the parent table is published.
536 */
538 relid);
539
541
542 return myself;
543}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define CStringGetTextDatum(s)
Definition: builtins.h:97
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:419
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
Definition: dependency.c:1596
@ DEPENDENCY_AUTO
Definition: dependency.h:34
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
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)
Definition: objectaddress.h:40
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
Definition: objectaddress.h:33
char * nodeToString(const void *obj)
Definition: outfuncs.c:794
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:45
static int2vector * attnumstoint2vector(Bitmapset *attrs)
Bitmapset * pub_collist_validate(Relation targetrel, List *columns)
static void check_publication_add_relation(Relation targetrel)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
void InvalidatePublicationRels(List *relids)
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:102

References attnumstoint2vector(), bms_next_member(), CatalogTupleInsert(), check_publication_add_relation(), PublicationRelInfo::columns, CStringGetTextDatum, DEPENDENCY_AUTO, DEPENDENCY_NORMAL, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, 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(), PublicationRelInfo::relation, RelationGetDescr, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, SearchSysCacheExists2, table_close(), table_open(), values, and PublicationRelInfo::whereClause.

Referenced by PublicationAddTables().

◆ publication_add_schema()

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

Definition at line 650 of file pg_publication.c.

651{
652 Relation rel;
653 HeapTuple tup;
654 Datum values[Natts_pg_publication_namespace];
655 bool nulls[Natts_pg_publication_namespace];
656 Oid psschid;
657 Publication *pub = GetPublication(pubid);
658 List *schemaRels = NIL;
659 ObjectAddress myself,
660 referenced;
661
662 rel = table_open(PublicationNamespaceRelationId, RowExclusiveLock);
663
664 /*
665 * Check for duplicates. Note that this does not really prevent
666 * duplicates, it's here just to provide nicer error message in common
667 * case. The real protection is the unique key on the catalog.
668 */
669 if (SearchSysCacheExists2(PUBLICATIONNAMESPACEMAP,
670 ObjectIdGetDatum(schemaid),
671 ObjectIdGetDatum(pubid)))
672 {
674
675 if (if_not_exists)
677
680 errmsg("schema \"%s\" is already member of publication \"%s\"",
681 get_namespace_name(schemaid), pub->name)));
682 }
683
685
686 /* Form a tuple */
687 memset(values, 0, sizeof(values));
688 memset(nulls, false, sizeof(nulls));
689
690 psschid = GetNewOidWithIndex(rel, PublicationNamespaceObjectIndexId,
691 Anum_pg_publication_namespace_oid);
692 values[Anum_pg_publication_namespace_oid - 1] = ObjectIdGetDatum(psschid);
693 values[Anum_pg_publication_namespace_pnpubid - 1] =
694 ObjectIdGetDatum(pubid);
695 values[Anum_pg_publication_namespace_pnnspid - 1] =
696 ObjectIdGetDatum(schemaid);
697
698 tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
699
700 /* Insert tuple into catalog */
701 CatalogTupleInsert(rel, tup);
702 heap_freetuple(tup);
703
704 ObjectAddressSet(myself, PublicationNamespaceRelationId, psschid);
705
706 /* Add dependency on the publication */
707 ObjectAddressSet(referenced, PublicationRelationId, pubid);
708 recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
709
710 /* Add dependency on the schema */
711 ObjectAddressSet(referenced, NamespaceRelationId, schemaid);
712 recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
713
714 /* Close the table */
716
717 /*
718 * Invalidate relcache so that publication info is rebuilt. See
719 * publication_add_relation for why we need to consider all the
720 * partitions.
721 */
722 schemaRels = GetSchemaPublicationRelations(schemaid,
724 InvalidatePublicationRels(schemaRels);
725
726 return myself;
727}
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
static void check_publication_add_schema(Oid schemaid)

References CatalogTupleInsert(), check_publication_add_schema(), DEPENDENCY_AUTO, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, 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().

Variable Documentation

◆ FormData_pg_publication

FormData_pg_publication

Definition at line 60 of file pg_publication.h.