PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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#include "access/amapi.h"
21#include "access/genam.h"
22#include "access/table.h"
23#include "catalog/namespace.h"
25#include "executor/executor.h"
26#include "nodes/makefuncs.h"
29#include "utils/inval.h"
30#include "utils/lsyscache.h"
31#include "utils/syscache.h"
32#include "utils/typcache.h"
33
34
36
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 */
57
59 AttrMap *attrMap);
60
61/*
62 * Relcache invalidation callback for our relation map cache.
63 */
64static void
66{
68
69 /* Just to be sure. */
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 */
105static 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 */
132static 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 */
164void
166{
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_array(char *, remoterel->natts);
193 entry->remoterel.atttyps = palloc_array(Oid, remoterel->natts);
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
201 /*
202 * XXX The walsender currently does not transmit the relkind of the remote
203 * relation when replicating changes. Since we support replicating only
204 * table changes at present, we default to initializing relkind as
205 * RELKIND_RELATION. This is needed in CheckSubscriptionRelkind() to check
206 * if the publisher and subscriber relation kinds are compatible.
207 */
208 entry->remoterel.relkind =
209 (remoterel->relkind == 0) ? RELKIND_RELATION : remoterel->relkind;
210
211 entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
213}
214
215/*
216 * Find attribute index in TupleDesc struct by attribute name.
217 *
218 * Returns -1 if not found.
219 */
220static int
222{
223 int i;
224
225 for (i = 0; i < remoterel->natts; i++)
226 {
227 if (strcmp(remoterel->attnames[i], attname) == 0)
228 return i;
229 }
230
231 return -1;
232}
233
234/*
235 * Returns a comma-separated string of attribute names based on the provided
236 * relation and bitmap indicating which attributes to include.
237 */
238static char *
240{
242 int attcnt = 0;
243 int i = -1;
244
245 Assert(!bms_is_empty(atts));
246
248
249 while ((i = bms_next_member(atts, i)) >= 0)
250 {
251 attcnt++;
252 if (attcnt > 1)
253 /* translator: This is a separator in a list of entity names. */
255
256 appendStringInfo(&attsbuf, _("\"%s\""), remoterel->attnames[i]);
257 }
258
259 return attsbuf.data;
260}
261
262/*
263 * If attempting to replicate missing or generated columns, report an error.
264 * Prioritize 'missing' errors if both occur though the prioritization is
265 * arbitrary.
266 */
267static void
271{
275 errmsg_plural("logical replication target relation \"%s.%s\" is missing replicated column: %s",
276 "logical replication target relation \"%s.%s\" is missing replicated columns: %s",
278 remoterel->nspname,
279 remoterel->relname,
280 logicalrep_get_attrs_str(remoterel,
281 missingatts)));
282
286 errmsg_plural("logical replication target relation \"%s.%s\" has incompatible generated column: %s",
287 "logical replication target relation \"%s.%s\" has incompatible generated columns: %s",
289 remoterel->nspname,
290 remoterel->relname,
291 logicalrep_get_attrs_str(remoterel,
292 generatedatts)));
293}
294
295/*
296 * Check if replica identity matches and mark the updatable flag.
297 *
298 * We allow for stricter replica identity (fewer columns) on subscriber as
299 * that will not stop us from finding unique tuple. IE, if publisher has
300 * identity (id,timestamp) and subscriber just (id) this will not be a
301 * problem, but in the opposite scenario it will.
302 *
303 * We just mark the relation entry as not updatable here if the local
304 * replica identity is found to be insufficient for applying
305 * updates/deletes (inserts don't care!) and leave it to
306 * check_relation_updatable() to throw the actual error if needed.
307 */
308static void
310{
312 LogicalRepRelation *remoterel = &entry->remoterel;
313 int i;
314
315 entry->updatable = true;
316
319 /* fallback to PK if no replica identity */
320 if (idkey == NULL)
321 {
324
325 /*
326 * If no replica identity index and no PK, the published table must
327 * have replica identity FULL.
328 */
329 if (idkey == NULL && remoterel->replident != REPLICA_IDENTITY_FULL)
330 entry->updatable = false;
331 }
332
333 i = -1;
334 while ((i = bms_next_member(idkey, i)) >= 0)
335 {
337
341 errmsg("logical replication target relation \"%s.%s\" uses "
342 "system columns in REPLICA IDENTITY index",
343 remoterel->nspname, remoterel->relname)));
344
346
347 if (entry->attrmap->attnums[attnum] < 0 ||
348 !bms_is_member(entry->attrmap->attnums[attnum], remoterel->attkeys))
349 {
350 entry->updatable = false;
351 break;
352 }
353 }
354}
355
356/*
357 * Open the local relation associated with the remote one.
358 *
359 * Rebuilds the Relcache mapping if it was invalidated by local DDL.
360 */
363{
365 bool found;
366 LogicalRepRelation *remoterel;
367
368 if (LogicalRepRelMap == NULL)
370
371 /* Search for existing entry. */
372 entry = hash_search(LogicalRepRelMap, &remoteid,
373 HASH_FIND, &found);
374
375 if (!found)
376 elog(ERROR, "no relation map entry for remote relation ID %u",
377 remoteid);
378
379 remoterel = &entry->remoterel;
380
381 /* Ensure we don't leak a relcache refcount. */
382 if (entry->localrel)
383 elog(ERROR, "remote relation ID %u is already open", remoteid);
384
385 /*
386 * When opening and locking a relation, pending invalidation messages are
387 * processed which can invalidate the relation. Hence, if the entry is
388 * currently considered valid, try to open the local relation by OID and
389 * see if invalidation ensues.
390 */
391 if (entry->localrelvalid)
392 {
393 entry->localrel = try_table_open(entry->localreloid, lockmode);
394 if (!entry->localrel)
395 {
396 /* Table was renamed or dropped. */
397 entry->localrelvalid = false;
398 }
399 else if (!entry->localrelvalid)
400 {
401 /* Note we release the no-longer-useful lock here. */
402 table_close(entry->localrel, lockmode);
403 entry->localrel = NULL;
404 }
405 }
406
407 /*
408 * If the entry has been marked invalid since we last had lock on it,
409 * re-open the local relation by name and rebuild all derived data.
410 */
411 if (!entry->localrelvalid)
412 {
413 Oid relid;
414 TupleDesc desc;
416 int i;
419
420 /* Release the no-longer-useful attrmap, if any. */
421 if (entry->attrmap)
422 {
423 free_attrmap(entry->attrmap);
424 entry->attrmap = NULL;
425 }
426
427 /* Try to find and lock the relation by name. */
428 relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
429 remoterel->relname, -1),
430 lockmode, true);
431 if (!OidIsValid(relid))
434 errmsg("logical replication target relation \"%s.%s\" does not exist",
435 remoterel->nspname, remoterel->relname)));
436 entry->localrel = table_open(relid, NoLock);
437 entry->localreloid = relid;
438
439 /* Check for supported relkind. */
441 remoterel->relkind,
442 remoterel->nspname, remoterel->relname);
443
444 /*
445 * Build the mapping of local attribute numbers to remote attribute
446 * numbers and validate that we don't miss any replicated columns as
447 * that would result in potentially unwanted data loss.
448 */
449 desc = RelationGetDescr(entry->localrel);
451 entry->attrmap = make_attrmap(desc->natts);
453
454 /* check and report missing attrs, if any */
455 missingatts = bms_add_range(NULL, 0, remoterel->natts - 1);
456 for (i = 0; i < desc->natts; i++)
457 {
458 int attnum;
459 Form_pg_attribute attr = TupleDescAttr(desc, i);
460
461 if (attr->attisdropped)
462 {
463 entry->attrmap->attnums[i] = -1;
464 continue;
465 }
466
468 NameStr(attr->attname));
469
470 entry->attrmap->attnums[i] = attnum;
471 if (attnum >= 0)
472 {
473 /* Remember which subscriber columns are generated. */
474 if (attr->attgenerated)
476
478 }
479 }
480
483
484 /* be tidy */
487
488 /*
489 * Set if the table's replica identity is enough to apply
490 * update/delete.
491 */
493
494 /*
495 * Finding a usable index is an infrequent task. It occurs when an
496 * operation is first performed on the relation, or after invalidation
497 * of the relation cache entry (such as ANALYZE or CREATE/DROP index
498 * on the relation).
499 */
500 entry->localindexoid = FindLogicalRepLocalIndex(entry->localrel, remoterel,
501 entry->attrmap);
502
503 entry->localrelvalid = true;
504 }
505
506 if (entry->state != SUBREL_STATE_READY)
508 entry->localreloid,
509 &entry->statelsn);
510
511 return entry;
512}
513
514/*
515 * Close the previously opened logical relation.
516 */
517void
519{
520 table_close(rel->localrel, lockmode);
521 rel->localrel = NULL;
522}
523
524/*
525 * Partition cache: look up partition LogicalRepRelMapEntry's
526 *
527 * Unlike relation map cache, this is keyed by partition OID, not remote
528 * relation OID, because we only have to use this cache in the case where
529 * partitions are not directly mapped to any remote relation, such as when
530 * replication is occurring with one of their ancestors as target.
531 */
532
533/*
534 * Relcache invalidation callback
535 */
536static void
538{
540
541 /* Just to be sure. */
542 if (LogicalRepPartMap == NULL)
543 return;
544
545 if (reloid != InvalidOid)
546 {
547 HASH_SEQ_STATUS status;
548
550
551 /* TODO, use inverse lookup hashtable? */
552 while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
553 {
554 if (entry->relmapentry.localreloid == reloid)
555 {
556 entry->relmapentry.localrelvalid = false;
557 hash_seq_term(&status);
558 break;
559 }
560 }
561 }
562 else
563 {
564 /* invalidate all cache entries */
565 HASH_SEQ_STATUS status;
566
568
569 while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
570 entry->relmapentry.localrelvalid = false;
571 }
572}
573
574/*
575 * Reset the entries in the partition map that refer to remoterel.
576 *
577 * Called when new relation mapping is sent by the publisher to update our
578 * expected view of incoming data from said publisher.
579 *
580 * Note that we don't update the remoterel information in the entry here,
581 * we will update the information in logicalrep_partition_open to avoid
582 * unnecessary work.
583 */
584void
586{
587 HASH_SEQ_STATUS status;
590
591 if (LogicalRepPartMap == NULL)
592 return;
593
595 while ((part_entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
596 {
597 entry = &part_entry->relmapentry;
598
599 if (entry->remoterel.remoteid != remoterel->remoteid)
600 continue;
601
603
604 memset(entry, 0, sizeof(LogicalRepRelMapEntry));
605 }
606}
607
608/*
609 * Initialize the partition map cache.
610 */
611static void
613{
614 HASHCTL ctl;
615
619 "LogicalRepPartMapContext",
621
622 /* Initialize the relation hash table. */
623 ctl.keysize = sizeof(Oid); /* partition OID */
624 ctl.entrysize = sizeof(LogicalRepPartMapEntry);
626
627 LogicalRepPartMap = hash_create("logicalrep partition map cache", 64, &ctl,
629
630 /* Watch for invalidation events. */
632 (Datum) 0);
633}
634
635/*
636 * logicalrep_partition_open
637 *
638 * Returned entry reuses most of the values of the root table's entry, save
639 * the attribute map, which can be different for the partition. However,
640 * we must physically copy all the data, in case the root table's entry
641 * gets freed/rebuilt.
642 *
643 * Note there's no logicalrep_partition_close, because the caller closes the
644 * component relation.
645 */
648 Relation partrel, AttrMap *map)
649{
652 LogicalRepRelation *remoterel = &root->remoterel;
653 Oid partOid = RelationGetRelid(partrel);
654 AttrMap *attrmap = root->attrmap;
655 bool found;
657
658 if (LogicalRepPartMap == NULL)
660
661 /* Search for existing entry. */
663 &partOid,
664 HASH_ENTER, &found);
665
666 entry = &part_entry->relmapentry;
667
668 /*
669 * We must always overwrite entry->localrel with the latest partition
670 * Relation pointer, because the Relation pointed to by the old value may
671 * have been cleared after the caller would have closed the partition
672 * relation after the last use of this entry. Note that localrelvalid is
673 * only updated by the relcache invalidation callback, so it may still be
674 * true irrespective of whether the Relation pointed to by localrel has
675 * been cleared or not.
676 */
677 if (found && entry->localrelvalid)
678 {
679 entry->localrel = partrel;
680 return entry;
681 }
682
683 /* Switch to longer-lived context. */
685
686 if (!found)
687 {
689 part_entry->partoid = partOid;
690 }
691
692 /* Release the no-longer-useful attrmap, if any. */
693 if (entry->attrmap)
694 {
695 free_attrmap(entry->attrmap);
696 entry->attrmap = NULL;
697 }
698
699 if (!entry->remoterel.remoteid)
700 {
701 int i;
702
703 /* Remote relation is copied as-is from the root entry. */
704 entry->remoterel.remoteid = remoterel->remoteid;
705 entry->remoterel.nspname = pstrdup(remoterel->nspname);
706 entry->remoterel.relname = pstrdup(remoterel->relname);
707 entry->remoterel.natts = remoterel->natts;
708 entry->remoterel.attnames = palloc_array(char *, remoterel->natts);
709 entry->remoterel.atttyps = palloc_array(Oid, remoterel->natts);
710 for (i = 0; i < remoterel->natts; i++)
711 {
712 entry->remoterel.attnames[i] = pstrdup(remoterel->attnames[i]);
713 entry->remoterel.atttyps[i] = remoterel->atttyps[i];
714 }
715 entry->remoterel.replident = remoterel->replident;
716 entry->remoterel.attkeys = bms_copy(remoterel->attkeys);
717 }
718
719 entry->localrel = partrel;
720 entry->localreloid = partOid;
721
722 /*
723 * If the partition's attributes don't match the root relation's, we'll
724 * need to make a new attrmap which maps partition attribute numbers to
725 * remoterel's, instead of the original which maps root relation's
726 * attribute numbers to remoterel's.
727 *
728 * Note that 'map' which comes from the tuple routing data structure
729 * contains 1-based attribute numbers (of the parent relation). However,
730 * the map in 'entry', a logical replication data structure, contains
731 * 0-based attribute numbers (of the remote relation).
732 */
733 if (map)
734 {
735 AttrNumber attno;
736
737 entry->attrmap = make_attrmap(map->maplen);
738 for (attno = 0; attno < entry->attrmap->maplen; attno++)
739 {
740 AttrNumber root_attno = map->attnums[attno];
741
742 /* 0 means it's a dropped attribute. See comments atop AttrMap. */
743 if (root_attno == 0)
744 entry->attrmap->attnums[attno] = -1;
745 else
746 entry->attrmap->attnums[attno] = attrmap->attnums[root_attno - 1];
747 }
748 }
749 else
750 {
751 /* Lacking copy_attmap, do this the hard way. */
752 entry->attrmap = make_attrmap(attrmap->maplen);
753 memcpy(entry->attrmap->attnums, attrmap->attnums,
754 attrmap->maplen * sizeof(AttrNumber));
755 }
756
757 /* Set if the table's replica identity is enough to apply update/delete. */
759
760 /* state and statelsn are left set to 0. */
762
763 /*
764 * Finding a usable index is an infrequent task. It occurs when an
765 * operation is first performed on the relation, or after invalidation of
766 * the relation cache entry (such as ANALYZE or CREATE/DROP index on the
767 * relation).
768 *
769 * We also prefer to run this code on the oldctx so that we do not leak
770 * anything in the LogicalRepPartMapContext (hence CacheMemoryContext).
771 */
772 entry->localindexoid = FindLogicalRepLocalIndex(partrel, remoterel,
773 entry->attrmap);
774
775 entry->localrelvalid = true;
776
777 return entry;
778}
779
780/*
781 * Returns the oid of an index that can be used by the apply worker to scan
782 * the relation.
783 *
784 * We expect to call this function when REPLICA IDENTITY FULL is defined for
785 * the remote relation.
786 *
787 * If no suitable index is found, returns InvalidOid.
788 */
789static Oid
791{
792 List *idxlist = RelationGetIndexList(localrel);
793
795 {
796 bool isUsableIdx;
798
802
803 /* Return the first eligible index found */
804 if (isUsableIdx)
805 return idxoid;
806 }
807
808 return InvalidOid;
809}
810
811/*
812 * Returns true if the index is usable for replica identity full.
813 *
814 * The index must have an equal strategy for each key column, be non-partial,
815 * and the leftmost field must be a column (not an expression) that references
816 * the remote relation column. These limitations help to keep the index scan
817 * similar to PK/RI index scans.
818 *
819 * attrmap is a map of local attributes to remote ones. We can consult this
820 * map to check whether the local index attribute has a corresponding remote
821 * attribute.
822 *
823 * Note that the limitations of index scans for replica identity full only
824 * adheres to a subset of the limitations of PK/RI. For example, we support
825 * columns that are marked as [NULL] or we are not interested in the [NOT
826 * DEFERRABLE] aspect of constraints here. It works for us because we always
827 * compare the tuples for non-PK/RI index scans. See
828 * RelationFindReplTupleByIndex().
829 *
830 * XXX: To support partial indexes, the required changes are likely to be larger.
831 * If none of the tuples satisfy the expression for the index scan, we fall-back
832 * to sequential execution, which might not be a good idea in some cases.
833 */
834bool
836{
839
840 /* The index must not be a partial index */
841 if (!heap_attisnull(idxrel->rd_indextuple, Anum_pg_index_indpred, NULL))
842 return false;
843
844 Assert(idxrel->rd_index->indnatts >= 1);
845
847 idxrel->rd_indextuple,
849
850 /* Ensure that the index has a valid equal strategy for each key column */
851 for (int i = 0; i < idxrel->rd_index->indnkeyatts; i++)
852 {
853 Oid opfamily;
854
855 opfamily = get_opclass_family(indclass->values[i]);
856 if (IndexAmTranslateCompareType(COMPARE_EQ, idxrel->rd_rel->relam, opfamily, true) == InvalidStrategy)
857 return false;
858 }
859
860 /*
861 * For indexes other than PK and REPLICA IDENTITY, we need to match the
862 * local and remote tuples. The equality routine tuples_equal() cannot
863 * accept a data type where the type cache cannot provide an equality
864 * operator.
865 */
866 for (int i = 0; i < idxrel->rd_att->natts; i++)
867 {
868 TypeCacheEntry *typentry;
869
870 typentry = lookup_type_cache(TupleDescAttr(idxrel->rd_att, i)->atttypid, TYPECACHE_EQ_OPR_FINFO);
871 if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
872 return false;
873 }
874
875 /* The leftmost index field must not be an expression */
876 keycol = idxrel->rd_index->indkey.values[0];
878 return false;
879
880 /*
881 * And the leftmost index field must reference the remote relation column.
882 * This is because if it doesn't, the sequential scan is favorable over
883 * index scan in most cases.
884 */
885 if (attrmap->maplen <= AttrNumberGetAttrOffset(keycol) ||
887 return false;
888
889 /*
890 * The given index access method must implement "amgettuple", which will
891 * be used later to fetch the tuples. See RelationFindReplTupleByIndex().
892 */
893 if (GetIndexAmRoutineByAmId(idxrel->rd_rel->relam, false)->amgettuple == NULL)
894 return false;
895
896 return true;
897}
898
899/*
900 * Return the OID of the replica identity index if one is defined;
901 * the OID of the PK if one exists and is not deferrable;
902 * otherwise, InvalidOid.
903 */
904Oid
906{
907 Oid idxoid;
908
910
911 if (!OidIsValid(idxoid))
913
914 return idxoid;
915}
916
917/*
918 * Returns the index oid if we can use an index for subscriber. Otherwise,
919 * returns InvalidOid.
920 */
921static Oid
923 AttrMap *attrMap)
924{
925 Oid idxoid;
926
927 /*
928 * We never need index oid for partitioned tables, always rely on leaf
929 * partition's index.
930 */
931 if (localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
932 return InvalidOid;
933
934 /*
935 * Simple case, we already have a primary key or a replica identity index.
936 */
938 if (OidIsValid(idxoid))
939 return idxoid;
940
941 if (remoterel->replident == REPLICA_IDENTITY_FULL)
942 {
943 /*
944 * We are looking for one more opportunity for using an index. If
945 * there are any indexes defined on the local relation, try to pick a
946 * suitable index.
947 *
948 * The index selection safely assumes that all the columns are going
949 * to be available for the index scan given that remote relation has
950 * replica identity full.
951 *
952 * Note that we are not using the planner to find the cheapest method
953 * to scan the relation as that would require us to either use lower
954 * level planner functions which would be a maintenance burden in the
955 * long run or use the full-fledged planner which could cause
956 * overhead.
957 */
958 return FindUsableIndexForReplicaIdentityFull(localrel, attrMap);
959 }
960
961 return InvalidOid;
962}
const IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition amapi.c:69
StrategyNumber IndexAmTranslateCompareType(CompareType cmptype, Oid amoid, Oid opfamily, bool missing_ok)
Definition amapi.c:161
void free_attrmap(AttrMap *map)
Definition attmap.c:56
AttrMap * make_attrmap(int maplen)
Definition attmap.c:40
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:479
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1305
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition bitmapset.c:1018
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:867
void bms_free(Bitmapset *a)
Definition bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:750
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:814
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
#define bms_is_empty(a)
Definition bitmapset.h:118
#define NameStr(name)
Definition c.h:765
#define Assert(condition)
Definition c.h:873
#define OidIsValid(objectId)
Definition c.h:788
@ COMPARE_EQ
Definition cmptype.h:36
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition dynahash.c:1509
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition elog.c:1193
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define _(x)
Definition elog.c:91
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
void CheckSubscriptionRelkind(char localrelkind, char remoterelkind, const char *nspname, const char *relname)
#define palloc_array(type, count)
Definition fe_memutils.h:76
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition heaptuple.c:456
@ 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
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:133
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition inval.c:1858
int i
Definition isn.c:77
int LOCKMODE
Definition lockdefs.h:26
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
uint32 LogicalRepRelId
Oid get_opclass_family(Oid opclass)
Definition lsyscache.c:1292
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition makefuncs.c:473
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext CacheMemoryContext
Definition mcxt.c:169
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition namespace.h:98
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
NameData attname
int16 attnum
FormData_pg_attribute * Form_pg_attribute
void * arg
#define foreach_oid(var, lst)
Definition pg_list.h:471
char GetSubscriptionRelState(Oid subid, Oid relid, XLogRecPtr *sublsn)
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
#define InvalidOid
unsigned int Oid
static int fb(int x)
tree ctl
Definition radixtree.h:1838
tree ctl root
Definition radixtree.h:1857
#define RelationGetRelid(relation)
Definition rel.h:514
#define RelationGetDescr(relation)
Definition rel.h:540
List * RelationGetIndexList(Relation relation)
Definition relcache.c:4831
Oid RelationGetPrimaryKeyIndex(Relation relation, bool deferrable_ok)
Definition relcache.c:5042
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition relcache.c:5298
Oid RelationGetReplicaIndex(Relation relation)
Definition relcache.c:5067
@ INDEX_ATTR_BITMAP_PRIMARY_KEY
Definition relcache.h:70
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition relcache.h:71
static MemoryContext LogicalRepPartMapContext
Definition relation.c:50
void logicalrep_partmap_reset_relmap(LogicalRepRelation *remoterel)
Definition relation.c:585
static void logicalrep_partmap_init(void)
Definition relation.c:612
static void logicalrep_report_missing_or_gen_attrs(LogicalRepRelation *remoterel, Bitmapset *missingatts, Bitmapset *generatedatts)
Definition relation.c:268
LogicalRepRelMapEntry * logicalrep_partition_open(LogicalRepRelMapEntry *root, Relation partrel, AttrMap *map)
Definition relation.c:647
static void logicalrep_relmap_free_entry(LogicalRepRelMapEntry *entry)
Definition relation.c:133
static char * logicalrep_get_attrs_str(LogicalRepRelation *remoterel, Bitmapset *atts)
Definition relation.c:239
bool IsIndexUsableForReplicaIdentityFull(Relation idxrel, AttrMap *attrmap)
Definition relation.c:835
static void logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
Definition relation.c:537
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:309
static MemoryContext LogicalRepRelMapContext
Definition relation.c:35
Oid GetRelationIdentityOrPK(Relation rel)
Definition relation.c:905
void logicalrep_relmap_update(LogicalRepRelation *remoterel)
Definition relation.c:165
static void logicalrep_relmap_init(void)
Definition relation.c:106
static int logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
Definition relation.c:221
static Oid FindUsableIndexForReplicaIdentityFull(Relation localrel, AttrMap *attrmap)
Definition relation.c:790
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:518
LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
Definition relation.c:362
static Oid FindLogicalRepLocalIndex(Relation localrel, LogicalRepRelation *remoterel, AttrMap *attrMap)
Definition relation.c:922
#define InvalidStrategy
Definition stratnum.h:24
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
int maplen
Definition attmap.h:37
AttrNumber * attnums
Definition attmap.h:36
Oid fn_oid
Definition fmgr.h:59
amgettuple_function amgettuple
Definition amapi.h:311
Definition pg_list.h:54
LogicalRepRelMapEntry relmapentry
Definition relation.c:55
LogicalRepRelation remoterel
LogicalRepRelId remoteid
Bitmapset * attkeys
Form_pg_class rd_rel
Definition rel.h:111
FmgrInfo eq_opr_finfo
Definition typcache.h:76
Definition c.h:745
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:625
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
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:160
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition typcache.c:386
#define TYPECACHE_EQ_OPR_FINFO
Definition typcache.h:143