@@ -113,7 +113,10 @@ static MemoryContext changesStackContext = NULL;
113
113
AssertMacro(changesStack != NULL), \
114
114
(dlist_head_element(ChangesStackNode, node, changesStack)) \
115
115
)
116
-
116
+ #define pack_hctx (pack , is_trans ) \
117
+ (is_trans ? pack->hctxTransact : pack->hctxRegular)
118
+ #define pack_htab (pack , is_trans ) \
119
+ (is_trans ? pack->varHashTransact : pack->varHashRegular)
117
120
118
121
#define PGV_MCXT_MAIN "pg_variables: main memory context"
119
122
#define PGV_MCXT_VARS "pg_variables: variables hash"
@@ -157,9 +160,7 @@ variable_set(text *package_name, text *var_name,
157
160
scalar -> is_null = is_null ;
158
161
if (!scalar -> is_null )
159
162
{
160
- oldcxt = MemoryContextSwitchTo (is_transactional ?
161
- package -> hctxTransact :
162
- package -> hctxRegular );
163
+ oldcxt = MemoryContextSwitchTo (pack_hctx (package , is_transactional ));
163
164
scalar -> value = datumCopy (value , scalar -> typbyval , scalar -> typlen );
164
165
MemoryContextSwitchTo (oldcxt );
165
166
}
@@ -357,8 +358,7 @@ variable_insert(PG_FUNCTION_ARGS)
357
358
/*
358
359
* This is the first record for the var_name. Initialize attributes.
359
360
*/
360
- init_attributes (variable , tupdesc , is_transactional ?
361
- package -> hctxTransact : package -> hctxRegular );
361
+ init_attributes (variable , tupdesc , pack_hctx (package , is_transactional ));
362
362
}
363
363
else
364
364
check_attributes (variable , tupdesc );
@@ -1025,12 +1025,13 @@ get_packages_and_variables(PG_FUNCTION_ARGS)
1025
1025
{
1026
1026
HashVariableEntry * variable ;
1027
1027
HASH_SEQ_STATUS vstat ;
1028
+ int i ;
1028
1029
1029
1030
/* Skip packages marked as deleted */
1030
1031
if (!get_actual_pack_state (package )-> is_valid )
1031
1032
continue ;
1032
1033
/* Get variables list for package */
1033
- for ( int i = 0 ; i < 2 ; i ++ )
1034
+ for ( i = 0 ; i < 2 ; i ++ )
1034
1035
{
1035
1036
hash_seq_init (& vstat , i ? package -> varHashTransact :
1036
1037
package -> varHashRegular );
@@ -1268,7 +1269,7 @@ makePackHTAB(HashPackageEntry *package, bool is_trans)
1268
1269
package -> hctxRegular = AllocSetContextCreate (ModuleContext ,
1269
1270
PGV_MCXT_VARS ,
1270
1271
ALLOCSET_DEFAULT_SIZES );
1271
- sprintf (hash_name , "%s variables hash for package \"%s\"" ,
1272
+ snprintf (hash_name , BUFSIZ , "%s variables hash for package \"%s\"" ,
1272
1273
is_trans ? "Transactional" : "Regular" , key );
1273
1274
ctl .keysize = NAMEDATALEN ;
1274
1275
ctl .entrysize = sizeof (HashVariableEntry );
@@ -1447,9 +1448,8 @@ createVariableInternal(HashPackageEntry *package, text *name, Oid typid,
1447
1448
errmsg ("variable \"%s\" already created as %sTRANSACTIONAL" ,
1448
1449
key , is_transactional ? "NOT " : "" )));
1449
1450
1450
- variable = (HashVariableEntry * ) hash_search (is_transactional ?
1451
- package -> varHashTransact :
1452
- package -> varHashRegular ,
1451
+ variable = (HashVariableEntry * ) hash_search (
1452
+ pack_htab (package , is_transactional ),
1453
1453
key , HASH_ENTER , & found );
1454
1454
1455
1455
/* Check variable type */
@@ -1486,9 +1486,7 @@ createVariableInternal(HashPackageEntry *package, text *name, Oid typid,
1486
1486
variable -> typid = typid ;
1487
1487
variable -> is_transactional = is_transactional ;
1488
1488
dlist_init (& variable -> data );
1489
- historyEntry = MemoryContextAllocZero (is_transactional ?
1490
- package -> hctxTransact :
1491
- package -> hctxRegular ,
1489
+ historyEntry = MemoryContextAllocZero (pack_hctx (package , is_transactional ),
1492
1490
sizeof (ValueHistoryEntry ));
1493
1491
1494
1492
dlist_push_head (& variable -> data , & historyEntry -> node );
@@ -1816,15 +1814,15 @@ pushChangesStack(void)
1816
1814
changesStackContext = AllocSetContextCreate (ModuleContext ,
1817
1815
PGV_MCXT_STACK ,
1818
1816
ALLOCSET_START_SMALL_SIZES );
1819
-
1817
+ Assert ( changesStackContext );
1820
1818
oldcxt = MemoryContextSwitchTo (changesStackContext );
1821
1819
1822
1820
if (!changesStack )
1823
1821
{
1824
1822
changesStack = palloc0 (sizeof (dlist_head ));
1825
1823
dlist_init (changesStack );
1826
1824
}
1827
-
1825
+ Assert ( changesStack );
1828
1826
csn = palloc0 (sizeof (ChangesStackNode ));
1829
1827
csn -> changedVarsList = palloc0 (sizeof (dlist_head ));
1830
1828
csn -> changedPacksList = palloc0 (sizeof (dlist_head ));
@@ -1841,13 +1839,11 @@ pushChangesStack(void)
1841
1839
}
1842
1840
1843
1841
/*
1844
- * Add a package to list of created or removed packs in current transaction level
1842
+ * Create a changesStack with the required depth.
1845
1843
*/
1846
1844
static void
1847
- addToChangedPacks ( HashPackageEntry * package )
1845
+ prepareChangesStack ( void )
1848
1846
{
1849
- ChangesStackNode * csn ;
1850
-
1851
1847
if (!changesStack )
1852
1848
{
1853
1849
int level = GetCurrentTransactionNestLevel ();
@@ -1857,11 +1853,18 @@ addToChangedPacks(HashPackageEntry *package)
1857
1853
pushChangesStack ();
1858
1854
}
1859
1855
}
1856
+ }
1860
1857
1861
- Assert (changesStack && changesStackContext );
1862
-
1858
+ /*
1859
+ * Add a package to list of created or removed packs in current transaction level
1860
+ */
1861
+ static void
1862
+ addToChangedPacks (HashPackageEntry * package )
1863
+ {
1864
+ prepareChangesStack ();
1863
1865
if (!isPackChangedInCurrentTrans (package ))
1864
1866
{
1867
+ ChangesStackNode * csn ;
1865
1868
ChangedPacksNode * cpn ;
1866
1869
1867
1870
csn = get_actual_changes_list ();
@@ -1879,22 +1882,10 @@ addToChangedPacks(HashPackageEntry *package)
1879
1882
static void
1880
1883
addToChangedVars (HashPackageEntry * package , HashVariableEntry * variable )
1881
1884
{
1882
- ChangesStackNode * csn ;
1883
-
1884
- if (!changesStack )
1885
- {
1886
- int level = GetCurrentTransactionNestLevel ();
1887
-
1888
- while (level -- > 0 )
1889
- {
1890
- pushChangesStack ();
1891
- }
1892
- }
1893
-
1894
- Assert (changesStack && changesStackContext );
1895
-
1885
+ prepareChangesStack ();
1896
1886
if (!isVarChangedInCurrentTrans (variable ))
1897
1887
{
1888
+ ChangesStackNode * csn ;
1898
1889
ChangedVarsNode * cvn ;
1899
1890
1900
1891
csn = get_actual_changes_list ();
@@ -1955,7 +1946,7 @@ typedef enum Action
1955
1946
* apply corresponding action on them
1956
1947
*/
1957
1948
static void
1958
- proceedChanges (Action action )
1949
+ processChanges (Action action )
1959
1950
{
1960
1951
1961
1952
ChangesStackNode * bottom_list ;
@@ -2093,10 +2084,10 @@ pgvSubTransCallback(SubXactEvent event, SubTransactionId mySubid,
2093
2084
pushChangesStack ();
2094
2085
break ;
2095
2086
case SUBXACT_EVENT_COMMIT_SUB :
2096
- proceedChanges (RELEASE_SAVEPOINT );
2087
+ processChanges (RELEASE_SAVEPOINT );
2097
2088
break ;
2098
2089
case SUBXACT_EVENT_ABORT_SUB :
2099
- proceedChanges (ROLLBACK_TO_SAVEPOINT );
2090
+ processChanges (ROLLBACK_TO_SAVEPOINT );
2100
2091
break ;
2101
2092
case SUBXACT_EVENT_PRE_COMMIT_SUB :
2102
2093
break ;
@@ -2115,16 +2106,16 @@ pgvTransCallback(XactEvent event, void *arg)
2115
2106
switch (event )
2116
2107
{
2117
2108
case XACT_EVENT_PRE_COMMIT :
2118
- proceedChanges (RELEASE_SAVEPOINT );
2109
+ processChanges (RELEASE_SAVEPOINT );
2119
2110
break ;
2120
2111
case XACT_EVENT_ABORT :
2121
- proceedChanges (ROLLBACK_TO_SAVEPOINT );
2112
+ processChanges (ROLLBACK_TO_SAVEPOINT );
2122
2113
break ;
2123
2114
case XACT_EVENT_PARALLEL_PRE_COMMIT :
2124
- proceedChanges (RELEASE_SAVEPOINT );
2115
+ processChanges (RELEASE_SAVEPOINT );
2125
2116
break ;
2126
2117
case XACT_EVENT_PARALLEL_ABORT :
2127
- proceedChanges (ROLLBACK_TO_SAVEPOINT );
2118
+ processChanges (ROLLBACK_TO_SAVEPOINT );
2128
2119
break ;
2129
2120
default :
2130
2121
break ;
0 commit comments