PostgreSQL Source Code  git master
pg_publication.c File Reference
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.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/objectaccess.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_type.h"
#include "catalog/pg_publication.h"
#include "catalog/pg_publication_rel.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)
 
ListGetAllTablesPublications (void)
 
ListGetAllTablesPublicationRelations (void)
 
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 51 of file pg_publication.c.

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

Referenced by publication_add_relation().

52 {
53  /* Give more specific error for partitioned tables */
54  if (RelationGetForm(targetrel)->relkind == RELKIND_PARTITIONED_TABLE)
55  ereport(ERROR,
56  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
57  errmsg("\"%s\" is a partitioned table",
58  RelationGetRelationName(targetrel)),
59  errdetail("Adding partitioned tables to publications is not supported."),
60  errhint("You can add the table partitions individually.")));
61 
62  /* Must be table */
63  if (RelationGetForm(targetrel)->relkind != RELKIND_RELATION)
64  ereport(ERROR,
65  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
66  errmsg("\"%s\" is not a table",
67  RelationGetRelationName(targetrel)),
68  errdetail("Only tables can be added to publications.")));
69 
70  /* Can't be system table */
71  if (IsCatalogRelation(targetrel))
72  ereport(ERROR,
73  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
74  errmsg("\"%s\" is a system table",
75  RelationGetRelationName(targetrel)),
76  errdetail("System tables cannot be added to publications.")));
77 
78  /* UNLOGGED and TEMP relations cannot be part of publication. */
79  if (!RelationNeedsWAL(targetrel))
80  ereport(ERROR,
81  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
82  errmsg("table \"%s\" cannot be replicated",
83  RelationGetRelationName(targetrel)),
84  errdetail("Temporary and unlogged relations cannot be replicated.")));
85 }
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:100
int errhint(const char *fmt,...)
Definition: elog.c:974
#define RelationGetForm(relation)
Definition: rel.h:410
int errcode(int sqlerrcode)
Definition: elog.c:570
char relkind
Definition: pg_class.h:81
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define RelationGetRelationName(relation)
Definition: rel.h:450
#define ereport(elevel, rest)
Definition: elog.h:141
#define RelationNeedsWAL(relation)
Definition: rel.h:518
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ get_publication_name()

char* get_publication_name ( Oid  pubid,
bool  missing_ok 
)

Definition at line 451 of file pg_publication.c.

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

Referenced by getObjectDescription(), and getObjectIdentityParts().

452 {
453  HeapTuple tup;
454  char *pubname;
455  Form_pg_publication pubform;
456 
458 
459  if (!HeapTupleIsValid(tup))
460  {
461  if (!missing_ok)
462  elog(ERROR, "cache lookup failed for publication %u", pubid);
463  return NULL;
464  }
465 
466  pubform = (Form_pg_publication) GETSTRUCT(tup);
467  pubname = pstrdup(NameStr(pubform->pubname));
468 
469  ReleaseSysCache(tup);
470 
471  return pubname;
472 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
FormData_pg_publication * Form_pg_publication

◆ get_publication_oid()

Oid get_publication_oid ( const char *  pubname,
bool  missing_ok 
)

Definition at line 431 of file pg_publication.c.

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

Referenced by get_object_address_unqualified().

432 {
433  Oid oid;
434 
435  oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
436  CStringGetDatum(pubname));
437  if (!OidIsValid(oid) && !missing_ok)
438  ereport(ERROR,
439  (errcode(ERRCODE_UNDEFINED_OBJECT),
440  errmsg("publication \"%s\" does not exist", pubname)));
441  return oid;
442 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ GetAllTablesPublicationRelations()

List* GetAllTablesPublicationRelations ( void  )

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

336 {
337  Relation classRel;
338  ScanKeyData key[1];
339  TableScanDesc scan;
340  HeapTuple tuple;
341  List *result = NIL;
342 
343  classRel = table_open(RelationRelationId, AccessShareLock);
344 
345  ScanKeyInit(&key[0],
346  Anum_pg_class_relkind,
347  BTEqualStrategyNumber, F_CHAREQ,
348  CharGetDatum(RELKIND_RELATION));
349 
350  scan = table_beginscan_catalog(classRel, 1, key);
351 
352  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
353  {
354  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
355  Oid relid = relForm->oid;
356 
357  if (is_publishable_class(relid, relForm))
358  result = lappend_oid(result, relid);
359  }
360 
361  table_endscan(scan);
362  table_close(classRel, AccessShareLock);
363 
364  return result;
365 }
#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:98
#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:1290
#define CharGetDatum(X)
Definition: postgres.h:416
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:831
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
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 298 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().

299 {
300  List *result;
301  Relation rel;
302  ScanKeyData scankey;
303  SysScanDesc scan;
304  HeapTuple tup;
305 
306  /* Find all publications that are marked as for all tables. */
307  rel = table_open(PublicationRelationId, AccessShareLock);
308 
309  ScanKeyInit(&scankey,
310  Anum_pg_publication_puballtables,
311  BTEqualStrategyNumber, F_BOOLEQ,
312  BoolGetDatum(true));
313 
314  scan = systable_beginscan(rel, InvalidOid, false,
315  NULL, 1, &scankey);
316 
317  result = NIL;
318  while (HeapTupleIsValid(tup = systable_getnext(scan)))
319  {
320  Oid oid = ((Form_pg_publication) GETSTRUCT(tup))->oid;
321 
322  result = lappend_oid(result, oid);
323  }
324 
325  systable_endscan(scan);
327 
328  return result;
329 }
#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:525
#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:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#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 373 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, ReleaseSysCache(), and SearchSysCache1().

Referenced by GetPublicationByName(), and publication_add_relation().

374 {
375  HeapTuple tup;
376  Publication *pub;
377  Form_pg_publication pubform;
378 
380 
381  if (!HeapTupleIsValid(tup))
382  elog(ERROR, "cache lookup failed for publication %u", pubid);
383 
384  pubform = (Form_pg_publication) GETSTRUCT(tup);
385 
386  pub = (Publication *) palloc(sizeof(Publication));
387  pub->oid = pubid;
388  pub->name = pstrdup(NameStr(pubform->pubname));
389  pub->alltables = pubform->puballtables;
390  pub->pubactions.pubinsert = pubform->pubinsert;
391  pub->pubactions.pubupdate = pubform->pubupdate;
392  pub->pubactions.pubdelete = pubform->pubdelete;
393  pub->pubactions.pubtruncate = pubform->pubtruncate;
394 
395  ReleaseSysCache(tup);
396 
397  return pub;
398 }
PublicationActions pubactions
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void * palloc(Size size)
Definition: mcxt.c:924
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
FormData_pg_publication * Form_pg_publication

◆ GetPublicationByName()

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

Definition at line 405 of file pg_publication.c.

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

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

406 {
407  Oid oid;
408 
409  oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
410  CStringGetDatum(pubname));
411  if (!OidIsValid(oid))
412  {
413  if (missing_ok)
414  return NULL;
415 
416  ereport(ERROR,
417  (errcode(ERRCODE_UNDEFINED_OBJECT),
418  errmsg("publication \"%s\" does not exist", pubname)));
419  }
420 
421  return GetPublication(oid);
422 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
Publication * GetPublication(Oid pubid)
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ GetPublicationRelations()

List* GetPublicationRelations ( Oid  pubid)

Definition at line 259 of file pg_publication.c.

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

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

260 {
261  List *result;
262  Relation pubrelsrel;
263  ScanKeyData scankey;
264  SysScanDesc scan;
265  HeapTuple tup;
266 
267  /* Find all publications associated with the relation. */
268  pubrelsrel = table_open(PublicationRelRelationId, AccessShareLock);
269 
270  ScanKeyInit(&scankey,
271  Anum_pg_publication_rel_prpubid,
272  BTEqualStrategyNumber, F_OIDEQ,
273  ObjectIdGetDatum(pubid));
274 
276  true, NULL, 1, &scankey);
277 
278  result = NIL;
279  while (HeapTupleIsValid(tup = systable_getnext(scan)))
280  {
282 
283  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
284 
285  result = lappend_oid(result, pubrel->prrelid);
286  }
287 
288  systable_endscan(scan);
289  table_close(pubrelsrel, AccessShareLock);
290 
291  return result;
292 }
#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:525
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define AccessShareLock
Definition: lockdefs.h:36
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:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define PublicationRelPrrelidPrpubidIndexId
Definition: indexing.h:358
#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

◆ GetRelationPublications()

List* GetRelationPublications ( Oid  relid)

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

231 {
232  List *result = NIL;
233  CatCList *pubrellist;
234  int i;
235 
236  /* Find all publications associated with the relation. */
238  ObjectIdGetDatum(relid));
239  for (i = 0; i < pubrellist->n_members; i++)
240  {
241  HeapTuple tup = &pubrellist->members[i]->tuple;
242  Oid pubid = ((Form_pg_publication_rel) GETSTRUCT(tup))->prpubid;
243 
244  result = lappend_oid(result, pubid);
245  }
246 
247  ReleaseSysCacheList(pubrellist);
248 
249  return result;
250 }
#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 107 of file pg_publication.c.

References FirstNormalObjectId, and IsCatalogRelationOid().

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

108 {
109  return reltuple->relkind == RELKIND_RELATION &&
110  !IsCatalogRelationOid(relid) &&
111  reltuple->relpersistence == RELPERSISTENCE_PERMANENT &&
112  relid >= FirstNormalObjectId;
113 }
#define FirstNormalObjectId
Definition: transam.h:141
bool IsCatalogRelationOid(Oid relid)
Definition: catalog.c:117

◆ is_publishable_relation()

bool is_publishable_relation ( Relation  rel)

Definition at line 119 of file pg_publication.c.

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

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

120 {
121  return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
122 }
Form_pg_class rd_rel
Definition: rel.h:83
static bool is_publishable_class(Oid relid, Form_pg_class reltuple)
#define RelationGetRelid(relation)
Definition: rel.h:416

◆ pg_get_publication_tables()

Datum pg_get_publication_tables ( PG_FUNCTION_ARGS  )

Definition at line 478 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, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, text_to_cstring(), and FuncCallContext::user_fctx.

479 {
480  FuncCallContext *funcctx;
481  char *pubname = text_to_cstring(PG_GETARG_TEXT_PP(0));
482  Publication *publication;
483  List *tables;
484 
485  /* stuff done only on the first call of the function */
486  if (SRF_IS_FIRSTCALL())
487  {
488  MemoryContext oldcontext;
489 
490  /* create a function context for cross-call persistence */
491  funcctx = SRF_FIRSTCALL_INIT();
492 
493  /* switch to memory context appropriate for multiple function calls */
494  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
495 
496  publication = GetPublicationByName(pubname, false);
497  if (publication->alltables)
499  else
500  tables = GetPublicationRelations(publication->oid);
501  funcctx->user_fctx = (void *) tables;
502 
503  MemoryContextSwitchTo(oldcontext);
504  }
505 
506  /* stuff done on every call of the function */
507  funcctx = SRF_PERCALL_SETUP();
508  tables = (List *) funcctx->user_fctx;
509 
510  if (funcctx->call_cntr < list_length(tables))
511  {
512  Oid relid = list_nth_oid(tables, funcctx->call_cntr);
513 
514  SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(relid));
515  }
516 
517  SRF_RETURN_DONE(funcctx);
518 }
uint64 call_cntr
Definition: funcapi.h:66
List * GetAllTablesPublicationRelations(void)
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
Publication * GetPublicationByName(const char *pubname, bool missing_ok)
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
List * GetPublicationRelations(Oid pubid)
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
static int list_length(const List *l)
Definition: pg_list.h:169
char * text_to_cstring(const text *t)
Definition: varlena.c:204
void * user_fctx
Definition: funcapi.h:83
Definition: pg_list.h:50
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ pg_relation_is_publishable()

Datum pg_relation_is_publishable ( PG_FUNCTION_ARGS  )

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

134 {
135  Oid relid = PG_GETARG_OID(0);
136  HeapTuple tuple;
137  bool result;
138 
139  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
140  if (!HeapTupleIsValid(tuple))
141  PG_RETURN_NULL();
142  result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple));
143  ReleaseSysCache(tuple);
144  PG_RETURN_BOOL(result);
145 }
#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:270
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
static bool is_publishable_class(Oid relid, Form_pg_class reltuple)
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ publication_add_relation()

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

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

154 {
155  Relation rel;
156  HeapTuple tup;
157  Datum values[Natts_pg_publication_rel];
158  bool nulls[Natts_pg_publication_rel];
159  Oid relid = RelationGetRelid(targetrel);
160  Oid prrelid;
161  Publication *pub = GetPublication(pubid);
162  ObjectAddress myself,
163  referenced;
164 
165  rel = table_open(PublicationRelRelationId, RowExclusiveLock);
166 
167  /*
168  * Check for duplicates. Note that this does not really prevent
169  * duplicates, it's here just to provide nicer error message in common
170  * case. The real protection is the unique key on the catalog.
171  */
173  ObjectIdGetDatum(pubid)))
174  {
176 
177  if (if_not_exists)
178  return InvalidObjectAddress;
179 
180  ereport(ERROR,
182  errmsg("relation \"%s\" is already member of publication \"%s\"",
183  RelationGetRelationName(targetrel), pub->name)));
184  }
185 
187 
188  /* Form a tuple. */
189  memset(values, 0, sizeof(values));
190  memset(nulls, false, sizeof(nulls));
191 
193  Anum_pg_publication_rel_oid);
194  values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(prrelid);
195  values[Anum_pg_publication_rel_prpubid - 1] =
196  ObjectIdGetDatum(pubid);
197  values[Anum_pg_publication_rel_prrelid - 1] =
198  ObjectIdGetDatum(relid);
199 
200  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
201 
202  /* Insert tuple into catalog. */
203  CatalogTupleInsert(rel, tup);
204  heap_freetuple(tup);
205 
206  ObjectAddressSet(myself, PublicationRelRelationId, prrelid);
207 
208  /* Add dependency on the publication */
209  ObjectAddressSet(referenced, PublicationRelationId, pubid);
210  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
211 
212  /* Add dependency on the relation */
213  ObjectAddressSet(referenced, RelationRelationId, relid);
214  recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
215 
216  /* Close the table. */
218 
219  /* Invalidate relcache so that publication info is rebuilt. */
220  CacheInvalidateRelcache(targetrel);
221 
222  return myself;
223 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:323
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define RelationGetDescr(relation)
Definition: rel.h:442
int errcode(int sqlerrcode)
Definition: elog.c:570
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:450
#define ereport(elevel, rest)
Definition: elog.h:141
uintptr_t Datum
Definition: postgres.h:367
#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:355
void CacheInvalidateRelcache(Relation relation)
Definition: inval.c:1270
static Datum values[MAXATTR]
Definition: bootstrap.c:167
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:784
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:33
#define RelationGetRelid(relation)
Definition: rel.h:416
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183