@@ -43,7 +43,7 @@ static shmem_startup_hook_type shmem_startup_hook_original = NULL;
43
43
44
44
void _PG_init (void );
45
45
void _PG_fini (void );
46
- static void my_shmem_startup (void );
46
+ static void pathman_shmem_startup (void );
47
47
static void my_hook (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte );
48
48
static PlannedStmt * my_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams );
49
49
@@ -106,7 +106,7 @@ _PG_init(void)
106
106
set_rel_pathlist_hook_original = set_rel_pathlist_hook ;
107
107
set_rel_pathlist_hook = my_hook ;
108
108
shmem_startup_hook_original = shmem_startup_hook ;
109
- shmem_startup_hook = my_shmem_startup ;
109
+ shmem_startup_hook = pathman_shmem_startup ;
110
110
111
111
planner_hook = my_planner_hook ;
112
112
}
@@ -179,8 +179,11 @@ disable_inheritance(Query *parse)
179
179
}
180
180
}
181
181
182
+ /*
183
+ * Shared memory startup hook
184
+ */
182
185
static void
183
- my_shmem_startup (void )
186
+ pathman_shmem_startup (void )
184
187
{
185
188
/* initialize locks */
186
189
RequestAddinLWLocks (2 );
@@ -195,6 +198,10 @@ my_shmem_startup(void)
195
198
create_range_restrictions_hashtable ();
196
199
197
200
LWLockRelease (AddinShmemInitLock );
201
+
202
+ /* Invoke original hook if needed */
203
+ if (shmem_startup_hook_original != NULL )
204
+ shmem_startup_hook_original ();
198
205
}
199
206
200
207
/*
@@ -205,7 +212,7 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
205
212
{
206
213
PartRelationInfo * prel = NULL ;
207
214
208
- /* This workson for SELECT queries */
215
+ /* This worksonly for SELECT queries */
209
216
if (root -> parse -> commandType != CMD_SELECT || !inheritance_disabled )
210
217
return ;
211
218
@@ -585,6 +592,8 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
585
592
int i ,
586
593
int_value ,
587
594
strategy ;
595
+ bool is_less ,
596
+ is_greater ;
588
597
FmgrInfo * cmp_func ;
589
598
const OpExpr * expr = (const OpExpr * )result -> orig ;
590
599
TypeCacheEntry * tce ;
@@ -672,15 +681,10 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
672
681
re = & ranges [i ];
673
682
cmp_min = FunctionCall2 (cmp_func ,value ,re -> min );
674
683
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
- else if (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 )
684
688
{
685
689
if (strategy == BTGreaterEqualStrategyNumber && cmp_min == 0 )
686
690
lossy = false;
@@ -691,6 +695,19 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
691
695
found = true;
692
696
break ;
693
697
}
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
+ else if (is_greater )
709
+ startidx = i + 1 ;
710
+
694
711
/* for debug's sake */
695
712
Assert (++ counter < 100 );
696
713
}
@@ -757,39 +774,40 @@ make_hash(const PartRelationInfo *prel, int value)
757
774
* foundPtr to false.
758
775
*/
759
776
static int
760
- range_binary_search (const RangeRelation * rangerel ,FmgrInfo * cmp_func ,Datum value ,bool * fountPtr )
777
+ range_binary_search (const RangeRelation * rangerel ,FmgrInfo * cmp_func ,Datum value ,bool * foundPtr )
761
778
{
762
- int i ;
763
- int startidx = 0 ;
764
- int endidx = rangerel -> ranges .length - 1 ;
765
- int counter = 0 ;
766
779
RangeEntry * ranges = dsm_array_get_pointer (& rangerel -> ranges );
767
780
RangeEntry * re ;
768
-
769
- * fountPtr = false;
781
+ int cmp_min ,
782
+ cmp_max ,
783
+ i ,
784
+ startidx = 0 ,
785
+ endidx = rangerel -> ranges .length - 1 ,
786
+ counter = 0 ;
787
+
788
+ * foundPtr = false;
770
789
while (true)
771
790
{
772
791
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 )
774
798
{
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
- return i ;
785
-
786
- if (check_lt (cmp_func ,value ,re -> min ))
787
- endidx = i - 1 ;
788
- else if (check_ge (cmp_func ,value ,re -> max ))
789
- startidx = i + 1 ;
790
- }
791
- else
799
+ * foundPtr = true;
792
800
break ;
801
+ }
802
+
803
+ if (startidx == endidx )
804
+ return i ;
805
+
806
+ if (cmp_min < 0 )
807
+ endidx = i - 1 ;
808
+ else if (cmp_max >=0 )
809
+ startidx = i + 1 ;
810
+
793
811
/* for debug's sake */
794
812
Assert (++ counter < 100 );
795
813
}
@@ -1171,15 +1189,15 @@ get_partition_range(PG_FUNCTION_ARGS)
1171
1189
{
1172
1190
int parent_oid = DatumGetInt32 (PG_GETARG_DATUM (0 ));
1173
1191
int child_oid = DatumGetInt32 (PG_GETARG_DATUM (1 ));
1174
- int nelems = 2 ;
1175
- Datum * elems ;
1192
+ int nelems = 2 ;
1193
+ int i ;
1194
+ bool found = false;
1195
+ Datum * elems ;
1176
1196
PartRelationInfo * prel ;
1177
1197
RangeRelation * rangerel ;
1178
1198
RangeEntry * ranges ;
1179
1199
TypeCacheEntry * tce ;
1180
1200
ArrayType * arr ;
1181
- bool found ;
1182
- int i ;
1183
1201
1184
1202
prel = (PartRelationInfo * )
1185
1203
hash_search (relations , (const void * )& parent_oid ,HASH_FIND ,NULL );