LCOV - code coverage report
Current view: top level - src/backend/executor - nodeMemoize.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 268 324 82.7 %
Date: 2021-12-03 04:09:03 Functions: 18 19 94.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * nodeMemoize.c
       4             :  *    Routines to handle caching of results from parameterized nodes
       5             :  *
       6             :  * Portions Copyright (c) 2021, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/executor/nodeMemoize.c
      12             :  *
      13             :  * Memoize nodes are intended to sit above parameterized nodes in the plan
      14             :  * tree in order to cache results from them.  The intention here is that a
      15             :  * repeat scan with a parameter value that has already been seen by the node
      16             :  * can fetch tuples from the cache rather than having to re-scan the outer
      17             :  * node all over again.  The query planner may choose to make use of one of
      18             :  * these when it thinks rescans for previously seen values are likely enough
      19             :  * to warrant adding the additional node.
      20             :  *
      21             :  * The method of cache we use is a hash table.  When the cache fills, we never
      22             :  * spill tuples to disk, instead, we choose to evict the least recently used
      23             :  * cache entry from the cache.  We remember the least recently used entry by
      24             :  * always pushing new entries and entries we look for onto the tail of a
      25             :  * doubly linked list.  This means that older items always bubble to the top
      26             :  * of this LRU list.
      27             :  *
      28             :  * Sometimes our callers won't run their scans to completion. For example a
      29             :  * semi-join only needs to run until it finds a matching tuple, and once it
      30             :  * does, the join operator skips to the next outer tuple and does not execute
      31             :  * the inner side again on that scan.  Because of this, we must keep track of
      32             :  * when a cache entry is complete, and by default, we know it is when we run
      33             :  * out of tuples to read during the scan.  However, there are cases where we
      34             :  * can mark the cache entry as complete without exhausting the scan of all
      35             :  * tuples.  One case is unique joins, where the join operator knows that there
      36             :  * will only be at most one match for any given outer tuple.  In order to
      37             :  * support such cases we allow the "singlerow" option to be set for the cache.
      38             :  * This option marks the cache entry as complete after we read the first tuple
      39             :  * from the subnode.
      40             :  *
      41             :  * It's possible when we're filling the cache for a given set of parameters
      42             :  * that we're unable to free enough memory to store any more tuples.  If this
      43             :  * happens then we'll have already evicted all other cache entries.  When
      44             :  * caching another tuple would cause us to exceed our memory budget, we must
      45             :  * free the entry that we're currently populating and move the state machine
      46             :  * into MEMO_CACHE_BYPASS_MODE.  This means that we'll not attempt to cache
      47             :  * any further tuples for this particular scan.  We don't have the memory for
      48             :  * it.  The state machine will be reset again on the next rescan.  If the
      49             :  * memory requirements to cache the next parameter's tuples are less
      50             :  * demanding, then that may allow us to start putting useful entries back into
      51             :  * the cache again.
      52             :  *
      53             :  *
      54             :  * INTERFACE ROUTINES
      55             :  *      ExecMemoize         - lookup cache, exec subplan when not found
      56             :  *      ExecInitMemoize     - initialize node and subnodes
      57             :  *      ExecEndMemoize      - shutdown node and subnodes
      58             :  *      ExecReScanMemoize   - rescan the memoize node
      59             :  *
      60             :  *      ExecMemoizeEstimate     estimates DSM space needed for parallel plan
      61             :  *      ExecMemoizeInitializeDSM initialize DSM for parallel plan
      62             :  *      ExecMemoizeInitializeWorker attach to DSM info in parallel worker
      63             :  *      ExecMemoizeRetrieveInstrumentation get instrumentation from worker
      64             :  *-------------------------------------------------------------------------
      65             :  */
      66             : 
      67             : #include "postgres.h"
      68             : 
      69             : #include "common/hashfn.h"
      70             : #include "executor/executor.h"
      71             : #include "executor/nodeMemoize.h"
      72             : #include "lib/ilist.h"
      73             : #include "miscadmin.h"
      74             : #include "utils/datum.h"
      75             : #include "utils/lsyscache.h"
      76             : 
      77             : /* States of the ExecMemoize state machine */
      78             : #define MEMO_CACHE_LOOKUP           1   /* Attempt to perform a cache lookup */
      79             : #define MEMO_CACHE_FETCH_NEXT_TUPLE 2   /* Get another tuple from the cache */
      80             : #define MEMO_FILLING_CACHE          3   /* Read outer node to fill cache */
      81             : #define MEMO_CACHE_BYPASS_MODE      4   /* Bypass mode.  Just read from our
      82             :                                          * subplan without caching anything */
      83             : #define MEMO_END_OF_SCAN            5   /* Ready for rescan */
      84             : 
      85             : 
      86             : /* Helper macros for memory accounting */
      87             : #define EMPTY_ENTRY_MEMORY_BYTES(e)     (sizeof(MemoizeEntry) + \
      88             :                                          sizeof(MemoizeKey) + \
      89             :                                          (e)->key->params->t_len);
      90             : #define CACHE_TUPLE_BYTES(t)            (sizeof(MemoizeTuple) + \
      91             :                                          (t)->mintuple->t_len)
      92             : 
      93             :  /* MemoizeTuple Stores an individually cached tuple */
      94             : typedef struct MemoizeTuple
      95             : {
      96             :     MinimalTuple mintuple;      /* Cached tuple */
      97             :     struct MemoizeTuple *next;  /* The next tuple with the same parameter
      98             :                                  * values or NULL if it's the last one */
      99             : } MemoizeTuple;
     100             : 
     101             : /*
     102             :  * MemoizeKey
     103             :  * The hash table key for cached entries plus the LRU list link
     104             :  */
     105             : typedef struct MemoizeKey
     106             : {
     107             :     MinimalTuple params;
     108             :     dlist_node  lru_node;       /* Pointer to next/prev key in LRU list */
     109             : } MemoizeKey;
     110             : 
     111             : /*
     112             :  * MemoizeEntry
     113             :  *      The data struct that the cache hash table stores
     114             :  */
     115             : typedef struct MemoizeEntry
     116             : {
     117             :     MemoizeKey *key;            /* Hash key for hash table lookups */
     118             :     MemoizeTuple *tuplehead;    /* Pointer to the first tuple or NULL if no
     119             :                                  * tuples are cached for this entry */
     120             :     uint32      hash;           /* Hash value (cached) */
     121             :     char        status;         /* Hash status */
     122             :     bool        complete;       /* Did we read the outer plan to completion? */
     123             : } MemoizeEntry;
     124             : 
     125             : 
     126             : #define SH_PREFIX memoize
     127             : #define SH_ELEMENT_TYPE MemoizeEntry
     128             : #define SH_KEY_TYPE MemoizeKey *
     129             : #define SH_SCOPE static inline
     130             : #define SH_DECLARE
     131             : #include "lib/simplehash.h"
     132             : 
     133             : static uint32 MemoizeHash_hash(struct memoize_hash *tb,
     134             :                                const MemoizeKey *key);
     135             : static bool MemoizeHash_equal(struct memoize_hash *tb,
     136             :                               const MemoizeKey *params1,
     137             :                               const MemoizeKey *params2);
     138             : 
     139             : #define SH_PREFIX memoize
     140             : #define SH_ELEMENT_TYPE MemoizeEntry
     141             : #define SH_KEY_TYPE MemoizeKey *
     142             : #define SH_KEY key
     143             : #define SH_HASH_KEY(tb, key) MemoizeHash_hash(tb, key)
     144             : #define SH_EQUAL(tb, a, b) MemoizeHash_equal(tb, a, b)
     145             : #define SH_SCOPE static inline
     146             : #define SH_STORE_HASH
     147             : #define SH_GET_HASH(tb, a) a->hash
     148             : #define SH_DEFINE
     149             : #include "lib/simplehash.h"
     150             : 
     151             : /*
     152             :  * MemoizeHash_hash
     153             :  *      Hash function for simplehash hashtable.  'key' is unused here as we
     154             :  *      require that all table lookups first populate the MemoizeState's
     155             :  *      probeslot with the key values to be looked up.
     156             :  */
     157             : static uint32
     158      286256 : MemoizeHash_hash(struct memoize_hash *tb, const MemoizeKey *key)
     159             : {
     160      286256 :     MemoizeState *mstate = (MemoizeState *) tb->private_data;
     161      286256 :     TupleTableSlot *pslot = mstate->probeslot;
     162      286256 :     uint32      hashkey = 0;
     163      286256 :     int         numkeys = mstate->nkeys;
     164             : 
     165      286256 :     if (mstate->binary_mode)
     166             :     {
     167       89772 :         for (int i = 0; i < numkeys; i++)
     168             :         {
     169             :             /* rotate hashkey left 1 bit at each step */
     170       46330 :             hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
     171             : 
     172       46330 :             if (!pslot->tts_isnull[i])   /* treat nulls as having hash key 0 */
     173             :             {
     174             :                 FormData_pg_attribute *attr;
     175             :                 uint32      hkey;
     176             : 
     177       46330 :                 attr = &pslot->tts_tupleDescriptor->attrs[i];
     178             : 
     179       46330 :                 hkey = datum_image_hash(pslot->tts_values[i], attr->attbyval, attr->attlen);
     180             : 
     181       46330 :                 hashkey ^= hkey;
     182             :             }
     183             :         }
     184             :     }
     185             :     else
     186             :     {
     187      242814 :         FmgrInfo   *hashfunctions = mstate->hashfunctions;
     188      242814 :         Oid        *collations = mstate->collations;
     189             : 
     190      485628 :         for (int i = 0; i < numkeys; i++)
     191             :         {
     192             :             /* rotate hashkey left 1 bit at each step */
     193      242814 :             hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
     194             : 
     195      242814 :             if (!pslot->tts_isnull[i])   /* treat nulls as having hash key 0 */
     196             :             {
     197             :                 uint32      hkey;
     198             : 
     199      242512 :                 hkey = DatumGetUInt32(FunctionCall1Coll(&hashfunctions[i],
     200             :                                                         collations[i], pslot->tts_values[i]));
     201      242512 :                 hashkey ^= hkey;
     202             :             }
     203             :         }
     204             :     }
     205             : 
     206      286256 :     return murmurhash32(hashkey);
     207             : }
     208             : 
     209             : /*
     210             :  * MemoizeHash_equal
     211             :  *      Equality function for confirming hash value matches during a hash
     212             :  *      table lookup.  'key2' is never used.  Instead the MemoizeState's
     213             :  *      probeslot is always populated with details of what's being looked up.
     214             :  */
     215             : static bool
     216      234050 : MemoizeHash_equal(struct memoize_hash *tb, const MemoizeKey *key1,
     217             :                   const MemoizeKey *key2)
     218             : {
     219      234050 :     MemoizeState *mstate = (MemoizeState *) tb->private_data;
     220      234050 :     ExprContext *econtext = mstate->ss.ps.ps_ExprContext;
     221      234050 :     TupleTableSlot *tslot = mstate->tableslot;
     222      234050 :     TupleTableSlot *pslot = mstate->probeslot;
     223             : 
     224             :     /* probeslot should have already been prepared by prepare_probe_slot() */
     225      234050 :     ExecStoreMinimalTuple(key1->params, tslot, false);
     226             : 
     227      234050 :     if (mstate->binary_mode)
     228             :     {
     229       43264 :         int         numkeys = mstate->nkeys;
     230             : 
     231       43264 :         slot_getallattrs(tslot);
     232       43264 :         slot_getallattrs(pslot);
     233             : 
     234       89376 :         for (int i = 0; i < numkeys; i++)
     235             :         {
     236             :             FormData_pg_attribute *attr;
     237             : 
     238       46112 :             if (tslot->tts_isnull[i] != pslot->tts_isnull[i])
     239           0 :                 return false;
     240             : 
     241             :             /* both NULL? they're equal */
     242       46112 :             if (tslot->tts_isnull[i])
     243           0 :                 continue;
     244             : 
     245             :             /* perform binary comparison on the two datums */
     246       46112 :             attr = &tslot->tts_tupleDescriptor->attrs[i];
     247       46112 :             if (!datum_image_eq(tslot->tts_values[i], pslot->tts_values[i],
     248       46112 :                                 attr->attbyval, attr->attlen))
     249           0 :                 return false;
     250             :         }
     251       43264 :         return true;
     252             :     }
     253             :     else
     254             :     {
     255      190786 :         econtext->ecxt_innertuple = tslot;
     256      190786 :         econtext->ecxt_outertuple = pslot;
     257      190786 :         return ExecQualAndReset(mstate->cache_eq_expr, econtext);
     258             :     }
     259             : }
     260             : 
     261             : /*
     262             :  * Initialize the hash table to empty.
     263             :  */
     264             : static void
     265         942 : build_hash_table(MemoizeState *mstate, uint32 size)
     266             : {
     267             :     /* Make a guess at a good size when we're not given a valid size. */
     268         942 :     if (size == 0)
     269           0 :         size = 1024;
     270             : 
     271             :     /* memoize_create will convert the size to a power of 2 */
     272         942 :     mstate->hashtable = memoize_create(mstate->tableContext, size, mstate);
     273         942 : }
     274             : 
     275             : /*
     276             :  * prepare_probe_slot
     277             :  *      Populate mstate's probeslot with the values from the tuple stored
     278             :  *      in 'key'.  If 'key' is NULL, then perform the population by evaluating
     279             :  *      mstate's param_exprs.
     280             :  */
     281             : static inline void
     282      286256 : prepare_probe_slot(MemoizeState *mstate, MemoizeKey *key)
     283             : {
     284      286256 :     TupleTableSlot *pslot = mstate->probeslot;
     285      286256 :     TupleTableSlot *tslot = mstate->tableslot;
     286      286256 :     int         numKeys = mstate->nkeys;
     287             : 
     288      286256 :     ExecClearTuple(pslot);
     289             : 
     290      286256 :     if (key == NULL)
     291             :     {
     292             :         /* Set the probeslot's values based on the current parameter values */
     293      572200 :         for (int i = 0; i < numKeys; i++)
     294      287544 :             pslot->tts_values[i] = ExecEvalExpr(mstate->param_exprs[i],
     295             :                                                 mstate->ss.ps.ps_ExprContext,
     296      287544 :                                                 &pslot->tts_isnull[i]);
     297             :     }
     298             :     else
     299             :     {
     300             :         /* Process the key's MinimalTuple and store the values in probeslot */
     301        1600 :         ExecStoreMinimalTuple(key->params, tslot, false);
     302        1600 :         slot_getallattrs(tslot);
     303        1600 :         memcpy(pslot->tts_values, tslot->tts_values, sizeof(Datum) * numKeys);
     304        1600 :         memcpy(pslot->tts_isnull, tslot->tts_isnull, sizeof(bool) * numKeys);
     305             :     }
     306             : 
     307      286256 :     ExecStoreVirtualTuple(pslot);
     308      286256 : }
     309             : 
     310             : /*
     311             :  * entry_purge_tuples
     312             :  *      Remove all tuples from the cache entry pointed to by 'entry'.  This
     313             :  *      leaves an empty cache entry.  Also, update the memory accounting to
     314             :  *      reflect the removal of the tuples.
     315             :  */
     316             : static inline void
     317        1592 : entry_purge_tuples(MemoizeState *mstate, MemoizeEntry *entry)
     318             : {
     319        1592 :     MemoizeTuple *tuple = entry->tuplehead;
     320        1592 :     uint64      freed_mem = 0;
     321             : 
     322        3184 :     while (tuple != NULL)
     323             :     {
     324        1592 :         MemoizeTuple *next = tuple->next;
     325             : 
     326        1592 :         freed_mem += CACHE_TUPLE_BYTES(tuple);
     327             : 
     328             :         /* Free memory used for this tuple */
     329        1592 :         pfree(tuple->mintuple);
     330        1592 :         pfree(tuple);
     331             : 
     332        1592 :         tuple = next;
     333             :     }
     334             : 
     335        1592 :     entry->complete = false;
     336        1592 :     entry->tuplehead = NULL;
     337             : 
     338             :     /* Update the memory accounting */
     339        1592 :     mstate->mem_used -= freed_mem;
     340        1592 : }
     341             : 
     342             : /*
     343             :  * remove_cache_entry
     344             :  *      Remove 'entry' from the cache and free memory used by it.
     345             :  */
     346             : static void
     347        1592 : remove_cache_entry(MemoizeState *mstate, MemoizeEntry *entry)
     348             : {
     349        1592 :     MemoizeKey *key = entry->key;
     350             : 
     351        1592 :     dlist_delete(&entry->key->lru_node);
     352             : 
     353             :     /* Remove all of the tuples from this entry */
     354        1592 :     entry_purge_tuples(mstate, entry);
     355             : 
     356             :     /*
     357             :      * Update memory accounting. entry_purge_tuples should have already
     358             :      * subtracted the memory used for each cached tuple.  Here we just update
     359             :      * the amount used by the entry itself.
     360             :      */
     361        1592 :     mstate->mem_used -= EMPTY_ENTRY_MEMORY_BYTES(entry);
     362             : 
     363             :     /* Remove the entry from the cache */
     364        1592 :     memoize_delete_item(mstate->hashtable, entry);
     365             : 
     366        1592 :     pfree(key->params);
     367        1592 :     pfree(key);
     368        1592 : }
     369             : 
     370             : /*
     371             :  * cache_purge_all
     372             :  *      Remove all items from the cache
     373             :  */
     374             : static void
     375          12 : cache_purge_all(MemoizeState *mstate)
     376             : {
     377          12 :     uint64      evictions = mstate->hashtable->members;
     378          12 :     PlanState *pstate = (PlanState *) mstate;
     379             : 
     380             :     /*
     381             :      * Likely the most efficient way to remove all items is to just reset the
     382             :      * memory context for the cache and then rebuild a fresh hash table.  This
     383             :      * saves having to remove each item one by one and pfree each cached tuple
     384             :      */
     385          12 :     MemoryContextReset(mstate->tableContext);
     386             : 
     387             :     /* Make the hash table the same size as the original size */
     388          12 :     build_hash_table(mstate, ((Memoize *) pstate->plan)->est_entries);
     389             : 
     390             :     /* reset the LRU list */
     391          12 :     dlist_init(&mstate->lru_list);
     392          12 :     mstate->last_tuple = NULL;
     393          12 :     mstate->entry = NULL;
     394             : 
     395          12 :     mstate->mem_used = 0;
     396             : 
     397             :     /* XXX should we add something new to track these purges? */
     398          12 :     mstate->stats.cache_evictions += evictions; /* Update Stats */
     399          12 : }
     400             : 
     401             : /*
     402             :  * cache_reduce_memory
     403             :  *      Evict older and less recently used items from the cache in order to
     404             :  *      reduce the memory consumption back to something below the
     405             :  *      MemoizeState's mem_limit.
     406             :  *
     407             :  * 'specialkey', if not NULL, causes the function to return false if the entry
     408             :  * which the key belongs to is removed from the cache.
     409             :  */
     410             : static bool
     411        1592 : cache_reduce_memory(MemoizeState *mstate, MemoizeKey *specialkey)
     412             : {
     413        1592 :     bool        specialkey_intact = true;   /* for now */
     414             :     dlist_mutable_iter iter;
     415        1592 :     uint64      evictions = 0;
     416             : 
     417             :     /* Update peak memory usage */
     418        1592 :     if (mstate->mem_used > mstate->stats.mem_peak)
     419           4 :         mstate->stats.mem_peak = mstate->mem_used;
     420             : 
     421             :     /* We expect only to be called when we've gone over budget on memory */
     422             :     Assert(mstate->mem_used > mstate->mem_limit);
     423             : 
     424             :     /* Start the eviction process starting at the head of the LRU list. */
     425        1592 :     dlist_foreach_modify(iter, &mstate->lru_list)
     426             :     {
     427        1592 :         MemoizeKey *key = dlist_container(MemoizeKey, lru_node, iter.cur);
     428             :         MemoizeEntry *entry;
     429             : 
     430             :         /*
     431             :          * Populate the hash probe slot in preparation for looking up this LRU
     432             :          * entry.
     433             :          */
     434        1592 :         prepare_probe_slot(mstate, key);
     435             : 
     436             :         /*
     437             :          * Ideally the LRU list pointers would be stored in the entry itself
     438             :          * rather than in the key.  Unfortunately, we can't do that as the
     439             :          * simplehash.h code may resize the table and allocate new memory for
     440             :          * entries which would result in those pointers pointing to the old
     441             :          * buckets.  However, it's fine to use the key to store this as that's
     442             :          * only referenced by a pointer in the entry, which of course follows
     443             :          * the entry whenever the hash table is resized.  Since we only have a
     444             :          * pointer to the key here, we must perform a hash table lookup to
     445             :          * find the entry that the key belongs to.
     446             :          */
     447        1592 :         entry = memoize_lookup(mstate->hashtable, NULL);
     448             : 
     449             :         /* A good spot to check for corruption of the table and LRU list. */
     450             :         Assert(entry != NULL);
     451             :         Assert(entry->key == key);
     452             : 
     453             :         /*
     454             :          * If we're being called to free memory while the cache is being
     455             :          * populated with new tuples, then we'd better take some care as we
     456             :          * could end up freeing the entry which 'specialkey' belongs to.
     457             :          * Generally callers will pass 'specialkey' as the key for the cache
     458             :          * entry which is currently being populated, so we must set
     459             :          * 'specialkey_intact' to false to inform the caller the specialkey
     460             :          * entry has been removed.
     461             :          */
     462        1592 :         if (key == specialkey)
     463           0 :             specialkey_intact = false;
     464             : 
     465             :         /*
     466             :          * Finally remove the entry.  This will remove from the LRU list too.
     467             :          */
     468        1592 :         remove_cache_entry(mstate, entry);
     469             : 
     470        1592 :         evictions++;
     471             : 
     472             :         /* Exit if we've freed enough memory */
     473        1592 :         if (mstate->mem_used <= mstate->mem_limit)
     474        1592 :             break;
     475             :     }
     476             : 
     477        1592 :     mstate->stats.cache_evictions += evictions; /* Update Stats */
     478             : 
     479        1592 :     return specialkey_intact;
     480             : }
     481             : 
     482             : /*
     483             :  * cache_lookup
     484             :  *      Perform a lookup to see if we've already cached tuples based on the
     485             :  *      scan's current parameters.  If we find an existing entry we move it to
     486             :  *      the end of the LRU list, set *found to true then return it.  If we
     487             :  *      don't find an entry then we create a new one and add it to the end of
     488             :  *      the LRU list.  We also update cache memory accounting and remove older
     489             :  *      entries if we go over the memory budget.  If we managed to free enough
     490             :  *      memory we return the new entry, else we return NULL.
     491             :  *
     492             :  * Callers can assume we'll never return NULL when *found is true.
     493             :  */
     494             : static MemoizeEntry *
     495      284656 : cache_lookup(MemoizeState *mstate, bool *found)
     496             : {
     497             :     MemoizeKey *key;
     498             :     MemoizeEntry *entry;
     499             :     MemoryContext oldcontext;
     500             : 
     501             :     /* prepare the probe slot with the current scan parameters */
     502      284656 :     prepare_probe_slot(mstate, NULL);
     503             : 
     504             :     /*
     505             :      * Add the new entry to the cache.  No need to pass a valid key since the
     506             :      * hash function uses mstate's probeslot, which we populated above.
     507             :      */
     508      284656 :     entry = memoize_insert(mstate->hashtable, NULL, found);
     509             : 
     510      284656 :     if (*found)
     511             :     {
     512             :         /*
     513             :          * Move existing entry to the tail of the LRU list to mark it as the
     514             :          * most recently used item.
     515             :          */
     516      232450 :         dlist_move_tail(&mstate->lru_list, &entry->key->lru_node);
     517             : 
     518      232450 :         return entry;
     519             :     }
     520             : 
     521       52206 :     oldcontext = MemoryContextSwitchTo(mstate->tableContext);
     522             : 
     523             :     /* Allocate a new key */
     524       52206 :     entry->key = key = (MemoizeKey *) palloc(sizeof(MemoizeKey));
     525       52206 :     key->params = ExecCopySlotMinimalTuple(mstate->probeslot);
     526             : 
     527             :     /* Update the total cache memory utilization */
     528       52206 :     mstate->mem_used += EMPTY_ENTRY_MEMORY_BYTES(entry);
     529             : 
     530             :     /* Initialize this entry */
     531       52206 :     entry->complete = false;
     532       52206 :     entry->tuplehead = NULL;
     533             : 
     534             :     /*
     535             :      * Since this is the most recently used entry, push this entry onto the
     536             :      * end of the LRU list.
     537             :      */
     538       52206 :     dlist_push_tail(&mstate->lru_list, &entry->key->lru_node);
     539             : 
     540       52206 :     mstate->last_tuple = NULL;
     541             : 
     542       52206 :     MemoryContextSwitchTo(oldcontext);
     543             : 
     544             :     /*
     545             :      * If we've gone over our memory budget, then we'll free up some space in
     546             :      * the cache.
     547             :      */
     548       52206 :     if (mstate->mem_used > mstate->mem_limit)
     549             :     {
     550             :         /*
     551             :          * Try to free up some memory.  It's highly unlikely that we'll fail
     552             :          * to do so here since the entry we've just added is yet to contain
     553             :          * any tuples and we're able to remove any other entry to reduce the
     554             :          * memory consumption.
     555             :          */
     556        1592 :         if (unlikely(!cache_reduce_memory(mstate, key)))
     557           0 :             return NULL;
     558             : 
     559             :         /*
     560             :          * The process of removing entries from the cache may have caused the
     561             :          * code in simplehash.h to shuffle elements to earlier buckets in the
     562             :          * hash table.  If it has, we'll need to find the entry again by
     563             :          * performing a lookup.  Fortunately, we can detect if this has
     564             :          * happened by seeing if the entry is still in use and that the key
     565             :          * pointer matches our expected key.
     566             :          */
     567        1592 :         if (entry->status != memoize_SH_IN_USE || entry->key != key)
     568             :         {
     569             :             /*
     570             :              * We need to repopulate the probeslot as lookups performed during
     571             :              * the cache evictions above will have stored some other key.
     572             :              */
     573           8 :             prepare_probe_slot(mstate, key);
     574             : 
     575             :             /* Re-find the newly added entry */
     576           8 :             entry = memoize_lookup(mstate->hashtable, NULL);
     577             :             Assert(entry != NULL);
     578             :         }
     579             :     }
     580             : 
     581       52206 :     return entry;
     582             : }
     583             : 
     584             : /*
     585             :  * cache_store_tuple
     586             :  *      Add the tuple stored in 'slot' to the mstate's current cache entry.
     587             :  *      The cache entry must have already been made with cache_lookup().
     588             :  *      mstate's last_tuple field must point to the tail of mstate->entry's
     589             :  *      list of tuples.
     590             :  */
     591             : static bool
     592       48166 : cache_store_tuple(MemoizeState *mstate, TupleTableSlot *slot)
     593             : {
     594             :     MemoizeTuple *tuple;
     595       48166 :     MemoizeEntry *entry = mstate->entry;
     596             :     MemoryContext oldcontext;
     597             : 
     598             :     Assert(slot != NULL);
     599             :     Assert(entry != NULL);
     600             : 
     601       48166 :     oldcontext = MemoryContextSwitchTo(mstate->tableContext);
     602             : 
     603       48166 :     tuple = (MemoizeTuple *) palloc(sizeof(MemoizeTuple));
     604       48166 :     tuple->mintuple = ExecCopySlotMinimalTuple(slot);
     605       48166 :     tuple->next = NULL;
     606             : 
     607             :     /* Account for the memory we just consumed */
     608       48166 :     mstate->mem_used += CACHE_TUPLE_BYTES(tuple);
     609             : 
     610       48166 :     if (entry->tuplehead == NULL)
     611             :     {
     612             :         /*
     613             :          * This is the first tuple for this entry, so just point the list head
     614             :          * to it.
     615             :          */
     616       47980 :         entry->tuplehead = tuple;
     617             :     }
     618             :     else
     619             :     {
     620             :         /* push this tuple onto the tail of the list */
     621         186 :         mstate->last_tuple->next = tuple;
     622             :     }
     623             : 
     624       48166 :     mstate->last_tuple = tuple;
     625       48166 :     MemoryContextSwitchTo(oldcontext);
     626             : 
     627             :     /*
     628             :      * If we've gone over our memory budget then free up some space in the
     629             :      * cache.
     630             :      */
     631       48166 :     if (mstate->mem_used > mstate->mem_limit)
     632             :     {
     633           0 :         MemoizeKey *key = entry->key;
     634             : 
     635           0 :         if (!cache_reduce_memory(mstate, key))
     636           0 :             return false;
     637             : 
     638             :         /*
     639             :          * The process of removing entries from the cache may have caused the
     640             :          * code in simplehash.h to shuffle elements to earlier buckets in the
     641             :          * hash table.  If it has, we'll need to find the entry again by
     642             :          * performing a lookup.  Fortunately, we can detect if this has
     643             :          * happened by seeing if the entry is still in use and that the key
     644             :          * pointer matches our expected key.
     645             :          */
     646           0 :         if (entry->status != memoize_SH_IN_USE || entry->key != key)
     647             :         {
     648             :             /*
     649             :              * We need to repopulate the probeslot as lookups performed during
     650             :              * the cache evictions above will have stored some other key.
     651             :              */
     652           0 :             prepare_probe_slot(mstate, key);
     653             : 
     654             :             /* Re-find the entry */
     655           0 :             mstate->entry = entry = memoize_lookup(mstate->hashtable, NULL);
     656             :             Assert(entry != NULL);
     657             :         }
     658             :     }
     659             : 
     660       48166 :     return true;
     661             : }
     662             : 
     663             : static TupleTableSlot *
     664      374222 : ExecMemoize(PlanState *pstate)
     665             : {
     666      374222 :     MemoizeState *node = castNode(MemoizeState, pstate);
     667             :     PlanState  *outerNode;
     668             :     TupleTableSlot *slot;
     669             : 
     670      374222 :     switch (node->mstatus)
     671             :     {
     672      284656 :         case MEMO_CACHE_LOOKUP:
     673             :             {
     674             :                 MemoizeEntry *entry;
     675             :                 TupleTableSlot *outerslot;
     676             :                 bool        found;
     677             : 
     678             :                 Assert(node->entry == NULL);
     679             : 
     680             :                 /*
     681             :                  * We're only ever in this state for the first call of the
     682             :                  * scan.  Here we have a look to see if we've already seen the
     683             :                  * current parameters before and if we have already cached a
     684             :                  * complete set of records that the outer plan will return for
     685             :                  * these parameters.
     686             :                  *
     687             :                  * When we find a valid cache entry, we'll return the first
     688             :                  * tuple from it. If not found, we'll create a cache entry and
     689             :                  * then try to fetch a tuple from the outer scan.  If we find
     690             :                  * one there, we'll try to cache it.
     691             :                  */
     692             : 
     693             :                 /* see if we've got anything cached for the current parameters */
     694      284656 :                 entry = cache_lookup(node, &found);
     695             : 
     696      284656 :                 if (found && entry->complete)
     697             :                 {
     698      232450 :                     node->stats.cache_hits += 1; /* stats update */
     699             : 
     700             :                     /*
     701             :                      * Set last_tuple and entry so that the state
     702             :                      * MEMO_CACHE_FETCH_NEXT_TUPLE can easily find the next
     703             :                      * tuple for these parameters.
     704             :                      */
     705      232450 :                     node->last_tuple = entry->tuplehead;
     706      232450 :                     node->entry = entry;
     707             : 
     708             :                     /* Fetch the first cached tuple, if there is one */
     709      232450 :                     if (entry->tuplehead)
     710             :                     {
     711       54032 :                         node->mstatus = MEMO_CACHE_FETCH_NEXT_TUPLE;
     712             : 
     713       54032 :                         slot = node->ss.ps.ps_ResultTupleSlot;
     714       54032 :                         ExecStoreMinimalTuple(entry->tuplehead->mintuple,
     715             :                                               slot, false);
     716             : 
     717       54032 :                         return slot;
     718             :                     }
     719             : 
     720             :                     /* The cache entry is void of any tuples. */
     721      178418 :                     node->mstatus = MEMO_END_OF_SCAN;
     722      178418 :                     return NULL;
     723             :                 }
     724             : 
     725             :                 /* Handle cache miss */
     726       52206 :                 node->stats.cache_misses += 1;   /* stats update */
     727             : 
     728       52206 :                 if (found)
     729             :                 {
     730             :                     /*
     731             :                      * A cache entry was found, but the scan for that entry
     732             :                      * did not run to completion.  We'll just remove all
     733             :                      * tuples and start again.  It might be tempting to
     734             :                      * continue where we left off, but there's no guarantee
     735             :                      * the outer node will produce the tuples in the same
     736             :                      * order as it did last time.
     737             :                      */
     738           0 :                     entry_purge_tuples(node, entry);
     739             :                 }
     740             : 
     741             :                 /* Scan the outer node for a tuple to cache */
     742       52206 :                 outerNode = outerPlanState(node);
     743       52206 :                 outerslot = ExecProcNode(outerNode);
     744       52206 :                 if (TupIsNull(outerslot))
     745             :                 {
     746             :                     /*
     747             :                      * cache_lookup may have returned NULL due to failure to
     748             :                      * free enough cache space, so ensure we don't do anything
     749             :                      * here that assumes it worked. There's no need to go into
     750             :                      * bypass mode here as we're setting mstatus to end of
     751             :                      * scan.
     752             :                      */
     753        4226 :                     if (likely(entry))
     754        4226 :                         entry->complete = true;
     755             : 
     756        4226 :                     node->mstatus = MEMO_END_OF_SCAN;
     757        4226 :                     return NULL;
     758             :                 }
     759             : 
     760       47980 :                 node->entry = entry;
     761             : 
     762             :                 /*
     763             :                  * If we failed to create the entry or failed to store the
     764             :                  * tuple in the entry, then go into bypass mode.
     765             :                  */
     766       47980 :                 if (unlikely(entry == NULL ||
     767             :                              !cache_store_tuple(node, outerslot)))
     768             :                 {
     769           0 :                     node->stats.cache_overflows += 1;    /* stats update */
     770             : 
     771           0 :                     node->mstatus = MEMO_CACHE_BYPASS_MODE;
     772             : 
     773             :                     /*
     774             :                      * No need to clear out last_tuple as we'll stay in bypass
     775             :                      * mode until the end of the scan.
     776             :                      */
     777             :                 }
     778             :                 else
     779             :                 {
     780             :                     /*
     781             :                      * If we only expect a single row from this scan then we
     782             :                      * can mark that we're not expecting more.  This allows
     783             :                      * cache lookups to work even when the scan has not been
     784             :                      * executed to completion.
     785             :                      */
     786       47980 :                     entry->complete = node->singlerow;
     787       47980 :                     node->mstatus = MEMO_FILLING_CACHE;
     788             :                 }
     789             : 
     790       47980 :                 slot = node->ss.ps.ps_ResultTupleSlot;
     791       47980 :                 ExecCopySlot(slot, outerslot);
     792       47980 :                 return slot;
     793             :             }
     794             : 
     795       43928 :         case MEMO_CACHE_FETCH_NEXT_TUPLE:
     796             :             {
     797             :                 /* We shouldn't be in this state if these are not set */
     798             :                 Assert(node->entry != NULL);
     799             :                 Assert(node->last_tuple != NULL);
     800             : 
     801             :                 /* Skip to the next tuple to output */
     802       43928 :                 node->last_tuple = node->last_tuple->next;
     803             : 
     804             :                 /* No more tuples in the cache */
     805       43928 :                 if (node->last_tuple == NULL)
     806             :                 {
     807       40716 :                     node->mstatus = MEMO_END_OF_SCAN;
     808       40716 :                     return NULL;
     809             :                 }
     810             : 
     811        3212 :                 slot = node->ss.ps.ps_ResultTupleSlot;
     812        3212 :                 ExecStoreMinimalTuple(node->last_tuple->mintuple, slot,
     813             :                                       false);
     814             : 
     815        3212 :                 return slot;
     816             :             }
     817             : 
     818       45638 :         case MEMO_FILLING_CACHE:
     819             :             {
     820             :                 TupleTableSlot *outerslot;
     821       45638 :                 MemoizeEntry *entry = node->entry;
     822             : 
     823             :                 /* entry should already have been set by MEMO_CACHE_LOOKUP */
     824             :                 Assert(entry != NULL);
     825             : 
     826             :                 /*
     827             :                  * When in the MEMO_FILLING_CACHE state, we've just had a
     828             :                  * cache miss and are populating the cache with the current
     829             :                  * scan tuples.
     830             :                  */
     831       45638 :                 outerNode = outerPlanState(node);
     832       45638 :                 outerslot = ExecProcNode(outerNode);
     833       45638 :                 if (TupIsNull(outerslot))
     834             :                 {
     835             :                     /* No more tuples.  Mark it as complete */
     836       45452 :                     entry->complete = true;
     837       45452 :                     node->mstatus = MEMO_END_OF_SCAN;
     838       45452 :                     return NULL;
     839             :                 }
     840             : 
     841             :                 /*
     842             :                  * Validate if the planner properly set the singlerow flag. It
     843             :                  * should only set that if each cache entry can, at most,
     844             :                  * return 1 row.
     845             :                  */
     846         186 :                 if (unlikely(entry->complete))
     847           0 :                     elog(ERROR, "cache entry already complete");
     848             : 
     849             :                 /* Record the tuple in the current cache entry */
     850         186 :                 if (unlikely(!cache_store_tuple(node, outerslot)))
     851             :                 {
     852             :                     /* Couldn't store it?  Handle overflow */
     853           0 :                     node->stats.cache_overflows += 1;    /* stats update */
     854             : 
     855           0 :                     node->mstatus = MEMO_CACHE_BYPASS_MODE;
     856             : 
     857             :                     /*
     858             :                      * No need to clear out entry or last_tuple as we'll stay
     859             :                      * in bypass mode until the end of the scan.
     860             :                      */
     861             :                 }
     862             : 
     863         186 :                 slot = node->ss.ps.ps_ResultTupleSlot;
     864         186 :                 ExecCopySlot(slot, outerslot);
     865         186 :                 return slot;
     866             :             }
     867             : 
     868           0 :         case MEMO_CACHE_BYPASS_MODE:
     869             :             {
     870             :                 TupleTableSlot *outerslot;
     871             : 
     872             :                 /*
     873             :                  * When in bypass mode we just continue to read tuples without
     874             :                  * caching.  We need to wait until the next rescan before we
     875             :                  * can come out of this mode.
     876             :                  */
     877           0 :                 outerNode = outerPlanState(node);
     878           0 :                 outerslot = ExecProcNode(outerNode);
     879           0 :                 if (TupIsNull(outerslot))
     880             :                 {
     881           0 :                     node->mstatus = MEMO_END_OF_SCAN;
     882           0 :                     return NULL;
     883             :                 }
     884             : 
     885           0 :                 slot = node->ss.ps.ps_ResultTupleSlot;
     886           0 :                 ExecCopySlot(slot, outerslot);
     887           0 :                 return slot;
     888             :             }
     889             : 
     890           0 :         case MEMO_END_OF_SCAN:
     891             : 
     892             :             /*
     893             :              * We've already returned NULL for this scan, but just in case
     894             :              * something calls us again by mistake.
     895             :              */
     896           0 :             return NULL;
     897             : 
     898           0 :         default:
     899           0 :             elog(ERROR, "unrecognized memoize state: %d",
     900             :                  (int) node->mstatus);
     901             :             return NULL;
     902             :     }                           /* switch */
     903             : }
     904             : 
     905             : MemoizeState *
     906         930 : ExecInitMemoize(Memoize *node, EState *estate, int eflags)
     907             : {
     908         930 :     MemoizeState *mstate = makeNode(MemoizeState);
     909             :     Plan       *outerNode;
     910             :     int         i;
     911             :     int         nkeys;
     912             :     Oid        *eqfuncoids;
     913             : 
     914             :     /* check for unsupported flags */
     915             :     Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
     916             : 
     917         930 :     mstate->ss.ps.plan = (Plan *) node;
     918         930 :     mstate->ss.ps.state = estate;
     919         930 :     mstate->ss.ps.ExecProcNode = ExecMemoize;
     920             : 
     921             :     /*
     922             :      * Miscellaneous initialization
     923             :      *
     924             :      * create expression context for node
     925             :      */
     926         930 :     ExecAssignExprContext(estate, &mstate->ss.ps);
     927             : 
     928         930 :     outerNode = outerPlan(node);
     929         930 :     outerPlanState(mstate) = ExecInitNode(outerNode, estate, eflags);
     930             : 
     931             :     /*
     932             :      * Initialize return slot and type. No need to initialize projection info
     933             :      * because this node doesn't do projections.
     934             :      */
     935         930 :     ExecInitResultTupleSlotTL(&mstate->ss.ps, &TTSOpsMinimalTuple);
     936         930 :     mstate->ss.ps.ps_ProjInfo = NULL;
     937             : 
     938             :     /*
     939             :      * Initialize scan slot and type.
     940             :      */
     941         930 :     ExecCreateScanSlotFromOuterPlan(estate, &mstate->ss, &TTSOpsMinimalTuple);
     942             : 
     943             :     /*
     944             :      * Set the state machine to lookup the cache.  We won't find anything
     945             :      * until we cache something, but this saves a special case to create the
     946             :      * first entry.
     947             :      */
     948         930 :     mstate->mstatus = MEMO_CACHE_LOOKUP;
     949             : 
     950         930 :     mstate->nkeys = nkeys = node->numKeys;
     951         930 :     mstate->hashkeydesc = ExecTypeFromExprList(node->param_exprs);
     952         930 :     mstate->tableslot = MakeSingleTupleTableSlot(mstate->hashkeydesc,
     953             :                                                  &TTSOpsMinimalTuple);
     954         930 :     mstate->probeslot = MakeSingleTupleTableSlot(mstate->hashkeydesc,
     955             :                                                  &TTSOpsVirtual);
     956             : 
     957         930 :     mstate->param_exprs = (ExprState **) palloc(nkeys * sizeof(ExprState *));
     958         930 :     mstate->collations = node->collations;    /* Just point directly to the plan
     959             :                                              * data */
     960         930 :     mstate->hashfunctions = (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
     961             : 
     962         930 :     eqfuncoids = palloc(nkeys * sizeof(Oid));
     963             : 
     964        1872 :     for (i = 0; i < nkeys; i++)
     965             :     {
     966         942 :         Oid         hashop = node->hashOperators[i];
     967             :         Oid         left_hashfn;
     968             :         Oid         right_hashfn;
     969         942 :         Expr       *param_expr = (Expr *) list_nth(node->param_exprs, i);
     970             : 
     971         942 :         if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn))
     972           0 :             elog(ERROR, "could not find hash function for hash operator %u",
     973             :                  hashop);
     974             : 
     975         942 :         fmgr_info(left_hashfn, &mstate->hashfunctions[i]);
     976             : 
     977         942 :         mstate->param_exprs[i] = ExecInitExpr(param_expr, (PlanState *) mstate);
     978         942 :         eqfuncoids[i] = get_opcode(hashop);
     979             :     }
     980             : 
     981        1860 :     mstate->cache_eq_expr = ExecBuildParamSetEqual(mstate->hashkeydesc,
     982             :                                                    &TTSOpsMinimalTuple,
     983             :                                                    &TTSOpsVirtual,
     984             :                                                    eqfuncoids,
     985         930 :                                                    node->collations,
     986         930 :                                                    node->param_exprs,
     987             :                                                    (PlanState *) mstate);
     988             : 
     989         930 :     pfree(eqfuncoids);
     990         930 :     mstate->mem_used = 0;
     991             : 
     992             :     /* Limit the total memory consumed by the cache to this */
     993         930 :     mstate->mem_limit = get_hash_memory_limit();
     994             : 
     995             :     /* A memory context dedicated for the cache */
     996         930 :     mstate->tableContext = AllocSetContextCreate(CurrentMemoryContext,
     997             :                                                  "MemoizeHashTable",
     998             :                                                  ALLOCSET_DEFAULT_SIZES);
     999             : 
    1000         930 :     dlist_init(&mstate->lru_list);
    1001         930 :     mstate->last_tuple = NULL;
    1002         930 :     mstate->entry = NULL;
    1003             : 
    1004             :     /*
    1005             :      * Mark if we can assume the cache entry is completed after we get the
    1006             :      * first record for it.  Some callers might not call us again after
    1007             :      * getting the first match. e.g. A join operator performing a unique join
    1008             :      * is able to skip to the next outer tuple after getting the first
    1009             :      * matching inner tuple.  In this case, the cache entry is complete after
    1010             :      * getting the first tuple.  This allows us to mark it as so.
    1011             :      */
    1012         930 :     mstate->singlerow = node->singlerow;
    1013         930 :     mstate->keyparamids = node->keyparamids;
    1014             : 
    1015             :     /*
    1016             :      * Record if the cache keys should be compared bit by bit, or logically
    1017             :      * using the type's hash equality operator
    1018             :      */
    1019         930 :     mstate->binary_mode = node->binary_mode;
    1020             : 
    1021             :     /* Zero the statistics counters */
    1022         930 :     memset(&mstate->stats, 0, sizeof(MemoizeInstrumentation));
    1023             : 
    1024             :     /* Allocate and set up the actual cache */
    1025         930 :     build_hash_table(mstate, node->est_entries);
    1026             : 
    1027         930 :     return mstate;
    1028             : }
    1029             : 
    1030             : void
    1031         930 : ExecEndMemoize(MemoizeState *node)
    1032             : {
    1033             : #ifdef USE_ASSERT_CHECKING
    1034             :     /* Validate the memory accounting code is correct in assert builds. */
    1035             :     {
    1036             :         int         count;
    1037             :         uint64      mem = 0;
    1038             :         memoize_iterator i;
    1039             :         MemoizeEntry *entry;
    1040             : 
    1041             :         memoize_start_iterate(node->hashtable, &i);
    1042             : 
    1043             :         count = 0;
    1044             :         while ((entry = memoize_iterate(node->hashtable, &i)) != NULL)
    1045             :         {
    1046             :             MemoizeTuple *tuple = entry->tuplehead;
    1047             : 
    1048             :             mem += EMPTY_ENTRY_MEMORY_BYTES(entry);
    1049             :             while (tuple != NULL)
    1050             :             {
    1051             :                 mem += CACHE_TUPLE_BYTES(tuple);
    1052             :                 tuple = tuple->next;
    1053             :             }
    1054             :             count++;
    1055             :         }
    1056             : 
    1057             :         Assert(count == node->hashtable->members);
    1058             :         Assert(mem == node->mem_used);
    1059             :     }
    1060             : #endif
    1061             : 
    1062             :     /*
    1063             :      * When ending a parallel worker, copy the statistics gathered by the
    1064             :      * worker back into shared memory so that it can be picked up by the main
    1065             :      * process to report in EXPLAIN ANALYZE.
    1066             :      */
    1067         930 :     if (node->shared_info != NULL && IsParallelWorker())
    1068             :     {
    1069             :         MemoizeInstrumentation *si;
    1070             : 
    1071             :         /* Make mem_peak available for EXPLAIN */
    1072           0 :         if (node->stats.mem_peak == 0)
    1073           0 :             node->stats.mem_peak = node->mem_used;
    1074             : 
    1075             :         Assert(ParallelWorkerNumber <= node->shared_info->num_workers);
    1076           0 :         si = &node->shared_info->sinstrument[ParallelWorkerNumber];
    1077           0 :         memcpy(si, &node->stats, sizeof(MemoizeInstrumentation));
    1078             :     }
    1079             : 
    1080             :     /* Remove the cache context */
    1081         930 :     MemoryContextDelete(node->tableContext);
    1082             : 
    1083         930 :     ExecClearTuple(node->ss.ss_ScanTupleSlot);
    1084             :     /* must drop pointer to cache result tuple */
    1085         930 :     ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
    1086             : 
    1087             :     /*
    1088             :      * free exprcontext
    1089             :      */
    1090         930 :     ExecFreeExprContext(&node->ss.ps);
    1091             : 
    1092             :     /*
    1093             :      * shut down the subplan
    1094             :      */
    1095         930 :     ExecEndNode(outerPlanState(node));
    1096         930 : }
    1097             : 
    1098             : void
    1099      284656 : ExecReScanMemoize(MemoizeState *node)
    1100             : {
    1101      284656 :     PlanState  *outerPlan = outerPlanState(node);
    1102             : 
    1103             :     /* Mark that we must lookup the cache for a new set of parameters */
    1104      284656 :     node->mstatus = MEMO_CACHE_LOOKUP;
    1105             : 
    1106             :     /* nullify pointers used for the last scan */
    1107      284656 :     node->entry = NULL;
    1108      284656 :     node->last_tuple = NULL;
    1109             : 
    1110             :     /*
    1111             :      * if chgParam of subnode is not null then plan will be re-scanned by
    1112             :      * first ExecProcNode.
    1113             :      */
    1114      284656 :     if (outerPlan->chgParam == NULL)
    1115           0 :         ExecReScan(outerPlan);
    1116             : 
    1117             :     /*
    1118             :      * Purge the entire cache if a parameter changed that is not part of the
    1119             :      * cache key.
    1120             :      */
    1121      284656 :     if (bms_nonempty_difference(outerPlan->chgParam, node->keyparamids))
    1122          12 :         cache_purge_all(node);
    1123      284656 : }
    1124             : 
    1125             : /*
    1126             :  * ExecEstimateCacheEntryOverheadBytes
    1127             :  *      For use in the query planner to help it estimate the amount of memory
    1128             :  *      required to store a single entry in the cache.
    1129             :  */
    1130             : double
    1131      490028 : ExecEstimateCacheEntryOverheadBytes(double ntuples)
    1132             : {
    1133      490028 :     return sizeof(MemoizeEntry) + sizeof(MemoizeKey) + sizeof(MemoizeTuple) *
    1134             :         ntuples;
    1135             : }
    1136             : 
    1137             : /* ----------------------------------------------------------------
    1138             :  *                      Parallel Query Support
    1139             :  * ----------------------------------------------------------------
    1140             :  */
    1141             : 
    1142             :  /* ----------------------------------------------------------------
    1143             :   *     ExecMemoizeEstimate
    1144             :   *
    1145             :   *     Estimate space required to propagate memoize statistics.
    1146             :   * ----------------------------------------------------------------
    1147             :   */
    1148             : void
    1149           4 : ExecMemoizeEstimate(MemoizeState *node, ParallelContext *pcxt)
    1150             : {
    1151             :     Size        size;
    1152             : 
    1153             :     /* don't need this if not instrumenting or no workers */
    1154           4 :     if (!node->ss.ps.instrument || pcxt->nworkers == 0)
    1155           4 :         return;
    1156             : 
    1157           0 :     size = mul_size(pcxt->nworkers, sizeof(MemoizeInstrumentation));
    1158           0 :     size = add_size(size, offsetof(SharedMemoizeInfo, sinstrument));
    1159           0 :     shm_toc_estimate_chunk(&pcxt->estimator, size);
    1160           0 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
    1161             : }
    1162             : 
    1163             : /* ----------------------------------------------------------------
    1164             :  *      ExecMemoizeInitializeDSM
    1165             :  *
    1166             :  *      Initialize DSM space for memoize statistics.
    1167             :  * ----------------------------------------------------------------
    1168             :  */
    1169             : void
    1170           4 : ExecMemoizeInitializeDSM(MemoizeState *node, ParallelContext *pcxt)
    1171             : {
    1172             :     Size        size;
    1173             : 
    1174             :     /* don't need this if not instrumenting or no workers */
    1175           4 :     if (!node->ss.ps.instrument || pcxt->nworkers == 0)
    1176           4 :         return;
    1177             : 
    1178           0 :     size = offsetof(SharedMemoizeInfo, sinstrument)
    1179           0 :         + pcxt->nworkers * sizeof(MemoizeInstrumentation);
    1180           0 :     node->shared_info = shm_toc_allocate(pcxt->toc, size);
    1181             :     /* ensure any unfilled slots will contain zeroes */
    1182           0 :     memset(node->shared_info, 0, size);
    1183           0 :     node->shared_info->num_workers = pcxt->nworkers;
    1184           0 :     shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
    1185           0 :                    node->shared_info);
    1186             : }
    1187             : 
    1188             : /* ----------------------------------------------------------------
    1189             :  *      ExecMemoizeInitializeWorker
    1190             :  *
    1191             :  *      Attach worker to DSM space for memoize statistics.
    1192             :  * ----------------------------------------------------------------
    1193             :  */
    1194             : void
    1195           8 : ExecMemoizeInitializeWorker(MemoizeState *node, ParallelWorkerContext *pwcxt)
    1196             : {
    1197           8 :     node->shared_info =
    1198           8 :         shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
    1199           8 : }
    1200             : 
    1201             : /* ----------------------------------------------------------------
    1202             :  *      ExecMemoizeRetrieveInstrumentation
    1203             :  *
    1204             :  *      Transfer memoize statistics from DSM to private memory.
    1205             :  * ----------------------------------------------------------------
    1206             :  */
    1207             : void
    1208           0 : ExecMemoizeRetrieveInstrumentation(MemoizeState *node)
    1209             : {
    1210             :     Size        size;
    1211             :     SharedMemoizeInfo *si;
    1212             : 
    1213           0 :     if (node->shared_info == NULL)
    1214           0 :         return;
    1215             : 
    1216           0 :     size = offsetof(SharedMemoizeInfo, sinstrument)
    1217           0 :         + node->shared_info->num_workers * sizeof(MemoizeInstrumentation);
    1218           0 :     si = palloc(size);
    1219           0 :     memcpy(si, node->shared_info, size);
    1220           0 :     node->shared_info = si;
    1221             : }

Generated by: LCOV version 1.14