Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit12f7198

Browse files
zilderzilder
zilder
authored and
zilder
committed
pathman: fix binary search (stop condition added)
1 parent1d92db0 commit12f7198

File tree

3 files changed

+61
-64
lines changed

3 files changed

+61
-64
lines changed

‎contrib/pathman/init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include"access/htup_details.h"
1010
#include"utils/builtins.h"
1111
#include"utils/typcache.h"
12+
#include"utils/lsyscache.h"
1213

1314

1415
HTAB*relations=NULL;

‎contrib/pathman/pathman.c

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static shmem_startup_hook_type shmem_startup_hook_original = NULL;
4343

4444
void_PG_init(void);
4545
void_PG_fini(void);
46-
staticvoidmy_shmem_startup(void);
46+
staticvoidpathman_shmem_startup(void);
4747
staticvoidmy_hook(PlannerInfo*root,RelOptInfo*rel,Indexrti,RangeTblEntry*rte);
4848
staticPlannedStmt*my_planner_hook(Query*parse,intcursorOptions,ParamListInfoboundParams);
4949

@@ -106,7 +106,7 @@ _PG_init(void)
106106
set_rel_pathlist_hook_original=set_rel_pathlist_hook;
107107
set_rel_pathlist_hook=my_hook;
108108
shmem_startup_hook_original=shmem_startup_hook;
109-
shmem_startup_hook=my_shmem_startup;
109+
shmem_startup_hook=pathman_shmem_startup;
110110

111111
planner_hook=my_planner_hook;
112112
}
@@ -179,8 +179,11 @@ disable_inheritance(Query *parse)
179179
}
180180
}
181181

182+
/*
183+
* Shared memory startup hook
184+
*/
182185
staticvoid
183-
my_shmem_startup(void)
186+
pathman_shmem_startup(void)
184187
{
185188
/* initialize locks */
186189
RequestAddinLWLocks(2);
@@ -195,6 +198,10 @@ my_shmem_startup(void)
195198
create_range_restrictions_hashtable();
196199

197200
LWLockRelease(AddinShmemInitLock);
201+
202+
/* Invoke original hook if needed */
203+
if (shmem_startup_hook_original!=NULL)
204+
shmem_startup_hook_original();
198205
}
199206

200207
/*
@@ -205,7 +212,7 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
205212
{
206213
PartRelationInfo*prel=NULL;
207214

208-
/* This workson for SELECT queries */
215+
/* This worksonly for SELECT queries */
209216
if (root->parse->commandType!=CMD_SELECT|| !inheritance_disabled)
210217
return;
211218

@@ -585,6 +592,8 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
585592
inti,
586593
int_value,
587594
strategy;
595+
boolis_less,
596+
is_greater;
588597
FmgrInfo*cmp_func;
589598
constOpExpr*expr= (constOpExpr*)result->orig;
590599
TypeCacheEntry*tce;
@@ -672,15 +681,10 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
672681
re=&ranges[i];
673682
cmp_min=FunctionCall2(cmp_func,value,re->min);
674683
cmp_max=FunctionCall2(cmp_func,value,re->max);
675-
if (cmp_min<0|| (cmp_min==0&&strategy==BTLessStrategyNumber))
676-
{
677-
endidx=i-1;
678-
}
679-
elseif (cmp_max>0|| (cmp_max >=0&&strategy!=BTLessStrategyNumber))
680-
{
681-
startidx=i+1;
682-
}
683-
else
684+
is_less= (cmp_min<0|| (cmp_min==0&&strategy==BTLessStrategyNumber));
685+
is_greater= (cmp_max>0|| (cmp_max >=0&&strategy!=BTLessStrategyNumber));
686+
687+
if (!is_less&& !is_greater)
684688
{
685689
if (strategy==BTGreaterEqualStrategyNumber&&cmp_min==0)
686690
lossy= false;
@@ -691,6 +695,19 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
691695
found= true;
692696
break;
693697
}
698+
699+
/* If we still didn't find partition then it doesn't exist */
700+
if (startidx==endidx)
701+
{
702+
result->rangeset=NIL;
703+
return;
704+
}
705+
706+
if (is_less)
707+
endidx=i-1;
708+
elseif (is_greater)
709+
startidx=i+1;
710+
694711
/* for debug's sake */
695712
Assert(++counter<100);
696713
}
@@ -757,39 +774,40 @@ make_hash(const PartRelationInfo *prel, int value)
757774
* foundPtr to false.
758775
*/
759776
staticint
760-
range_binary_search(constRangeRelation*rangerel,FmgrInfo*cmp_func,Datumvalue,bool*fountPtr)
777+
range_binary_search(constRangeRelation*rangerel,FmgrInfo*cmp_func,Datumvalue,bool*foundPtr)
761778
{
762-
inti;
763-
intstartidx=0;
764-
intendidx=rangerel->ranges.length-1;
765-
intcounter=0;
766779
RangeEntry*ranges=dsm_array_get_pointer(&rangerel->ranges);
767780
RangeEntry*re;
768-
769-
*fountPtr= false;
781+
intcmp_min,
782+
cmp_max,
783+
i,
784+
startidx=0,
785+
endidx=rangerel->ranges.length-1,
786+
counter=0;
787+
788+
*foundPtr= false;
770789
while (true)
771790
{
772791
i=startidx+ (endidx-startidx) /2;
773-
if (i >=0&&i<rangerel->ranges.length)
792+
Assert(i >=0&&i<rangerel->ranges.length);
793+
re=&ranges[i];
794+
cmp_min=FunctionCall2(cmp_func,value,re->min);
795+
cmp_max=FunctionCall2(cmp_func,value,re->max);
796+
797+
if (cmp_min >=0&&cmp_max<0)
774798
{
775-
re=&ranges[i];
776-
if (check_le(cmp_func,re->min,value)&&check_lt(cmp_func,value,re->max))
777-
{
778-
*fountPtr= true;
779-
break;
780-
}
781-
782-
/* if we still didn't find position then it is not in array */
783-
if (startidx==endidx)
784-
returni;
785-
786-
if (check_lt(cmp_func,value,re->min))
787-
endidx=i-1;
788-
elseif (check_ge(cmp_func,value,re->max))
789-
startidx=i+1;
790-
}
791-
else
799+
*foundPtr= true;
792800
break;
801+
}
802+
803+
if (startidx==endidx)
804+
returni;
805+
806+
if (cmp_min<0)
807+
endidx=i-1;
808+
elseif (cmp_max >=0)
809+
startidx=i+1;
810+
793811
/* for debug's sake */
794812
Assert(++counter<100);
795813
}
@@ -1171,15 +1189,15 @@ get_partition_range(PG_FUNCTION_ARGS)
11711189
{
11721190
intparent_oid=DatumGetInt32(PG_GETARG_DATUM(0));
11731191
intchild_oid=DatumGetInt32(PG_GETARG_DATUM(1));
1174-
intnelems=2;
1175-
Datum*elems;
1192+
intnelems=2;
1193+
inti;
1194+
boolfound= false;
1195+
Datum*elems;
11761196
PartRelationInfo*prel;
11771197
RangeRelation*rangerel;
11781198
RangeEntry*ranges;
11791199
TypeCacheEntry*tce;
11801200
ArrayType*arr;
1181-
boolfound;
1182-
inti;
11831201

11841202
prel= (PartRelationInfo*)
11851203
hash_search(relations, (constvoid*)&parent_oid,HASH_FIND,NULL);

‎contrib/pathman/sql/init.sql

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@ CREATE TABLE IF NOT EXISTS @extschema@.pg_pathman_rels (
88
parttypeINTEGER
99
);
1010

11-
/*
12-
* Relations using hash strategy
13-
*/
14-
-- CREATE TABLE IF NOT EXISTS @extschema@.pg_pathman_hash_rels (
15-
-- id SERIAL PRIMARY KEY,
16-
-- parent VARCHAR(127),
17-
-- hash INTEGER,
18-
-- child VARCHAR(127)
19-
-- );
20-
21-
/*
22-
* Relations using range strategy
23-
*/
24-
-- CREATE TABLE IF NOT EXISTS @extschema@.pg_pathman_range_rels (
25-
-- id SERIAL PRIMARY KEY,
26-
-- parent VARCHAR(127),
27-
-- min_num DOUBLE PRECISION,
28-
-- max_num DOUBLE PRECISION,
29-
-- min_dt TIMESTAMP,
30-
-- max_dt TIMESTAMP,
31-
-- child VARCHAR(127)
32-
-- );
3311

3412
CREATE OR REPLACEFUNCTIONpg_pathman_on_create_partitions(relidINTEGER)
3513
RETURNS VOIDAS'pathman','on_partitions_created' LANGUAGE C STRICT;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp