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