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/objectaccess.h"
#include "catalog/objectaddress.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)
 
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 48 of file pg_publication.c.

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

Referenced by publication_add_relation().

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

◆ get_publication_name()

char* get_publication_name ( Oid  pubid,
bool  missing_ok 
)

Definition at line 437 of file pg_publication.c.

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

Referenced by getObjectDescription(), and getObjectIdentityParts().

438 {
439  HeapTuple tup;
440  char *pubname;
441  Form_pg_publication pubform;
442 
444 
445  if (!HeapTupleIsValid(tup))
446  {
447  if (!missing_ok)
448  elog(ERROR, "cache lookup failed for publication %u", pubid);
449  return NULL;
450  }
451 
452  pubform = (Form_pg_publication) GETSTRUCT(tup);
453  pubname = pstrdup(NameStr(pubform->pubname));
454 
455  ReleaseSysCache(tup);
456 
457  return pubname;
458 }
#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:228
#define NameStr(name)
Definition: c.h:616
FormData_pg_publication * Form_pg_publication

◆ get_publication_oid()

Oid get_publication_oid ( const char *  pubname,
bool  missing_ok 
)

Definition at line 417 of file pg_publication.c.

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

Referenced by get_object_address_unqualified(), and GetPublicationByName().

418 {
419  Oid oid;
420 
421  oid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
422  CStringGetDatum(pubname));
423  if (!OidIsValid(oid) && !missing_ok)
424  ereport(ERROR,
425  (errcode(ERRCODE_UNDEFINED_OBJECT),
426  errmsg("publication \"%s\" does not exist", pubname)));
427  return oid;
428 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
#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:822

◆ GetAllTablesPublicationRelations()

List* GetAllTablesPublicationRelations ( void  )

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

333 {
334  Relation classRel;
335  ScanKeyData key[1];
336  TableScanDesc scan;
337  HeapTuple tuple;
338  List *result = NIL;
339 
340  classRel = table_open(RelationRelationId, AccessShareLock);
341 
342  ScanKeyInit(&key[0],
343  Anum_pg_class_relkind,
344  BTEqualStrategyNumber, F_CHAREQ,
345  CharGetDatum(RELKIND_RELATION));
346 
347  scan = table_beginscan_catalog(classRel, 1, key);
348 
349  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
350  {
351  Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
352  Oid relid = relForm->oid;
353 
354  if (is_publishable_class(relid, relForm))
355  result = lappend_oid(result, relid);
356  }
357 
358  table_endscan(scan);
359  table_close(classRel, AccessShareLock);
360 
361  return result;
362 }
#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:358
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:849
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 295 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().

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

371 {
372  HeapTuple tup;
373  Publication *pub;
374  Form_pg_publication pubform;
375 
377  if (!HeapTupleIsValid(tup))
378  elog(ERROR, "cache lookup failed for publication %u", pubid);
379 
380  pubform = (Form_pg_publication) GETSTRUCT(tup);
381 
382  pub = (Publication *) palloc(sizeof(Publication));
383  pub->oid = pubid;
384  pub->name = pstrdup(NameStr(pubform->pubname));
385  pub->alltables = pubform->puballtables;
386  pub->pubactions.pubinsert = pubform->pubinsert;
387  pub->pubactions.pubupdate = pubform->pubupdate;
388  pub->pubactions.pubdelete = pubform->pubdelete;
389  pub->pubactions.pubtruncate = pubform->pubtruncate;
390 
391  ReleaseSysCache(tup);
392 
393  return pub;
394 }
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:228
#define NameStr(name)
Definition: c.h:616
FormData_pg_publication * Form_pg_publication

◆ GetPublicationByName()

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

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

402 {
403  Oid oid;
404 
405  oid = get_publication_oid(pubname, missing_ok);
406 
407  return OidIsValid(oid) ? GetPublication(oid) : NULL;
408 }
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
Publication * GetPublication(Oid pubid)
Oid get_publication_oid(const char *pubname, bool missing_ok)

◆ GetPublicationRelations()

List* GetPublicationRelations ( Oid  pubid)

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

257 {
258  List *result;
259  Relation pubrelsrel;
260  ScanKeyData scankey;
261  SysScanDesc scan;
262  HeapTuple tup;
263 
264  /* Find all publications associated with the relation. */
265  pubrelsrel = table_open(PublicationRelRelationId, AccessShareLock);
266 
267  ScanKeyInit(&scankey,
268  Anum_pg_publication_rel_prpubid,
269  BTEqualStrategyNumber, F_OIDEQ,
270  ObjectIdGetDatum(pubid));
271 
273  true, NULL, 1, &scankey);
274 
275  result = NIL;
276  while (HeapTupleIsValid(tup = systable_getnext(scan)))
277  {
279 
280  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
281 
282  result = lappend_oid(result, pubrel->prrelid);
283  }
284 
285  systable_endscan(scan);
286  table_close(pubrelsrel, AccessShareLock);
287 
288  return result;
289 }
#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:358
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 227 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().

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

References FirstNormalObjectId, and IsCatalogRelationOid().

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

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

◆ is_publishable_relation()

bool is_publishable_relation ( Relation  rel)

Definition at line 116 of file pg_publication.c.

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

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

117 {
118  return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
119 }
Form_pg_class rd_rel
Definition: rel.h:84
static bool is_publishable_class(Oid relid, Form_pg_class reltuple)
#define RelationGetRelid(relation)
Definition: rel.h:428

◆ pg_get_publication_tables()

Datum pg_get_publication_tables ( PG_FUNCTION_ARGS  )

Definition at line 464 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.

465 {
466  FuncCallContext *funcctx;
467  char *pubname = text_to_cstring(PG_GETARG_TEXT_PP(0));
468  Publication *publication;
469  List *tables;
470 
471  /* stuff done only on the first call of the function */
472  if (SRF_IS_FIRSTCALL())
473  {
474  MemoryContext oldcontext;
475 
476  /* create a function context for cross-call persistence */
477  funcctx = SRF_FIRSTCALL_INIT();
478 
479  /* switch to memory context appropriate for multiple function calls */
480  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
481 
482  publication = GetPublicationByName(pubname, false);
483  if (publication->alltables)
485  else
486  tables = GetPublicationRelations(publication->oid);
487  funcctx->user_fctx = (void *) tables;
488 
489  MemoryContextSwitchTo(oldcontext);
490  }
491 
492  /* stuff done on every call of the function */
493  funcctx = SRF_PERCALL_SETUP();
494  tables = (List *) funcctx->user_fctx;
495 
496  if (funcctx->call_cntr < list_length(tables))
497  {
498  Oid relid = list_nth_oid(tables, funcctx->call_cntr);
499 
500  SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(relid));
501  }
502 
503  SRF_RETURN_DONE(funcctx);
504 }
uint64 call_cntr
Definition: funcapi.h:65
List * GetAllTablesPublicationRelations(void)
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:282
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:286
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:288
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
List * GetPublicationRelations(Oid pubid)
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:204
void * user_fctx
Definition: funcapi.h:82
Definition: pg_list.h:50
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:306
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:284

◆ pg_relation_is_publishable()

Datum pg_relation_is_publishable ( PG_FUNCTION_ARGS  )

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

131 {
132  Oid relid = PG_GETARG_OID(0);
133  HeapTuple tuple;
134  bool result;
135 
136  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
137  if (!HeapTupleIsValid(tuple))
138  PG_RETURN_NULL();
139  result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple));
140  ReleaseSysCache(tuple);
141  PG_RETURN_BOOL(result);
142 }
#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:1116
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
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: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 149 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().

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