Skip to content

Commit 4d05145

Browse files
committed
pathman:
* insert trigger for RANGE partitioning fixed; * IndexScan for OR-clauses fixed; * on_partitions_updated callback fixed
1 parent 688798c commit 4d05145

File tree

4 files changed

+182
-81
lines changed

4 files changed

+182
-81
lines changed

contrib/pathman/init.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ load_range_restrictions(Oid parent_oid)
210210
"rr.min_dt, "
211211
"rr.max_dt - '1 microsecond'::INTERVAL, "
212212
"rr.min_dt::DATE, "
213-
"(rr.max_dt - '1 day'::INTERVAL)::DATE "
213+
"(rr.max_dt - '1 day'::INTERVAL)::DATE, "
214+
"rr.min_num::INTEGER, "
215+
"rr.max_num::INTEGER - 1 "
214216
"FROM pg_pathman_range_rels rr "
215217
"JOIN pg_class p ON p.relname = rr.parent "
216218
"JOIN pg_class c ON c.relname = rr.child "
@@ -224,6 +226,10 @@ load_range_restrictions(Oid parent_oid)
224226
TupleDesc tupdesc = SPI_tuptable->tupdesc;
225227
SPITupleTable *tuptable = SPI_tuptable;
226228

229+
rangerel = (RangeRelation *)
230+
hash_search(range_restrictions, (void *) &parent_oid, HASH_ENTER, &found);
231+
rangerel->nranges = 0;
232+
227233
for (i=0; i<proc; i++)
228234
{
229235
Datum min;
@@ -234,9 +240,6 @@ load_range_restrictions(Oid parent_oid)
234240
// int parent_oid = DatumGetObjectId(SPI_getbinval(tuple, tupdesc, 1, &arg1_isnull));
235241
re.child_oid = DatumGetObjectId(SPI_getbinval(tuple, tupdesc, 2, &arg1_isnull));
236242

237-
rangerel = (RangeRelation *)
238-
hash_search(range_restrictions, (void *) &parent_oid, HASH_ENTER, &found);
239-
240243
/* date */
241244
// switch (prinfo->atttype)
242245
// {
@@ -264,6 +267,12 @@ load_range_restrictions(Oid parent_oid)
264267
re.min = SPI_getbinval(tuple, tupdesc, 5, &arg1_isnull);
265268
re.max = SPI_getbinval(tuple, tupdesc, 6, &arg2_isnull);
266269
break;
270+
case INT2OID:
271+
case INT4OID:
272+
case INT8OID:
273+
re.min = SPI_getbinval(tuple, tupdesc, 9, &arg1_isnull);
274+
re.max = SPI_getbinval(tuple, tupdesc, 10, &arg2_isnull);
275+
break;
267276
default:
268277
re.min = SPI_getbinval(tuple, tupdesc, 3, &arg1_isnull);
269278
re.max = SPI_getbinval(tuple, tupdesc, 4, &arg2_isnull);

contrib/pathman/pathman.c

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,25 @@ static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblE
4545
static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte);
4646
static List *accumulate_append_subpath(List *subpaths, Path *path);
4747

48+
PG_FUNCTION_INFO_V1( on_partitions_created );
49+
PG_FUNCTION_INFO_V1( on_partitions_updated );
50+
PG_FUNCTION_INFO_V1( on_partitions_removed );
51+
4852
typedef struct
4953
{
5054
Oid old_varno;
5155
Oid new_varno;
5256
} change_varno_context;
5357

58+
static void change_varnos_in_restrinct_info(RestrictInfo *rinfo, Oid old_varno, Oid new_varno);
5459
static void change_varnos(Node *node, Oid old_varno, Oid new_varno);
5560
static bool change_varno_walker(Node *node, change_varno_context *context);
5661

57-
5862
PG_FUNCTION_INFO_V1( on_partitions_created );
5963
PG_FUNCTION_INFO_V1( on_partitions_updated );
6064
PG_FUNCTION_INFO_V1( on_partitions_removed );
6165

6266

63-
6467
/*
6568
* Entry point
6669
*/
@@ -181,11 +184,11 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
181184

182185
// }
183186

184-
if (length(children) > 0)
187+
if (list_length(children) > 0)
185188
{
186189
RelOptInfo **new_rel_array;
187190
RangeTblEntry **new_rte_array;
188-
int len = length(children);
191+
int len = list_length(children);
189192

190193
/* Expand simple_rel_array and simple_rte_array */
191194
ereport(LOG, (errmsg("Expanding simple_rel_array")));
@@ -322,28 +325,8 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEnt
322325
new_rinfo = copyObject(node);
323326

324327
/* replace old relids with new ones */
325-
change_varnos(new_rinfo->clause, rel->relid, childrel->relid);
326-
if (new_rinfo->left_em)
327-
change_varnos(new_rinfo->left_em->em_expr, rel->relid, childrel->relid);
328-
if (new_rinfo->right_em)
329-
change_varnos(new_rinfo->right_em->em_expr, rel->relid, childrel->relid);
330-
331-
/* TODO: find some elegant way to do this */
332-
if (bms_is_member(rel->relid, new_rinfo->clause_relids))
333-
{
334-
bms_del_member(new_rinfo->clause_relids, rel->relid);
335-
bms_add_member(new_rinfo->clause_relids, childrel->relid);
336-
}
337-
if (bms_is_member(rel->relid, new_rinfo->left_relids))
338-
{
339-
bms_del_member(new_rinfo->left_relids, rel->relid);
340-
bms_add_member(new_rinfo->left_relids, childrel->relid);
341-
}
342-
if (bms_is_member(rel->relid, new_rinfo->right_relids))
343-
{
344-
bms_del_member(new_rinfo->right_relids, rel->relid);
345-
bms_add_member(new_rinfo->right_relids, childrel->relid);
346-
}
328+
change_varnos_in_restrinct_info(new_rinfo, rel->relid, childrel->relid);
329+
347330
childrel->baserestrictinfo = lappend(childrel->baserestrictinfo,
348331
new_rinfo);
349332
}
@@ -371,7 +354,11 @@ change_varnos(Node *node, Oid old_varno, Oid new_varno)
371354
change_varno_walker(node, &context);
372355
}
373356

357+
<<<<<<< HEAD
374358
static bool
359+
=======
360+
bool
361+
>>>>>>> pathman:
375362
change_varno_walker(Node *node, change_varno_context *context)
376363
{
377364
if (node == NULL)
@@ -392,6 +379,42 @@ change_varno_walker(Node *node, change_varno_context *context)
392379
}
393380

394381

382+
void
383+
change_varnos_in_restrinct_info(RestrictInfo *rinfo, Oid old_varno, Oid new_varno)
384+
{
385+
ListCell *lc;
386+
387+
change_varnos((Node *) rinfo->clause, old_varno, new_varno);
388+
if (rinfo->left_em)
389+
change_varnos((Node *) rinfo->left_em->em_expr, old_varno, new_varno);
390+
if (rinfo->right_em)
391+
change_varnos((Node *) rinfo->right_em->em_expr, old_varno, new_varno);
392+
if (rinfo->orclause)
393+
foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
394+
{
395+
RestrictInfo *rinfo2 = (RestrictInfo *) lfirst(lc);
396+
change_varnos_in_restrinct_info(rinfo2, old_varno, new_varno);
397+
}
398+
399+
/* TODO: find some elegant way to do this */
400+
if (bms_is_member(old_varno, rinfo->clause_relids))
401+
{
402+
bms_del_member(rinfo->clause_relids, old_varno);
403+
bms_add_member(rinfo->clause_relids, new_varno);
404+
}
405+
if (bms_is_member(old_varno, rinfo->left_relids))
406+
{
407+
bms_del_member(rinfo->left_relids, old_varno);
408+
bms_add_member(rinfo->left_relids, new_varno);
409+
}
410+
if (bms_is_member(old_varno, rinfo->right_relids))
411+
{
412+
bms_del_member(rinfo->right_relids, old_varno);
413+
bms_add_member(rinfo->right_relids, new_varno);
414+
}
415+
}
416+
417+
395418
/*
396419
* Recursive function to walk through conditions tree
397420
*/
@@ -432,7 +455,6 @@ handle_binary_opexpr(const PartRelationInfo *prel, const OpExpr *expr,
432455
HashRelation *hashrel;
433456
RangeRelation *rangerel;
434457
int int_value;
435-
DateADT dt_value;
436458
Datum value;
437459
int i, j;
438460
int startidx, endidx;
@@ -459,9 +481,6 @@ handle_binary_opexpr(const PartRelationInfo *prel, const OpExpr *expr,
459481
return ALL;
460482
}
461483
case PT_RANGE:
462-
// if (c->consttype == DATEOID)
463-
// value = TimeTzADTPGetDatum(date2timestamp_no_overflow(c->constvalue));
464-
// else
465484
value = c->constvalue;
466485
rangerel = (RangeRelation *)
467486
hash_search(range_restrictions, (const void *)&prel->oid, HASH_FIND, NULL);
@@ -471,9 +490,10 @@ handle_binary_opexpr(const PartRelationInfo *prel, const OpExpr *expr,
471490
List *children = NIL;
472491
bool found = false;
473492
startidx = 0;
474-
endidx = rangerel->nranges-1;
475493
int counter = 0;
476494

495+
endidx = rangerel->nranges-1;
496+
477497
/* check boundaries */
478498
if (rangerel->nranges == 0 || rangerel->ranges[0].min > value ||
479499
rangerel->ranges[rangerel->nranges-1].max < value)
@@ -553,7 +573,7 @@ handle_opexpr(const OpExpr *expr, const PartRelationInfo *prel)
553573
Node *firstarg = NULL;
554574
Node *secondarg = NULL;
555575

556-
if (length(expr->args) == 2)
576+
if (list_length(expr->args) == 2)
557577
{
558578
firstarg = (Node*) linitial(expr->args);
559579
secondarg = (Node*) lsecond(expr->args);
@@ -796,10 +816,8 @@ on_partitions_created(PG_FUNCTION_ARGS) {
796816

797817
Datum
798818
on_partitions_updated(PG_FUNCTION_ARGS) {
799-
HashRelationKey key;
800819
Oid relid;
801820
PartRelationInfo *prel;
802-
int i;
803821

804822
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
805823
/* parent relation oid */

contrib/pathman/pathman.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
typedef enum PartType
1919
{
2020
PT_HASH = 1,
21-
PT_RANGE,
22-
PT_LIST
21+
PT_RANGE
2322
} PartType;
2423

2524
/*

0 commit comments

Comments
 (0)