LCOV - code coverage report
Current view: top level - src/backend/executor - nodeMemoize.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 82.9 % 345 286
Test Date: 2026-08-22 10:15:50 Functions: 95.2 % 21 20
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 57.0 % 142 81

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

Generated by: LCOV version 2.0-1