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)
 
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)
 

Variables

 FormData_pg_publication
 

Typedef Documentation

◆ Form_pg_publication

Definition at line 64 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 129 of file pg_publication.h.

130 {
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;
#define BKI_LOOKUP(catalog)
Definition: genbki.h:46
FormData_pg_publication
unsigned int Oid
Definition: postgres_ext.h:31
Definition: c.h:741

References BKI_LOOKUP.

◆ 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 964 of file pg_publication.c.

965 {
966  List *result = NIL;
967  List *pubschemalist = GetPublicationSchemas(pubid);
968  ListCell *cell;
969 
970  foreach(cell, pubschemalist)
971  {
972  Oid schemaid = lfirst_oid(cell);
973  List *schemaRels = NIL;
974 
975  schemaRels = GetSchemaPublicationRelations(schemaid, pub_partopt);
976  result = list_concat(result, schemaRels);
977  }
978 
979  return result;
980 }
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 782 of file pg_publication.c.

783 {
784  Relation classRel;
785  ScanKeyData key[1];
786  TableScanDesc scan;
787  HeapTuple tuple;
788  List *result = NIL;
789 
790  classRel = table_open(RelationRelationId, AccessShareLock);
791 
792  ScanKeyInit(&key[0],
793  Anum_pg_class_relkind,
794  BTEqualStrategyNumber, F_CHAREQ,
795  CharGetDatum(RELKIND_RELATION));
796 
797  scan = table_beginscan_catalog(classRel, 1, key);
798 
799  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
800  {
801  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
802  Oid relid = relForm->oid;
803 
804  if (is_publishable_class(relid, relForm) &&
805  !(relForm->relispartition && pubviaroot))
806  result = lappend_oid(result, relid);
807  }
808 
809  table_endscan(scan);
810 
811  if (pubviaroot)
812  {
813  ScanKeyInit(&key[0],
814  Anum_pg_class_relkind,
815  BTEqualStrategyNumber, F_CHAREQ,
816  CharGetDatum(RELKIND_PARTITIONED_TABLE));
817 
818  scan = table_beginscan_catalog(classRel, 1, key);
819 
820  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
821  {
822  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
823  Oid relid = relForm->oid;
824 
825  if (is_publishable_class(relid, relForm) &&
826  !relForm->relispartition)
827  result = lappend_oid(result, relid);
828  }
829 
830  table_endscan(scan);
831  }
832 
833  table_close(classRel, AccessShareLock);
834  return result;
835 }
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1234
#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:122
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:1019

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 741 of file pg_publication.c.

742 {
743  List *result;
744  Relation rel;
745  ScanKeyData scankey;
746  SysScanDesc scan;
747  HeapTuple tup;
748 
749  /* Find all publications that are marked as for all tables. */
750  rel = table_open(PublicationRelationId, AccessShareLock);
751 
752  ScanKeyInit(&scankey,
753  Anum_pg_publication_puballtables,
754  BTEqualStrategyNumber, F_BOOLEQ,
755  BoolGetDatum(true));
756 
757  scan = systable_beginscan(rel, InvalidOid, false,
758  NULL, 1, &scankey);
759 
760  result = NIL;
761  while (HeapTupleIsValid(tup = systable_getnext(scan)))
762  {
763  Oid oid = ((Form_pg_publication) GETSTRUCT(tup))->oid;
764 
765  result = lappend_oid(result, oid);
766  }
767 
768  systable_endscan(scan);
770 
771  return result;
772 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:602
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:509
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:385
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_publication * Form_pg_publication
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
#define InvalidOid
Definition: postgres_ext.h:36

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 988 of file pg_publication.c.

989 {
990  HeapTuple tup;
991  Publication *pub;
992  Form_pg_publication pubform;
993 
994  tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
995  if (!HeapTupleIsValid(tup))
996  elog(ERROR, "cache lookup failed for publication %u", pubid);
997 
998  pubform = (Form_pg_publication) GETSTRUCT(tup);
999 
1000  pub = (Publication *) palloc(sizeof(Publication));
1001  pub->oid = pubid;
1002  pub->name = pstrdup(NameStr(pubform->pubname));
1003  pub->alltables = pubform->puballtables;
1004  pub->pubactions.pubinsert = pubform->pubinsert;
1005  pub->pubactions.pubupdate = pubform->pubupdate;
1006  pub->pubactions.pubdelete = pubform->pubdelete;
1007  pub->pubactions.pubtruncate = pubform->pubtruncate;
1008  pub->pubviaroot = pubform->pubviaroot;
1009 
1010  ReleaseSysCache(tup);
1011 
1012  return pub;
1013 }
#define NameStr(name)
Definition: c.h:746
#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
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
PublicationActions pubactions
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218

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

Referenced by GetPublicationByName(), pg_get_publication_tables(), publication_add_relation(), and publication_add_schema().

◆ GetPublicationByName()

Publication* GetPublicationByName ( const char *  pubname,
bool  missing_ok 
)

Definition at line 1019 of file pg_publication.c.

1020 {
1021  Oid oid;
1022 
1023  oid = get_publication_oid(pubname, missing_ok);
1024 
1025  return OidIsValid(oid) ? GetPublication(oid) : NULL;
1026 }
#define OidIsValid(objectId)
Definition: c.h:775
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 698 of file pg_publication.c.

699 {
700  List *result;
701  Relation pubrelsrel;
702  ScanKeyData scankey;
703  SysScanDesc scan;
704  HeapTuple tup;
705 
706  /* Find all publications associated with the relation. */
707  pubrelsrel = table_open(PublicationRelRelationId, AccessShareLock);
708 
709  ScanKeyInit(&scankey,
710  Anum_pg_publication_rel_prpubid,
711  BTEqualStrategyNumber, F_OIDEQ,
712  ObjectIdGetDatum(pubid));
713 
714  scan = systable_beginscan(pubrelsrel, PublicationRelPrpubidIndexId,
715  true, NULL, 1, &scankey);
716 
717  result = NIL;
718  while (HeapTupleIsValid(tup = systable_getnext(scan)))
719  {
721 
722  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
723  result = GetPubPartitionOptionRelations(result, pub_partopt,
724  pubrel->prrelid);
725  }
726 
727  systable_endscan(scan);
728  table_close(pubrelsrel, AccessShareLock);
729 
730  /* Now sort and de-duplicate the result list */
731  list_sort(result, list_oid_cmp);
732  list_deduplicate_oid(result);
733 
734  return result;
735 }
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 843 of file pg_publication.c.

844 {
845  List *result = NIL;
846  Relation pubschsrel;
847  ScanKeyData scankey;
848  SysScanDesc scan;
849  HeapTuple tup;
850 
851  /* Find all schemas associated with the publication */
852  pubschsrel = table_open(PublicationNamespaceRelationId, AccessShareLock);
853 
854  ScanKeyInit(&scankey,
855  Anum_pg_publication_namespace_pnpubid,
856  BTEqualStrategyNumber, F_OIDEQ,
857  ObjectIdGetDatum(pubid));
858 
859  scan = systable_beginscan(pubschsrel,
860  PublicationNamespacePnnspidPnpubidIndexId,
861  true, NULL, 1, &scankey);
862  while (HeapTupleIsValid(tup = systable_getnext(scan)))
863  {
865 
867 
868  result = lappend_oid(result, pubsch->pnnspid);
869  }
870 
871  systable_endscan(scan);
872  table_close(pubschsrel, AccessShareLock);
873 
874  return result;
875 }
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 264 of file pg_publication.c.

266 {
267  if (get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE &&
268  pub_partopt != PUBLICATION_PART_ROOT)
269  {
270  List *all_parts = find_all_inheritors(relid, NoLock,
271  NULL);
272 
273  if (pub_partopt == PUBLICATION_PART_ALL)
274  result = list_concat(result, all_parts);
275  else if (pub_partopt == PUBLICATION_PART_LEAF)
276  {
277  ListCell *lc;
278 
279  foreach(lc, all_parts)
280  {
281  Oid partOid = lfirst_oid(lc);
282 
283  if (get_rel_relkind(partOid) != RELKIND_PARTITIONED_TABLE)
284  result = lappend_oid(result, partOid);
285  }
286  }
287  else
288  Assert(false);
289  }
290  else
291  result = lappend_oid(result, relid);
292 
293  return result;
294 }
#define Assert(condition)
Definition: c.h:858
#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 669 of file pg_publication.c.

670 {
671  List *result = NIL;
672  CatCList *pubrellist;
673  int i;
674 
675  /* Find all publications associated with the relation. */
676  pubrellist = SearchSysCacheList1(PUBLICATIONRELMAP,
677  ObjectIdGetDatum(relid));
678  for (i = 0; i < pubrellist->n_members; i++)
679  {
680  HeapTuple tup = &pubrellist->members[i]->tuple;
681  Oid pubid = ((Form_pg_publication_rel) GETSTRUCT(tup))->prpubid;
682 
683  result = lappend_oid(result, pubid);
684  }
685 
686  ReleaseSysCacheList(pubrellist);
687 
688  return result;
689 }
int i
Definition: isn.c:73
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:129
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:122

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 907 of file pg_publication.c.

908 {
909  Relation classRel;
910  ScanKeyData key[1];
911  TableScanDesc scan;
912  HeapTuple tuple;
913  List *result = NIL;
914 
915  Assert(OidIsValid(schemaid));
916 
917  classRel = table_open(RelationRelationId, AccessShareLock);
918 
919  ScanKeyInit(&key[0],
920  Anum_pg_class_relnamespace,
921  BTEqualStrategyNumber, F_OIDEQ,
922  schemaid);
923 
924  /* get all the relations present in the specified schema */
925  scan = table_beginscan_catalog(classRel, 1, key);
926  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
927  {
928  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
929  Oid relid = relForm->oid;
930  char relkind;
931 
932  if (!is_publishable_class(relid, relForm))
933  continue;
934 
935  relkind = get_rel_relkind(relid);
936  if (relkind == RELKIND_RELATION)
937  result = lappend_oid(result, relid);
938  else if (relkind == RELKIND_PARTITIONED_TABLE)
939  {
940  List *partitionrels = NIL;
941 
942  /*
943  * It is quite possible that some of the partitions are in a
944  * different schema than the parent table, so we need to get such
945  * partitions separately.
946  */
947  partitionrels = GetPubPartitionOptionRelations(partitionrels,
948  pub_partopt,
949  relForm->oid);
950  result = list_concat_unique_oid(result, partitionrels);
951  }
952  }
953 
954  table_endscan(scan);
955  table_close(classRel, AccessShareLock);
956  return result;
957 }
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 881 of file pg_publication.c.

882 {
883  List *result = NIL;
884  CatCList *pubschlist;
885  int i;
886 
887  /* Find all publications associated with the schema */
888  pubschlist = SearchSysCacheList1(PUBLICATIONNAMESPACEMAP,
889  ObjectIdGetDatum(schemaid));
890  for (i = 0; i < pubschlist->n_members; i++)
891  {
892  HeapTuple tup = &pubschlist->members[i]->tuple;
893  Oid pubid = ((Form_pg_publication_namespace) GETSTRUCT(tup))->pnpubid;
894 
895  result = lappend_oid(result, pubid);
896  }
897 
898  ReleaseSysCacheList(pubschlist);
899 
900  return result;
901 }

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 308 of file pg_publication.c.

309 {
310  ListCell *lc;
311  Oid topmost_relid = InvalidOid;
312  int level = 0;
313 
314  /*
315  * Find the "topmost" ancestor that is in this publication.
316  */
317  foreach(lc, ancestors)
318  {
319  Oid ancestor = lfirst_oid(lc);
320  List *apubids = GetRelationPublications(ancestor);
321  List *aschemaPubids = NIL;
322 
323  level++;
324 
325  if (list_member_oid(apubids, puboid))
326  {
327  topmost_relid = ancestor;
328 
329  if (ancestor_level)
330  *ancestor_level = level;
331  }
332  else
333  {
334  aschemaPubids = GetSchemaPublications(get_rel_namespace(ancestor));
335  if (list_member_oid(aschemaPubids, puboid))
336  {
337  topmost_relid = ancestor;
338 
339  if (ancestor_level)
340  *ancestor_level = level;
341  }
342  }
343 
344  list_free(apubids);
345  list_free(aschemaPubids);
346  }
347 
348  return topmost_relid;
349 }
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 * GetSchemaPublications(Oid schemaid)
List * GetRelationPublications(Oid relid)

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

Referenced by get_rel_sync_entry(), pub_collist_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 {
149  return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
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 559 of file pg_publication.c.

560 {
561  Bitmapset *result = columns;
562  ArrayType *arr;
563  int nelems;
564  int16 *elems;
565  MemoryContext oldcxt = NULL;
566 
567  arr = DatumGetArrayTypeP(pubcols);
568  nelems = ARR_DIMS(arr)[0];
569  elems = (int16 *) ARR_DATA_PTR(arr);
570 
571  /* If a memory context was specified, switch to it. */
572  if (mcxt)
573  oldcxt = MemoryContextSwitchTo(mcxt);
574 
575  for (int i = 0; i < nelems; i++)
576  result = bms_add_member(result, elems[i]);
577 
578  if (mcxt)
579  MemoryContextSwitchTo(oldcxt);
580 
581  return result;
582 }
#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
signed short int16
Definition: c.h:493
MemoryContextSwitchTo(old_ctx)

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

Referenced by AlterPublicationTables(), pgoutput_column_list_init(), and pub_collist_contains_invalid_column().

◆ pub_collist_validate()

Bitmapset* pub_collist_validate ( Relation  targetrel,
List columns 
)

Definition at line 510 of file pg_publication.c.

511 {
512  Bitmapset *set = NULL;
513  ListCell *lc;
514  TupleDesc tupdesc = RelationGetDescr(targetrel);
515 
516  foreach(lc, columns)
517  {
518  char *colname = strVal(lfirst(lc));
519  AttrNumber attnum = get_attnum(RelationGetRelid(targetrel), colname);
520 
521  if (attnum == InvalidAttrNumber)
522  ereport(ERROR,
523  errcode(ERRCODE_UNDEFINED_COLUMN),
524  errmsg("column \"%s\" of relation \"%s\" does not exist",
525  colname, RelationGetRelationName(targetrel)));
526 
528  ereport(ERROR,
529  errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
530  errmsg("cannot use system column \"%s\" in publication column list",
531  colname));
532 
533  if (TupleDescAttr(tupdesc, attnum - 1)->attgenerated)
534  ereport(ERROR,
535  errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
536  errmsg("cannot use generated column \"%s\" in publication column list",
537  colname));
538 
539  if (bms_is_member(attnum, set))
540  ereport(ERROR,
542  errmsg("duplicate column \"%s\" in publication column list",
543  colname));
544 
545  set = bms_add_member(set, attnum);
546  }
547 
548  return set;
549 }
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 RelationGetDescr(relation)
Definition: rel.h:531
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#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, RelationGetDescr, RelationGetRelationName, RelationGetRelid, strVal, and TupleDescAttr.

Referenced by AlterPublicationTables(), and publication_add_relation().

◆ publication_add_relation()

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

Definition at line 382 of file pg_publication.c.

384 {
385  Relation rel;
386  HeapTuple tup;
387  Datum values[Natts_pg_publication_rel];
388  bool nulls[Natts_pg_publication_rel];
389  Relation targetrel = pri->relation;
390  Oid relid = RelationGetRelid(targetrel);
391  Oid pubreloid;
392  Bitmapset *attnums;
393  Publication *pub = GetPublication(pubid);
394  ObjectAddress myself,
395  referenced;
396  List *relids = NIL;
397  int i;
398 
399  rel = table_open(PublicationRelRelationId, RowExclusiveLock);
400 
401  /*
402  * Check for duplicates. Note that this does not really prevent
403  * duplicates, it's here just to provide nicer error message in common
404  * case. The real protection is the unique key on the catalog.
405  */
406  if (SearchSysCacheExists2(PUBLICATIONRELMAP, ObjectIdGetDatum(relid),
407  ObjectIdGetDatum(pubid)))
408  {
410 
411  if (if_not_exists)
412  return InvalidObjectAddress;
413 
414  ereport(ERROR,
416  errmsg("relation \"%s\" is already member of publication \"%s\"",
417  RelationGetRelationName(targetrel), pub->name)));
418  }
419 
421 
422  /* Validate and translate column names into a Bitmapset of attnums. */
423  attnums = pub_collist_validate(pri->relation, pri->columns);
424 
425  /* Form a tuple. */
426  memset(values, 0, sizeof(values));
427  memset(nulls, false, sizeof(nulls));
428 
429  pubreloid = GetNewOidWithIndex(rel, PublicationRelObjectIndexId,
430  Anum_pg_publication_rel_oid);
431  values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(pubreloid);
432  values[Anum_pg_publication_rel_prpubid - 1] =
433  ObjectIdGetDatum(pubid);
434  values[Anum_pg_publication_rel_prrelid - 1] =
435  ObjectIdGetDatum(relid);
436 
437  /* Add qualifications, if available */
438  if (pri->whereClause != NULL)
439  values[Anum_pg_publication_rel_prqual - 1] = CStringGetTextDatum(nodeToString(pri->whereClause));
440  else
441  nulls[Anum_pg_publication_rel_prqual - 1] = true;
442 
443  /* Add column list, if available */
444  if (pri->columns)
445  values[Anum_pg_publication_rel_prattrs - 1] = PointerGetDatum(attnumstoint2vector(attnums));
446  else
447  nulls[Anum_pg_publication_rel_prattrs - 1] = true;
448 
449  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
450 
451  /* Insert tuple into catalog. */
452  CatalogTupleInsert(rel, tup);
453  heap_freetuple(tup);
454 
455  /* Register dependencies as needed */
456  ObjectAddressSet(myself, PublicationRelRelationId, pubreloid);
457 
458  /* Add dependency on the publication */
459  ObjectAddressSet(referenced, PublicationRelationId, pubid);
460  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
461 
462  /* Add dependency on the relation */
463  ObjectAddressSet(referenced, RelationRelationId, relid);
464  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
465 
466  /* Add dependency on the objects mentioned in the qualifications */
467  if (pri->whereClause)
468  recordDependencyOnSingleRelExpr(&myself, pri->whereClause, relid,
470  false);
471 
472  /* Add dependency on the columns, if any are listed */
473  i = -1;
474  while ((i = bms_next_member(attnums, i)) >= 0)
475  {
476  ObjectAddressSubSet(referenced, RelationRelationId, relid, i);
477  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
478  }
479 
480  /* Close the table. */
482 
483  /*
484  * Invalidate relcache so that publication info is rebuilt.
485  *
486  * For the partitioned tables, we must invalidate all partitions contained
487  * in the respective partition hierarchies, not just the one explicitly
488  * mentioned in the publication. This is required because we implicitly
489  * publish the child tables when the parent table is published.
490  */
492  relid);
493 
495 
496  return myself;
497 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
static Datum values[MAXATTR]
Definition: bootstrap.c:150
#define CStringGetTextDatum(s)
Definition: builtins.h:97
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:412
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:1116
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
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:46
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:322
uintptr_t Datum
Definition: postgres.h:64
void InvalidatePublicationRels(List *relids)
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:97

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 588 of file pg_publication.c.

589 {
590  Relation rel;
591  HeapTuple tup;
592  Datum values[Natts_pg_publication_namespace];
593  bool nulls[Natts_pg_publication_namespace];
594  Oid psschid;
595  Publication *pub = GetPublication(pubid);
596  List *schemaRels = NIL;
597  ObjectAddress myself,
598  referenced;
599 
600  rel = table_open(PublicationNamespaceRelationId, RowExclusiveLock);
601 
602  /*
603  * Check for duplicates. Note that this does not really prevent
604  * duplicates, it's here just to provide nicer error message in common
605  * case. The real protection is the unique key on the catalog.
606  */
607  if (SearchSysCacheExists2(PUBLICATIONNAMESPACEMAP,
608  ObjectIdGetDatum(schemaid),
609  ObjectIdGetDatum(pubid)))
610  {
612 
613  if (if_not_exists)
614  return InvalidObjectAddress;
615 
616  ereport(ERROR,
618  errmsg("schema \"%s\" is already member of publication \"%s\"",
619  get_namespace_name(schemaid), pub->name)));
620  }
621 
623 
624  /* Form a tuple */
625  memset(values, 0, sizeof(values));
626  memset(nulls, false, sizeof(nulls));
627 
628  psschid = GetNewOidWithIndex(rel, PublicationNamespaceObjectIndexId,
629  Anum_pg_publication_namespace_oid);
630  values[Anum_pg_publication_namespace_oid - 1] = ObjectIdGetDatum(psschid);
631  values[Anum_pg_publication_namespace_pnpubid - 1] =
632  ObjectIdGetDatum(pubid);
633  values[Anum_pg_publication_namespace_pnnspid - 1] =
634  ObjectIdGetDatum(schemaid);
635 
636  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
637 
638  /* Insert tuple into catalog */
639  CatalogTupleInsert(rel, tup);
640  heap_freetuple(tup);
641 
642  ObjectAddressSet(myself, PublicationNamespaceRelationId, psschid);
643 
644  /* Add dependency on the publication */
645  ObjectAddressSet(referenced, PublicationRelationId, pubid);
646  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
647 
648  /* Add dependency on the schema */
649  ObjectAddressSet(referenced, NamespaceRelationId, schemaid);
650  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
651 
652  /* Close the table */
654 
655  /*
656  * Invalidate relcache so that publication info is rebuilt. See
657  * publication_add_relation for why we need to consider all the
658  * partitions.
659  */
660  schemaRels = GetSchemaPublicationRelations(schemaid,
662  InvalidatePublicationRels(schemaRels);
663 
664  return myself;
665 }
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 57 of file pg_publication.h.