PostgreSQL Source Code  git master
pg_publication.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/index.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/partition.h"
#include "catalog/objectaccess.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_publication.h"
#include "catalog/pg_publication_rel.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for pg_publication.c:

Go to the source code of this file.

Functions

static void check_publication_add_relation (Relation targetrel)
 
static bool is_publishable_class (Oid relid, Form_pg_class reltuple)
 
bool is_publishable_relation (Relation rel)
 
Datum pg_relation_is_publishable (PG_FUNCTION_ARGS)
 
ObjectAddress publication_add_relation (Oid pubid, Relation targetrel, bool if_not_exists)
 
ListGetRelationPublications (Oid relid)
 
ListGetPublicationRelations (Oid pubid, PublicationPartOpt pub_partopt)
 
ListGetAllTablesPublications (void)
 
ListGetAllTablesPublicationRelations (bool pubviaroot)
 
PublicationGetPublication (Oid pubid)
 
PublicationGetPublicationByName (const char *pubname, bool missing_ok)
 
Oid get_publication_oid (const char *pubname, bool missing_ok)
 
char * get_publication_name (Oid pubid, bool missing_ok)
 
Datum pg_get_publication_tables (PG_FUNCTION_ARGS)
 

Function Documentation

◆ check_publication_add_relation()

static void check_publication_add_relation ( Relation  targetrel)
static

Definition at line 50 of file pg_publication.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, IsCatalogRelation(), RelationGetForm, RelationGetRelationName, and RelationNeedsWAL.

Referenced by publication_add_relation().

51 {
52  /* Must be a regular or partitioned table */
53  if (RelationGetForm(targetrel)->relkind != RELKIND_RELATION &&
54  RelationGetForm(targetrel)->relkind != RELKIND_PARTITIONED_TABLE)
55  ereport(ERROR,
56  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
57  errmsg("\"%s\" is not a table",
58  RelationGetRelationName(targetrel)),
59  errdetail("Only tables can be added to publications.")));
60 
61  /* Can't be system table */
62  if (IsCatalogRelation(targetrel))
63  ereport(ERROR,
64  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
65  errmsg("\"%s\" is a system table",
66  RelationGetRelationName(targetrel)),
67  errdetail("System tables cannot be added to publications.")));
68 
69  /* UNLOGGED and TEMP relations cannot be part of publication. */
70  if (!RelationNeedsWAL(targetrel))
71  ereport(ERROR,
72  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
73  errmsg("table \"%s\" cannot be replicated",
74  RelationGetRelationName(targetrel)),
75  errdetail("Temporary and unlogged relations cannot be replicated.")));
76 }
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:98
#define RelationGetForm(relation)
Definition: rel.h:450
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:957
#define RelationGetRelationName(relation)
Definition: rel.h:490
#define ereport(elevel,...)
Definition: elog.h:144
#define RelationNeedsWAL(relation)
Definition: rel.h:562
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ get_publication_name()

char* get_publication_name ( Oid  pubid,
bool  missing_ok 
)

Definition at line 481 of file pg_publication.c.

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

Referenced by getObjectDescription(), and getObjectIdentityParts().

482 {
483  HeapTuple tup;
484  char *pubname;
485  Form_pg_publication pubform;
486 
488 
489  if (!HeapTupleIsValid(tup))
490  {
491  if (!missing_ok)
492  elog(ERROR, "cache lookup failed for publication %u", pubid);
493  return NULL;
494  }
495 
496  pubform = (Form_pg_publication) GETSTRUCT(tup);
497  pubname = pstrdup(NameStr(pubform->pubname));
498 
499  ReleaseSysCache(tup);
500 
501  return pubname;
502 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:622
FormData_pg_publication * Form_pg_publication

◆ get_publication_oid()

Oid get_publication_oid ( const char *  pubname,
bool  missing_ok 
)

Definition at line 461 of file pg_publication.c.

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

Referenced by get_object_address_unqualified(), and GetPublicationByName().

462 {
463  Oid oid;
464 
465  oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
466  CStringGetDatum(pubname));
467  if (!OidIsValid(oid) && !missing_ok)
468  ereport(ERROR,
469  (errcode(ERRCODE_UNDEFINED_OBJECT),
470  errmsg("publication \"%s\" does not exist", pubname)));
471  return oid;
472 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:610
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ GetAllTablesPublicationRelations()

List* GetAllTablesPublicationRelations ( bool  pubviaroot)

Definition at line 352 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().

353 {
354  Relation classRel;
355  ScanKeyData key[1];
356  TableScanDesc scan;
357  HeapTuple tuple;
358  List *result = NIL;
359 
360  classRel = table_open(RelationRelationId, AccessShareLock);
361 
362  ScanKeyInit(&key[0],
363  Anum_pg_class_relkind,
364  BTEqualStrategyNumber, F_CHAREQ,
365  CharGetDatum(RELKIND_RELATION));
366 
367  scan = table_beginscan_catalog(classRel, 1, key);
368 
369  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
370  {
371  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
372  Oid relid = relForm->oid;
373 
374  if (is_publishable_class(relid, relForm) &&
375  !(relForm->relispartition && pubviaroot))
376  result = lappend_oid(result, relid);
377  }
378 
379  table_endscan(scan);
380 
381  if (pubviaroot)
382  {
383  ScanKeyInit(&key[0],
384  Anum_pg_class_relkind,
385  BTEqualStrategyNumber, F_CHAREQ,
386  CharGetDatum(RELKIND_PARTITIONED_TABLE));
387 
388  scan = table_beginscan_catalog(classRel, 1, key);
389 
390  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
391  {
392  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
393  Oid relid = relForm->oid;
394 
395  if (is_publishable_class(relid, relForm) &&
396  !relForm->relispartition)
397  result = lappend_oid(result, relid);
398  }
399 
400  table_endscan(scan);
401  }
402 
403  table_close(classRel, AccessShareLock);
404  return result;
405 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
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:357
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1286
#define CharGetDatum(X)
Definition: postgres.h:416
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:862
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 311 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().

312 {
313  List *result;
314  Relation rel;
315  ScanKeyData scankey;
316  SysScanDesc scan;
317  HeapTuple tup;
318 
319  /* Find all publications that are marked as for all tables. */
320  rel = table_open(PublicationRelationId, AccessShareLock);
321 
322  ScanKeyInit(&scankey,
323  Anum_pg_publication_puballtables,
324  BTEqualStrategyNumber, F_BOOLEQ,
325  BoolGetDatum(true));
326 
327  scan = systable_beginscan(rel, InvalidOid, false,
328  NULL, 1, &scankey);
329 
330  result = NIL;
331  while (HeapTupleIsValid(tup = systable_getnext(scan)))
332  {
333  Oid oid = ((Form_pg_publication) GETSTRUCT(tup))->oid;
334 
335  result = lappend_oid(result, oid);
336  }
337 
338  systable_endscan(scan);
340 
341  return result;
342 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:529
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:356
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:448
#define BoolGetDatum(X)
Definition: postgres.h:402
#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 413 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().

414 {
415  HeapTuple tup;
416  Publication *pub;
417  Form_pg_publication pubform;
418 
420  if (!HeapTupleIsValid(tup))
421  elog(ERROR, "cache lookup failed for publication %u", pubid);
422 
423  pubform = (Form_pg_publication) GETSTRUCT(tup);
424 
425  pub = (Publication *) palloc(sizeof(Publication));
426  pub->oid = pubid;
427  pub->name = pstrdup(NameStr(pubform->pubname));
428  pub->alltables = pubform->puballtables;
429  pub->pubactions.pubinsert = pubform->pubinsert;
430  pub->pubactions.pubupdate = pubform->pubupdate;
431  pub->pubactions.pubdelete = pubform->pubdelete;
432  pub->pubactions.pubtruncate = pubform->pubtruncate;
433  pub->pubviaroot = pubform->pubviaroot;
434 
435  ReleaseSysCache(tup);
436 
437  return pub;
438 }
PublicationActions pubactions
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:622
FormData_pg_publication * Form_pg_publication

◆ GetPublicationByName()

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

Definition at line 445 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().

446 {
447  Oid oid;
448 
449  oid = get_publication_oid(pubname, missing_ok);
450 
451  return OidIsValid(oid) ? GetPublication(oid) : NULL;
452 }
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
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 248 of file pg_publication.c.

References AccessShareLock, Assert, BTEqualStrategyNumber, find_all_inheritors(), get_rel_relkind(), GETSTRUCT, HeapTupleIsValid, lappend_oid(), lfirst_oid, list_concat(), NIL, NoLock, ObjectIdGetDatum, PUBLICATION_PART_ALL, PUBLICATION_PART_LEAF, PUBLICATION_PART_ROOT, PublicationRelPrrelidPrpubidIndexId, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

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

249 {
250  List *result;
251  Relation pubrelsrel;
252  ScanKeyData scankey;
253  SysScanDesc scan;
254  HeapTuple tup;
255 
256  /* Find all publications associated with the relation. */
257  pubrelsrel = table_open(PublicationRelRelationId, AccessShareLock);
258 
259  ScanKeyInit(&scankey,
260  Anum_pg_publication_rel_prpubid,
261  BTEqualStrategyNumber, F_OIDEQ,
262  ObjectIdGetDatum(pubid));
263 
265  true, NULL, 1, &scankey);
266 
267  result = NIL;
268  while (HeapTupleIsValid(tup = systable_getnext(scan)))
269  {
271 
272  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
273 
274  if (get_rel_relkind(pubrel->prrelid) == RELKIND_PARTITIONED_TABLE &&
275  pub_partopt != PUBLICATION_PART_ROOT)
276  {
277  List *all_parts = find_all_inheritors(pubrel->prrelid, NoLock,
278  NULL);
279 
280  if (pub_partopt == PUBLICATION_PART_ALL)
281  result = list_concat(result, all_parts);
282  else if (pub_partopt == PUBLICATION_PART_LEAF)
283  {
284  ListCell *lc;
285 
286  foreach(lc, all_parts)
287  {
288  Oid partOid = lfirst_oid(lc);
289 
290  if (get_rel_relkind(partOid) != RELKIND_PARTITIONED_TABLE)
291  result = lappend_oid(result, partOid);
292  }
293  }
294  else
295  Assert(false);
296  }
297  else
298  result = lappend_oid(result, pubrel->prrelid);
299  }
300 
301  systable_endscan(scan);
302  table_close(pubrelsrel, AccessShareLock);
303 
304  return result;
305 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:529
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1915
#define AccessShareLock
Definition: lockdefs.h:36
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:356
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:448
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define NoLock
Definition: lockdefs.h:34
#define PublicationRelPrrelidPrpubidIndexId
Definition: indexing.h:360
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:745
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:165
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
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ GetRelationPublications()

List* GetRelationPublications ( Oid  relid)

Definition at line 219 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().

220 {
221  List *result = NIL;
222  CatCList *pubrellist;
223  int i;
224 
225  /* Find all publications associated with the relation. */
227  ObjectIdGetDatum(relid));
228  for (i = 0; i < pubrellist->n_members; i++)
229  {
230  HeapTuple tup = &pubrellist->members[i]->tuple;
231  Oid pubid = ((Form_pg_publication_rel) GETSTRUCT(tup))->prpubid;
232 
233  result = lappend_oid(result, pubid);
234  }
235 
236  ReleaseSysCacheList(pubrellist);
237 
238  return result;
239 }
#define NIL
Definition: pg_list.h:65
int n_members
Definition: catcache.h:176
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:178
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define SearchSysCacheList1(cacheId, key1)
Definition: syscache.h:210
#define ReleaseSysCacheList(x)
Definition: syscache.h:217
FormData_pg_publication_rel * Form_pg_publication_rel
int i
HeapTupleData tuple
Definition: catcache.h:121
Definition: pg_list.h:50

◆ is_publishable_class()

static bool is_publishable_class ( Oid  relid,
Form_pg_class  reltuple 
)
static

Definition at line 98 of file pg_publication.c.

References FirstNormalObjectId, and IsCatalogRelationOid().

Referenced by GetAllTablesPublicationRelations(), is_publishable_relation(), and pg_relation_is_publishable().

99 {
100  return (reltuple->relkind == RELKIND_RELATION ||
101  reltuple->relkind == RELKIND_PARTITIONED_TABLE) &&
102  !IsCatalogRelationOid(relid) &&
103  reltuple->relpersistence == RELPERSISTENCE_PERMANENT &&
104  relid >= FirstNormalObjectId;
105 }
#define FirstNormalObjectId
Definition: transam.h:155
bool IsCatalogRelationOid(Oid relid)
Definition: catalog.c:115

◆ is_publishable_relation()

bool is_publishable_relation ( Relation  rel)

Definition at line 111 of file pg_publication.c.

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

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

112 {
113  return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
114 }
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:456

◆ pg_get_publication_tables()

Datum pg_get_publication_tables ( PG_FUNCTION_ARGS  )

Definition at line 508 of file pg_publication.c.

References Publication::alltables, FuncCallContext::call_cntr, GetAllTablesPublicationRelations(), GetPublicationByName(), GetPublicationRelations(), list_length(), list_nth_oid(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, ObjectIdGetDatum, Publication::oid, PG_GETARG_TEXT_PP, PUBLICATION_PART_LEAF, PUBLICATION_PART_ROOT, Publication::pubviaroot, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, text_to_cstring(), and FuncCallContext::user_fctx.

509 {
510  FuncCallContext *funcctx;
511  char *pubname = text_to_cstring(PG_GETARG_TEXT_PP(0));
512  Publication *publication;
513  List *tables;
514 
515  /* stuff done only on the first call of the function */
516  if (SRF_IS_FIRSTCALL())
517  {
518  MemoryContext oldcontext;
519 
520  /* create a function context for cross-call persistence */
521  funcctx = SRF_FIRSTCALL_INIT();
522 
523  /* switch to memory context appropriate for multiple function calls */
524  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
525 
526  publication = GetPublicationByName(pubname, false);
527 
528  /*
529  * Publications support partitioned tables, although all changes are
530  * replicated using leaf partition identity and schema, so we only
531  * need those.
532  */
533  if (publication->alltables)
534  tables = GetAllTablesPublicationRelations(publication->pubviaroot);
535  else
536  tables = GetPublicationRelations(publication->oid,
537  publication->pubviaroot ?
540  funcctx->user_fctx = (void *) tables;
541 
542  MemoryContextSwitchTo(oldcontext);
543  }
544 
545  /* stuff done on every call of the function */
546  funcctx = SRF_PERCALL_SETUP();
547  tables = (List *) funcctx->user_fctx;
548 
549  if (funcctx->call_cntr < list_length(tables))
550  {
551  Oid relid = list_nth_oid(tables, funcctx->call_cntr);
552 
553  SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(relid));
554  }
555 
556  SRF_RETURN_DONE(funcctx);
557 }
uint64 call_cntr
Definition: funcapi.h:65
List * GetPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt)
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
Publication * GetPublicationByName(const char *pubname, bool missing_ok)
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
static int list_length(const List *l)
Definition: pg_list.h:169
char * text_to_cstring(const text *t)
Definition: varlena.c:205
void * user_fctx
Definition: funcapi.h:82
List * GetAllTablesPublicationRelations(bool pubviaroot)
Definition: pg_list.h:50
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ pg_relation_is_publishable()

Datum pg_relation_is_publishable ( PG_FUNCTION_ARGS  )

Definition at line 125 of file pg_publication.c.

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

126 {
127  Oid relid = PG_GETARG_OID(0);
128  HeapTuple tuple;
129  bool result;
130 
131  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
132  if (!HeapTupleIsValid(tuple))
133  PG_RETURN_NULL();
134  result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple));
135  ReleaseSysCache(tuple);
136  PG_RETURN_BOOL(result);
137 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
static bool is_publishable_class(Oid relid, Form_pg_class reltuple)
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ publication_add_relation()

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

Definition at line 144 of file pg_publication.c.

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

Referenced by PublicationAddTables().

146 {
147  Relation rel;
148  HeapTuple tup;
149  Datum values[Natts_pg_publication_rel];
150  bool nulls[Natts_pg_publication_rel];
151  Oid relid = RelationGetRelid(targetrel);
152  Oid prrelid;
153  Publication *pub = GetPublication(pubid);
154  ObjectAddress myself,
155  referenced;
156 
157  rel = table_open(PublicationRelRelationId, RowExclusiveLock);
158 
159  /*
160  * Check for duplicates. Note that this does not really prevent
161  * duplicates, it's here just to provide nicer error message in common
162  * case. The real protection is the unique key on the catalog.
163  */
165  ObjectIdGetDatum(pubid)))
166  {
168 
169  if (if_not_exists)
170  return InvalidObjectAddress;
171 
172  ereport(ERROR,
174  errmsg("relation \"%s\" is already member of publication \"%s\"",
175  RelationGetRelationName(targetrel), pub->name)));
176  }
177 
179 
180  /* Form a tuple. */
181  memset(values, 0, sizeof(values));
182  memset(nulls, false, sizeof(nulls));
183 
185  Anum_pg_publication_rel_oid);
186  values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(prrelid);
187  values[Anum_pg_publication_rel_prpubid - 1] =
188  ObjectIdGetDatum(pubid);
189  values[Anum_pg_publication_rel_prrelid - 1] =
190  ObjectIdGetDatum(relid);
191 
192  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
193 
194  /* Insert tuple into catalog. */
195  CatalogTupleInsert(rel, tup);
196  heap_freetuple(tup);
197 
198  ObjectAddressSet(myself, PublicationRelRelationId, prrelid);
199 
200  /* Add dependency on the publication */
201  ObjectAddressSet(referenced, PublicationRelationId, pubid);
202  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
203 
204  /* Add dependency on the relation */
205  ObjectAddressSet(referenced, RelationRelationId, relid);
206  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
207 
208  /* Close the table. */
210 
211  /* Invalidate relcache so that publication info is rebuilt. */
212  CacheInvalidateRelcache(targetrel);
213 
214  return myself;
215 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:317
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define RelationGetDescr(relation)
Definition: rel.h:482
int errcode(int sqlerrcode)
Definition: elog.c:610
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
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:507
#define ERROR
Definition: elog.h:43
static void check_publication_add_relation(Relation targetrel)
#define RowExclusiveLock
Definition: lockdefs.h:38
#define RelationGetRelationName(relation)
Definition: rel.h:490
uintptr_t Datum
Definition: postgres.h:367
#define ereport(elevel,...)
Definition: elog.h:144
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:185
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define PublicationRelObjectIndexId
Definition: indexing.h:357
void CacheInvalidateRelcache(Relation relation)
Definition: inval.c:1278
static Datum values[MAXATTR]
Definition: bootstrap.c:167
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:824
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
#define RelationGetRelid(relation)
Definition: rel.h:456
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221