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

983 {
984  List *result = NIL;
985  List *pubschemalist = GetPublicationSchemas(pubid);
986  ListCell *cell;
987 
988  foreach(cell, pubschemalist)
989  {
990  Oid schemaid = lfirst_oid(cell);
991  List *schemaRels = NIL;
992 
993  schemaRels = GetSchemaPublicationRelations(schemaid, pub_partopt);
994  result = list_concat(result, schemaRels);
995  }
996 
997  return result;
998 }
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 800 of file pg_publication.c.

801 {
802  Relation classRel;
803  ScanKeyData key[1];
804  TableScanDesc scan;
805  HeapTuple tuple;
806  List *result = NIL;
807 
808  classRel = table_open(RelationRelationId, AccessShareLock);
809 
810  ScanKeyInit(&key[0],
811  Anum_pg_class_relkind,
812  BTEqualStrategyNumber, F_CHAREQ,
813  CharGetDatum(RELKIND_RELATION));
814 
815  scan = table_beginscan_catalog(classRel, 1, key);
816 
817  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
818  {
819  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
820  Oid relid = relForm->oid;
821 
822  if (is_publishable_class(relid, relForm) &&
823  !(relForm->relispartition && pubviaroot))
824  result = lappend_oid(result, relid);
825  }
826 
827  table_endscan(scan);
828 
829  if (pubviaroot)
830  {
831  ScanKeyInit(&key[0],
832  Anum_pg_class_relkind,
833  BTEqualStrategyNumber, F_CHAREQ,
834  CharGetDatum(RELKIND_PARTITIONED_TABLE));
835 
836  scan = table_beginscan_catalog(classRel, 1, key);
837 
838  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
839  {
840  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
841  Oid relid = relForm->oid;
842 
843  if (is_publishable_class(relid, relForm) &&
844  !relForm->relispartition)
845  result = lappend_oid(result, relid);
846  }
847 
848  table_endscan(scan);
849  }
850 
851  table_close(classRel, AccessShareLock);
852  return result;
853 }
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1251
#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 759 of file pg_publication.c.

760 {
761  List *result;
762  Relation rel;
763  ScanKeyData scankey;
764  SysScanDesc scan;
765  HeapTuple tup;
766 
767  /* Find all publications that are marked as for all tables. */
768  rel = table_open(PublicationRelationId, AccessShareLock);
769 
770  ScanKeyInit(&scankey,
771  Anum_pg_publication_puballtables,
772  BTEqualStrategyNumber, F_BOOLEQ,
773  BoolGetDatum(true));
774 
775  scan = systable_beginscan(rel, InvalidOid, false,
776  NULL, 1, &scankey);
777 
778  result = NIL;
779  while (HeapTupleIsValid(tup = systable_getnext(scan)))
780  {
781  Oid oid = ((Form_pg_publication) GETSTRUCT(tup))->oid;
782 
783  result = lappend_oid(result, oid);
784  }
785 
786  systable_endscan(scan);
788 
789  return result;
790 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:596
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:503
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:384
#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 1006 of file pg_publication.c.

1007 {
1008  HeapTuple tup;
1009  Publication *pub;
1010  Form_pg_publication pubform;
1011 
1012  tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
1013  if (!HeapTupleIsValid(tup))
1014  elog(ERROR, "cache lookup failed for publication %u", pubid);
1015 
1016  pubform = (Form_pg_publication) GETSTRUCT(tup);
1017 
1018  pub = (Publication *) palloc(sizeof(Publication));
1019  pub->oid = pubid;
1020  pub->name = pstrdup(NameStr(pubform->pubname));
1021  pub->alltables = pubform->puballtables;
1022  pub->pubactions.pubinsert = pubform->pubinsert;
1023  pub->pubactions.pubupdate = pubform->pubupdate;
1024  pub->pubactions.pubdelete = pubform->pubdelete;
1025  pub->pubactions.pubtruncate = pubform->pubtruncate;
1026  pub->pubviaroot = pubform->pubviaroot;
1027 
1028  ReleaseSysCache(tup);
1029 
1030  return pub;
1031 }
#define NameStr(name)
Definition: c.h:746
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void * palloc(Size size)
Definition: mcxt.c:1316
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 1037 of file pg_publication.c.

1038 {
1039  Oid oid;
1040 
1041  oid = get_publication_oid(pubname, missing_ok);
1042 
1043  return OidIsValid(oid) ? GetPublication(oid) : NULL;
1044 }
#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 716 of file pg_publication.c.

717 {
718  List *result;
719  Relation pubrelsrel;
720  ScanKeyData scankey;
721  SysScanDesc scan;
722  HeapTuple tup;
723 
724  /* Find all publications associated with the relation. */
725  pubrelsrel = table_open(PublicationRelRelationId, AccessShareLock);
726 
727  ScanKeyInit(&scankey,
728  Anum_pg_publication_rel_prpubid,
729  BTEqualStrategyNumber, F_OIDEQ,
730  ObjectIdGetDatum(pubid));
731 
732  scan = systable_beginscan(pubrelsrel, PublicationRelPrpubidIndexId,
733  true, NULL, 1, &scankey);
734 
735  result = NIL;
736  while (HeapTupleIsValid(tup = systable_getnext(scan)))
737  {
739 
740  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
741  result = GetPubPartitionOptionRelations(result, pub_partopt,
742  pubrel->prrelid);
743  }
744 
745  systable_endscan(scan);
746  table_close(pubrelsrel, AccessShareLock);
747 
748  /* Now sort and de-duplicate the result list */
749  list_sort(result, list_oid_cmp);
750  list_deduplicate_oid(result);
751 
752  return result;
753 }
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 861 of file pg_publication.c.

862 {
863  List *result = NIL;
864  Relation pubschsrel;
865  ScanKeyData scankey;
866  SysScanDesc scan;
867  HeapTuple tup;
868 
869  /* Find all schemas associated with the publication */
870  pubschsrel = table_open(PublicationNamespaceRelationId, AccessShareLock);
871 
872  ScanKeyInit(&scankey,
873  Anum_pg_publication_namespace_pnpubid,
874  BTEqualStrategyNumber, F_OIDEQ,
875  ObjectIdGetDatum(pubid));
876 
877  scan = systable_beginscan(pubschsrel,
878  PublicationNamespacePnnspidPnpubidIndexId,
879  true, NULL, 1, &scankey);
880  while (HeapTupleIsValid(tup = systable_getnext(scan)))
881  {
883 
885 
886  result = lappend_oid(result, pubsch->pnnspid);
887  }
888 
889  systable_endscan(scan);
890  table_close(pubschsrel, AccessShareLock);
891 
892  return result;
893 }
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 267 of file pg_publication.c.

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

688 {
689  List *result = NIL;
690  CatCList *pubrellist;
691  int i;
692 
693  /* Find all publications associated with the relation. */
694  pubrellist = SearchSysCacheList1(PUBLICATIONRELMAP,
695  ObjectIdGetDatum(relid));
696  for (i = 0; i < pubrellist->n_members; i++)
697  {
698  HeapTuple tup = &pubrellist->members[i]->tuple;
699  Oid pubid = ((Form_pg_publication_rel) GETSTRUCT(tup))->prpubid;
700 
701  result = lappend_oid(result, pubid);
702  }
703 
704  ReleaseSysCacheList(pubrellist);
705 
706  return result;
707 }
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 925 of file pg_publication.c.

926 {
927  Relation classRel;
928  ScanKeyData key[1];
929  TableScanDesc scan;
930  HeapTuple tuple;
931  List *result = NIL;
932 
933  Assert(OidIsValid(schemaid));
934 
935  classRel = table_open(RelationRelationId, AccessShareLock);
936 
937  ScanKeyInit(&key[0],
938  Anum_pg_class_relnamespace,
939  BTEqualStrategyNumber, F_OIDEQ,
940  schemaid);
941 
942  /* get all the relations present in the specified schema */
943  scan = table_beginscan_catalog(classRel, 1, key);
944  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
945  {
946  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
947  Oid relid = relForm->oid;
948  char relkind;
949 
950  if (!is_publishable_class(relid, relForm))
951  continue;
952 
953  relkind = get_rel_relkind(relid);
954  if (relkind == RELKIND_RELATION)
955  result = lappend_oid(result, relid);
956  else if (relkind == RELKIND_PARTITIONED_TABLE)
957  {
958  List *partitionrels = NIL;
959 
960  /*
961  * It is quite possible that some of the partitions are in a
962  * different schema than the parent table, so we need to get such
963  * partitions separately.
964  */
965  partitionrels = GetPubPartitionOptionRelations(partitionrels,
966  pub_partopt,
967  relForm->oid);
968  result = list_concat_unique_oid(result, partitionrels);
969  }
970  }
971 
972  table_endscan(scan);
973  table_close(classRel, AccessShareLock);
974  return result;
975 }
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 899 of file pg_publication.c.

900 {
901  List *result = NIL;
902  CatCList *pubschlist;
903  int i;
904 
905  /* Find all publications associated with the schema */
906  pubschlist = SearchSysCacheList1(PUBLICATIONNAMESPACEMAP,
907  ObjectIdGetDatum(schemaid));
908  for (i = 0; i < pubschlist->n_members; i++)
909  {
910  HeapTuple tup = &pubschlist->members[i]->tuple;
911  Oid pubid = ((Form_pg_publication_namespace) GETSTRUCT(tup))->pnpubid;
912 
913  result = lappend_oid(result, pubid);
914  }
915 
916  ReleaseSysCacheList(pubschlist);
917 
918  return result;
919 }

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

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

151 {
152  return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
153 }
#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 236 of file pg_publication.c.

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

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

571 {
572  Bitmapset *result = NULL;
573  ArrayType *arr;
574  int nelems;
575  int16 *elems;
576  MemoryContext oldcxt = NULL;
577 
578  /*
579  * If an existing bitmap was provided, use it. Otherwise just use NULL and
580  * build a new bitmap.
581  */
582  if (columns)
583  result = columns;
584 
585  arr = DatumGetArrayTypeP(pubcols);
586  nelems = ARR_DIMS(arr)[0];
587  elems = (int16 *) ARR_DATA_PTR(arr);
588 
589  /* If a memory context was specified, switch to it. */
590  if (mcxt)
591  oldcxt = MemoryContextSwitchTo(mcxt);
592 
593  for (int i = 0; i < nelems; i++)
594  result = bms_add_member(result, elems[i]);
595 
596  if (mcxt)
597  MemoryContextSwitchTo(oldcxt);
598 
599  return result;
600 }
#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().

◆ publication_add_relation()

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

Definition at line 358 of file pg_publication.c.

360 {
361  Relation rel;
362  HeapTuple tup;
363  Datum values[Natts_pg_publication_rel];
364  bool nulls[Natts_pg_publication_rel];
365  Relation targetrel = pri->relation;
366  Oid relid = RelationGetRelid(targetrel);
367  Oid pubreloid;
368  Publication *pub = GetPublication(pubid);
369  AttrNumber *attarray = NULL;
370  int natts = 0;
371  ObjectAddress myself,
372  referenced;
373  List *relids = NIL;
374 
375  rel = table_open(PublicationRelRelationId, RowExclusiveLock);
376 
377  /*
378  * Check for duplicates. Note that this does not really prevent
379  * duplicates, it's here just to provide nicer error message in common
380  * case. The real protection is the unique key on the catalog.
381  */
382  if (SearchSysCacheExists2(PUBLICATIONRELMAP, ObjectIdGetDatum(relid),
383  ObjectIdGetDatum(pubid)))
384  {
386 
387  if (if_not_exists)
388  return InvalidObjectAddress;
389 
390  ereport(ERROR,
392  errmsg("relation \"%s\" is already member of publication \"%s\"",
393  RelationGetRelationName(targetrel), pub->name)));
394  }
395 
397 
398  /*
399  * Translate column names to attnums and make sure the column list
400  * contains only allowed elements (no system or generated columns etc.).
401  * Also build an array of attnums, for storing in the catalog.
402  */
404  &natts, &attarray);
405 
406  /* Form a tuple. */
407  memset(values, 0, sizeof(values));
408  memset(nulls, false, sizeof(nulls));
409 
410  pubreloid = GetNewOidWithIndex(rel, PublicationRelObjectIndexId,
411  Anum_pg_publication_rel_oid);
412  values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(pubreloid);
413  values[Anum_pg_publication_rel_prpubid - 1] =
414  ObjectIdGetDatum(pubid);
415  values[Anum_pg_publication_rel_prrelid - 1] =
416  ObjectIdGetDatum(relid);
417 
418  /* Add qualifications, if available */
419  if (pri->whereClause != NULL)
420  values[Anum_pg_publication_rel_prqual - 1] = CStringGetTextDatum(nodeToString(pri->whereClause));
421  else
422  nulls[Anum_pg_publication_rel_prqual - 1] = true;
423 
424  /* Add column list, if available */
425  if (pri->columns)
426  values[Anum_pg_publication_rel_prattrs - 1] = PointerGetDatum(buildint2vector(attarray, natts));
427  else
428  nulls[Anum_pg_publication_rel_prattrs - 1] = true;
429 
430  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
431 
432  /* Insert tuple into catalog. */
433  CatalogTupleInsert(rel, tup);
434  heap_freetuple(tup);
435 
436  /* Register dependencies as needed */
437  ObjectAddressSet(myself, PublicationRelRelationId, pubreloid);
438 
439  /* Add dependency on the publication */
440  ObjectAddressSet(referenced, PublicationRelationId, pubid);
441  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
442 
443  /* Add dependency on the relation */
444  ObjectAddressSet(referenced, RelationRelationId, relid);
445  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
446 
447  /* Add dependency on the objects mentioned in the qualifications */
448  if (pri->whereClause)
449  recordDependencyOnSingleRelExpr(&myself, pri->whereClause, relid,
451  false);
452 
453  /* Add dependency on the columns, if any are listed */
454  for (int i = 0; i < natts; i++)
455  {
456  ObjectAddressSubSet(referenced, RelationRelationId, relid, attarray[i]);
457  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
458  }
459 
460  /* Close the table. */
462 
463  /*
464  * Invalidate relcache so that publication info is rebuilt.
465  *
466  * For the partitioned tables, we must invalidate all partitions contained
467  * in the respective partition hierarchies, not just the one explicitly
468  * mentioned in the publication. This is required because we implicitly
469  * publish the child tables when the parent table is published.
470  */
472  relid);
473 
475 
476  return myself;
477 }
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define CStringGetTextDatum(s)
Definition: builtins.h:97
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:391
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
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
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
int2vector * buildint2vector(const int16 *int2s, int n)
Definition: int.c:114
#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:791
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:46
static void check_publication_add_relation(Relation targetrel)
static void publication_translate_columns(Relation targetrel, List *columns, int *natts, AttrNumber **attrs)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
void InvalidatePublicationRels(List *relids)
#define RelationGetDescr(relation)
Definition: rel.h:531
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:97

References buildint2vector(), 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(), PUBLICATION_PART_ALL, publication_translate_columns(), 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 606 of file pg_publication.c.

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