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  Publication
 
struct  PublicationRelInfo
 

Typedefs

typedef FormData_pg_publicationForm_pg_publication
 
typedef struct PublicationActions PublicationActions
 
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, on pg_publication using btree(oid oid_ops))
 
 DECLARE_UNIQUE_INDEX (pg_publication_pubname_index, 6111, PublicationNameIndexId, on pg_publication using btree(pubname name_ops))
 
PublicationGetPublication (Oid pubid)
 
PublicationGetPublicationByName (const char *pubname, bool missing_ok)
 
ListGetRelationPublications (Oid relid)
 
ListGetPublicationRelations (Oid pubid, PublicationPartOpt pub_partopt)
 
ListGetAllTablesPublications (void)
 
ListGetAllTablesPublicationRelations (bool pubviaroot)
 
ListGetPubPartitionOptionRelations (List *result, PublicationPartOpt pub_partopt, Oid relid)
 
bool is_publishable_relation (Relation rel)
 
ObjectAddress publication_add_relation (Oid pubid, PublicationRelInfo *targetrel, bool if_not_exists)
 
Oid get_publication_oid (const char *pubname, bool missing_ok)
 
char * get_publication_name (Oid pubid, bool missing_ok)
 

Variables

 FormData_pg_publication
 

Typedef Documentation

◆ Form_pg_publication

Definition at line 64 of file pg_publication.h.

◆ Publication

typedef struct Publication Publication

◆ PublicationActions

◆ PublicationPartOpt

◆ PublicationRelInfo

Enumeration Type Documentation

◆ PublicationPartOpt

Enumerator
PUBLICATION_PART_ROOT 
PUBLICATION_PART_LEAF 
PUBLICATION_PART_ALL 

Definition at line 104 of file pg_publication.h.

Function Documentation

◆ CATALOG()

CATALOG ( pg_publication  ,
6104  ,
PublicationRelationId   
)

Definition at line 29 of file pg_publication.h.

References BKI_LOOKUP.

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

◆ DECLARE_UNIQUE_INDEX()

DECLARE_UNIQUE_INDEX ( pg_publication_pubname_index  ,
6111  ,
PublicationNameIndexId  ,
on pg_publication using   btreepubname name_ops 
)

◆ DECLARE_UNIQUE_INDEX_PKEY()

DECLARE_UNIQUE_INDEX_PKEY ( pg_publication_oid_index  ,
6110  ,
PublicationObjectIndexId  ,
on pg_publication using   btreeoid oid_ops 
)

◆ get_publication_name()

char* get_publication_name ( Oid  pubid,
bool  missing_ok 
)

Definition at line 505 of file pg_publication.c.

References elog, ERROR, GETSTRUCT, HeapTupleIsValid, NameStr, ObjectIdGetDatum, pstrdup(), PUBLICATIONOID, ReleaseSysCache(), and SearchSysCache1().

Referenced by getObjectDescription(), and getObjectIdentityParts().

506 {
507  HeapTuple tup;
508  char *pubname;
509  Form_pg_publication pubform;
510 
512 
513  if (!HeapTupleIsValid(tup))
514  {
515  if (!missing_ok)
516  elog(ERROR, "cache lookup failed for publication %u", pubid);
517  return NULL;
518  }
519 
520  pubform = (Form_pg_publication) GETSTRUCT(tup);
521  pubname = pstrdup(NameStr(pubform->pubname));
522 
523  ReleaseSysCache(tup);
524 
525  return pubname;
526 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
FormData_pg_publication * Form_pg_publication

◆ get_publication_oid()

Oid get_publication_oid ( const char *  pubname,
bool  missing_ok 
)

Definition at line 485 of file pg_publication.c.

References CStringGetDatum, ereport, errcode(), errmsg(), ERROR, GetSysCacheOid1, OidIsValid, and PUBLICATIONNAME.

Referenced by get_object_address_unqualified(), and GetPublicationByName().

486 {
487  Oid oid;
488 
489  oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
490  CStringGetDatum(pubname));
491  if (!OidIsValid(oid) && !missing_ok)
492  ereport(ERROR,
493  (errcode(ERRCODE_UNDEFINED_OBJECT),
494  errmsg("publication \"%s\" does not exist", pubname)));
495  return oid;
496 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:193
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
#define CStringGetDatum(X)
Definition: postgres.h:622
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ GetAllTablesPublicationRelations()

List* GetAllTablesPublicationRelations ( bool  pubviaroot)

Definition at line 376 of file pg_publication.c.

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

377 {
378  Relation classRel;
379  ScanKeyData key[1];
380  TableScanDesc scan;
381  HeapTuple tuple;
382  List *result = NIL;
383 
384  classRel = table_open(RelationRelationId, AccessShareLock);
385 
386  ScanKeyInit(&key[0],
387  Anum_pg_class_relkind,
388  BTEqualStrategyNumber, F_CHAREQ,
389  CharGetDatum(RELKIND_RELATION));
390 
391  scan = table_beginscan_catalog(classRel, 1, key);
392 
393  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
394  {
395  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
396  Oid relid = relForm->oid;
397 
398  if (is_publishable_class(relid, relForm) &&
399  !(relForm->relispartition && pubviaroot))
400  result = lappend_oid(result, relid);
401  }
402 
403  table_endscan(scan);
404 
405  if (pubviaroot)
406  {
407  ScanKeyInit(&key[0],
408  Anum_pg_class_relkind,
409  BTEqualStrategyNumber, F_CHAREQ,
410  CharGetDatum(RELKIND_PARTITIONED_TABLE));
411 
412  scan = table_beginscan_catalog(classRel, 1, key);
413 
414  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
415  {
416  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
417  Oid relid = relForm->oid;
418 
419  if (is_publishable_class(relid, relForm) &&
420  !relForm->relispartition)
421  result = lappend_oid(result, relid);
422  }
423 
424  table_endscan(scan);
425  }
426 
427  table_close(classRel, AccessShareLock);
428  return result;
429 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1340
#define CharGetDatum(X)
Definition: postgres.h:460
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:991
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
static bool is_publishable_class(Oid relid, Form_pg_class reltuple)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetAllTablesPublications()

List* GetAllTablesPublications ( void  )

Definition at line 335 of file pg_publication.c.

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

Referenced by GetRelationPublicationActions().

336 {
337  List *result;
338  Relation rel;
339  ScanKeyData scankey;
340  SysScanDesc scan;
341  HeapTuple tup;
342 
343  /* Find all publications that are marked as for all tables. */
344  rel = table_open(PublicationRelationId, AccessShareLock);
345 
346  ScanKeyInit(&scankey,
347  Anum_pg_publication_puballtables,
348  BTEqualStrategyNumber, F_BOOLEQ,
349  BoolGetDatum(true));
350 
351  scan = systable_beginscan(rel, InvalidOid, false,
352  NULL, 1, &scankey);
353 
354  result = NIL;
355  while (HeapTupleIsValid(tup = systable_getnext(scan)))
356  {
357  Oid oid = ((Form_pg_publication) GETSTRUCT(tup))->oid;
358 
359  result = lappend_oid(result, oid);
360  }
361 
362  systable_endscan(scan);
364 
365  return result;
366 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define BoolGetDatum(X)
Definition: postgres.h:446
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
FormData_pg_publication * Form_pg_publication
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetPublication()

Publication* GetPublication ( Oid  pubid)

Definition at line 437 of file pg_publication.c.

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

Referenced by GetPublicationByName(), and publication_add_relation().

438 {
439  HeapTuple tup;
440  Publication *pub;
441  Form_pg_publication pubform;
442 
444  if (!HeapTupleIsValid(tup))
445  elog(ERROR, "cache lookup failed for publication %u", pubid);
446 
447  pubform = (Form_pg_publication) GETSTRUCT(tup);
448 
449  pub = (Publication *) palloc(sizeof(Publication));
450  pub->oid = pubid;
451  pub->name = pstrdup(NameStr(pubform->pubname));
452  pub->alltables = pubform->puballtables;
453  pub->pubactions.pubinsert = pubform->pubinsert;
454  pub->pubactions.pubupdate = pubform->pubupdate;
455  pub->pubactions.pubdelete = pubform->pubdelete;
456  pub->pubactions.pubtruncate = pubform->pubtruncate;
457  pub->pubviaroot = pubform->pubviaroot;
458 
459  ReleaseSysCache(tup);
460 
461  return pub;
462 }
PublicationActions pubactions
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
FormData_pg_publication * Form_pg_publication

◆ GetPublicationByName()

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

Definition at line 469 of file pg_publication.c.

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

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

470 {
471  Oid oid;
472 
473  oid = get_publication_oid(pubname, missing_ok);
474 
475  return OidIsValid(oid) ? GetPublication(oid) : NULL;
476 }
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
Publication * GetPublication(Oid pubid)
Oid get_publication_oid(const char *pubname, bool missing_ok)

◆ GetPublicationRelations()

List* GetPublicationRelations ( Oid  pubid,
PublicationPartOpt  pub_partopt 
)

Definition at line 296 of file pg_publication.c.

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

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

297 {
298  List *result;
299  Relation pubrelsrel;
300  ScanKeyData scankey;
301  SysScanDesc scan;
302  HeapTuple tup;
303 
304  /* Find all publications associated with the relation. */
305  pubrelsrel = table_open(PublicationRelRelationId, AccessShareLock);
306 
307  ScanKeyInit(&scankey,
308  Anum_pg_publication_rel_prpubid,
309  BTEqualStrategyNumber, F_OIDEQ,
310  ObjectIdGetDatum(pubid));
311 
312  scan = systable_beginscan(pubrelsrel, PublicationRelPrrelidPrpubidIndexId,
313  true, NULL, 1, &scankey);
314 
315  result = NIL;
316  while (HeapTupleIsValid(tup = systable_getnext(scan)))
317  {
319 
320  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
321  result = GetPubPartitionOptionRelations(result, pub_partopt,
322  pubrel->prrelid);
323  }
324 
325  systable_endscan(scan);
326  table_close(pubrelsrel, AccessShareLock);
327 
328  return result;
329 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define AccessShareLock
Definition: lockdefs.h:36
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
List * GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt, Oid relid)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_publication_rel * Form_pg_publication_rel
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ GetPubPartitionOptionRelations()

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

Definition at line 145 of file pg_publication.c.

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 GetPublicationRelations(), publication_add_relation(), and RemovePublicationRelById().

147 {
148  if (get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE &&
149  pub_partopt != PUBLICATION_PART_ROOT)
150  {
151  List *all_parts = find_all_inheritors(relid, NoLock,
152  NULL);
153 
154  if (pub_partopt == PUBLICATION_PART_ALL)
155  result = list_concat(result, all_parts);
156  else if (pub_partopt == PUBLICATION_PART_LEAF)
157  {
158  ListCell *lc;
159 
160  foreach(lc, all_parts)
161  {
162  Oid partOid = lfirst_oid(lc);
163 
164  if (get_rel_relkind(partOid) != RELKIND_PARTITIONED_TABLE)
165  result = lappend_oid(result, partOid);
166  }
167  }
168  else
169  Assert(false);
170  }
171  else
172  result = lappend_oid(result, relid);
173 
174  return result;
175 }
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1974
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define NoLock
Definition: lockdefs.h:34
#define Assert(condition)
Definition: c.h:804
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:256
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ GetRelationPublications()

List* GetRelationPublications ( Oid  relid)

Definition at line 267 of file pg_publication.c.

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

Referenced by ATPrepChangePersistence(), get_rel_sync_entry(), and GetRelationPublicationActions().

268 {
269  List *result = NIL;
270  CatCList *pubrellist;
271  int i;
272 
273  /* Find all publications associated with the relation. */
275  ObjectIdGetDatum(relid));
276  for (i = 0; i < pubrellist->n_members; i++)
277  {
278  HeapTuple tup = &pubrellist->members[i]->tuple;
279  Oid pubid = ((Form_pg_publication_rel) GETSTRUCT(tup))->prpubid;
280 
281  result = lappend_oid(result, pubid);
282  }
283 
284  ReleaseSysCacheList(pubrellist);
285 
286  return result;
287 }
#define NIL
Definition: pg_list.h:65
int n_members
Definition: catcache.h:176
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:178
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:211
#define ReleaseSysCacheList(x)
Definition: syscache.h:218
FormData_pg_publication_rel * Form_pg_publication_rel
int i
HeapTupleData tuple
Definition: catcache.h:121
Definition: pg_list.h:50

◆ is_publishable_relation()

bool is_publishable_relation ( Relation  rel)

Definition at line 112 of file pg_publication.c.

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

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

113 {
114  return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
115 }
Form_pg_class rd_rel
Definition: rel.h:109
static bool is_publishable_class(Oid relid, Form_pg_class reltuple)
#define RelationGetRelid(relation)
Definition: rel.h:477

◆ publication_add_relation()

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

Definition at line 181 of file pg_publication.c.

References CatalogTupleInsert(), check_publication_add_relation(), DEPENDENCY_AUTO, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, GetNewOidWithIndex(), GetPublication(), GetPubPartitionOptionRelations(), heap_form_tuple(), heap_freetuple(), InvalidatePublicationRels(), InvalidObjectAddress, Publication::name, NIL, ObjectAddressSet, ObjectIdGetDatum, PUBLICATION_PART_ALL, PUBLICATIONRELMAP, recordDependencyOn(), PublicationRelInfo::relation, RelationGetDescr, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, SearchSysCacheExists2, table_close(), table_open(), and values.

Referenced by PublicationAddTables().

183 {
184  Relation rel;
185  HeapTuple tup;
186  Datum values[Natts_pg_publication_rel];
187  bool nulls[Natts_pg_publication_rel];
188  Oid relid = RelationGetRelid(targetrel->relation);
189  Oid prrelid;
190  Publication *pub = GetPublication(pubid);
191  ObjectAddress myself,
192  referenced;
193  List *relids = NIL;
194 
195  rel = table_open(PublicationRelRelationId, RowExclusiveLock);
196 
197  /*
198  * Check for duplicates. Note that this does not really prevent
199  * duplicates, it's here just to provide nicer error message in common
200  * case. The real protection is the unique key on the catalog.
201  */
203  ObjectIdGetDatum(pubid)))
204  {
206 
207  if (if_not_exists)
208  return InvalidObjectAddress;
209 
210  ereport(ERROR,
212  errmsg("relation \"%s\" is already member of publication \"%s\"",
213  RelationGetRelationName(targetrel->relation), pub->name)));
214  }
215 
217 
218  /* Form a tuple. */
219  memset(values, 0, sizeof(values));
220  memset(nulls, false, sizeof(nulls));
221 
222  prrelid = GetNewOidWithIndex(rel, PublicationRelObjectIndexId,
223  Anum_pg_publication_rel_oid);
224  values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(prrelid);
225  values[Anum_pg_publication_rel_prpubid - 1] =
226  ObjectIdGetDatum(pubid);
227  values[Anum_pg_publication_rel_prrelid - 1] =
228  ObjectIdGetDatum(relid);
229 
230  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
231 
232  /* Insert tuple into catalog. */
233  CatalogTupleInsert(rel, tup);
234  heap_freetuple(tup);
235 
236  ObjectAddressSet(myself, PublicationRelRelationId, prrelid);
237 
238  /* Add dependency on the publication */
239  ObjectAddressSet(referenced, PublicationRelationId, pubid);
240  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
241 
242  /* Add dependency on the relation */
243  ObjectAddressSet(referenced, RelationRelationId, relid);
244  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
245 
246  /* Close the table. */
248 
249  /*
250  * Invalidate relcache so that publication info is rebuilt.
251  *
252  * For the partitioned tables, we must invalidate all partitions contained
253  * in the respective partition hierarchies, not just the one explicitly
254  * mentioned in the publication. This is required because we implicitly
255  * publish the child tables when the parent table is published.
256  */
258  relid);
259 
261 
262  return myself;
263 }
#define NIL
Definition: pg_list.h:65
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:381
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define RelationGetDescr(relation)
Definition: rel.h:503
int errcode(int sqlerrcode)
Definition: elog.c:698
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
Publication * GetPublication(Oid pubid)
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
static void check_publication_add_relation(Relation targetrel)
#define RowExclusiveLock
Definition: lockdefs.h:38
List * GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt, Oid relid)
#define RelationGetRelationName(relation)
Definition: rel.h:511
uintptr_t Datum
Definition: postgres.h:411
#define ereport(elevel,...)
Definition: elog.h:157
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:186
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:156
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:909
void InvalidatePublicationRels(List *relids)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:477
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221

Variable Documentation

◆ FormData_pg_publication

FormData_pg_publication

Definition at line 57 of file pg_publication.h.