12
12
#include "utils/typcache.h"
13
13
#include "utils/array.h"
14
14
#include "utils/snapmgr.h"
15
+ #include "utils/memutils.h"
15
16
#include "access/nbtree.h"
16
17
#include "access/xact.h"
17
18
#include "catalog/pg_type.h"
18
19
#include "executor/spi.h"
19
20
#include "storage/lmgr.h"
20
21
22
+ static void on_rollback_after_partitions_created (XactEvent event , void * arg );
21
23
22
24
/* declarations */
23
25
PG_FUNCTION_INFO_V1 ( on_partitions_created );
@@ -34,18 +36,50 @@ PG_FUNCTION_INFO_V1( get_max_range_value );
34
36
PG_FUNCTION_INFO_V1 ( get_type_hash_func );
35
37
PG_FUNCTION_INFO_V1 ( get_hash );
36
38
39
+ static void
40
+ on_rollback_after_partitions_created (XactEvent event , void * arg )
41
+ {
42
+ Oid relid = * (Oid * ) arg ;
43
+
44
+ /* Catch abort event */
45
+ if (event == XACT_EVENT_ABORT )
46
+ {
47
+ /* Clear cache */
48
+ LWLockAcquire (pmstate -> load_config_lock , LW_EXCLUSIVE );
49
+ elog (WARNING , "Removing '%s' partitions from pg_pathman's cache" , get_rel_name (relid ));
50
+ remove_relation_info (relid );
51
+ LWLockRelease (pmstate -> load_config_lock );
52
+ }
53
+
54
+ UnregisterXactCallback (on_rollback_after_partitions_created , arg );
55
+ pfree (arg );
56
+ }
57
+
37
58
/*
38
59
* Callbacks
39
60
*/
40
61
Datum
41
62
on_partitions_created (PG_FUNCTION_ARGS )
42
63
{
64
+ MemoryContext old_ctx ;
65
+ Oid * relid ;
66
+
67
+ /* Acquire lock */
43
68
LWLockAcquire (pmstate -> load_config_lock , LW_EXCLUSIVE );
44
69
45
70
/* Reload config */
46
71
/* TODO: reload just the specified relation */
47
72
load_relations (false);
48
73
74
+ /* Register callback to handle transaction abortion */
75
+ old_ctx = CurrentMemoryContext ;
76
+ MemoryContextSwitchTo (CurTransactionContext );
77
+ relid = palloc0 (sizeof (Oid ));
78
+ * relid = PG_GETARG_OID (0 );
79
+ RegisterXactCallback (on_rollback_after_partitions_created , relid );
80
+ MemoryContextSwitchTo (old_ctx );
81
+
82
+ /* Release lock */
49
83
LWLockRelease (pmstate -> load_config_lock );
50
84
51
85
PG_RETURN_NULL ();
0 commit comments