Skip to content

Commit 173dd50

Browse files
committed
disable some of the concurrent data modification operations for partitioned tables
1 parent 027fe76 commit 173dd50

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

hash.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ BEGIN
2929
/* Acquire exclusive lock on parent */
3030
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
3131

32+
IF partition_data = true THEN
33+
/* Acquire data modification lock */
34+
PERFORM @extschema@.lock_relation_modification(parent_relid);
35+
END IF;
36+
3237
PERFORM @extschema@.validate_relname(parent_relid);
3338
attribute := lower(attribute);
3439
PERFORM @extschema@.common_relation_checks(parent_relid, attribute);

init.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,14 @@ LANGUAGE C STRICT;
656656
LANGUAGE C STRICT;
657657

658658

659+
/*
660+
* Lock relation to restrict concurrent modification of data.
661+
*/
662+
CREATE OR REPLACE FUNCTION @extschema@.lock_relation_modification(
663+
REGCLASS)
664+
RETURNS VOID AS 'pg_pathman', 'lock_relation_modification'
665+
LANGUAGE C STRICT;
666+
659667
/*
660668
* DEBUG: Place this inside some plpgsql fuction and set breakpoint.
661669
*/

range.sql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ BEGIN
9797
/* Acquire exclusive lock on parent */
9898
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
9999

100+
IF partition_data = true THEN
101+
/* Acquire data modification lock */
102+
PERFORM @extschema@.lock_relation_modification(parent_relid);
103+
END IF;
104+
100105
PERFORM @extschema@.validate_relname(parent_relid);
101106
p_attribute := lower(p_attribute);
102107
PERFORM @extschema@.common_relation_checks(parent_relid, p_attribute);
@@ -194,6 +199,11 @@ BEGIN
194199
/* Acquire exclusive lock on parent */
195200
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
196201

202+
IF partition_data = true THEN
203+
/* Acquire data modification lock */
204+
PERFORM @extschema@.lock_relation_modification(parent_relid);
205+
END IF;
206+
197207
PERFORM @extschema@.validate_relname(parent_relid);
198208
p_attribute := lower(p_attribute);
199209
PERFORM @extschema@.common_relation_checks(parent_relid, p_attribute);
@@ -289,6 +299,11 @@ BEGIN
289299
/* Acquire exclusive lock on parent */
290300
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
291301

302+
IF partition_data = true THEN
303+
/* Acquire data modification lock */
304+
PERFORM @extschema@.lock_relation_modification(parent_relid);
305+
END IF;
306+
292307
PERFORM @extschema@.validate_relname(parent_relid);
293308
p_attribute := lower(p_attribute);
294309
PERFORM @extschema@.common_relation_checks(parent_relid, p_attribute);
@@ -357,6 +372,11 @@ BEGIN
357372
/* Acquire exclusive lock on parent */
358373
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
359374

375+
IF partition_data = true THEN
376+
/* Acquire data modification lock */
377+
PERFORM @extschema@.lock_relation_modification(parent_relid);
378+
END IF;
379+
360380
PERFORM @extschema@.validate_relname(parent_relid);
361381
p_attribute := lower(p_attribute);
362382
PERFORM @extschema@.common_relation_checks(parent_relid, p_attribute);
@@ -501,6 +521,9 @@ BEGIN
501521
/* Acquire exclusive lock on parent */
502522
PERFORM @extschema@.lock_partitioned_relation(v_parent_relid);
503523

524+
/* Acquire data modification lock */
525+
PERFORM @extschema@.lock_relation_modification(p_partition);
526+
504527
SELECT attname, parttype
505528
FROM @extschema@.pathman_config
506529
WHERE partrel = v_parent_relid
@@ -585,6 +608,10 @@ BEGIN
585608
v_parent_relid1 := @extschema@.get_parent_of_partition(partition1);
586609
v_parent_relid2 := @extschema@.get_parent_of_partition(partition2);
587610

611+
/* Acquire data modification lock */
612+
PERFORM @extschema@.lock_relation_modification(partition1);
613+
PERFORM @extschema@.lock_relation_modification(partition2);
614+
588615
IF v_parent_relid1 != v_parent_relid2 THEN
589616
RAISE EXCEPTION 'Cannot merge partitions with different parents';
590617
END IF;

src/pl_funcs.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ PG_FUNCTION_INFO_V1( is_attribute_nullable );
5353
PG_FUNCTION_INFO_V1( add_to_pathman_config );
5454
PG_FUNCTION_INFO_V1( invalidate_relcache );
5555
PG_FUNCTION_INFO_V1( lock_partitioned_relation );
56+
PG_FUNCTION_INFO_V1( lock_relation_modification );
5657
PG_FUNCTION_INFO_V1( debug_capture );
5758

5859

@@ -701,6 +702,17 @@ lock_partitioned_relation(PG_FUNCTION_ARGS)
701702
PG_RETURN_VOID();
702703
}
703704

705+
Datum
706+
lock_relation_modification(PG_FUNCTION_ARGS)
707+
{
708+
Oid relid = PG_GETARG_OID(0);
709+
710+
/* Lock partitioned relation till transaction's end */
711+
xact_lock_rel_data(relid);
712+
713+
PG_RETURN_VOID();
714+
}
715+
704716

705717
/*
706718
* NOTE: used for DEBUG, set breakpoint here.

src/xact_handling.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ xact_unlock_partitioned_rel(Oid relid)
3939
UnlockRelationOid(relid, ShareUpdateExclusiveLock);
4040
}
4141

42+
/*
43+
* Lock certain relation's data (INSERT | UPDATE | DELETE).
44+
*/
45+
void
46+
xact_lock_rel_data(Oid relid)
47+
{
48+
LockRelationOid(relid, RowExclusiveLock);
49+
}
50+
51+
/*
52+
* Unlock relation's data.
53+
*/
54+
void
55+
xact_unlock_rel_data(Oid relid)
56+
{
57+
UnlockRelationOid(relid, RowExclusiveLock);
58+
}
59+
4260
/*
4361
* Check whether we already hold a lock that
4462
* might conflict with partition spawning BGW.

src/xact_handling.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ extern List *locked_by_me;
2424
*/
2525
void xact_lock_partitioned_rel(Oid relid);
2626
void xact_unlock_partitioned_rel(Oid relid);
27+
28+
void xact_lock_rel_data(Oid relid);
29+
void xact_unlock_rel_data(Oid relid);
30+
2731
bool xact_conflicting_lock_exists(Oid relid);
2832

2933
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy