@@ -83,6 +83,7 @@ static int _numOfTransVars(Package *package);
8383static void makePackHTAB (Package * package , bool is_trans );
8484static inline ChangedObject * makeChangedObject (TransObject * object ,
8585 MemoryContext ctx );
86+ static void initObjectHistory (TransObject * object , TransObjectType type );
8687
8788/* Hook functions */
8889static void variable_ExecutorEnd (QueryDesc * queryDesc );
@@ -1334,6 +1335,37 @@ makePackHTAB(Package *package, bool is_trans)
13341335 HASH_ELEM | HASH_CONTEXT );
13351336}
13361337
1338+ static void
1339+ initObjectHistory (TransObject * object , TransObjectType type )
1340+ {
1341+ /* Initialize history */
1342+ TransState * state ;
1343+ int size ;
1344+
1345+ size = (type == TRANS_PACKAGE ? sizeof (PackState ) : sizeof (VarState ));
1346+ dlist_init (& object -> states );
1347+ state = MemoryContextAllocZero (ModuleContext , size );
1348+ dlist_push_head (& object -> states , & (state -> node ));
1349+
1350+ /* Initialize state */
1351+ state -> is_valid = true;
1352+ if (type == TRANS_PACKAGE )
1353+ ((PackState * )state )-> trans_var_num = 0 ;
1354+ else
1355+ {
1356+ Variable * variable = (Variable * ) object ;
1357+ if (!variable -> is_record )
1358+ {
1359+ VarState * varState = (VarState * ) state ;
1360+ ScalarVar * scalar = & (varState -> value .scalar );
1361+
1362+ get_typlenbyval (variable -> typid , & scalar -> typlen ,
1363+ & scalar -> typbyval );
1364+ varState -> value .scalar .is_null = true;
1365+ }
1366+ }
1367+ }
1368+
13371369static Package *
13381370getPackage (text * name , bool strict )
13391371{
@@ -1410,18 +1442,12 @@ createPackage(text *name, bool is_trans)
14101442 }
14111443 else
14121444 {
1413- PackState * packState ;
1414-
1445+ /* Package entry was created, so initialize it. */
14151446 package -> varHashRegular = NULL ;
14161447 package -> varHashTransact = NULL ;
14171448 package -> hctxRegular = NULL ;
14181449 package -> hctxTransact = NULL ;
1419- /* Initialize history */
1420- dlist_init (GetStateStorage (package ));
1421- packState = MemoryContextAllocZero (ModuleContext , sizeof (PackState ));
1422- dlist_push_head (GetStateStorage (package ), & (packState -> state .node ));
1423- packState -> state .is_valid = true;
1424- packState -> trans_var_num = 0 ;
1450+ initObjectHistory (& package -> transObject , TRANS_PACKAGE );
14251451 /* Add to changes list */
14261452 if (!isObjectChangedInCurrentTrans (& package -> transObject ))
14271453 addToChangesStack (& package -> transObject , TRANS_PACKAGE );
@@ -1566,27 +1592,12 @@ createVariableInternal(Package *package, text *name, Oid typid, bool is_record,
15661592 }
15671593 else
15681594 {
1569- VarState * varState ;
1570-
15711595 /* Variable entry was created, so initialize new variable. */
15721596 variable -> typid = typid ;
15731597 variable -> package = package ;
15741598 variable -> is_record = is_record ;
15751599 variable -> is_transactional = is_transactional ;
1576-
1577- dlist_init (GetStateStorage (variable ));
1578- varState = MemoryContextAllocZero (pack_hctx (package , is_transactional ),
1579- sizeof (VarState ));
1580-
1581- dlist_push_head (GetStateStorage (variable ), & varState -> state .node );
1582- if (!variable -> is_record )
1583- {
1584- ScalarVar * scalar = & (varState -> value .scalar );
1585-
1586- get_typlenbyval (variable -> typid , & scalar -> typlen ,
1587- & scalar -> typbyval );
1588- varState -> value .scalar .is_null = true;
1589- }
1600+ initObjectHistory (transObject , TRANS_VARIABLE );
15901601
15911602 if (!isObjectChangedInCurrentTrans (& package -> transObject ))
15921603 {
@@ -1771,18 +1782,10 @@ rollbackSavepoint(TransObject *object, TransObjectType type)
17711782 {
17721783 if (type == TRANS_PACKAGE && numOfRegVars ((Package * )object ))
17731784 {
1774- PackState * packState ;
1775-
1776- packState = MemoryContextAllocZero (ModuleContext , sizeof (PackState ));
1777- dlist_push_head (& object -> states , & (packState -> state .node ));
1778- packState -> state .is_valid = true;
1779- packState -> state .level = GetCurrentTransactionNestLevel () - 1 ;
1780- packState -> trans_var_num = 0 ;
1781-
1785+ initObjectHistory (object , type );
1786+ GetActualState (object )-> level = GetCurrentTransactionNestLevel () - 1 ;
17821787 if (!dlist_is_empty (changesStack ))
1783- {
17841788 addToChangesStackUpperLevel (object , type );
1785- }
17861789 }
17871790 else
17881791 removeObject (object , type );
0 commit comments