Skip to content

Commit aca6586

Browse files
committed
pathman: locks fixed
1 parent fad8d2f commit aca6586

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

contrib/pathman/init.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ init(void)
1515
{
1616
initialization_needed = false;
1717
create_dsm_segment(32);
18+
19+
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
1820
load_part_relations_hashtable();
21+
LWLockRelease(load_config_lock);
1922
}
2023

2124
void
@@ -29,8 +32,6 @@ load_part_relations_hashtable()
2932
List *part_oids = NIL;
3033
ListCell *lc;
3134

32-
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
33-
3435
/* if hashtable is empty */
3536
if (hash_get_num_entries(relations) == 0)
3637
{
@@ -86,8 +87,6 @@ load_part_relations_hashtable()
8687
}
8788
SPI_finish();
8889
}
89-
90-
LWLockRelease(load_config_lock);
9190
}
9291

9392
void
@@ -325,3 +324,45 @@ create_range_restrictions_hashtable()
325324
512, 512,
326325
&ctl, HASH_ELEM | HASH_BLOBS);
327326
}
327+
328+
/*
329+
* Remove partitions
330+
*/
331+
void
332+
remove_relation_info(Oid relid)
333+
{
334+
PartRelationInfo *prel;
335+
HashRelationKey key;
336+
RangeRelation *rangerel;
337+
int i;
338+
339+
prel = (PartRelationInfo *)
340+
hash_search(relations, (const void *) &relid, HASH_FIND, 0);
341+
342+
/* if there is nothing to remove then just return */
343+
if (!prel)
344+
return;
345+
346+
/* remove children relations */
347+
switch (prel->parttype)
348+
{
349+
case PT_HASH:
350+
for (i=0; i<prel->children_count; i++)
351+
{
352+
key.parent_oid = relid;
353+
key.hash = i;
354+
hash_search(hash_restrictions, (const void *) &key, HASH_REMOVE, 0);
355+
}
356+
free_dsm_array(&prel->children);
357+
break;
358+
case PT_RANGE:
359+
rangerel = (RangeRelation *)
360+
hash_search(range_restrictions, (const void *) &relid, HASH_FIND, 0);
361+
free_dsm_array(&rangerel->ranges);
362+
free_dsm_array(&prel->children);
363+
hash_search(range_restrictions, (const void *) &relid, HASH_REMOVE, 0);
364+
break;
365+
}
366+
prel->children_count = 0;
367+
hash_search(relations, (const void *) &relid, HASH_REMOVE, 0);
368+
}

contrib/pathman/pathman.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,9 @@ accumulate_append_subpath(List *subpaths, Path *path)
830830
Datum
831831
on_partitions_created(PG_FUNCTION_ARGS) {
832832
/* Reload config */
833-
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
833+
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
834834
load_part_relations_hashtable();
835-
LWLockRelease(AddinShmemInitLock);
835+
LWLockRelease(load_config_lock);
836836

837837
PG_RETURN_NULL();
838838
}
@@ -842,53 +842,33 @@ on_partitions_updated(PG_FUNCTION_ARGS) {
842842
Oid relid;
843843
PartRelationInfo *prel;
844844

845-
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
846845
/* parent relation oid */
847846
relid = DatumGetInt32(PG_GETARG_DATUM(0));
848847
prel = (PartRelationInfo *)
849848
hash_search(relations, (const void *) &relid, HASH_FIND, 0);
850849
if (prel != NULL)
851850
{
852-
prel->children_count = 0;
851+
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
852+
remove_relation_info(relid);
853853
load_part_relations_hashtable();
854+
LWLockRelease(load_config_lock);
854855
}
855-
LWLockRelease(AddinShmemInitLock);
856856

857857
PG_RETURN_NULL();
858858
}
859859

860860
Datum
861861
on_partitions_removed(PG_FUNCTION_ARGS) {
862-
HashRelationKey key;
863862
Oid relid;
864-
PartRelationInfo *prel;
865863
int i;
866864

867-
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
865+
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
868866

869867
/* parent relation oid */
870868
relid = DatumGetInt32(PG_GETARG_DATUM(0));
871-
prel = (PartRelationInfo *)
872-
hash_search(relations, (const void *) &relid, HASH_FIND, 0);
869+
remove_relation_info(relid);
873870

874-
/* remove children relations */
875-
switch (prel->parttype)
876-
{
877-
case PT_HASH:
878-
for (i=0; i<prel->children_count; i++)
879-
{
880-
key.parent_oid = relid;
881-
key.hash = i;
882-
hash_search(hash_restrictions, (const void *) &key, HASH_REMOVE, 0);
883-
}
884-
break;
885-
case PT_RANGE:
886-
hash_search(range_restrictions, (const void *) &relid, HASH_REMOVE, 0);
887-
}
888-
prel->children_count = 0;
889-
hash_search(relations, (const void *) &relid, HASH_REMOVE, 0);
890-
891-
LWLockRelease(AddinShmemInitLock);
871+
LWLockRelease(load_config_lock);
892872

893873
PG_RETURN_NULL();
894874
}

contrib/pathman/pathman.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,4 @@ void create_range_restrictions_hashtable(void);
161161
void load_part_relations_hashtable(void);
162162
void load_hash_restrictions(Oid relid);
163163
void load_range_restrictions(Oid relid);
164+
void remove_relation_info(Oid relid);

contrib/pathman/sql/hash.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ BEGIN
121121
relid := relfilenode FROM pg_class WHERE relname = relation;
122122
partitions_count := COUNT(*) FROM pg_pathman_hash_rels WHERE parent = relation;
123123

124+
IF partitions_count > 0 THEN
125+
RETURN
126+
124127
FOR partnum IN 0..partitions_count-1
125128
LOOP
126129
EXECUTE format(q, relation, partnum);

0 commit comments

Comments
 (0)