Skip to content

Commit 61c84bd

Browse files
committed
Merge branch 'pathman_pgpro9_5' of gitlab.postgrespro.ru:pgpro-dev/postgrespro into pathman_pgpro9_5
2 parents eda9922 + 3e546d8 commit 61c84bd

File tree

4 files changed

+73
-47
lines changed

4 files changed

+73
-47
lines changed

contrib/pg_pathman/init.c

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "pathman.h"
2+
#include "miscadmin.h"
23
#include "executor/spi.h"
34
#include "catalog/pg_type.h"
45
#include "catalog/pg_class.h"
@@ -109,12 +110,15 @@ load_relations_hashtable(bool reinitialize)
109110

110111
for (i=0; i<proc; i++)
111112
{
113+
RelationKey key;
112114
HeapTuple tuple = tuptable->vals[i];
113115
int oid = DatumGetObjectId(SPI_getbinval(tuple, tupdesc, 1, &isnull));
114116

117+
key.dbid = MyDatabaseId;
118+
key.relid = oid;
115119
prel = (PartRelationInfo*)
116-
hash_search(relations, (const void *)&oid, HASH_ENTER, NULL);
117-
prel->oid = oid;
120+
hash_search(relations, (const void *) &key, HASH_ENTER, NULL);
121+
118122
prel->attnum = DatumGetInt32(SPI_getbinval(tuple, tupdesc, 2, &isnull));
119123
prel->parttype = DatumGetInt32(SPI_getbinval(tuple, tupdesc, 3, &isnull));
120124
prel->atttype = DatumGetObjectId(SPI_getbinval(tuple, tupdesc, 4, &isnull));
@@ -129,16 +133,13 @@ load_relations_hashtable(bool reinitialize)
129133
{
130134
Oid oid = (int) lfirst_int(lc);
131135

132-
prel = (PartRelationInfo*)
133-
hash_search(relations, (const void *)&oid, HASH_FIND, NULL);
134-
136+
prel = get_pathman_relation_info(oid, NULL);
135137
switch(prel->parttype)
136138
{
137139
case PT_RANGE:
138140
if (reinitialize && prel->children.length > 0)
139141
{
140-
RangeRelation *rangerel = (RangeRelation *)
141-
hash_search(range_restrictions, (void *) &oid, HASH_FIND, NULL);
142+
RangeRelation *rangerel = get_pathman_range_relation(oid, NULL);
142143
free_dsm_array(&prel->children);
143144
free_dsm_array(&rangerel->ranges);
144145
prel->children_count = 0;
@@ -164,14 +165,14 @@ create_relations_hashtable()
164165
HASHCTL ctl;
165166

166167
memset(&ctl, 0, sizeof(ctl));
167-
ctl.keysize = sizeof(int);
168+
ctl.keysize = sizeof(RelationKey);
168169
ctl.entrysize = sizeof(PartRelationInfo);
169170

170171
/* Already exists, recreate */
171172
if (relations != NULL)
172173
hash_destroy(relations);
173174

174-
relations = ShmemInitHash("Partitioning relation info", 1024, &ctl, HASH_ELEM);
175+
relations = ShmemInitHash("Partitioning relation info", 1024, &ctl, HASH_ELEM | HASH_BLOBS);
175176
}
176177

177178
/*
@@ -199,8 +200,7 @@ load_check_constraints(Oid parent_oid)
199200
// "where inhparent = %d and contype='c'";
200201
// char *query;
201202

202-
prel = (PartRelationInfo*)
203-
hash_search(relations, (const void *) &parent_oid, HASH_FIND, &found);
203+
prel = get_pathman_relation_info(parent_oid, NULL);
204204

205205
/* Skip if already loaded */
206206
if (prel->children.length > 0)
@@ -236,8 +236,12 @@ load_check_constraints(Oid parent_oid)
236236

237237
if (prel->parttype == PT_RANGE)
238238
{
239+
RelationKey key;
240+
key.dbid = MyDatabaseId;
241+
key.relid = parent_oid;
242+
239243
rangerel = (RangeRelation *)
240-
hash_search(range_restrictions, (void *) &parent_oid, HASH_ENTER, &found);
244+
hash_search(range_restrictions, (void *) &key, HASH_ENTER, &found);
241245

242246
alloc_dsm_array(&rangerel->ranges, sizeof(RangeEntry), proc);
243247
ranges = (RangeEntry *) dsm_array_get_pointer(&rangerel->ranges);
@@ -314,9 +318,13 @@ load_check_constraints(Oid parent_oid)
314318
{
315319
if (ranges[i].max > ranges[i+1].min)
316320
{
321+
RelationKey key;
322+
key.dbid = MyDatabaseId;
323+
key.relid = parent_oid;
324+
317325
elog(WARNING, "Partitions %u and %u overlap. Disabling pathman for relation %u...",
318326
ranges[i].child_oid, ranges[i+1].child_oid, parent_oid);
319-
hash_search(relations, (const void *) &parent_oid, HASH_REMOVE, &found);
327+
hash_search(relations, (const void *) &key, HASH_REMOVE, &found);
320328
}
321329
}
322330
}
@@ -450,7 +458,7 @@ create_range_restrictions_hashtable()
450458
HASHCTL ctl;
451459

452460
memset(&ctl, 0, sizeof(ctl));
453-
ctl.keysize = sizeof(int);
461+
ctl.keysize = sizeof(RelationKey);
454462
ctl.entrysize = sizeof(RangeRelation);
455463
range_restrictions = ShmemInitHash("pg_pathman range restrictions",
456464
1024, &ctl, HASH_ELEM | HASH_BLOBS);
@@ -464,9 +472,12 @@ remove_relation_info(Oid relid)
464472
{
465473
PartRelationInfo *prel;
466474
RangeRelation *rangerel;
475+
RelationKey key;
476+
477+
key.dbid = MyDatabaseId;
478+
key.relid = relid;
467479

468-
prel = (PartRelationInfo *)
469-
hash_search(relations, (const void *) &relid, HASH_FIND, 0);
480+
prel = get_pathman_relation_info(relid, NULL);
470481

471482
/* If there is nothing to remove then just return */
472483
if (!prel)
@@ -479,11 +490,10 @@ remove_relation_info(Oid relid)
479490
free_dsm_array(&prel->children);
480491
break;
481492
case PT_RANGE:
482-
rangerel = (RangeRelation *)
483-
hash_search(range_restrictions, (const void *) &relid, HASH_FIND, 0);
493+
rangerel = get_pathman_range_relation(relid, NULL);
484494
free_dsm_array(&rangerel->ranges);
485495
free_dsm_array(&prel->children);
486-
hash_search(range_restrictions, (const void *) &relid, HASH_REMOVE, 0);
496+
hash_search(range_restrictions, (const void *) &key, HASH_REMOVE, 0);
487497
break;
488498
}
489499
prel->children_count = 0;

contrib/pg_pathman/pathman.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ typedef struct DsmArray
3131
size_t length;
3232
} DsmArray;
3333

34+
/*
35+
* Hashtable key for relations
36+
*/
37+
typedef struct RelationKey
38+
{
39+
Oid dbid;
40+
Oid relid;
41+
} RelationKey;
42+
3443
/*
3544
* PartRelationInfo
3645
* Per-relation partitioning information
@@ -42,8 +51,8 @@ typedef struct DsmArray
4251
*/
4352
typedef struct PartRelationInfo
4453
{
45-
Oid oid;
46-
DsmArray children;
54+
RelationKey key;
55+
DsmArray children;
4756
int children_count;
4857
PartType parttype;
4958
Index attnum;
@@ -78,7 +87,7 @@ typedef struct RangeEntry
7887

7988
typedef struct RangeRelation
8089
{
81-
Oid parent_oid;
90+
RelationKey key;
8291
DsmArray ranges;
8392
} RangeRelation;
8493

@@ -143,6 +152,8 @@ void load_check_constraints(Oid parent_oid);
143152
void remove_relation_info(Oid relid);
144153

145154
/* utility functions */
155+
PartRelationInfo *get_pathman_relation_info(Oid relid, bool *found);
156+
RangeRelation *get_pathman_range_relation(Oid relid, bool *found);
146157
int range_binary_search(const RangeRelation *rangerel, FmgrInfo *cmp_func, Datum value, bool *fountPtr);
147158

148159
#endif /* PATHMAN_H */

contrib/pg_pathman/pg_pathman.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ _PG_fini(void)
127127
planner_hook = planner_hook_original;
128128
}
129129

130+
PartRelationInfo *
131+
get_pathman_relation_info(Oid relid, bool *found)
132+
{
133+
RelationKey key;
134+
135+
key.dbid = MyDatabaseId;
136+
key.relid = relid;
137+
return hash_search(relations, (const void *) &key, HASH_FIND, found);
138+
}
139+
140+
RangeRelation *
141+
get_pathman_range_relation(Oid relid, bool *found)
142+
{
143+
RelationKey key;
144+
145+
key.dbid = MyDatabaseId;
146+
key.relid = relid;
147+
return hash_search(range_restrictions, (const void *) &key, HASH_FIND, found);
148+
}
149+
130150
/*
131151
* Planner hook. It disables inheritance for tables that have been partitioned
132152
* by pathman to prevent standart PostgreSQL partitioning mechanism from
@@ -174,8 +194,7 @@ disable_inheritance(Query *parse)
174194
if (rte->inh)
175195
{
176196
/* Look up this relation in pathman relations */
177-
prel = (PartRelationInfo *)
178-
hash_search(relations, (const void *) &rte->relid, HASH_FIND, 0);
197+
prel = get_pathman_relation_info(rte->relid, NULL);
179198
if (prel != NULL)
180199
{
181200
rte->inh = false;
@@ -240,8 +259,7 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
240259
return;
241260

242261
/* Lookup partitioning information for parent relation */
243-
prel = (PartRelationInfo *)
244-
hash_search(relations, (const void *) &rte->relid, HASH_FIND, 0);
262+
prel = get_pathman_relation_info(rte->relid, NULL);
245263

246264
if (prel != NULL)
247265
{
@@ -649,8 +667,7 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
649667
}
650668
case PT_RANGE:
651669
value = c->constvalue;
652-
rangerel = (RangeRelation *)
653-
hash_search(range_restrictions, (const void *)&prel->oid, HASH_FIND, NULL);
670+
rangerel = get_pathman_range_relation(prel->key.relid, NULL);
654671
if (rangerel != NULL)
655672
{
656673
RangeEntry *re;
@@ -830,8 +847,6 @@ range_binary_search(const RangeRelation *rangerel, FmgrInfo *cmp_func, Datum val
830847
/* Check boundaries */
831848
cmp_min = FunctionCall2(cmp_func, value, ranges[0].min),
832849
cmp_max = FunctionCall2(cmp_func, value, ranges[rangerel->ranges.length - 1].max);
833-
// cmp_min = OidFunctionCall2(cmp_proc, value, ranges[0].min);
834-
// cmp_max = OidFunctionCall2(cmp_proc, value, ranges[rangerel->ranges.length - 1].max);
835850
if (cmp_min < 0 || cmp_max >0)
836851
{
837852
return i;
@@ -844,8 +859,6 @@ range_binary_search(const RangeRelation *rangerel, FmgrInfo *cmp_func, Datum val
844859
re = &ranges[i];
845860
cmp_min = FunctionCall2(cmp_func, value, re->min);
846861
cmp_max = FunctionCall2(cmp_func, value, re->max);
847-
// cmp_min = OidFunctionCall2(cmp_proc, value, re->min);
848-
// cmp_max = OidFunctionCall2(cmp_proc, value, re->max);
849862

850863
if (cmp_min >= 0 && cmp_max < 0)
851864
{

contrib/pg_pathman/pl_funcs.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ on_partitions_updated(PG_FUNCTION_ARGS)
4242

4343
/* Parent relation oid */
4444
relid = DatumGetInt32(PG_GETARG_DATUM(0));
45-
prel = (PartRelationInfo *)
46-
hash_search(relations, (const void *) &relid, HASH_FIND, 0);
45+
prel = get_pathman_relation_info(relid, NULL);
4746
if (prel != NULL)
4847
{
4948
LWLockAcquire(load_config_lock, LW_EXCLUSIVE);
@@ -137,17 +136,14 @@ find_range_partition(PG_FUNCTION_ARGS)
137136
TYPECACHE_EQ_OPR | TYPECACHE_LT_OPR | TYPECACHE_GT_OPR |
138137
TYPECACHE_CMP_PROC | TYPECACHE_CMP_PROC_FINFO);
139138

140-
prel = (PartRelationInfo *)
141-
hash_search(relations, (const void *) &relid, HASH_FIND, NULL);
142-
139+
prel = get_pathman_relation_info(relid, NULL);
143140
cmp_proc_oid = get_opfamily_proc(tce->btree_opf,
144141
value_type,
145142
prel->atttype,
146143
BTORDER_PROC);
147144
fmgr_info(cmp_proc_oid, &cmp_func);
148145

149-
rangerel = (RangeRelation *)
150-
hash_search(range_restrictions, (const void *) &relid, HASH_FIND, NULL);
146+
rangerel = get_pathman_range_relation(relid, NULL);
151147

152148
if (!rangerel || rangerel->ranges.length == 0)
153149
PG_RETURN_NULL();
@@ -236,11 +232,9 @@ get_partition_range(PG_FUNCTION_ARGS)
236232
TypeCacheEntry *tce;
237233
ArrayType *arr;
238234

239-
prel = (PartRelationInfo *)
240-
hash_search(relations, (const void *) &parent_oid, HASH_FIND, NULL);
235+
prel = get_pathman_relation_info(parent_oid, NULL);
241236

242-
rangerel = (RangeRelation *)
243-
hash_search(range_restrictions, (const void *) &parent_oid, HASH_FIND, NULL);
237+
rangerel = get_pathman_range_relation(parent_oid, NULL);
244238

245239
if (!prel || !rangerel)
246240
PG_RETURN_NULL();
@@ -290,11 +284,9 @@ get_range_by_idx(PG_FUNCTION_ARGS)
290284
Datum *elems;
291285
TypeCacheEntry *tce;
292286

293-
prel = (PartRelationInfo *)
294-
hash_search(relations, (const void *) &parent_oid, HASH_FIND, NULL);
287+
prel = get_pathman_relation_info(parent_oid, NULL);
295288

296-
rangerel = (RangeRelation *)
297-
hash_search(range_restrictions, (const void *) &parent_oid, HASH_FIND, NULL);
289+
rangerel = get_pathman_range_relation(parent_oid, NULL);
298290

299291
if (!prel || !rangerel || idx >= (int)rangerel->ranges.length)
300292
PG_RETURN_NULL();

0 commit comments

Comments
 (0)