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