This is an automated email from the ASF dual-hosted git repository.

yjhjstz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit ed1d76f4fa6e3bf0a90c4fb46729177696121ff1
Author: Soumyadeep Chakraborty <[email protected]>
AuthorDate: Sat Oct 29 15:58:23 2022 -0700

    ao/co: Refactor DML states
    
    This commit makes the DML state nomenclature more canonical, cleans up
    struct declarations etc.
---
 src/backend/access/aocs/aocsam_handler.c           | 115 ++++++++++----------
 .../access/appendonly/appendonlyam_handler.c       | 116 +++++++++------------
 2 files changed, 109 insertions(+), 122 deletions(-)

diff --git a/src/backend/access/aocs/aocsam_handler.c 
b/src/backend/access/aocs/aocsam_handler.c
index 6ebe7402b3..0fc06c4ad9 100644
--- a/src/backend/access/aocs/aocsam_handler.c
+++ b/src/backend/access/aocs/aocsam_handler.c
@@ -122,23 +122,16 @@ static void reset_state_cb(void *arg);
  *             a memory context that should be long lived enough and is
  *                     responsible for reseting the state via its reset cb
  */
-static struct AOCOLocal
+typedef struct AOCODMLStates
 {
        AOCODMLState           *last_used_state;
-       HTAB                               *dmlDescriptorTab;
+       HTAB                               *state_table;
 
        MemoryContext                   stateCxt;
        MemoryContextCallback   cb;
-} aocoLocal = {
-       .last_used_state  = NULL,
-       .dmlDescriptorTab = NULL,
-
-       .stateCxt                 = NULL,
-       .cb                               = {
-               .func   = reset_state_cb,
-               .arg    = NULL
-       },
-};
+} AOCODMLStates;
+
+static AOCODMLStates aocoDMLStates;
 
 /*
  * There are two cases that we are called from, during context destruction
@@ -150,32 +143,42 @@ static struct AOCOLocal
 static void
 reset_state_cb(void *arg)
 {
-       aocoLocal.dmlDescriptorTab = NULL;
-       aocoLocal.last_used_state = NULL;
-       aocoLocal.stateCxt = NULL;
+       aocoDMLStates.state_table = NULL;
+       aocoDMLStates.last_used_state = NULL;
+       aocoDMLStates.stateCxt = NULL;
 }
 
+
+/*
+ * Initialize the backend local AOCODMLStates object for this backend for the
+ * current DML or DML-like command (if not already initialized).
+ *
+ * This function should be called with a current memory context whose life
+ * span is enough to last until the end of this command execution.
+ */
 static void
-init_dml_local_state(void)
+init_aoco_dml_states()
 {
        HASHCTL hash_ctl;
 
-       if (!aocoLocal.dmlDescriptorTab)
+       if (!aocoDMLStates.state_table)
        {
-               Assert(aocoLocal.stateCxt == NULL);
-               aocoLocal.stateCxt = AllocSetContextCreate(
+               Assert(aocoDMLStates.stateCxt == NULL);
+               aocoDMLStates.stateCxt = AllocSetContextCreate(
                        CurrentMemoryContext,
                        "AppendOnly DML State Context",
                        ALLOCSET_SMALL_SIZES);
-               MemoryContextRegisterResetCallback(
-                       aocoLocal.stateCxt,
-                       &aocoLocal.cb);
+
+               aocoDMLStates.cb.func = reset_state_cb;
+               aocoDMLStates.cb.arg = NULL;
+               MemoryContextRegisterResetCallback(aocoDMLStates.stateCxt,
+                                                                               
   &aocoDMLStates.cb);
 
                memset(&hash_ctl, 0, sizeof(hash_ctl));
                hash_ctl.keysize = sizeof(Oid);
                hash_ctl.entrysize = sizeof(AOCODMLState);
-               hash_ctl.hcxt = aocoLocal.stateCxt;
-               aocoLocal.dmlDescriptorTab =
+               hash_ctl.hcxt = aocoDMLStates.stateCxt;
+               aocoDMLStates.state_table =
                        hash_create("AppendOnly DML state", 128, &hash_ctl,
                                    HASH_CONTEXT | HASH_ELEM | HASH_BLOBS);
        }
@@ -187,19 +190,18 @@ init_dml_local_state(void)
  *
  * Should be called exactly once per relation.
  */
-static inline AOCODMLState *
-enter_dml_state(const Oid relationOid)
+static inline void
+init_dml_state(const Oid relationOid)
 {
        AOCODMLState *state;
        bool                            found;
 
-       Assert(aocoLocal.dmlDescriptorTab);
+       Assert(aocoDMLStates.state_table);
 
-       state = (AOCODMLState *) hash_search(
-               aocoLocal.dmlDescriptorTab,
-               &relationOid,
-               HASH_ENTER,
-               &found);
+       state = (AOCODMLState *) hash_search(aocoDMLStates.state_table,
+                                                                               
 &relationOid,
+                                                                               
 HASH_ENTER,
+                                                                               
 &found);
 
        state->insertDesc = NULL;
        state->deleteDesc = NULL;
@@ -210,8 +212,7 @@ enter_dml_state(const Oid relationOid)
 
        Assert(!found);
 
-       aocoLocal.last_used_state = state;
-       return state;
+       aocoDMLStates.last_used_state = state;
 }
 
 /*
@@ -222,21 +223,20 @@ static inline AOCODMLState *
 find_dml_state(const Oid relationOid)
 {
        AOCODMLState *state;
-       Assert(aocoLocal.dmlDescriptorTab);
+       Assert(aocoDMLStates.state_table);
 
-       if (aocoLocal.last_used_state &&
-               aocoLocal.last_used_state->relationOid == relationOid)
-               return aocoLocal.last_used_state;
+       if (aocoDMLStates.last_used_state &&
+               aocoDMLStates.last_used_state->relationOid == relationOid)
+               return aocoDMLStates.last_used_state;
 
-       state = (AOCODMLState *) hash_search(
-               aocoLocal.dmlDescriptorTab,
-               &relationOid,
-               HASH_FIND,
-               NULL);
+       state = (AOCODMLState *) hash_search(aocoDMLStates.state_table,
+                                                                               
 &relationOid,
+                                                                               
 HASH_FIND,
+                                                                               
 NULL);
 
        Assert(state);
 
-       aocoLocal.last_used_state = state;
+       aocoDMLStates.last_used_state = state;
        return state;
 }
 
@@ -250,20 +250,19 @@ static inline AOCODMLState *
 remove_dml_state(const Oid relationOid)
 {
        AOCODMLState *state;
-       Assert(aocoLocal.dmlDescriptorTab);
+       Assert(aocoDMLStates.state_table);
 
-       state = (AOCODMLState *) hash_search(
-               aocoLocal.dmlDescriptorTab,
-               &relationOid,
-               HASH_REMOVE,
-               NULL);
+       state = (AOCODMLState *) hash_search(aocoDMLStates.state_table,
+                                                                               
 &relationOid,
+                                                                               
 HASH_REMOVE,
+                                                                               
 NULL);
 
        if (!state)
                return NULL;
 
-       if (aocoLocal.last_used_state &&
-               aocoLocal.last_used_state->relationOid == relationOid)
-               aocoLocal.last_used_state = NULL;
+       if (aocoDMLStates.last_used_state &&
+               aocoDMLStates.last_used_state->relationOid == relationOid)
+               aocoDMLStates.last_used_state = NULL;
 
        return state;
 }
@@ -277,8 +276,8 @@ remove_dml_state(const Oid relationOid)
 void
 aoco_dml_init(Relation relation, CmdType operation)
 {
-       init_dml_local_state();
-       (void) enter_dml_state(RelationGetRelid(relation));
+       init_aoco_dml_states();
+       init_dml_state(RelationGetRelid(relation));
 }
 
 /*
@@ -372,7 +371,7 @@ get_insert_descriptor(const Relation relation)
        MemoryContext oldcxt;
 
        state = find_dml_state(RelationGetRelid(relation));
-       oldcxt = MemoryContextSwitchTo(aocoLocal.stateCxt);
+       oldcxt = MemoryContextSwitchTo(aocoDMLStates.stateCxt);
        if (state->insertDesc == NULL)
        {
 
@@ -490,7 +489,7 @@ get_delete_descriptor(const Relation relation, bool 
forUpdate)
                                                 errmsg("deletes on append-only 
tables are not supported in serializable transactions")));
                }
 
-               oldcxt = MemoryContextSwitchTo(aocoLocal.stateCxt);
+               oldcxt = MemoryContextSwitchTo(aocoDMLStates.stateCxt);
                state->deleteDesc = aocs_delete_init(relation);
                MemoryContextSwitchTo(oldcxt);
        }
@@ -508,7 +507,7 @@ get_or_create_unique_check_desc(Relation relation, Snapshot 
snapshot)
                MemoryContext oldcxt;
                AOCSUniqueCheckDesc uniqueCheckDesc;
 
-               oldcxt = MemoryContextSwitchTo(aocoLocal.stateCxt);
+               oldcxt = MemoryContextSwitchTo(aocoDMLStates.stateCxt);
                uniqueCheckDesc = palloc0(sizeof(AOCSUniqueCheckDescData));
 
                /* Initialize the block directory */
diff --git a/src/backend/access/appendonly/appendonlyam_handler.c 
b/src/backend/access/appendonly/appendonlyam_handler.c
index f1b07d2ada..2151ade951 100644
--- a/src/backend/access/appendonly/appendonlyam_handler.c
+++ b/src/backend/access/appendonly/appendonlyam_handler.c
@@ -92,23 +92,16 @@ typedef struct AppendOnlyDMLState
  *             a memory context that should be long lived enough and is
  *                     responsible for reseting the state via its reset cb
  */
-static struct AppendOnlyLocal
+typedef struct AppendOnlyDMLStates
 {
        AppendOnlyDMLState         *last_used_state;
-       HTAB                               *dmlDescriptorTab;
+       HTAB                               *state_table;
 
        MemoryContext                   stateCxt;
        MemoryContextCallback   cb;
-} appendOnlyLocal        = {
-       .last_used_state  = NULL,
-       .dmlDescriptorTab = NULL,
-
-       .stateCxt                 = NULL,
-       .cb                               = {
-               .func   = reset_state_cb,
-               .arg    = NULL
-       },
-};
+} AppendOnlyDMLStates;
+
+static AppendOnlyDMLStates appendOnlyDMLStates;
 
 /* ------------------------------------------------------------------------
  * DML state related functions
@@ -116,60 +109,58 @@ static struct AppendOnlyLocal
  */
 
 /*
+ * Initialize the backend local AppendOnlyDMLStates object for this backend for
+ * the current DML or DML-like command (if not already initialized).
+ *
  * This function should be called with a current memory context whose life
  * span is enough to last until the end of this command execution.
  */
 static void
-init_dml_local_state(void)
+init_appendonly_dml_states()
 {
        HASHCTL hash_ctl;
 
-       if (!appendOnlyLocal.dmlDescriptorTab)
+       if (!appendOnlyDMLStates.state_table)
        {
-               Assert(appendOnlyLocal.stateCxt == NULL);
-               appendOnlyLocal.stateCxt = AllocSetContextCreate(
+               Assert(appendOnlyDMLStates.stateCxt == NULL);
+               appendOnlyDMLStates.stateCxt = AllocSetContextCreate(
                                                                                
                CurrentMemoryContext,
                                                                                
                "AppendOnly DML State Context",
                                                                                
                ALLOCSET_SMALL_SIZES);
-               MemoryContextRegisterResetCallback(
-                                                               
appendOnlyLocal.stateCxt,
-                                                          &appendOnlyLocal.cb);
+
+               appendOnlyDMLStates.cb.func = reset_state_cb;
+               appendOnlyDMLStates.cb.arg = NULL;
+               MemoryContextRegisterResetCallback(appendOnlyDMLStates.stateCxt,
+                                                                               
   &appendOnlyDMLStates.cb);
 
                memset(&hash_ctl, 0, sizeof(hash_ctl));
                hash_ctl.keysize = sizeof(Oid);
                hash_ctl.entrysize = sizeof(AppendOnlyDMLState);
-               hash_ctl.hcxt = appendOnlyLocal.stateCxt;
-               appendOnlyLocal.dmlDescriptorTab =
+               hash_ctl.hcxt = appendOnlyDMLStates.stateCxt;
+               appendOnlyDMLStates.state_table =
                        hash_create("AppendOnly DML state", 128, &hash_ctl,
                                                HASH_CONTEXT | HASH_ELEM | 
HASH_BLOBS);
        }
 }
 
-
-/*
- * There are disctinct *_dm_state functions in order to document a bit better
- * the intention behind each one of those and keep them as thin as possible.
- */
-
 /*
  * Create and insert a state entry for a relation. The actual descriptors will
  * be created lazily when/if needed.
  *
  * Should be called exactly once per relation.
  */
-static inline AppendOnlyDMLState *
-enter_dml_state(const Oid relationOid)
+static inline void
+init_dml_state(const Oid relationOid)
 {
        AppendOnlyDMLState *state;
        bool                            found;
 
-       Assert(appendOnlyLocal.dmlDescriptorTab);
+       Assert(appendOnlyDMLStates.state_table);
 
-       state = (AppendOnlyDMLState *) hash_search(
-                                                                               
appendOnlyLocal.dmlDescriptorTab,
-                                                                          
&relationOid,
-                                                                               
HASH_ENTER,
-                                                                          
&found);
+       state = (AppendOnlyDMLState *) 
hash_search(appendOnlyDMLStates.state_table,
+                                                                               
           &relationOid,
+                                                                               
           HASH_ENTER,
+                                                                               
           &found);
 
        state->insertDesc = NULL;
        state->deleteDesc = NULL;
@@ -180,8 +171,7 @@ enter_dml_state(const Oid relationOid)
 
        Assert(!found);
 
-       appendOnlyLocal.last_used_state = state;
-       return state;
+       appendOnlyDMLStates.last_used_state = state;
 }
 
 /*
@@ -192,21 +182,20 @@ static inline AppendOnlyDMLState *
 find_dml_state(const Oid relationOid)
 {
        AppendOnlyDMLState *state;
-       Assert(appendOnlyLocal.dmlDescriptorTab);
+       Assert(appendOnlyDMLStates.state_table);
 
-       if (appendOnlyLocal.last_used_state &&
-                       appendOnlyLocal.last_used_state->relationOid == 
relationOid)
-               return appendOnlyLocal.last_used_state;
+       if (appendOnlyDMLStates.last_used_state &&
+                       appendOnlyDMLStates.last_used_state->relationOid == 
relationOid)
+               return appendOnlyDMLStates.last_used_state;
 
-       state = (AppendOnlyDMLState *) hash_search(
-                                                                               
appendOnlyLocal.dmlDescriptorTab,
-                                                                          
&relationOid,
-                                                                               
HASH_FIND,
-                                                                               
NULL);
+       state = (AppendOnlyDMLState *) 
hash_search(appendOnlyDMLStates.state_table,
+                                                                               
           &relationOid,
+                                                                               
           HASH_FIND,
+                                                                               
           NULL);
 
        Assert(state);
 
-       appendOnlyLocal.last_used_state = state;
+       appendOnlyDMLStates.last_used_state = state;
        return state;
 }
 
@@ -220,20 +209,19 @@ static inline AppendOnlyDMLState *
 remove_dml_state(const Oid relationOid)
 {
        AppendOnlyDMLState *state;
-       Assert(appendOnlyLocal.dmlDescriptorTab);
+       Assert(appendOnlyDMLStates.state_table);
 
-       state = (AppendOnlyDMLState *) hash_search(
-                                                                               
appendOnlyLocal.dmlDescriptorTab,
-                                                                          
&relationOid,
-                                                                               
HASH_REMOVE,
-                                                                               
NULL);
+       state = (AppendOnlyDMLState *) 
hash_search(appendOnlyDMLStates.state_table,
+                                                                               
           &relationOid,
+                                                                               
           HASH_REMOVE,
+                                                                               
           NULL);
 
        if (!state)
                return NULL;
 
-       if (appendOnlyLocal.last_used_state &&
-                       appendOnlyLocal.last_used_state->relationOid == 
relationOid)
-               appendOnlyLocal.last_used_state = NULL;
+       if (appendOnlyDMLStates.last_used_state &&
+                       appendOnlyDMLStates.last_used_state->relationOid == 
relationOid)
+               appendOnlyDMLStates.last_used_state = NULL;
 
        return state;
 }
@@ -247,8 +235,8 @@ remove_dml_state(const Oid relationOid)
 void
 appendonly_dml_init(Relation relation, CmdType operation)
 {
-       init_dml_local_state();
-       (void) enter_dml_state(RelationGetRelid(relation));
+       init_appendonly_dml_states();
+       init_dml_state(RelationGetRelid(relation));
 }
 
 /*
@@ -340,9 +328,9 @@ appendonly_dml_finish(Relation relation, CmdType operation)
 static void
 reset_state_cb(void *arg)
 {
-       appendOnlyLocal.dmlDescriptorTab = NULL;
-       appendOnlyLocal.last_used_state = NULL;
-       appendOnlyLocal.stateCxt = NULL;
+       appendOnlyDMLStates.state_table = NULL;
+       appendOnlyDMLStates.last_used_state = NULL;
+       appendOnlyDMLStates.stateCxt = NULL;
 }
 
 /*
@@ -356,7 +344,7 @@ get_insert_descriptor(const Relation relation)
        MemoryContext oldcxt;
 
        state = find_dml_state(RelationGetRelid(relation));
-       oldcxt = MemoryContextSwitchTo(appendOnlyLocal.stateCxt);
+       oldcxt = MemoryContextSwitchTo(appendOnlyDMLStates.stateCxt);
 
        if (state->insertDesc == NULL)
        {
@@ -454,7 +442,7 @@ get_delete_descriptor(const Relation relation, bool 
forUpdate)
                                                 errmsg("deletes on append-only 
tables are not supported in serializable transactions")));
                }
 
-               oldcxt = MemoryContextSwitchTo(appendOnlyLocal.stateCxt);
+               oldcxt = MemoryContextSwitchTo(appendOnlyDMLStates.stateCxt);
                state->deleteDesc = appendonly_delete_init(relation);
                MemoryContextSwitchTo(oldcxt);
        }
@@ -472,7 +460,7 @@ get_or_create_unique_check_desc(Relation relation, Snapshot 
snapshot)
                MemoryContext oldcxt;
                AppendOnlyUniqueCheckDesc uniqueCheckDesc;
 
-               oldcxt = MemoryContextSwitchTo(appendOnlyLocal.stateCxt);
+               oldcxt = MemoryContextSwitchTo(appendOnlyDMLStates.stateCxt);
                uniqueCheckDesc = 
palloc0(sizeof(AppendOnlyUniqueCheckDescData));
 
                /* Initialize the block directory */


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to