PostgreSQL Source Code  git master
relation.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  * relation.c
3  * PostgreSQL logical replication relation mapping cache
4  *
5  * Copyright (c) 2016-2023, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/backend/replication/logical/relation.c
9  *
10  * NOTES
11  * Routines in this file mainly have to do with mapping the properties
12  * of local replication target relations to the properties of their
13  * remote counterpart.
14  *
15  *-------------------------------------------------------------------------
16  */
17 
18 #include "postgres.h"
19 
20 #ifdef USE_ASSERT_CHECKING
21 #include "access/amapi.h"
22 #endif
23 #include "access/genam.h"
24 #include "access/table.h"
25 #include "catalog/namespace.h"
26 #include "catalog/pg_am_d.h"
28 #include "executor/executor.h"
29 #include "nodes/makefuncs.h"
32 #include "utils/inval.h"
33 
34 
36 
37 static HTAB *LogicalRepRelMap = NULL;
38 
39 /*
40  * Partition map (LogicalRepPartMap)
41  *
42  * When a partitioned table is used as replication target, replicated
43  * operations are actually performed on its leaf partitions, which requires
44  * the partitions to also be mapped to the remote relation. Parent's entry
45  * (LogicalRepRelMapEntry) cannot be used as-is for all partitions, because
46  * individual partitions may have different attribute numbers, which means
47  * attribute mappings to remote relation's attributes must be maintained
48  * separately for each partition.
49  */
51 static HTAB *LogicalRepPartMap = NULL;
52 typedef struct LogicalRepPartMapEntry
53 {
54  Oid partoid; /* LogicalRepPartMap's key */
57 
58 static Oid FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel,
59  AttrMap *attrMap);
60 
61 /*
62  * Relcache invalidation callback for our relation map cache.
63  */
64 static void
66 {
67  LogicalRepRelMapEntry *entry;
68 
69  /* Just to be sure. */
70  if (LogicalRepRelMap == NULL)
71  return;
72 
73  if (reloid != InvalidOid)
74  {
75  HASH_SEQ_STATUS status;
76 
78 
79  /* TODO, use inverse lookup hashtable? */
80  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
81  {
82  if (entry->localreloid == reloid)
83  {
84  entry->localrelvalid = false;
85  hash_seq_term(&status);
86  break;
87  }
88  }
89  }
90  else
91  {
92  /* invalidate all cache entries */
93  HASH_SEQ_STATUS status;
94 
96 
97  while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
98  entry->localrelvalid = false;
99  }
100 }
101 
102 /*
103  * Initialize the relation map cache.
104  */
105 static void
107 {
108  HASHCTL ctl;
109 
113  "LogicalRepRelMapContext",
115 
116  /* Initialize the relation hash table. */
117  ctl.keysize = sizeof(LogicalRepRelId);
118  ctl.entrysize = sizeof(LogicalRepRelMapEntry);
120 
121  LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
123 
124  /* Watch for invalidation events. */
126  (Datum) 0);
127 }
128 
129 /*
130  * Free the entry of a relation map cache.
131  */
132 static void
134 {
135  LogicalRepRelation *remoterel;
136 
137  remoterel = &entry->remoterel;
138 
139  pfree(remoterel->nspname);
140  pfree(remoterel->relname);
141 
142  if (remoterel->natts > 0)
143  {
144  int i;
145 
146  for (i = 0; i < remoterel->natts; i++)
147  pfree(remoterel->attnames[i]);
148 
149  pfree(remoterel->attnames);
150  pfree(remoterel->atttyps);
151  }
152  bms_free(remoterel->attkeys);
153 
154  if (entry->attrmap)
155  free_attrmap(entry->attrmap);
156 }
157 
158 /*
159  * Add new entry or update existing entry in the relation map cache.
160  *
161  * Called when new relation mapping is sent by the publisher to update
162  * our expected view of incoming data from said publisher.
163  */
164 void
166 {
167  MemoryContext oldctx;
168  LogicalRepRelMapEntry *entry;
169  bool found;
170  int i;
171 
172  if (LogicalRepRelMap == NULL)
174 
175  /*
176  * HASH_ENTER returns the existing entry if present or creates a new one.
177  */
178  entry = hash_search(LogicalRepRelMap, &remoterel->remoteid,
179  HASH_ENTER, &found);
180 
181  if (found)
183 
184  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
185 
186  /* Make cached copy of the data */
188  entry->remoterel.remoteid = remoterel->remoteid;
189  entry->remoterel.nspname = pstrdup(remoterel->nspname);
190  entry->remoterel.relname = pstrdup(remoterel->relname);
191  entry->remoterel.natts = remoterel->natts;
192  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
193  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
194  for (i = 0; i < remoterel->natts; i++)
195  {
196  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
197  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
198  }
199  entry->remoterel.replident = remoterel->replident;
200  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
201  MemoryContextSwitchTo(oldctx);
202 }
203 
204 /*
205  * Find attribute index in TupleDesc struct by attribute name.
206  *
207  * Returns -1 if not found.
208  */
209 static int
211 {
212  int i;
213 
214  for (i = 0; i < remoterel->natts; i++)
215  {
216  if (strcmp(remoterel->attnames[i], attname) == 0)
217  return i;
218  }
219 
220  return -1;
221 }
222 
223 /*
224  * Report error with names of the missing local relation column(s), if any.
225  */
226 static void
228  Bitmapset *missingatts)
229 {
230  if (!bms_is_empty(missingatts))
231  {
232  StringInfoData missingattsbuf;
233  int missingattcnt = 0;
234  int i;
235 
236  initStringInfo(&missingattsbuf);
237 
238  i = -1;
239  while ((i = bms_next_member(missingatts, i)) >= 0)
240  {
241  missingattcnt++;
242  if (missingattcnt == 1)
243  appendStringInfo(&missingattsbuf, _("\"%s\""),
244  remoterel->attnames[i]);
245  else
246  appendStringInfo(&missingattsbuf, _(", \"%s\""),
247  remoterel->attnames[i]);
248  }
249 
250  ereport(ERROR,
251  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
252  errmsg_plural("logical replication target relation \"%s.%s\" is missing replicated column: %s",
253  "logical replication target relation \"%s.%s\" is missing replicated columns: %s",
254  missingattcnt,
255  remoterel->nspname,
256  remoterel->relname,
257  missingattsbuf.data)));
258  }
259 }
260 
261 /*
262  * Check if replica identity matches and mark the updatable flag.
263  *
264  * We allow for stricter replica identity (fewer columns) on subscriber as
265  * that will not stop us from finding unique tuple. IE, if publisher has
266  * identity (id,timestamp) and subscriber just (id) this will not be a
267  * problem, but in the opposite scenario it will.
268  *
269  * We just mark the relation entry as not updatable here if the local
270  * replica identity is found to be insufficient for applying
271  * updates/deletes (inserts don't care!) and leave it to
272  * check_relation_updatable() to throw the actual error if needed.
273  */
274 static void
276 {
277  Bitmapset *idkey;
278  LogicalRepRelation *remoterel = &entry->remoterel;
279  int i;
280 
281  entry->updatable = true;
282 
283  idkey = RelationGetIndexAttrBitmap(entry->localrel,
285  /* fallback to PK if no replica identity */
286  if (idkey == NULL)
287  {
288  idkey = RelationGetIndexAttrBitmap(entry->localrel,
290 
291  /*
292  * If no replica identity index and no PK, the published table must
293  * have replica identity FULL.
294  */
295  if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
296  entry->updatable = false;
297  }
298 
299  i = -1;
300  while ((i = bms_next_member(idkey, i)) >= 0)
301  {
303 
305  ereport(ERROR,
306  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
307  errmsg("logical replication target relation \"%s.%s\" uses "
308  "system columns in REPLICA IDENTITY index",
309  remoterel->nspname, remoterel->relname)));
310 
312 
313  if (entry->attrmap->attnums[attnum] < 0 ||
314  !bms_is_member(entry->attrmap->attnums[attnum], remoterel->attkeys))
315  {
316  entry->updatable = false;
317  break;
318  }
319  }
320 }
321 
322 /*
323  * Open the local relation associated with the remote one.
324  *
325  * Rebuilds the Relcache mapping if it was invalidated by local DDL.
326  */
329 {
330  LogicalRepRelMapEntry *entry;
331  bool found;
332  LogicalRepRelation *remoterel;
333 
334  if (LogicalRepRelMap == NULL)
336 
337  /* Search for existing entry. */
338  entry = hash_search(LogicalRepRelMap, &remoteid,
339  HASH_FIND, &found);
340 
341  if (!found)
342  elog(ERROR, "no relation map entry for remote relation ID %u",
343  remoteid);
344 
345  remoterel = &entry->remoterel;
346 
347  /* Ensure we don't leak a relcache refcount. */
348  if (entry->localrel)
349  elog(ERROR, "remote relation ID %u is already open", remoteid);
350 
351  /*
352  * When opening and locking a relation, pending invalidation messages are
353  * processed which can invalidate the relation. Hence, if the entry is
354  * currently considered valid, try to open the local relation by OID and
355  * see if invalidation ensues.
356  */
357  if (entry->localrelvalid)
358  {
359  entry->localrel = try_table_open(entry->localreloid, lockmode);
360  if (!entry->localrel)
361  {
362  /* Table was renamed or dropped. */
363  entry->localrelvalid = false;
364  }
365  else if (!entry->localrelvalid)
366  {
367  /* Note we release the no-longer-useful lock here. */
368  table_close(entry->localrel, lockmode);
369  entry->localrel = NULL;
370  }
371  }
372 
373  /*
374  * If the entry has been marked invalid since we last had lock on it,
375  * re-open the local relation by name and rebuild all derived data.
376  */
377  if (!entry->localrelvalid)
378  {
379  Oid relid;
380  TupleDesc desc;
381  MemoryContext oldctx;
382  int i;
383  Bitmapset *missingatts;
384 
385  /* Release the no-longer-useful attrmap, if any. */
386  if (entry->attrmap)
387  {
388  free_attrmap(entry->attrmap);
389  entry->attrmap = NULL;
390  }
391 
392  /* Try to find and lock the relation by name. */
393  relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
394  remoterel->relname, -1),
395  lockmode, true);
396  if (!OidIsValid(relid))
397  ereport(ERROR,
398  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
399  errmsg("logical replication target relation \"%s.%s\" does not exist",
400  remoterel->nspname, remoterel->relname)));
401  entry->localrel = table_open(relid, NoLock);
402  entry->localreloid = relid;
403 
404  /* Check for supported relkind. */
405  CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind,
406  remoterel->nspname, remoterel->relname);
407 
408  /*
409  * Build the mapping of local attribute numbers to remote attribute
410  * numbers and validate that we don't miss any replicated columns as
411  * that would result in potentially unwanted data loss.
412  */
413  desc = RelationGetDescr(entry->localrel);
415  entry->attrmap = make_attrmap(desc->natts);
416  MemoryContextSwitchTo(oldctx);
417 
418  /* check and report missing attrs, if any */
419  missingatts = bms_add_range(NULL, 0, remoterel->natts - 1);
420  for (i = 0; i < desc->natts; i++)
421  {
422  int attnum;
423  Form_pg_attribute attr = TupleDescAttr(desc, i);
424 
425  if (attr->attisdropped || attr->attgenerated)
426  {
427  entry->attrmap->attnums[i] = -1;
428  continue;
429  }
430 
432  NameStr(attr->attname));
433 
434  entry->attrmap->attnums[i] = attnum;
435  if (attnum >= 0)
436  missingatts = bms_del_member(missingatts, attnum);
437  }
438 
439  logicalrep_report_missing_attrs(remoterel, missingatts);
440 
441  /* be tidy */
442  bms_free(missingatts);
443 
444  /*
445  * Set if the table's replica identity is enough to apply
446  * update/delete.
447  */
449 
450  /*
451  * Finding a usable index is an infrequent task. It occurs when an
452  * operation is first performed on the relation, or after invalidation
453  * of the relation cache entry (such as ANALYZE or CREATE/DROP index
454  * on the relation).
455  */
456  entry->localindexoid = FindLogicalRepLocalIndex(entry->localrel, remoterel,
457  entry->attrmap);
458 
459  entry->localrelvalid = true;
460  }
461 
462  if (entry->state != SUBREL_STATE_READY)
464  entry->localreloid,
465  &entry->statelsn);
466 
467  return entry;
468 }
469 
470 /*
471  * Close the previously opened logical relation.
472  */
473 void
475 {
476  table_close(rel->localrel, lockmode);
477  rel->localrel = NULL;
478 }
479 
480 /*
481  * Partition cache: look up partition LogicalRepRelMapEntry's
482  *
483  * Unlike relation map cache, this is keyed by partition OID, not remote
484  * relation OID, because we only have to use this cache in the case where
485  * partitions are not directly mapped to any remote relation, such as when
486  * replication is occurring with one of their ancestors as target.
487  */
488 
489 /*
490  * Relcache invalidation callback
491  */
492 static void
494 {
495  LogicalRepPartMapEntry *entry;
496 
497  /* Just to be sure. */
498  if (LogicalRepPartMap == NULL)
499  return;
500 
501  if (reloid != InvalidOid)
502  {
503  HASH_SEQ_STATUS status;
504 
506 
507  /* TODO, use inverse lookup hashtable? */
508  while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
509  {
510  if (entry->relmapentry.localreloid == reloid)
511  {
512  entry->relmapentry.localrelvalid = false;
513  hash_seq_term(&status);
514  break;
515  }
516  }
517  }
518  else
519  {
520  /* invalidate all cache entries */
521  HASH_SEQ_STATUS status;
522 
524 
525  while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
526  entry->relmapentry.localrelvalid = false;
527  }
528 }
529 
530 /*
531  * Reset the entries in the partition map that refer to remoterel.
532  *
533  * Called when new relation mapping is sent by the publisher to update our
534  * expected view of incoming data from said publisher.
535  *
536  * Note that we don't update the remoterel information in the entry here,
537  * we will update the information in logicalrep_partition_open to avoid
538  * unnecessary work.
539  */
540 void
542 {
543  HASH_SEQ_STATUS status;
544  LogicalRepPartMapEntry *part_entry;
545  LogicalRepRelMapEntry *entry;
546 
547  if (LogicalRepPartMap == NULL)
548  return;
549 
551  while ((part_entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
552  {
553  entry = &part_entry->relmapentry;
554 
555  if (entry->remoterel.remoteid != remoterel->remoteid)
556  continue;
557 
559 
560  memset(entry, 0, sizeof(LogicalRepRelMapEntry));
561  }
562 }
563 
564 /*
565  * Initialize the partition map cache.
566  */
567 static void
569 {
570  HASHCTL ctl;
571 
575  "LogicalRepPartMapContext",
577 
578  /* Initialize the relation hash table. */
579  ctl.keysize = sizeof(Oid); /* partition OID */
580  ctl.entrysize = sizeof(LogicalRepPartMapEntry);
582 
583  LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
585 
586  /* Watch for invalidation events. */
588  (Datum) 0);
589 }
590 
591 /*
592  * logicalrep_partition_open
593  *
594  * Returned entry reuses most of the values of the root table's entry, save
595  * the attribute map, which can be different for the partition. However,
596  * we must physically copy all the data, in case the root table's entry
597  * gets freed/rebuilt.
598  *
599  * Note there's no logicalrep_partition_close, because the caller closes the
600  * component relation.
601  */
604  Relation partrel, AttrMap *map)
605 {
606  LogicalRepRelMapEntry *entry;
607  LogicalRepPartMapEntry *part_entry;
608  LogicalRepRelation *remoterel = &root->remoterel;
609  Oid partOid = RelationGetRelid(partrel);
610  AttrMap *attrmap = root->attrmap;
611  bool found;
612  MemoryContext oldctx;
613 
614  if (LogicalRepPartMap == NULL)
616 
617  /* Search for existing entry. */
619  &partOid,
620  HASH_ENTER, &found);
621 
622  entry = &part_entry->relmapentry;
623 
624  /*
625  * We must always overwrite entry->localrel with the latest partition
626  * Relation pointer, because the Relation pointed to by the old value may
627  * have been cleared after the caller would have closed the partition
628  * relation after the last use of this entry. Note that localrelvalid is
629  * only updated by the relcache invalidation callback, so it may still be
630  * true irrespective of whether the Relation pointed to by localrel has
631  * been cleared or not.
632  */
633  if (found && entry->localrelvalid)
634  {
635  entry->localrel = partrel;
636  return entry;
637  }
638 
639  /* Switch to longer-lived context. */
641 
642  if (!found)
643  {
644  memset(part_entry, 0, sizeof(LogicalRepPartMapEntry));
645  part_entry->partoid = partOid;
646  }
647 
648  /* Release the no-longer-useful attrmap, if any. */
649  if (entry->attrmap)
650  {
651  free_attrmap(entry->attrmap);
652  entry->attrmap = NULL;
653  }
654 
655  if (!entry->remoterel.remoteid)
656  {
657  int i;
658 
659  /* Remote relation is copied as-is from the root entry. */
660  entry = &part_entry->relmapentry;
661  entry->remoterel.remoteid = remoterel->remoteid;
662  entry->remoterel.nspname = pstrdup(remoterel->nspname);
663  entry->remoterel.relname = pstrdup(remoterel->relname);
664  entry->remoterel.natts = remoterel->natts;
665  entry->remoterel.attnames = palloc(remoterel->natts * sizeof(char *));
666  entry->remoterel.atttyps = palloc(remoterel->natts * sizeof(Oid));
667  for (i = 0; i < remoterel->natts; i++)
668  {
669  entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
670  entry->remoterel.atttyps[i] = remoterel->atttyps[i];
671  }
672  entry->remoterel.replident = remoterel->replident;
673  entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
674  }
675 
676  entry->localrel = partrel;
677  entry->localreloid = partOid;
678 
679  /*
680  * If the partition's attributes don't match the root relation's, we'll
681  * need to make a new attrmap which maps partition attribute numbers to
682  * remoterel's, instead of the original which maps root relation's
683  * attribute numbers to remoterel's.
684  *
685  * Note that 'map' which comes from the tuple routing data structure
686  * contains 1-based attribute numbers (of the parent relation). However,
687  * the map in 'entry', a logical replication data structure, contains
688  * 0-based attribute numbers (of the remote relation).
689  */
690  if (map)
691  {
692  AttrNumber attno;
693 
694  entry->attrmap = make_attrmap(map->maplen);
695  for (attno = 0; attno < entry->attrmap->maplen; attno++)
696  {
697  AttrNumber root_attno = map->attnums[attno];
698 
699  /* 0 means it's a dropped attribute. See comments atop AttrMap. */
700  if (root_attno == 0)
701  entry->attrmap->attnums[attno] = -1;
702  else
703  entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
704  }
705  }
706  else
707  {
708  /* Lacking copy_attmap, do this the hard way. */
709  entry->attrmap = make_attrmap(attrmap->maplen);
710  memcpy(entry->attrmap->attnums, attrmap->attnums,
711  attrmap->maplen * sizeof(AttrNumber));
712  }
713 
714  /* Set if the table's replica identity is enough to apply update/delete. */
716 
717  /* state and statelsn are left set to 0. */
718  MemoryContextSwitchTo(oldctx);
719 
720  /*
721  * Finding a usable index is an infrequent task. It occurs when an
722  * operation is first performed on the relation, or after invalidation of
723  * the relation cache entry (such as ANALYZE or CREATE/DROP index on the
724  * relation).
725  *
726  * We also prefer to run this code on the oldctx so that we do not leak
727  * anything in the LogicalRepPartMapContext (hence CacheMemoryContext).
728  */
729  entry->localindexoid = FindLogicalRepLocalIndex(partrel, remoterel,
730  entry->attrmap);
731 
732  entry->localrelvalid = true;
733 
734  return entry;
735 }
736 
737 /*
738  * Returns the oid of an index that can be used by the apply worker to scan
739  * the relation.
740  *
741  * We expect to call this function when REPLICA IDENTITY FULL is defined for
742  * the remote relation.
743  *
744  * If no suitable index is found, returns InvalidOid.
745  */
746 static Oid
748 {
749  List *idxlist = RelationGetIndexList(localrel);
750  ListCell *lc;
751 
752  foreach(lc, idxlist)
753  {
754  Oid idxoid = lfirst_oid(lc);
755  bool isUsableIdx;
756  Relation idxRel;
757  IndexInfo *idxInfo;
758 
759  idxRel = index_open(idxoid, AccessShareLock);
760  idxInfo = BuildIndexInfo(idxRel);
761  isUsableIdx = IsIndexUsableForReplicaIdentityFull(idxInfo, attrmap);
762  index_close(idxRel, AccessShareLock);
763 
764  /* Return the first eligible index found */
765  if (isUsableIdx)
766  return idxoid;
767  }
768 
769  return InvalidOid;
770 }
771 
772 /*
773  * Returns true if the index is usable for replica identity full.
774  *
775  * The index must be btree or hash, non-partial, and the leftmost field must be
776  * a column (not an expression) that references the remote relation column. These
777  * limitations help to keep the index scan similar to PK/RI index scans.
778  *
779  * attrmap is a map of local attributes to remote ones. We can consult this
780  * map to check whether the local index attribute has a corresponding remote
781  * attribute.
782  *
783  * Note that the limitations of index scans for replica identity full only
784  * adheres to a subset of the limitations of PK/RI. For example, we support
785  * columns that are marked as [NULL] or we are not interested in the [NOT
786  * DEFERRABLE] aspect of constraints here. It works for us because we always
787  * compare the tuples for non-PK/RI index scans. See
788  * RelationFindReplTupleByIndex().
789  *
790  * The reasons why only Btree and Hash indexes can be considered as usable are:
791  *
792  * 1) Other index access methods don't have a fixed strategy for equality
793  * operation. Refer get_equal_strategy_number_for_am().
794  *
795  * 2) For indexes other than PK and REPLICA IDENTITY, we need to match the
796  * local and remote tuples. The equality routine tuples_equal() cannot accept
797  * a datatype (e.g. point or box) that does not have a default operator class
798  * for Btree or Hash.
799  *
800  * XXX: Note that BRIN and GIN indexes do not implement "amgettuple" which
801  * will be used later to fetch the tuples. See RelationFindReplTupleByIndex().
802  *
803  * XXX: To support partial indexes, the required changes are likely to be larger.
804  * If none of the tuples satisfy the expression for the index scan, we fall-back
805  * to sequential execution, which might not be a good idea in some cases.
806  */
807 bool
809 {
810  AttrNumber keycol;
811 
812  /* Ensure that the index access method has a valid equal strategy */
814  return false;
815 
816  /* The index must not be a partial index */
817  if (indexInfo->ii_Predicate != NIL)
818  return false;
819 
820  Assert(indexInfo->ii_NumIndexAttrs >= 1);
821 
822  /* The leftmost index field must not be an expression */
823  keycol = indexInfo->ii_IndexAttrNumbers[0];
824  if (!AttributeNumberIsValid(keycol))
825  return false;
826 
827  /*
828  * And the leftmost index field must reference the remote relation column.
829  * This is because if it doesn't, the sequential scan is favorable over
830  * index scan in most cases.
831  */
832  if (attrmap->maplen <= AttrNumberGetAttrOffset(keycol) ||
833  attrmap->attnums[AttrNumberGetAttrOffset(keycol)] < 0)
834  return false;
835 
836 #ifdef USE_ASSERT_CHECKING
837  {
838  IndexAmRoutine *amroutine;
839 
840  /* The given index access method must implement amgettuple. */
841  amroutine = GetIndexAmRoutineByAmId(indexInfo->ii_Am, false);
842  Assert(amroutine->amgettuple != NULL);
843  }
844 #endif
845 
846  return true;
847 }
848 
849 /*
850  * Get replica identity index or if it is not defined a primary key.
851  *
852  * If neither is defined, returns InvalidOid
853  */
854 Oid
856 {
857  Oid idxoid;
858 
859  idxoid = RelationGetReplicaIndex(rel);
860 
861  if (!OidIsValid(idxoid))
862  idxoid = RelationGetPrimaryKeyIndex(rel);
863 
864  return idxoid;
865 }
866 
867 /*
868  * Returns the index oid if we can use an index for subscriber. Otherwise,
869  * returns InvalidOid.
870  */
871 static Oid
873  AttrMap *attrMap)
874 {
875  Oid idxoid;
876 
877  /*
878  * We never need index oid for partitioned tables, always rely on leaf
879  * partition's index.
880  */
881  if (localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
882  return InvalidOid;
883 
884  /*
885  * Simple case, we already have a primary key or a replica identity index.
886  */
887  idxoid = GetRelationIdentityOrPK(localrel);
888  if (OidIsValid(idxoid))
889  return idxoid;
890 
891  if (remoterel->replident == REPLICA_IDENTITY_FULL)
892  {
893  /*
894  * We are looking for one more opportunity for using an index. If
895  * there are any indexes defined on the local relation, try to pick a
896  * suitable index.
897  *
898  * The index selection safely assumes that all the columns are going
899  * to be available for the index scan given that remote relation has
900  * replica identity full.
901  *
902  * Note that we are not using the planner to find the cheapest method
903  * to scan the relation as that would require us to either use lower
904  * level planner functions which would be a maintenance burden in the
905  * long run or use the full-fledged planner which could cause
906  * overhead.
907  */
908  return FindUsableIndexForReplicaIdentityFull(localrel, attrMap);
909  }
910 
911  return InvalidOid;
912 }
IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition: amapi.c:56
void free_attrmap(AttrMap *map)
Definition: attmap.c:57
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:41
int16 AttrNumber
Definition: attnum.h:21
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
#define AttrNumberGetAttrOffset(attNum)
Definition: attnum.h:51
#define AttrNumberIsForUserDefinedAttr(attributeNumber)
Definition: attnum.h:41
Subscription * MySubscription
Definition: worker.c:316
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1106
void bms_free(Bitmapset *a)
Definition: bitmapset.c:194
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:793
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:80
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:879
#define bms_is_empty(a)
Definition: bitmapset.h:105
#define NameStr(name)
Definition: c.h:735
#define OidIsValid(objectId)
Definition: c.h:764
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1507
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1431
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1421
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1179
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define _(x)
Definition: elog.c:91
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void CheckSubscriptionRelkind(char relkind, const char *nspname, const char *relname)
StrategyNumber get_equal_strategy_number_for_am(Oid am)
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:2426
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1561
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
uint32 LogicalRepRelId
Definition: logicalproto.h:101
struct LogicalRepRelMapEntry LogicalRepRelMapEntry
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:425
char * pstrdup(const char *in)
Definition: mcxt.c:1644
void pfree(void *pointer)
Definition: mcxt.c:1456
MemoryContext CacheMemoryContext
Definition: mcxt.c:144
void * palloc(Size size)
Definition: mcxt.c:1226
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:79
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
NameData attname
Definition: pg_attribute.h:41
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
void * arg
#define NIL
Definition: pg_list.h:68
#define lfirst_oid(lc)
Definition: pg_list.h:174
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
uintptr_t Datum
Definition: postgres.h:64
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelid(relation)
Definition: rel.h:504
#define RelationGetDescr(relation)
Definition: rel.h:530
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4740
Oid RelationGetPrimaryKeyIndex(Relation relation)
Definition: relcache.c:4950
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:5198
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:4971
@ INDEX_ATTR_BITMAP_PRIMARY_KEY
Definition: relcache.h:63
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition: relcache.h:64
static MemoryContext LogicalRepPartMapContext
Definition: relation.c:50
void logicalrep_partmap_reset_relmap(LogicalRepRelation *remoterel)
Definition: relation.c:541
static void logicalrep_partmap_init(void)
Definition: relation.c:568
static void logicalrep_report_missing_attrs(LogicalRepRelation *remoterel, Bitmapset *missingatts)
Definition: relation.c:227
bool IsIndexUsableForReplicaIdentityFull(IndexInfo *indexInfo, AttrMap *attrmap)
Definition: relation.c:808
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition: relation.c:133
LogicalRepRelMapEntry * logicalrep_partition_open(LogicalRepRelMapEntry *root, Relation partrel, AttrMap *map)
Definition: relation.c:603
struct LogicalRepPartMapEntry LogicalRepPartMapEntry
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:493
static HTAB * LogicalRepPartMap
Definition: relation.c:51
static HTAB * LogicalRepRelMap
Definition: relation.c:37
static void logicalrep_rel_mark_updatable(LogicalRepRelMapEntry *entry)
Definition: relation.c:275
static MemoryContext LogicalRepRelMapContext
Definition: relation.c:35
Oid GetRelationIdentityOrPK(Relation rel)
Definition: relation.c:855
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition: relation.c:165
static void logicalrep_relmap_init(void)
Definition: relation.c:106
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition: relation.c:328
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
Definition: relation.c:210
static Oid FindUsableIndexForReplicaIdentityFull(Relation localrel, AttrMap *attrmap)
Definition: relation.c:747
static void logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
Definition: relation.c:65
void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
Definition: relation.c:474
static Oid FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel, AttrMap *attrMap)
Definition: relation.c:872
#define InvalidStrategy
Definition: stratnum.h:24
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Definition: attmap.h:35
int maplen
Definition: attmap.h:37
AttrNumber * attnums
Definition: attmap.h:36
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
Definition: dynahash.c:220
amgettuple_function amgettuple
Definition: amapi.h:275
int ii_NumIndexAttrs
Definition: execnodes.h:177
Oid ii_Am
Definition: execnodes.h:200
AttrNumber ii_IndexAttrNumbers[INDEX_MAX_KEYS]
Definition: execnodes.h:179
List * ii_Predicate
Definition: execnodes.h:182
Definition: pg_list.h:54
LogicalRepRelMapEntry relmapentry
Definition: relation.c:55
LogicalRepRelation remoterel
LogicalRepRelId remoteid
Definition: logicalproto.h:107
Bitmapset * attkeys
Definition: logicalproto.h:115
Form_pg_class rd_rel
Definition: rel.h:111
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
Relation try_table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:60
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92