LCOV - code coverage report
Current view: top level - src/backend/replication/logical - relation.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 237 265 89.4 %
Date: 2024-11-21 08:14:44 Functions: 17 17 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14