1212#include "utils/typcache.h"
1313#include "utils/array.h"
1414#include "utils/snapmgr.h"
15+ #include "utils/memutils.h"
1516#include "access/nbtree.h"
1617#include "access/xact.h"
1718#include "catalog/pg_type.h"
1819#include "executor/spi.h"
1920#include "storage/lmgr.h"
2021
22+ static void on_rollback_after_partitions_created (XactEvent event , void * arg );
2123
2224/* declarations */
2325PG_FUNCTION_INFO_V1 ( on_partitions_created );
@@ -34,18 +36,50 @@ PG_FUNCTION_INFO_V1( get_max_range_value );
3436PG_FUNCTION_INFO_V1 ( get_type_hash_func );
3537PG_FUNCTION_INFO_V1 ( get_hash );
3638
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+
3758/*
3859 * Callbacks
3960 */
4061Datum
4162on_partitions_created (PG_FUNCTION_ARGS )
4263{
64+ MemoryContext old_ctx ;
65+ Oid * relid ;
66+
67+ /* Acquire lock */
4368 LWLockAcquire (pmstate -> load_config_lock , LW_EXCLUSIVE );
4469
4570 /* Reload config */
4671 /* TODO: reload just the specified relation */
4772 load_relations (false);
4873
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 */
4983 LWLockRelease (pmstate -> load_config_lock );
5084
5185 PG_RETURN_NULL ();
0 commit comments