PostgreSQL Source Code  git master
execReplication.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/relscan.h"
#include "access/tableam.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/pg_am_d.h"
#include "commands/trigger.h"
#include "executor/executor.h"
#include "executor/nodeModifyTable.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
#include "replication/logicalrelation.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for execReplication.c:

Go to the source code of this file.

Functions

static bool tuples_equal (TupleTableSlot *slot1, TupleTableSlot *slot2, TypeCacheEntry **eq)
 
StrategyNumber get_equal_strategy_number_for_am (Oid am)
 
static StrategyNumber get_equal_strategy_number (Oid opclass)
 
static int build_replindex_scan_key (ScanKey skey, Relation rel, Relation idxrel, TupleTableSlot *searchslot)
 
bool RelationFindReplTupleByIndex (Relation rel, Oid idxoid, LockTupleMode lockmode, TupleTableSlot *searchslot, TupleTableSlot *outslot)
 
bool RelationFindReplTupleSeq (Relation rel, LockTupleMode lockmode, TupleTableSlot *searchslot, TupleTableSlot *outslot)
 
void ExecSimpleRelationInsert (ResultRelInfo *resultRelInfo, EState *estate, TupleTableSlot *slot)
 
void ExecSimpleRelationUpdate (ResultRelInfo *resultRelInfo, EState *estate, EPQState *epqstate, TupleTableSlot *searchslot, TupleTableSlot *slot)
 
void ExecSimpleRelationDelete (ResultRelInfo *resultRelInfo, EState *estate, EPQState *epqstate, TupleTableSlot *searchslot)
 
void CheckCmdReplicaIdentity (Relation rel, CmdType cmd)
 
void CheckSubscriptionRelkind (char relkind, const char *nspname, const char *relname)
 

Function Documentation

◆ build_replindex_scan_key()

static int build_replindex_scan_key ( ScanKey  skey,
Relation  rel,
Relation  idxrel,
TupleTableSlot searchslot 
)
static

Definition at line 102 of file execReplication.c.

104 {
105  int index_attoff;
106  int skey_attoff = 0;
107  Datum indclassDatum;
108  oidvector *opclass;
109  int2vector *indkey = &idxrel->rd_index->indkey;
110 
111  indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, idxrel->rd_indextuple,
112  Anum_pg_index_indclass);
113  opclass = (oidvector *) DatumGetPointer(indclassDatum);
114 
115  /* Build scankey for every non-expression attribute in the index. */
116  for (index_attoff = 0; index_attoff < IndexRelationGetNumberOfKeyAttributes(idxrel);
117  index_attoff++)
118  {
119  Oid operator;
120  Oid optype;
121  Oid opfamily;
122  RegProcedure regop;
123  int table_attno = indkey->values[index_attoff];
124  StrategyNumber eq_strategy;
125 
126  if (!AttributeNumberIsValid(table_attno))
127  {
128  /*
129  * XXX: Currently, we don't support expressions in the scan key,
130  * see code below.
131  */
132  continue;
133  }
134 
135  /*
136  * Load the operator info. We need this to get the equality operator
137  * function for the scan key.
138  */
139  optype = get_opclass_input_type(opclass->values[index_attoff]);
140  opfamily = get_opclass_family(opclass->values[index_attoff]);
141  eq_strategy = get_equal_strategy_number(opclass->values[index_attoff]);
142 
143  operator = get_opfamily_member(opfamily, optype,
144  optype,
145  eq_strategy);
146 
147  if (!OidIsValid(operator))
148  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
149  eq_strategy, optype, optype, opfamily);
150 
151  regop = get_opcode(operator);
152 
153  /* Initialize the scankey. */
154  ScanKeyInit(&skey[skey_attoff],
155  index_attoff + 1,
156  eq_strategy,
157  regop,
158  searchslot->tts_values[table_attno - 1]);
159 
160  skey[skey_attoff].sk_collation = idxrel->rd_indcollation[index_attoff];
161 
162  /* Check for null value. */
163  if (searchslot->tts_isnull[table_attno - 1])
164  skey[skey_attoff].sk_flags |= (SK_ISNULL | SK_SEARCHNULL);
165 
166  skey_attoff++;
167  }
168 
169  /* There must always be at least one attribute for the index scan. */
170  Assert(skey_attoff > 0);
171 
172  return skey_attoff;
173 }
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
regproc RegProcedure
Definition: c.h:639
#define OidIsValid(objectId)
Definition: c.h:764
#define ERROR
Definition: elog.h:39
static StrategyNumber get_equal_strategy_number(Oid opclass)
Assert(fmt[strlen(fmt) - 1] !='\n')
Oid get_opclass_input_type(Oid opclass)
Definition: lsyscache.c:1189
Oid get_opclass_family(Oid opclass)
Definition: lsyscache.c:1167
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1262
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:165
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
unsigned int Oid
Definition: postgres_ext.h:31
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition: rel.h:523
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define SK_SEARCHNULL
Definition: skey.h:121
#define SK_ISNULL
Definition: skey.h:115
uint16 StrategyNumber
Definition: stratnum.h:22
struct HeapTupleData * rd_indextuple
Definition: rel.h:193
Form_pg_index rd_index
Definition: rel.h:191
Oid * rd_indcollation
Definition: rel.h:216
int sk_flags
Definition: skey.h:66
Oid sk_collation
Definition: skey.h:70
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
Definition: c.h:704
int16 values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:711
Definition: c.h:715
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:722
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:511

References Assert(), AttributeNumberIsValid, DatumGetPointer(), elog(), ERROR, get_equal_strategy_number(), get_opclass_family(), get_opclass_input_type(), get_opcode(), get_opfamily_member(), IndexRelationGetNumberOfKeyAttributes, OidIsValid, RelationData::rd_indcollation, RelationData::rd_index, RelationData::rd_indextuple, ScanKeyInit(), ScanKeyData::sk_collation, ScanKeyData::sk_flags, SK_ISNULL, SK_SEARCHNULL, SysCacheGetAttrNotNull(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, int2vector::values, and oidvector::values.

Referenced by RelationFindReplTupleByIndex().

◆ CheckCmdReplicaIdentity()

void CheckCmdReplicaIdentity ( Relation  rel,
CmdType  cmd 
)

Definition at line 658 of file execReplication.c.

659 {
660  PublicationDesc pubdesc;
661 
662  /*
663  * Skip checking the replica identity for partitioned tables, because the
664  * operations are actually performed on the leaf partitions.
665  */
666  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
667  return;
668 
669  /* We only need to do checks for UPDATE and DELETE. */
670  if (cmd != CMD_UPDATE && cmd != CMD_DELETE)
671  return;
672 
673  /*
674  * It is only safe to execute UPDATE/DELETE when all columns, referenced
675  * in the row filters from publications which the relation is in, are
676  * valid - i.e. when all referenced columns are part of REPLICA IDENTITY
677  * or the table does not publish UPDATEs or DELETEs.
678  *
679  * XXX We could optimize it by first checking whether any of the
680  * publications have a row filter for this relation. If not and relation
681  * has replica identity then we can avoid building the descriptor but as
682  * this happens only one time it doesn't seem worth the additional
683  * complexity.
684  */
685  RelationBuildPublicationDesc(rel, &pubdesc);
686  if (cmd == CMD_UPDATE && !pubdesc.rf_valid_for_update)
687  ereport(ERROR,
688  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
689  errmsg("cannot update table \"%s\"",
691  errdetail("Column used in the publication WHERE expression is not part of the replica identity.")));
692  else if (cmd == CMD_UPDATE && !pubdesc.cols_valid_for_update)
693  ereport(ERROR,
694  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
695  errmsg("cannot update table \"%s\"",
697  errdetail("Column list used by the publication does not cover the replica identity.")));
698  else if (cmd == CMD_DELETE && !pubdesc.rf_valid_for_delete)
699  ereport(ERROR,
700  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
701  errmsg("cannot delete from table \"%s\"",
703  errdetail("Column used in the publication WHERE expression is not part of the replica identity.")));
704  else if (cmd == CMD_DELETE && !pubdesc.cols_valid_for_delete)
705  ereport(ERROR,
706  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
707  errmsg("cannot delete from table \"%s\"",
709  errdetail("Column list used by the publication does not cover the replica identity.")));
710 
711  /* If relation has replica identity we are always good. */
713  return;
714 
715  /* REPLICA IDENTITY FULL is also good for UPDATE/DELETE. */
716  if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
717  return;
718 
719  /*
720  * This is UPDATE/DELETE and there is no replica identity.
721  *
722  * Check if the table publishes UPDATES or DELETES.
723  */
724  if (cmd == CMD_UPDATE && pubdesc.pubactions.pubupdate)
725  ereport(ERROR,
726  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
727  errmsg("cannot update table \"%s\" because it does not have a replica identity and publishes updates",
729  errhint("To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.")));
730  else if (cmd == CMD_DELETE && pubdesc.pubactions.pubdelete)
731  ereport(ERROR,
732  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
733  errmsg("cannot delete from table \"%s\" because it does not have a replica identity and publishes deletes",
735  errhint("To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.")));
736 }
int errdetail(const char *fmt,...)
Definition: elog.c:1208
int errhint(const char *fmt,...)
Definition: elog.c:1322
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#define ereport(elevel,...)
Definition: elog.h:149
@ CMD_DELETE
Definition: nodes.h:258
@ CMD_UPDATE
Definition: nodes.h:256
#define RelationGetRelationName(relation)
Definition: rel.h:538
void RelationBuildPublicationDesc(Relation relation, PublicationDesc *pubdesc)
Definition: relcache.c:5698
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:4986
PublicationActions pubactions
bool cols_valid_for_delete
bool cols_valid_for_update
Form_pg_class rd_rel
Definition: rel.h:111

References CMD_DELETE, CMD_UPDATE, PublicationDesc::cols_valid_for_delete, PublicationDesc::cols_valid_for_update, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, OidIsValid, PublicationDesc::pubactions, PublicationActions::pubdelete, PublicationActions::pubupdate, RelationData::rd_rel, RelationBuildPublicationDesc(), RelationGetRelationName, RelationGetReplicaIndex(), PublicationDesc::rf_valid_for_delete, and PublicationDesc::rf_valid_for_update.

Referenced by CheckValidResultRel(), ExecSimpleRelationDelete(), ExecSimpleRelationInsert(), and ExecSimpleRelationUpdate().

◆ CheckSubscriptionRelkind()

void CheckSubscriptionRelkind ( char  relkind,
const char *  nspname,
const char *  relname 
)

Definition at line 745 of file execReplication.c.

747 {
748  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
749  ereport(ERROR,
750  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
751  errmsg("cannot use relation \"%s.%s\" as logical replication target",
752  nspname, relname),
754 }
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
NameData relname
Definition: pg_class.h:38

References ereport, errcode(), errdetail_relkind_not_supported(), errmsg(), ERROR, and relname.

Referenced by AlterSubscription_refresh(), apply_handle_tuple_routing(), CreateSubscription(), and logicalrep_rel_open().

◆ ExecSimpleRelationDelete()

void ExecSimpleRelationDelete ( ResultRelInfo resultRelInfo,
EState estate,
EPQState epqstate,
TupleTableSlot searchslot 
)

Definition at line 625 of file execReplication.c.

628 {
629  bool skip_tuple = false;
630  Relation rel = resultRelInfo->ri_RelationDesc;
631  ItemPointer tid = &searchslot->tts_tid;
632 
634 
635  /* BEFORE ROW DELETE Triggers */
636  if (resultRelInfo->ri_TrigDesc &&
637  resultRelInfo->ri_TrigDesc->trig_delete_before_row)
638  {
639  skip_tuple = !ExecBRDeleteTriggers(estate, epqstate, resultRelInfo,
640  tid, NULL, NULL, NULL, NULL);
641  }
642 
643  if (!skip_tuple)
644  {
645  /* OK, delete the tuple */
646  simple_table_tuple_delete(rel, tid, estate->es_snapshot);
647 
648  /* AFTER ROW DELETE Triggers */
649  ExecARDeleteTriggers(estate, resultRelInfo,
650  tid, NULL, NULL, false);
651  }
652 }
void CheckCmdReplicaIdentity(Relation rel, CmdType cmd)
Snapshot es_snapshot
Definition: execnodes.h:622
Relation ri_RelationDesc
Definition: execnodes.h:456
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:486
bool trig_delete_before_row
Definition: reltrigger.h:66
ItemPointerData tts_tid
Definition: tuptable.h:129
void simple_table_tuple_delete(Relation rel, ItemPointer tid, Snapshot snapshot)
Definition: tableam.c:300
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:2781
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot **epqslot, TM_Result *tmresult, TM_FailureData *tmfd)
Definition: trigger.c:2690

References CheckCmdReplicaIdentity(), CMD_DELETE, EState::es_snapshot, ExecARDeleteTriggers(), ExecBRDeleteTriggers(), ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, simple_table_tuple_delete(), TriggerDesc::trig_delete_before_row, and TupleTableSlot::tts_tid.

Referenced by apply_handle_delete_internal().

◆ ExecSimpleRelationInsert()

void ExecSimpleRelationInsert ( ResultRelInfo resultRelInfo,
EState estate,
TupleTableSlot slot 
)

Definition at line 496 of file execReplication.c.

498 {
499  bool skip_tuple = false;
500  Relation rel = resultRelInfo->ri_RelationDesc;
501 
502  /* For now we support only tables. */
503  Assert(rel->rd_rel->relkind == RELKIND_RELATION);
504 
506 
507  /* BEFORE ROW INSERT Triggers */
508  if (resultRelInfo->ri_TrigDesc &&
509  resultRelInfo->ri_TrigDesc->trig_insert_before_row)
510  {
511  if (!ExecBRInsertTriggers(estate, resultRelInfo, slot))
512  skip_tuple = true; /* "do nothing" */
513  }
514 
515  if (!skip_tuple)
516  {
517  List *recheckIndexes = NIL;
518 
519  /* Compute stored generated columns */
520  if (rel->rd_att->constr &&
522  ExecComputeStoredGenerated(resultRelInfo, estate, slot,
523  CMD_INSERT);
524 
525  /* Check the constraints of the tuple */
526  if (rel->rd_att->constr)
527  ExecConstraints(resultRelInfo, slot, estate);
528  if (rel->rd_rel->relispartition)
529  ExecPartitionCheck(resultRelInfo, slot, estate, true);
530 
531  /* OK, store the tuple and create index entries for it */
532  simple_table_tuple_insert(resultRelInfo->ri_RelationDesc, slot);
533 
534  if (resultRelInfo->ri_NumIndices > 0)
535  recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
536  slot, estate, false, false,
537  NULL, NIL, false);
538 
539  /* AFTER ROW INSERT Triggers */
540  ExecARInsertTriggers(estate, resultRelInfo, slot,
541  recheckIndexes, NULL);
542 
543  /*
544  * XXX we should in theory pass a TransitionCaptureState object to the
545  * above to capture transition tuples, but after statement triggers
546  * don't actually get fired by replication yet anyway
547  */
548 
549  list_free(recheckIndexes);
550  }
551 }
List * ExecInsertIndexTuples(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool update, bool noDupErr, bool *specConflict, List *arbiterIndexes, bool onlySummarizing)
Definition: execIndexing.c:298
bool ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool emitError)
Definition: execMain.c:1816
void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:1940
void list_free(List *list)
Definition: list.c:1546
void ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo, EState *estate, TupleTableSlot *slot, CmdType cmdtype)
@ CMD_INSERT
Definition: nodes.h:257
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54
TupleDesc rd_att
Definition: rel.h:112
int ri_NumIndices
Definition: execnodes.h:459
bool trig_insert_before_row
Definition: reltrigger.h:56
bool has_generated_stored
Definition: tupdesc.h:45
TupleConstr * constr
Definition: tupdesc.h:85
void simple_table_tuple_insert(Relation rel, TupleTableSlot *slot)
Definition: tableam.c:286
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2465
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2541

References Assert(), CheckCmdReplicaIdentity(), CMD_INSERT, TupleDescData::constr, ExecARInsertTriggers(), ExecBRInsertTriggers(), ExecComputeStoredGenerated(), ExecConstraints(), ExecInsertIndexTuples(), ExecPartitionCheck(), TupleConstr::has_generated_stored, list_free(), NIL, RelationData::rd_att, RelationData::rd_rel, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, simple_table_tuple_insert(), and TriggerDesc::trig_insert_before_row.

Referenced by apply_handle_insert_internal().

◆ ExecSimpleRelationUpdate()

void ExecSimpleRelationUpdate ( ResultRelInfo resultRelInfo,
EState estate,
EPQState epqstate,
TupleTableSlot searchslot,
TupleTableSlot slot 
)

Definition at line 560 of file execReplication.c.

563 {
564  bool skip_tuple = false;
565  Relation rel = resultRelInfo->ri_RelationDesc;
566  ItemPointer tid = &(searchslot->tts_tid);
567 
568  /* For now we support only tables. */
569  Assert(rel->rd_rel->relkind == RELKIND_RELATION);
570 
572 
573  /* BEFORE ROW UPDATE Triggers */
574  if (resultRelInfo->ri_TrigDesc &&
575  resultRelInfo->ri_TrigDesc->trig_update_before_row)
576  {
577  if (!ExecBRUpdateTriggers(estate, epqstate, resultRelInfo,
578  tid, NULL, slot, NULL, NULL))
579  skip_tuple = true; /* "do nothing" */
580  }
581 
582  if (!skip_tuple)
583  {
584  List *recheckIndexes = NIL;
585  TU_UpdateIndexes update_indexes;
586 
587  /* Compute stored generated columns */
588  if (rel->rd_att->constr &&
590  ExecComputeStoredGenerated(resultRelInfo, estate, slot,
591  CMD_UPDATE);
592 
593  /* Check the constraints of the tuple */
594  if (rel->rd_att->constr)
595  ExecConstraints(resultRelInfo, slot, estate);
596  if (rel->rd_rel->relispartition)
597  ExecPartitionCheck(resultRelInfo, slot, estate, true);
598 
599  simple_table_tuple_update(rel, tid, slot, estate->es_snapshot,
600  &update_indexes);
601 
602  if (resultRelInfo->ri_NumIndices > 0 && (update_indexes != TU_None))
603  recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
604  slot, estate, true, false,
605  NULL, NIL,
606  (update_indexes == TU_Summarizing));
607 
608  /* AFTER ROW UPDATE Triggers */
609  ExecARUpdateTriggers(estate, resultRelInfo,
610  NULL, NULL,
611  tid, NULL, slot,
612  recheckIndexes, NULL, false);
613 
614  list_free(recheckIndexes);
615  }
616 }
bool trig_update_before_row
Definition: reltrigger.h:61
void simple_table_tuple_update(Relation rel, ItemPointer otid, TupleTableSlot *slot, Snapshot snapshot, TU_UpdateIndexes *update_indexes)
Definition: tableam.c:345
TU_UpdateIndexes
Definition: tableam.h:110
@ TU_Summarizing
Definition: tableam.h:118
@ TU_None
Definition: tableam.h:112
bool ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *newslot, TM_Result *tmresult, TM_FailureData *tmfd)
Definition: trigger.c:2941
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ResultRelInfo *src_partinfo, ResultRelInfo *dst_partinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *newslot, List *recheckIndexes, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:3104

References Assert(), CheckCmdReplicaIdentity(), CMD_UPDATE, TupleDescData::constr, EState::es_snapshot, ExecARUpdateTriggers(), ExecBRUpdateTriggers(), ExecComputeStoredGenerated(), ExecConstraints(), ExecInsertIndexTuples(), ExecPartitionCheck(), TupleConstr::has_generated_stored, list_free(), NIL, RelationData::rd_att, RelationData::rd_rel, ResultRelInfo::ri_NumIndices, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_TrigDesc, simple_table_tuple_update(), TriggerDesc::trig_update_before_row, TupleTableSlot::tts_tid, TU_None, and TU_Summarizing.

Referenced by apply_handle_tuple_routing(), and apply_handle_update_internal().

◆ get_equal_strategy_number()

static StrategyNumber get_equal_strategy_number ( Oid  opclass)
static

Definition at line 81 of file execReplication.c.

82 {
83  Oid am = get_opclass_method(opclass);
84 
86 }
StrategyNumber get_equal_strategy_number_for_am(Oid am)
Oid get_opclass_method(Oid opclass)
Definition: lsyscache.c:1237

References get_equal_strategy_number_for_am(), and get_opclass_method().

Referenced by build_replindex_scan_key().

◆ get_equal_strategy_number_for_am()

StrategyNumber get_equal_strategy_number_for_am ( Oid  am)

Definition at line 55 of file execReplication.c.

56 {
57  int ret;
58 
59  switch (am)
60  {
61  case BTREE_AM_OID:
63  break;
64  case HASH_AM_OID:
66  break;
67  default:
68  /* XXX: Only Btree and Hash indexes are supported */
69  ret = InvalidStrategy;
70  break;
71  }
72 
73  return ret;
74 }
#define InvalidStrategy
Definition: stratnum.h:24
#define HTEqualStrategyNumber
Definition: stratnum.h:41
#define BTEqualStrategyNumber
Definition: stratnum.h:31

References BTEqualStrategyNumber, HTEqualStrategyNumber, and InvalidStrategy.

Referenced by get_equal_strategy_number(), and IsIndexUsableForReplicaIdentityFull().

◆ RelationFindReplTupleByIndex()

bool RelationFindReplTupleByIndex ( Relation  rel,
Oid  idxoid,
LockTupleMode  lockmode,
TupleTableSlot searchslot,
TupleTableSlot outslot 
)

Definition at line 182 of file execReplication.c.

186 {
188  int skey_attoff;
189  IndexScanDesc scan;
190  SnapshotData snap;
191  TransactionId xwait;
192  Relation idxrel;
193  bool found;
194  TypeCacheEntry **eq = NULL;
195  bool isIdxSafeToSkipDuplicates;
196 
197  /* Open the index. */
198  idxrel = index_open(idxoid, RowExclusiveLock);
199 
200  isIdxSafeToSkipDuplicates = (GetRelationIdentityOrPK(rel) == idxoid);
201 
202  InitDirtySnapshot(snap);
203 
204  /* Build scan key. */
205  skey_attoff = build_replindex_scan_key(skey, rel, idxrel, searchslot);
206 
207  /* Start an index scan. */
208  scan = index_beginscan(rel, idxrel, &snap, skey_attoff, 0);
209 
210 retry:
211  found = false;
212 
213  index_rescan(scan, skey, skey_attoff, NULL, 0);
214 
215  /* Try to find the tuple */
216  while (index_getnext_slot(scan, ForwardScanDirection, outslot))
217  {
218  /*
219  * Avoid expensive equality check if the index is primary key or
220  * replica identity index.
221  */
222  if (!isIdxSafeToSkipDuplicates)
223  {
224  if (eq == NULL)
225  eq = palloc0(sizeof(*eq) * outslot->tts_tupleDescriptor->natts);
226 
227  if (!tuples_equal(outslot, searchslot, eq))
228  continue;
229  }
230 
231  ExecMaterializeSlot(outslot);
232 
233  xwait = TransactionIdIsValid(snap.xmin) ?
234  snap.xmin : snap.xmax;
235 
236  /*
237  * If the tuple is locked, wait for locking transaction to finish and
238  * retry.
239  */
240  if (TransactionIdIsValid(xwait))
241  {
242  XactLockTableWait(xwait, NULL, NULL, XLTW_None);
243  goto retry;
244  }
245 
246  /* Found our tuple and it's not locked */
247  found = true;
248  break;
249  }
250 
251  /* Found tuple, try to lock it in the lockmode. */
252  if (found)
253  {
254  TM_FailureData tmfd;
255  TM_Result res;
256 
258 
259  res = table_tuple_lock(rel, &(outslot->tts_tid), GetLatestSnapshot(),
260  outslot,
261  GetCurrentCommandId(false),
262  lockmode,
264  0 /* don't follow updates */ ,
265  &tmfd);
266 
268 
269  switch (res)
270  {
271  case TM_Ok:
272  break;
273  case TM_Updated:
274  /* XXX: Improve handling here */
276  ereport(LOG,
278  errmsg("tuple to be locked was already moved to another partition due to concurrent update, retrying")));
279  else
280  ereport(LOG,
282  errmsg("concurrent update, retrying")));
283  goto retry;
284  case TM_Deleted:
285  /* XXX: Improve handling here */
286  ereport(LOG,
288  errmsg("concurrent delete, retrying")));
289  goto retry;
290  case TM_Invisible:
291  elog(ERROR, "attempted to lock invisible tuple");
292  break;
293  default:
294  elog(ERROR, "unexpected table_tuple_lock status: %u", res);
295  break;
296  }
297  }
298 
299  index_endscan(scan);
300 
301  /* Don't release lock until commit. */
302  index_close(idxrel, NoLock);
303 
304  return found;
305 }
uint32 TransactionId
Definition: c.h:641
#define LOG
Definition: elog.h:31
static bool tuples_equal(TupleTableSlot *slot1, TupleTableSlot *slot2, TypeCacheEntry **eq)
static int build_replindex_scan_key(ScanKey skey, Relation rel, Relation idxrel, TupleTableSlot *searchslot)
bool index_getnext_slot(IndexScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
Definition: indexam.c:676
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys)
Definition: indexam.c:257
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:379
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:353
static bool ItemPointerIndicatesMovedPartitions(const ItemPointerData *pointer)
Definition: itemptr.h:197
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:668
@ XLTW_None
Definition: lmgr.h:26
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
@ LockWaitBlock
Definition: lockoptions.h:39
void * palloc0(Size size)
Definition: mcxt.c:1232
#define INDEX_MAX_KEYS
#define ERRCODE_T_R_SERIALIZATION_FAILURE
Definition: pgbench.c:76
@ ForwardScanDirection
Definition: sdir.h:28
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:298
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:655
void PopActiveSnapshot(void)
Definition: snapmgr.c:750
#define InitDirtySnapshot(snapshotdata)
Definition: snapmgr.h:40
Oid GetRelationIdentityOrPK(Relation rel)
Definition: relation.c:852
TransactionId xmin
Definition: snapshot.h:157
TransactionId xmax
Definition: snapshot.h:158
ItemPointerData ctid
Definition: tableam.h:142
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:123
TM_Result
Definition: tableam.h:72
@ TM_Ok
Definition: tableam.h:77
@ TM_Deleted
Definition: tableam.h:92
@ TM_Updated
Definition: tableam.h:89
@ TM_Invisible
Definition: tableam.h:80
static TM_Result table_tuple_lock(Relation rel, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, uint8 flags, TM_FailureData *tmfd)
Definition: tableam.h:1575
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void ExecMaterializeSlot(TupleTableSlot *slot)
Definition: tuptable.h:451
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:818

References build_replindex_scan_key(), TM_FailureData::ctid, elog(), ereport, errcode(), ERRCODE_T_R_SERIALIZATION_FAILURE, errmsg(), ERROR, ExecMaterializeSlot(), ForwardScanDirection, GetCurrentCommandId(), GetLatestSnapshot(), GetRelationIdentityOrPK(), index_beginscan(), index_close(), index_endscan(), index_getnext_slot(), INDEX_MAX_KEYS, index_open(), index_rescan(), InitDirtySnapshot, ItemPointerIndicatesMovedPartitions(), LockWaitBlock, LOG, TupleDescData::natts, NoLock, palloc0(), PopActiveSnapshot(), PushActiveSnapshot(), res, RowExclusiveLock, table_tuple_lock(), TM_Deleted, TM_Invisible, TM_Ok, TM_Updated, TransactionIdIsValid, TupleTableSlot::tts_tid, TupleTableSlot::tts_tupleDescriptor, tuples_equal(), XactLockTableWait(), XLTW_None, SnapshotData::xmax, and SnapshotData::xmin.

Referenced by FindReplTupleInLocalRel().

◆ RelationFindReplTupleSeq()

bool RelationFindReplTupleSeq ( Relation  rel,
LockTupleMode  lockmode,
TupleTableSlot searchslot,
TupleTableSlot outslot 
)

Definition at line 384 of file execReplication.c.

386 {
387  TupleTableSlot *scanslot;
388  TableScanDesc scan;
389  SnapshotData snap;
390  TypeCacheEntry **eq;
391  TransactionId xwait;
392  bool found;
394 
395  Assert(equalTupleDescs(desc, outslot->tts_tupleDescriptor));
396 
397  eq = palloc0(sizeof(*eq) * outslot->tts_tupleDescriptor->natts);
398 
399  /* Start a heap scan. */
400  InitDirtySnapshot(snap);
401  scan = table_beginscan(rel, &snap, 0, NULL);
402  scanslot = table_slot_create(rel, NULL);
403 
404 retry:
405  found = false;
406 
407  table_rescan(scan, NULL);
408 
409  /* Try to find the tuple */
410  while (table_scan_getnextslot(scan, ForwardScanDirection, scanslot))
411  {
412  if (!tuples_equal(scanslot, searchslot, eq))
413  continue;
414 
415  found = true;
416  ExecCopySlot(outslot, scanslot);
417 
418  xwait = TransactionIdIsValid(snap.xmin) ?
419  snap.xmin : snap.xmax;
420 
421  /*
422  * If the tuple is locked, wait for locking transaction to finish and
423  * retry.
424  */
425  if (TransactionIdIsValid(xwait))
426  {
427  XactLockTableWait(xwait, NULL, NULL, XLTW_None);
428  goto retry;
429  }
430 
431  /* Found our tuple and it's not locked */
432  break;
433  }
434 
435  /* Found tuple, try to lock it in the lockmode. */
436  if (found)
437  {
438  TM_FailureData tmfd;
439  TM_Result res;
440 
442 
443  res = table_tuple_lock(rel, &(outslot->tts_tid), GetLatestSnapshot(),
444  outslot,
445  GetCurrentCommandId(false),
446  lockmode,
448  0 /* don't follow updates */ ,
449  &tmfd);
450 
452 
453  switch (res)
454  {
455  case TM_Ok:
456  break;
457  case TM_Updated:
458  /* XXX: Improve handling here */
460  ereport(LOG,
462  errmsg("tuple to be locked was already moved to another partition due to concurrent update, retrying")));
463  else
464  ereport(LOG,
466  errmsg("concurrent update, retrying")));
467  goto retry;
468  case TM_Deleted:
469  /* XXX: Improve handling here */
470  ereport(LOG,
472  errmsg("concurrent delete, retrying")));
473  goto retry;
474  case TM_Invisible:
475  elog(ERROR, "attempted to lock invisible tuple");
476  break;
477  default:
478  elog(ERROR, "unexpected table_tuple_lock status: %u", res);
479  break;
480  }
481  }
482 
483  table_endscan(scan);
485 
486  return found;
487 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:171
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1253
#define RelationGetDescr(relation)
Definition: rel.h:530
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:91
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key)
Definition: tableam.h:901
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1009
static void table_rescan(TableScanDesc scan, struct ScanKeyData *key)
Definition: tableam.h:1018
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition: tableam.h:1050
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:424
static TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: tuptable.h:488

References Assert(), TM_FailureData::ctid, elog(), equalTupleDescs(), ereport, errcode(), ERRCODE_T_R_SERIALIZATION_FAILURE, errmsg(), ERROR, ExecCopySlot(), ExecDropSingleTupleTableSlot(), ForwardScanDirection, GetCurrentCommandId(), GetLatestSnapshot(), InitDirtySnapshot, ItemPointerIndicatesMovedPartitions(), LockWaitBlock, LOG, TupleDescData::natts, palloc0(), PG_USED_FOR_ASSERTS_ONLY, PopActiveSnapshot(), PushActiveSnapshot(), RelationGetDescr, res, table_beginscan(), table_endscan(), table_rescan(), table_scan_getnextslot(), table_slot_create(), table_tuple_lock(), TM_Deleted, TM_Invisible, TM_Ok, TM_Updated, TransactionIdIsValid, TupleTableSlot::tts_tid, TupleTableSlot::tts_tupleDescriptor, tuples_equal(), XactLockTableWait(), XLTW_None, SnapshotData::xmax, and SnapshotData::xmin.

Referenced by FindReplTupleInLocalRel().

◆ tuples_equal()

static bool tuples_equal ( TupleTableSlot slot1,
TupleTableSlot slot2,
TypeCacheEntry **  eq 
)
static

Definition at line 311 of file execReplication.c.

313 {
314  int attrnum;
315 
316  Assert(slot1->tts_tupleDescriptor->natts ==
317  slot2->tts_tupleDescriptor->natts);
318 
319  slot_getallattrs(slot1);
320  slot_getallattrs(slot2);
321 
322  /* Check equality of the attributes. */
323  for (attrnum = 0; attrnum < slot1->tts_tupleDescriptor->natts; attrnum++)
324  {
325  Form_pg_attribute att;
326  TypeCacheEntry *typentry;
327 
328  att = TupleDescAttr(slot1->tts_tupleDescriptor, attrnum);
329 
330  /*
331  * Ignore dropped and generated columns as the publisher doesn't send
332  * those
333  */
334  if (att->attisdropped || att->attgenerated)
335  continue;
336 
337  /*
338  * If one value is NULL and other is not, then they are certainly not
339  * equal
340  */
341  if (slot1->tts_isnull[attrnum] != slot2->tts_isnull[attrnum])
342  return false;
343 
344  /*
345  * If both are NULL, they can be considered equal.
346  */
347  if (slot1->tts_isnull[attrnum] || slot2->tts_isnull[attrnum])
348  continue;
349 
350  typentry = eq[attrnum];
351  if (typentry == NULL)
352  {
353  typentry = lookup_type_cache(att->atttypid,
355  if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
356  ereport(ERROR,
357  (errcode(ERRCODE_UNDEFINED_FUNCTION),
358  errmsg("could not identify an equality operator for type %s",
359  format_type_be(att->atttypid))));
360  eq[attrnum] = typentry;
361  }
362 
364  att->attcollation,
365  slot1->tts_values[attrnum],
366  slot2->tts_values[attrnum])))
367  return false;
368  }
369 
370  return true;
371 }
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1149
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
Oid fn_oid
Definition: fmgr.h:59
FmgrInfo eq_opr_finfo
Definition: typcache.h:75
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:362
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:345
#define TYPECACHE_EQ_OPR_FINFO
Definition: typcache.h:142

References Assert(), DatumGetBool(), TypeCacheEntry::eq_opr_finfo, ereport, errcode(), errmsg(), ERROR, FmgrInfo::fn_oid, format_type_be(), FunctionCall2Coll(), lookup_type_cache(), TupleDescData::natts, OidIsValid, slot_getallattrs(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, TupleDescAttr, and TYPECACHE_EQ_OPR_FINFO.

Referenced by RelationFindReplTupleByIndex(), and RelationFindReplTupleSeq().