PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_subscription_rel.h File Reference
#include "access/xlogdefs.h"
#include "catalog/genbki.h"
#include "catalog/pg_subscription_rel_d.h"
#include "nodes/pg_list.h"
Include dependency graph for pg_subscription_rel.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SubscriptionRelState
 
struct  LogicalRepSequenceInfo
 

Typedefs

typedef struct SubscriptionRelState SubscriptionRelState
 
typedef struct LogicalRepSequenceInfo LogicalRepSequenceInfo
 

Functions

BEGIN_CATALOG_STRUCT CATALOG (pg_subscription_rel, 6102, SubscriptionRelRelationId)
 
 DECLARE_UNIQUE_INDEX_PKEY (pg_subscription_rel_srrelid_srsubid_index, 6117, SubscriptionRelSrrelidSrsubidIndexId, pg_subscription_rel, btree(srrelid oid_ops, srsubid oid_ops))
 
 MAKE_SYSCACHE (SUBSCRIPTIONRELMAP, pg_subscription_rel_srrelid_srsubid_index, 64)
 
void AddSubscriptionRelState (Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool retain_lock)
 
void UpdateSubscriptionRelState (Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool already_locked)
 
char GetSubscriptionRelState (Oid subid, Oid relid, XLogRecPtr *sublsn)
 
void RemoveSubscriptionRel (Oid subid, Oid relid)
 
bool HasSubscriptionTables (Oid subid)
 
ListGetSubscriptionRelations (Oid subid, bool tables, bool sequences, bool not_ready)
 
void UpdateDeadTupleRetentionStatus (Oid subid, bool active)
 

Variables

END_CATALOG_STRUCT typedef FormData_pg_subscription_relForm_pg_subscription_rel
 
 FormData_pg_subscription_rel
 

Typedef Documentation

◆ LogicalRepSequenceInfo

◆ SubscriptionRelState

Function Documentation

◆ AddSubscriptionRelState()

void AddSubscriptionRelState ( Oid  subid,
Oid  relid,
char  state,
XLogRecPtr  sublsn,
bool  retain_lock 
)
extern

Definition at line 320 of file pg_subscription.c.

322{
323 Relation rel;
325 bool nulls[Natts_pg_subscription_rel];
327
329
331
332 /* Try finding existing mapping. */
334 ObjectIdGetDatum(relid),
335 ObjectIdGetDatum(subid));
337 elog(ERROR, "subscription relation %u in subscription %u already exists",
338 relid, subid);
339
340 /* Form the tuple. */
341 memset(values, 0, sizeof(values));
342 memset(nulls, false, sizeof(nulls));
348 else
349 nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
350
352
353 /* Insert tuple into catalog. */
355
357
358 /* Cleanup. */
359 if (retain_lock)
360 {
361 table_close(rel, NoLock);
362 }
363 else
364 {
367 }
368}
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1025
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
#define HeapTupleIsValid(tuple)
Definition htup.h:78
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition lmgr.c:1088
void UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition lmgr.c:1148
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
#define RowExclusiveLock
Definition lockdefs.h:38
static Datum LSNGetDatum(XLogRecPtr X)
Definition pg_lsn.h:31
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
static Datum CharGetDatum(char X)
Definition postgres.h:132
static int fb(int x)
#define RelationGetDescr(relation)
Definition rel.h:542
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition syscache.h:93
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29

References AccessShareLock, CatalogTupleInsert(), CharGetDatum(), elog, ERROR, fb(), heap_form_tuple(), heap_freetuple(), HeapTupleIsValid, LockSharedObject(), LSNGetDatum(), NoLock, ObjectIdGetDatum(), RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy2, table_close(), table_open(), UnlockSharedObject(), values, and XLogRecPtrIsValid.

Referenced by AlterSubscription_refresh(), and binary_upgrade_add_sub_rel_state().

◆ CATALOG()

Definition at line 33 of file pg_subscription_rel.h.

34{
35 Oid srsubid BKI_LOOKUP(pg_subscription); /* Oid of subscription */
36 Oid srrelid BKI_LOOKUP(pg_class); /* Oid of relation */
37 char srsubstate; /* state of the relation in subscription */
38
39 /*
40 * Although srsublsn is a fixed-width type, it is allowed to be NULL, so
41 * we prevent direct C code access to it just as for a varlena field.
42 */
43#ifdef CATALOG_VARLEN /* variable-length fields start here */
44
45 XLogRecPtr srsublsn BKI_FORCE_NULL; /* remote LSN of the state change
46 * used for synchronization
47 * coordination, or NULL if not
48 * valid */
49#endif
#define BKI_LOOKUP(catalog)
Definition genbki.h:65
#define BKI_FORCE_NULL
Definition genbki.h:51
FormData_pg_subscription_rel
unsigned int Oid
uint64 XLogRecPtr
Definition xlogdefs.h:21

References BKI_FORCE_NULL, BKI_LOOKUP, and fb().

◆ DECLARE_UNIQUE_INDEX_PKEY()

◆ GetSubscriptionRelations()

List * GetSubscriptionRelations ( Oid  subid,
bool  tables,
bool  sequences,
bool  not_ready 
)
extern

Definition at line 618 of file pg_subscription.c.

620{
621 List *res = NIL;
622 Relation rel;
624 int nkeys = 0;
625 ScanKeyData skey[2];
626 SysScanDesc scan;
627
628 /* One or both of 'tables' and 'sequences' must be true. */
629 Assert(tables || sequences);
630
632
633 ScanKeyInit(&skey[nkeys++],
636 ObjectIdGetDatum(subid));
637
638 if (not_ready)
639 ScanKeyInit(&skey[nkeys++],
643
644 scan = systable_beginscan(rel, InvalidOid, false,
645 NULL, nkeys, skey);
646
647 while (HeapTupleIsValid(tup = systable_getnext(scan)))
648 {
650 SubscriptionRelState *relstate;
651 Datum d;
652 bool isnull;
653 char relkind;
654
656
657 /* Relation is either a sequence or a table */
658 relkind = get_rel_relkind(subrel->srrelid);
659 Assert(relkind == RELKIND_SEQUENCE || relkind == RELKIND_RELATION ||
660 relkind == RELKIND_PARTITIONED_TABLE);
661
662 /* Skip sequences if they were not requested */
663 if ((relkind == RELKIND_SEQUENCE) && !sequences)
664 continue;
665
666 /* Skip tables if they were not requested */
667 if ((relkind == RELKIND_RELATION ||
668 relkind == RELKIND_PARTITIONED_TABLE) && !tables)
669 continue;
670
672 relstate->relid = subrel->srrelid;
673 relstate->state = subrel->srsubstate;
676 if (isnull)
677 relstate->lsn = InvalidXLogRecPtr;
678 else
679 relstate->lsn = DatumGetLSN(d);
680
681 res = lappend(res, relstate);
682 }
683
684 /* Cleanup */
685 systable_endscan(scan);
687
688 return res;
689}
#define Assert(condition)
Definition c.h:999
#define palloc_object(type)
Definition fe_memutils.h:89
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:604
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:515
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
static void * GETSTRUCT(const HeapTupleData *tuple)
List * lappend(List *list, void *datum)
Definition list.c:339
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2309
static SequenceItem * sequences
Definition pg_dump.c:214
#define NIL
Definition pg_list.h:68
static XLogRecPtr DatumGetLSN(Datum X)
Definition pg_lsn.h:25
END_CATALOG_STRUCT typedef FormData_pg_subscription_rel * Form_pg_subscription_rel
#define InvalidOid
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31
Definition pg_list.h:54
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596
#define InvalidXLogRecPtr
Definition xlogdefs.h:28

References AccessShareLock, Assert, BTEqualStrategyNumber, CharGetDatum(), DatumGetLSN(), fb(), Form_pg_subscription_rel, get_rel_relkind(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, InvalidXLogRecPtr, lappend(), SubscriptionRelState::lsn, NIL, ObjectIdGetDatum(), palloc_object, SubscriptionRelState::relid, ScanKeyInit(), sequences, SubscriptionRelState::state, SysCacheGetAttr(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by AlterSubscription_refresh(), AlterSubscription_refresh_seq(), DropSubscription(), and FetchRelationStates().

◆ GetSubscriptionRelState()

char GetSubscriptionRelState ( Oid  subid,
Oid  relid,
XLogRecPtr sublsn 
)
extern

Definition at line 440 of file pg_subscription.c.

441{
443 char substate;
444 bool isnull;
445 Datum d;
446 Relation rel;
447
448 /*
449 * This is to avoid the race condition with AlterSubscription which tries
450 * to remove this relstate.
451 */
453
454 /* Try finding the mapping. */
456 ObjectIdGetDatum(relid),
457 ObjectIdGetDatum(subid));
458
459 if (!HeapTupleIsValid(tup))
460 {
464 }
465
466 /* Get the state. */
468
469 /* Get the LSN */
472 if (isnull)
474 else
475 *sublsn = DatumGetLSN(d);
476
477 /* Cleanup */
479
481
482 return substate;
483}
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:231

References AccessShareLock, DatumGetLSN(), fb(), Form_pg_subscription_rel, GETSTRUCT(), HeapTupleIsValid, InvalidXLogRecPtr, ObjectIdGetDatum(), ReleaseSysCache(), SearchSysCache2(), SysCacheGetAttr(), table_close(), and table_open().

Referenced by AlterSubscription_refresh(), logicalrep_rel_open(), LogicalRepSyncTableStart(), and wait_for_table_state_change().

◆ HasSubscriptionTables()

bool HasSubscriptionTables ( Oid  subid)
extern

Definition at line 569 of file pg_subscription.c.

570{
571 Relation rel;
572 ScanKeyData skey[1];
573 SysScanDesc scan;
575 bool has_subtables = false;
576
578
579 ScanKeyInit(&skey[0],
582 ObjectIdGetDatum(subid));
583
584 scan = systable_beginscan(rel, InvalidOid, false,
585 NULL, 1, skey);
586
587 while (HeapTupleIsValid(tup = systable_getnext(scan)))
588 {
590 char relkind;
591
593 relkind = get_rel_relkind(subrel->srrelid);
594
595 if (relkind == RELKIND_RELATION ||
596 relkind == RELKIND_PARTITIONED_TABLE)
597 {
598 has_subtables = true;
599 break;
600 }
601 }
602
603 /* Cleanup */
604 systable_endscan(scan);
606
607 return has_subtables;
608}

References AccessShareLock, BTEqualStrategyNumber, fb(), Form_pg_subscription_rel, get_rel_relkind(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, ObjectIdGetDatum(), ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by FetchRelationStates().

◆ MAKE_SYSCACHE()

◆ RemoveSubscriptionRel()

void RemoveSubscriptionRel ( Oid  subid,
Oid  relid 
)
extern

Definition at line 490 of file pg_subscription.c.

491{
492 Relation rel;
493 TableScanDesc scan;
494 ScanKeyData skey[2];
496 int nkeys = 0;
497
499
500 if (OidIsValid(subid))
501 {
502 ScanKeyInit(&skey[nkeys++],
505 F_OIDEQ,
506 ObjectIdGetDatum(subid));
507 }
508
509 if (OidIsValid(relid))
510 {
511 ScanKeyInit(&skey[nkeys++],
514 F_OIDEQ,
515 ObjectIdGetDatum(relid));
516 }
517
518 /* Do the search and delete what we found. */
519 scan = table_beginscan_catalog(rel, nkeys, skey);
521 {
523
525
526 /*
527 * We don't allow to drop the relation mapping when the table
528 * synchronization is in progress unless the caller updates the
529 * corresponding subscription as well. This is to ensure that we don't
530 * leave tablesync slots or origins in the system when the
531 * corresponding table is dropped. For sequences, however, it's ok to
532 * drop them since no separate slots or origins are created during
533 * synchronization.
534 */
535 if (!OidIsValid(subid) &&
536 subrel->srsubstate != SUBREL_STATE_READY &&
538 {
541 errmsg("could not drop relation mapping for subscription \"%s\"",
542 get_subscription_name(subrel->srsubid, false)),
543 errdetail("Table synchronization for relation \"%s\" is in progress and is in state \"%c\".",
544 get_rel_name(relid), subrel->srsubstate),
545
546 /*
547 * translator: first %s is a SQL ALTER command and second %s is a
548 * SQL DROP command
549 */
550 errhint("Use %s to enable subscription if not already enabled or use %s to drop the subscription.",
551 "ALTER SUBSCRIPTION ... ENABLE",
552 "DROP SUBSCRIPTION ...")));
553 }
554
555 CatalogTupleDelete(rel, &tup->t_self);
556 }
557 table_endscan(scan);
558
560}
#define OidIsValid(objectId)
Definition c.h:914
int errcode(int sqlerrcode)
Definition elog.c:875
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ereport(elevel,...)
Definition elog.h:152
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition heapam.c:1435
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
char * get_rel_name(Oid relid)
Definition lsyscache.c:2234
char * get_subscription_name(Oid subid, bool missing_ok)
Definition lsyscache.c:4048
static char * errmsg
@ ForwardScanDirection
Definition sdir.h:28
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKeyData *key)
Definition tableam.c:113
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1061

References BTEqualStrategyNumber, CatalogTupleDelete(), ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, fb(), Form_pg_subscription_rel, ForwardScanDirection, get_rel_name(), get_rel_relkind(), get_subscription_name(), GETSTRUCT(), heap_getnext(), HeapTupleIsValid, ObjectIdGetDatum(), OidIsValid, RowExclusiveLock, ScanKeyInit(), table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by AlterSubscription_refresh(), DropSubscription(), and heap_drop_with_catalog().

◆ UpdateDeadTupleRetentionStatus()

void UpdateDeadTupleRetentionStatus ( Oid  subid,
bool  active 
)
extern

Definition at line 695 of file pg_subscription.c.

696{
697 Relation rel;
698 bool nulls[Natts_pg_subscription];
702
703 /* Look up the subscription in the catalog */
706
707 if (!HeapTupleIsValid(tup))
708 elog(ERROR, "cache lookup failed for subscription %u", subid);
709
711
712 /* Form a new tuple. */
713 memset(values, 0, sizeof(values));
714 memset(nulls, false, sizeof(nulls));
715 memset(replaces, false, sizeof(replaces));
716
717 /* Set the subscription to disabled. */
720
721 /* Update the catalog */
723 replaces);
724 CatalogTupleUpdate(rel, &tup->t_self, tup);
726
727 table_close(rel, NoLock);
728}
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1118
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
#define SearchSysCacheCopy1(cacheId, key1)
Definition syscache.h:91

References AccessShareLock, BoolGetDatum(), CatalogTupleUpdate(), elog, ERROR, fb(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, LockSharedObject(), NoLock, ObjectIdGetDatum(), RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy1, table_close(), table_open(), and values.

Referenced by update_retention_status().

◆ UpdateSubscriptionRelState()

void UpdateSubscriptionRelState ( Oid  subid,
Oid  relid,
char  state,
XLogRecPtr  sublsn,
bool  already_locked 
)
extern

Definition at line 374 of file pg_subscription.c.

376{
377 Relation rel;
379 bool nulls[Natts_pg_subscription_rel];
382
383 if (already_locked)
384 {
385#ifdef USE_ASSERT_CHECKING
386 LOCKTAG tag;
387
389 RowExclusiveLock, true));
391 Assert(LockHeldByMe(&tag, AccessShareLock, true));
392#endif
393
395 }
396 else
397 {
400 }
401
402 /* Try finding existing mapping. */
404 ObjectIdGetDatum(relid),
405 ObjectIdGetDatum(subid));
406 if (!HeapTupleIsValid(tup))
407 elog(ERROR, "subscription relation %u in subscription %u does not exist",
408 relid, subid);
409
410 /* Update the tuple. */
411 memset(values, 0, sizeof(values));
412 memset(nulls, false, sizeof(nulls));
413 memset(replaces, false, sizeof(replaces));
414
417
421 else
422 nulls[Anum_pg_subscription_rel_srsublsn - 1] = true;
423
425 replaces);
426
427 /* Update the catalog. */
428 CatalogTupleUpdate(rel, &tup->t_self, tup);
429
430 /* Cleanup. */
431 table_close(rel, NoLock);
432}
bool CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode, bool orstronger)
Definition lmgr.c:351
bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode, bool orstronger)
Definition lock.c:640
#define SET_LOCKTAG_OBJECT(locktag, dboid, classoid, objoid, objsubid)
Definition locktag.h:162

References AccessShareLock, Assert, CatalogTupleUpdate(), CharGetDatum(), CheckRelationOidLockedByMe(), elog, ERROR, fb(), heap_modify_tuple(), HeapTupleIsValid, InvalidOid, LockHeldByMe(), LockSharedObject(), LSNGetDatum(), NoLock, ObjectIdGetDatum(), RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy2, SET_LOCKTAG_OBJECT, table_close(), table_open(), values, and XLogRecPtrIsValid.

Referenced by AlterSubscription_refresh_seq(), copy_sequence(), LogicalRepSyncTableStart(), ProcessSyncingTablesForApply(), and ProcessSyncingTablesForSync().

Variable Documentation

◆ Form_pg_subscription_rel

◆ FormData_pg_subscription_rel

FormData_pg_subscription_rel

Definition at line 50 of file pg_subscription_rel.h.