/* Per-backend state for a TidStore */
 struct TidStore
 {
-   /* MemoryContext where the TidStore is allocated */
-   MemoryContext context;
-
-   /* MemoryContext that the radix tree uses */
+   /*
+    * MemoryContext for the radix tree when using local memory, NULL for
+    * shared memory
+    */
    MemoryContext rt_context;
 
    /* Storage for TIDs. Use either one depending on TidStoreIsShared() */
    size_t      maxBlockSize = ALLOCSET_DEFAULT_MAXSIZE;
 
    ts = palloc0(sizeof(TidStore));
-   ts->context = CurrentMemoryContext;
 
    /* choose the maxBlockSize to be no larger than 1/16 of max_bytes */
    while (16 * maxBlockSize > max_bytes)
 
 /*
  * Similar to TidStoreCreateLocal() but create a shared TidStore on a
- * DSA area. The TID storage will live in the DSA area, and the memory
- * context rt_context will have only meta data of the radix tree.
+ * DSA area.
  *
  * The returned object is allocated in backend-local memory.
  */
    size_t      dsa_max_size = DSA_MAX_SEGMENT_SIZE;
 
    ts = palloc0(sizeof(TidStore));
-   ts->context = CurrentMemoryContext;
-
-   ts->rt_context = AllocSetContextCreate(CurrentMemoryContext,
-                                          "TID storage meta data",
-                                          ALLOCSET_SMALL_SIZES);
 
    /*
     * Choose the initial and maximum DSA segment sizes to be no longer than
        dsa_init_size = dsa_max_size;
 
    area = dsa_create_ext(tranche_id, dsa_init_size, dsa_max_size);
-   ts->tree.shared = shared_ts_create(ts->rt_context, area,
-                                      tranche_id);
+   ts->tree.shared = shared_ts_create(area, tranche_id);
    ts->area = area;
 
    return ts;
    if (TidStoreIsShared(ts))
    {
        shared_ts_free(ts->tree.shared);
-
        dsa_detach(ts->area);
    }
    else
+   {
        local_ts_free(ts->tree.local);
-
-   MemoryContextDelete(ts->rt_context);
+       MemoryContextDelete(ts->rt_context);
+   }
 
    pfree(ts);
 }
 
 #endif
 
 #ifdef RT_SHMEM
-RT_SCOPE   RT_RADIX_TREE *RT_CREATE(MemoryContext ctx, dsa_area *dsa, int tranche_id);
+RT_SCOPE   RT_RADIX_TREE *RT_CREATE(dsa_area *dsa, int tranche_id);
 RT_SCOPE   RT_RADIX_TREE *RT_ATTACH(dsa_area *dsa, dsa_pointer dp);
 RT_SCOPE void RT_DETACH(RT_RADIX_TREE * tree);
 RT_SCOPE   RT_HANDLE RT_GET_HANDLE(RT_RADIX_TREE * tree);
 /* Entry point for allocating and accessing the tree */
 struct RT_RADIX_TREE
 {
-   MemoryContext context;
-
    /* pointing to either local memory or DSA */
    RT_RADIX_TREE_CONTROL *ctl;
 
 /***************** SETUP / TEARDOWN *****************/
 
 /*
- * Create the radix tree in the given memory context and return it.
+ * Create the radix tree root in the caller's memory context and return it.
  *
- * All local memory required for a radix tree is allocated in the given
- * memory context and its children. Note that RT_FREE() will delete all
- * allocated space within the given memory context, so the dsa_area should
- * be created in a different context.
+ * The tree's nodes and leaves are allocated in "ctx" and its children for
+ * local memory, or in "dsa" for shared memory.
  */
 RT_SCOPE   RT_RADIX_TREE *
 #ifdef RT_SHMEM
-RT_CREATE(MemoryContext ctx, dsa_area *dsa, int tranche_id)
+RT_CREATE(dsa_area *dsa, int tranche_id)
 #else
 RT_CREATE(MemoryContext ctx)
 #endif
 {
    RT_RADIX_TREE *tree;
-   MemoryContext old_ctx;
    RT_CHILD_PTR rootnode;
 #ifdef RT_SHMEM
    dsa_pointer dp;
 #endif
 
-   old_ctx = MemoryContextSwitchTo(ctx);
-
    tree = (RT_RADIX_TREE *) palloc0(sizeof(RT_RADIX_TREE));
-   tree->context = ctx;
 
 #ifdef RT_SHMEM
    tree->dsa = dsa;
    }
 
    /* By default we use the passed context for leaves. */
-   tree->leaf_context = tree->context;
+   tree->leaf_context = ctx;
 
 #ifndef RT_VARLEN_VALUE_SIZE
 
    tree->ctl->start_shift = 0;
    tree->ctl->max_val = RT_SHIFT_GET_MAX_VAL(0);
 
-   MemoryContextSwitchTo(old_ctx);
-
    return tree;
 }
 
     */
    tree->ctl->magic = 0;
    dsa_free(tree->dsa, tree->ctl->handle);
-#endif
-
+#else
    /*
-    * Free all space allocated within the tree's context and delete all child
+    * Free all space allocated within the leaf context and delete all child
     * contexts such as those used for nodes.
     */
-   MemoryContextReset(tree->context);
+   MemoryContextReset(tree->leaf_context);
+
+   pfree(tree->ctl);
+#endif
+   pfree(tree);
 }
 
 /***************** ITERATION *****************/
    Assert(tree->ctl->magic == RT_RADIX_TREE_MAGIC);
    total = dsa_get_total_size(tree->dsa);
 #else
-   total = MemoryContextMemAllocated(tree->context, true);
+   total = MemoryContextMemAllocated(tree->leaf_context, true);
 #endif
 
    return total;
 
 static void
 test_empty(void)
 {
-   MemoryContext radixtree_ctx;
    rt_radix_tree *radixtree;
    rt_iter    *iter;
    uint64      key;
 #ifdef TEST_SHARED_RT
    int         tranche_id = LWLockNewTrancheId();
    dsa_area   *dsa;
-#endif
-
-   radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
-                                         "test_radix_tree",
-                                         ALLOCSET_SMALL_SIZES);
 
-#ifdef TEST_SHARED_RT
    LWLockRegisterTranche(tranche_id, "test_radix_tree");
    dsa = dsa_create(tranche_id);
-
-   radixtree = rt_create(radixtree_ctx, dsa, tranche_id);
+   radixtree = rt_create(dsa, tranche_id);
 #else
+   MemoryContext radixtree_ctx;
+
+   radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
+                                         "test_radix_tree",
+                                         ALLOCSET_SMALL_SIZES);
    radixtree = rt_create(radixtree_ctx);
 #endif
 
 static void
 test_basic(rt_node_class_test_elem *test_info, int shift, bool asc)
 {
-   MemoryContext radixtree_ctx;
    rt_radix_tree *radixtree;
    rt_iter    *iter;
    uint64     *keys;
 #ifdef TEST_SHARED_RT
    int         tranche_id = LWLockNewTrancheId();
    dsa_area   *dsa;
-#endif
 
-   radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
-                                         "test_radix_tree",
-                                         ALLOCSET_SMALL_SIZES);
-
-#ifdef TEST_SHARED_RT
    LWLockRegisterTranche(tranche_id, "test_radix_tree");
    dsa = dsa_create(tranche_id);
-
-   radixtree = rt_create(radixtree_ctx, dsa, tranche_id);
+   radixtree = rt_create(dsa, tranche_id);
 #else
+   MemoryContext radixtree_ctx;
+
+   radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
+                                         "test_radix_tree",
+                                         ALLOCSET_SMALL_SIZES);
    radixtree = rt_create(radixtree_ctx);
 #endif
 
 static void
 test_random(void)
 {
-   MemoryContext radixtree_ctx;
    rt_radix_tree *radixtree;
    rt_iter    *iter;
    pg_prng_state state;
 #ifdef TEST_SHARED_RT
    int         tranche_id = LWLockNewTrancheId();
    dsa_area   *dsa;
-#endif
 
-   radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
-                                         "test_radix_tree",
-                                         ALLOCSET_SMALL_SIZES);
-
-#ifdef TEST_SHARED_RT
    LWLockRegisterTranche(tranche_id, "test_radix_tree");
    dsa = dsa_create(tranche_id);
-
-   radixtree = rt_create(radixtree_ctx, dsa, tranche_id);
+   radixtree = rt_create(dsa, tranche_id);
 #else
+   MemoryContext radixtree_ctx;
+
+   radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
+                                         "test_radix_tree",
+                                         ALLOCSET_SMALL_SIZES);
    radixtree = rt_create(radixtree_ctx);
 #endif