43
43
#define CHILD_FACTOR 500
44
44
45
45
46
+ /* Various memory contexts for caches */
47
+ MemoryContext TopPathmanContext = NULL ;
48
+ MemoryContext PathmanRelationCacheContext = NULL ;
49
+ MemoryContext PathmanParentCacheContext = NULL ;
50
+ MemoryContext PathmanCostraintCacheContext = NULL ;
51
+
46
52
/* Storage for PartRelationInfos */
47
- HTAB * partitioned_rels = NULL ;
53
+ HTAB * partitioned_rels = NULL ;
48
54
49
55
/* Storage for PartParentInfos */
50
- HTAB * parent_cache = NULL ;
56
+ HTAB * parent_cache = NULL ;
51
57
52
58
/* Storage for partition constraints */
53
- HTAB * constraint_cache = NULL ;
59
+ HTAB * constraint_cache = NULL ;
54
60
55
61
/* pg_pathman's init status */
56
62
PathmanInitState pg_pathman_init_state ;
@@ -210,7 +216,7 @@ load_config(void)
210
216
/* Validate pg_pathman's Pl/PgSQL facade (might be outdated) */
211
217
validate_sql_facade_version (get_sql_facade_version ());
212
218
213
- init_local_cache (); /* create 'partitioned_rels' hash table */
219
+ init_local_cache (); /* create various hash tables (caches) */
214
220
read_pathman_config (); /* read PATHMAN_CONFIG table & fill cache */
215
221
216
222
/* Register pathman_relcache_hook(), currently we can't unregister it */
@@ -307,18 +313,60 @@ init_local_cache(void)
307
313
hash_destroy (parent_cache );
308
314
hash_destroy (constraint_cache );
309
315
316
+ /* Reset pg_pathman's memory contexts */
317
+ if (TopPathmanContext )
318
+ {
319
+ /* Check that child contexts exist */
320
+ Assert (MemoryContextIsValid (PathmanRelationCacheContext ));
321
+ Assert (MemoryContextIsValid (PathmanParentCacheContext ));
322
+ Assert (MemoryContextIsValid (PathmanCostraintCacheContext ));
323
+
324
+ /* Clear children */
325
+ MemoryContextResetChildren (TopPathmanContext );
326
+ }
327
+ /* Initialize pg_pathman's memory contexts */
328
+ else
329
+ {
330
+ Assert (PathmanRelationCacheContext == NULL );
331
+ Assert (PathmanParentCacheContext == NULL );
332
+ Assert (PathmanCostraintCacheContext == NULL );
333
+
334
+ TopPathmanContext =
335
+ AllocSetContextCreate (TopMemoryContext ,
336
+ CppAsString (TopPathmanContext ),
337
+ ALLOCSET_DEFAULT_SIZES );
338
+
339
+ /* For PartRelationInfo */
340
+ PathmanRelationCacheContext =
341
+ AllocSetContextCreate (TopPathmanContext ,
342
+ CppAsString (PathmanRelationCacheContext ),
343
+ ALLOCSET_DEFAULT_SIZES );
344
+
345
+ /* For PartParentInfo */
346
+ PathmanParentCacheContext =
347
+ AllocSetContextCreate (TopPathmanContext ,
348
+ CppAsString (PathmanParentCacheContext ),
349
+ ALLOCSET_DEFAULT_SIZES );
350
+
351
+ /* For PartConstraintInfo */
352
+ PathmanCostraintCacheContext =
353
+ AllocSetContextCreate (TopPathmanContext ,
354
+ CppAsString (PathmanCostraintCacheContext ),
355
+ ALLOCSET_DEFAULT_SIZES );
356
+ }
357
+
310
358
memset (& ctl , 0 , sizeof (ctl ));
311
359
ctl .keysize = sizeof (Oid );
312
360
ctl .entrysize = sizeof (PartRelationInfo );
313
- ctl .hcxt = TopMemoryContext ; /* place data to persistent mcxt */
361
+ ctl .hcxt = PathmanRelationCacheContext ;
314
362
315
363
partitioned_rels = hash_create ("pg_pathman's partitioned relations cache" ,
316
364
PART_RELS_SIZE , & ctl , HASH_ELEM | HASH_BLOBS );
317
365
318
366
memset (& ctl , 0 , sizeof (ctl ));
319
367
ctl .keysize = sizeof (Oid );
320
368
ctl .entrysize = sizeof (PartParentInfo );
321
- ctl .hcxt = TopMemoryContext ; /* place data to persistent mcxt */
369
+ ctl .hcxt = PathmanParentCacheContext ;
322
370
323
371
parent_cache = hash_create ("pg_pathman's partition parents cache" ,
324
372
PART_RELS_SIZE * CHILD_FACTOR ,
@@ -327,7 +375,7 @@ init_local_cache(void)
327
375
memset (& ctl , 0 , sizeof (ctl ));
328
376
ctl .keysize = sizeof (Oid );
329
377
ctl .entrysize = sizeof (PartConstraintInfo );
330
- ctl .hcxt = TopMemoryContext ; /* place data to persistent mcxt */
378
+ ctl .hcxt = PathmanCostraintCacheContext ;
331
379
332
380
constraint_cache = hash_create ("pg_pathman's partition constraints cache" ,
333
381
PART_RELS_SIZE * CHILD_FACTOR ,
@@ -340,24 +388,17 @@ init_local_cache(void)
340
388
static void
341
389
fini_local_cache (void )
342
390
{
343
- HASH_SEQ_STATUS status ;
344
- PartRelationInfo * prel ;
345
-
346
- hash_seq_init (& status , partitioned_rels );
347
- while ((prel = (PartRelationInfo * ) hash_seq_search (& status )) != NULL )
348
- {
349
- if (PrelIsValid (prel ))
350
- {
351
- FreeChildrenArray (prel );
352
- FreeRangesArray (prel );
353
- }
354
- }
355
-
356
- /* Now we can safely destroy hash tables */
391
+ /* First, destroy hash tables */
357
392
hash_destroy (partitioned_rels );
358
393
hash_destroy (parent_cache );
359
- partitioned_rels = NULL ;
360
- parent_cache = NULL ;
394
+ hash_destroy (constraint_cache );
395
+
396
+ partitioned_rels = NULL ;
397
+ parent_cache = NULL ;
398
+ constraint_cache = NULL ;
399
+
400
+ /* Now we can clear allocations */
401
+ MemoryContextResetChildren (TopPathmanContext );
361
402
}
362
403
363
404
/*
@@ -371,7 +412,7 @@ fill_prel_with_partitions(const Oid *partitions,
371
412
{
372
413
uint32 i ;
373
414
Expr * con_expr ;
374
- MemoryContext mcxt = TopMemoryContext ;
415
+ MemoryContext mcxt = PathmanRelationCacheContext ;
375
416
376
417
/* Allocate memory for 'prel->children' & 'prel->ranges' (if needed) */
377
418
prel -> children = MemoryContextAllocZero (mcxt , parts_count * sizeof (Oid ));
@@ -474,7 +515,7 @@ fill_prel_with_partitions(const Oid *partitions,
474
515
prel -> children [i ] = prel -> ranges [i ].child_oid ;
475
516
476
517
/* Copy all min & max Datums to the persistent mcxt */
477
- old_mcxt = MemoryContextSwitchTo (TopMemoryContext );
518
+ old_mcxt = MemoryContextSwitchTo (PathmanRelationCacheContext );
478
519
for (i = 0 ; i < PrelChildrenCount (prel ); i ++ )
479
520
{
480
521
prel -> ranges [i ].min = CopyBound (& prel -> ranges [i ].min ,
0 commit comments