PostgreSQL Source Code  git master
publicationcmds.h File Reference
Include dependency graph for publicationcmds.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress CreatePublication (CreatePublicationStmt *stmt)
 
void AlterPublication (AlterPublicationStmt *stmt)
 
void RemovePublicationById (Oid pubid)
 
void RemovePublicationRelById (Oid proid)
 
ObjectAddress AlterPublicationOwner (const char *name, Oid newOwnerId)
 
void AlterPublicationOwner_oid (Oid pubid, Oid newOwnerId)
 

Function Documentation

◆ AlterPublication()

void AlterPublication ( AlterPublicationStmt stmt)

Definition at line 402 of file publicationcmds.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, AlterPublicationOptions(), AlterPublicationTables(), CStringGetDatum, ereport, errcode(), errmsg(), ERROR, GetUserId(), heap_close, heap_freetuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, OBJECT_PUBLICATION, AlterPublicationStmt::options, pg_publication_ownercheck(), PUBLICATIONNAME, AlterPublicationStmt::pubname, RowExclusiveLock, and SearchSysCacheCopy1.

Referenced by ProcessUtilitySlow().

403 {
404  Relation rel;
405  HeapTuple tup;
406 
407  rel = heap_open(PublicationRelationId, RowExclusiveLock);
408 
410  CStringGetDatum(stmt->pubname));
411 
412  if (!HeapTupleIsValid(tup))
413  ereport(ERROR,
414  (errcode(ERRCODE_UNDEFINED_OBJECT),
415  errmsg("publication \"%s\" does not exist",
416  stmt->pubname)));
417 
418  /* must be owner */
421  stmt->pubname);
422 
423  if (stmt->options)
424  AlterPublicationOptions(stmt, rel, tup);
425  else
426  AlterPublicationTables(stmt, rel, tup);
427 
428  /* Cleanup. */
429  heap_freetuple(tup);
431 }
static void AlterPublicationOptions(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
Oid GetUserId(void)
Definition: miscinit.c:379
bool pg_publication_ownercheck(Oid pub_oid, Oid roleid)
Definition: aclchk.c:5289
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
static void AlterPublicationTables(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707

◆ AlterPublicationOwner()

ObjectAddress AlterPublicationOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 702 of file publicationcmds.c.

References AlterPublicationOwner_internal(), CStringGetDatum, ereport, errcode(), errmsg(), ERROR, heap_close, heap_freetuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, ObjectAddressSet, PUBLICATIONNAME, RowExclusiveLock, and SearchSysCacheCopy1.

Referenced by ExecAlterOwnerStmt().

703 {
704  Oid subid;
705  HeapTuple tup;
706  Relation rel;
707  ObjectAddress address;
708 
709  rel = heap_open(PublicationRelationId, RowExclusiveLock);
710 
712 
713  if (!HeapTupleIsValid(tup))
714  ereport(ERROR,
715  (errcode(ERRCODE_UNDEFINED_OBJECT),
716  errmsg("publication \"%s\" does not exist", name)));
717 
718  subid = HeapTupleGetOid(tup);
719 
720  AlterPublicationOwner_internal(rel, tup, newOwnerId);
721 
722  ObjectAddressSet(address, PublicationRelationId, subid);
723 
724  heap_freetuple(tup);
725 
727 
728  return address;
729 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
const char * name
Definition: encode.c:521
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707

◆ AlterPublicationOwner_oid()

void AlterPublicationOwner_oid ( Oid  pubid,
Oid  newOwnerId 
)

Definition at line 735 of file publicationcmds.c.

References AlterPublicationOwner_internal(), ereport, errcode(), errmsg(), ERROR, heap_close, heap_freetuple(), heap_open(), HeapTupleIsValid, ObjectIdGetDatum, PUBLICATIONOID, RowExclusiveLock, and SearchSysCacheCopy1.

Referenced by shdepReassignOwned().

736 {
737  HeapTuple tup;
738  Relation rel;
739 
740  rel = heap_open(PublicationRelationId, RowExclusiveLock);
741 
743 
744  if (!HeapTupleIsValid(tup))
745  ereport(ERROR,
746  (errcode(ERRCODE_UNDEFINED_OBJECT),
747  errmsg("publication with OID %u does not exist", subid)));
748 
749  AlterPublicationOwner_internal(rel, tup, newOwnerId);
750 
751  heap_freetuple(tup);
752 
754 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ CreatePublication()

ObjectAddress CreatePublication ( CreatePublicationStmt stmt)

Definition at line 141 of file publicationcmds.c.

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, Assert, BoolGetDatum, CatalogTupleInsert(), CloseTableList(), CommandCounterIncrement(), CStringGetDatum, DirectFunctionCall1, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, CreatePublicationStmt::for_all_tables, get_database_name(), GetSysCacheOid1, GetUserId(), heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), InvokeObjectPostCreateHook, list_length(), MyDatabaseId, namein(), OBJECT_DATABASE, ObjectAddressSet, ObjectIdGetDatum, OidIsValid, OpenTableList(), CreatePublicationStmt::options, parse_publication_options(), pg_database_aclcheck(), PublicationAddTables(), PUBLICATIONNAME, CreatePublicationStmt::pubname, recordDependencyOnOwner(), RelationGetDescr, RowExclusiveLock, superuser(), CreatePublicationStmt::tables, and values.

Referenced by ProcessUtilitySlow().

142 {
143  Relation rel;
144  ObjectAddress myself;
145  Oid puboid;
146  bool nulls[Natts_pg_publication];
147  Datum values[Natts_pg_publication];
148  HeapTuple tup;
149  bool publish_given;
150  bool publish_insert;
151  bool publish_update;
152  bool publish_delete;
153  bool publish_truncate;
154  AclResult aclresult;
155 
156  /* must have CREATE privilege on database */
158  if (aclresult != ACLCHECK_OK)
159  aclcheck_error(aclresult, OBJECT_DATABASE,
161 
162  /* FOR ALL TABLES requires superuser */
163  if (stmt->for_all_tables && !superuser())
164  ereport(ERROR,
165  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
166  (errmsg("must be superuser to create FOR ALL TABLES publication"))));
167 
168  rel = heap_open(PublicationRelationId, RowExclusiveLock);
169 
170  /* Check if name is used */
172  if (OidIsValid(puboid))
173  {
174  ereport(ERROR,
176  errmsg("publication \"%s\" already exists",
177  stmt->pubname)));
178  }
179 
180  /* Form a tuple. */
181  memset(values, 0, sizeof(values));
182  memset(nulls, false, sizeof(nulls));
183 
184  values[Anum_pg_publication_pubname - 1] =
186  values[Anum_pg_publication_pubowner - 1] = ObjectIdGetDatum(GetUserId());
187 
189  &publish_given, &publish_insert,
190  &publish_update, &publish_delete,
191  &publish_truncate);
192 
193  values[Anum_pg_publication_puballtables - 1] =
195  values[Anum_pg_publication_pubinsert - 1] =
196  BoolGetDatum(publish_insert);
197  values[Anum_pg_publication_pubupdate - 1] =
198  BoolGetDatum(publish_update);
199  values[Anum_pg_publication_pubdelete - 1] =
200  BoolGetDatum(publish_delete);
201  values[Anum_pg_publication_pubtruncate - 1] =
202  BoolGetDatum(publish_truncate);
203 
204  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
205 
206  /* Insert tuple into catalog. */
207  puboid = CatalogTupleInsert(rel, tup);
208  heap_freetuple(tup);
209 
210  recordDependencyOnOwner(PublicationRelationId, puboid, GetUserId());
211 
212  ObjectAddressSet(myself, PublicationRelationId, puboid);
213 
214  /* Make the changes visible. */
216 
217  if (stmt->tables)
218  {
219  List *rels;
220 
221  Assert(list_length(stmt->tables) > 0);
222 
223  rels = OpenTableList(stmt->tables);
224  PublicationAddTables(puboid, rels, true, NULL);
225  CloseTableList(rels);
226  }
227 
229 
230  InvokeObjectPostCreateHook(PublicationRelationId, puboid, 0);
231 
232  return myself;
233 }
static void PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, AlterPublicationStmt *stmt)
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:433
Oid GetUserId(void)
Definition: miscinit.c:379
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
#define heap_close(r, l)
Definition: heapam.h:97
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:590
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:159
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:191
static void CloseTableList(List *rels)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:84
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:163
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:563
#define ereport(elevel, rest)
Definition: elog.h:122
AclResult
Definition: acl.h:178
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:914
Oid MyDatabaseId
Definition: globals.c:84
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define BoolGetDatum(X)
Definition: postgres.h:387
AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4639
#define Assert(condition)
Definition: c.h:699
static void parse_publication_options(List *options, bool *publish_given, bool *publish_insert, bool *publish_update, bool *publish_delete, bool *publish_truncate)
static int list_length(const List *l)
Definition: pg_list.h:89
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: pg_list.h:45
static List * OpenTableList(List *tables)

◆ RemovePublicationById()

void RemovePublicationById ( Oid  pubid)

Definition at line 437 of file publicationcmds.c.

References CatalogTupleDelete(), elog, ERROR, heap_close, heap_open(), HeapTupleIsValid, ObjectIdGetDatum, PUBLICATIONOID, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), and HeapTupleData::t_self.

Referenced by doDeletion().

438 {
439  Relation rel;
440  HeapTuple tup;
441 
442  rel = heap_open(PublicationRelationId, RowExclusiveLock);
443 
445 
446  if (!HeapTupleIsValid(tup))
447  elog(ERROR, "cache lookup failed for publication %u", pubid);
448 
449  CatalogTupleDelete(rel, &tup->t_self);
450 
451  ReleaseSysCache(tup);
452 
454 }
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:256
#define heap_close(r, l)
Definition: heapam.h:97
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog
Definition: elog.h:219

◆ RemovePublicationRelById()

void RemovePublicationRelById ( Oid  proid)

Definition at line 460 of file publicationcmds.c.

References CacheInvalidateRelcacheByRelid(), CatalogTupleDelete(), elog, ERROR, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, ObjectIdGetDatum, PUBLICATIONREL, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), and HeapTupleData::t_self.

Referenced by doDeletion().

461 {
462  Relation rel;
463  HeapTuple tup;
465 
466  rel = heap_open(PublicationRelRelationId, RowExclusiveLock);
467 
469 
470  if (!HeapTupleIsValid(tup))
471  elog(ERROR, "cache lookup failed for publication table %u",
472  proid);
473 
474  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
475 
476  /* Invalidate relcache so that publication info is rebuilt. */
477  CacheInvalidateRelcacheByRelid(pubrel->prrelid);
478 
479  CatalogTupleDelete(rel, &tup->t_self);
480 
481  ReleaseSysCache(tup);
482 
484 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:256
#define heap_close(r, l)
Definition: heapam.h:97
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition: inval.c:1300
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_publication_rel * Form_pg_publication_rel
#define elog
Definition: elog.h:219