@@ -43,7 +43,7 @@ static shmem_startup_hook_type shmem_startup_hook_original = NULL;
4343
4444void _PG_init (void );
4545void _PG_fini (void );
46- static void my_shmem_startup (void );
46+ static void pathman_shmem_startup (void );
4747static void my_hook (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte );
4848static PlannedStmt * my_planner_hook (Query * parse ,int cursorOptions ,ParamListInfo boundParams );
4949
@@ -106,7 +106,7 @@ _PG_init(void)
106106set_rel_pathlist_hook_original = set_rel_pathlist_hook ;
107107set_rel_pathlist_hook = my_hook ;
108108shmem_startup_hook_original = shmem_startup_hook ;
109- shmem_startup_hook = my_shmem_startup ;
109+ shmem_startup_hook = pathman_shmem_startup ;
110110
111111planner_hook = my_planner_hook ;
112112}
@@ -179,8 +179,11 @@ disable_inheritance(Query *parse)
179179}
180180}
181181
182+ /*
183+ * Shared memory startup hook
184+ */
182185static void
183- my_shmem_startup (void )
186+ pathman_shmem_startup (void )
184187{
185188/* initialize locks */
186189RequestAddinLWLocks (2 );
@@ -195,6 +198,10 @@ my_shmem_startup(void)
195198create_range_restrictions_hashtable ();
196199
197200LWLockRelease (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{
206213PartRelationInfo * prel = NULL ;
207214
208- /* This workson for SELECT queries */
215+ /* This worksonly for SELECT queries */
209216if (root -> parse -> commandType != CMD_SELECT || !inheritance_disabled )
210217return ;
211218
@@ -585,6 +592,8 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
585592int i ,
586593int_value ,
587594strategy ;
595+ bool is_less ,
596+ is_greater ;
588597FmgrInfo * cmp_func ;
589598const OpExpr * expr = (const OpExpr * )result -> orig ;
590599TypeCacheEntry * tce ;
@@ -672,15 +681,10 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
672681re = & ranges [i ];
673682cmp_min = FunctionCall2 (cmp_func ,value ,re -> min );
674683cmp_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 )
684688{
685689if (strategy == BTGreaterEqualStrategyNumber && cmp_min == 0 )
686690lossy = false;
@@ -691,6 +695,19 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
691695found = true;
692696break ;
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+ else if (is_greater )
709+ startidx = i + 1 ;
710+
694711/* for debug's sake */
695712Assert (++ counter < 100 );
696713}
@@ -757,39 +774,40 @@ make_hash(const PartRelationInfo *prel, int value)
757774 * foundPtr to false.
758775 */
759776static 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 )
761778{
762- int i ;
763- int startidx = 0 ;
764- int endidx = rangerel -> ranges .length - 1 ;
765- int counter = 0 ;
766779RangeEntry * ranges = dsm_array_get_pointer (& rangerel -> ranges );
767780RangeEntry * 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;
770789while (true)
771790{
772791i = 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- 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;
792800break ;
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+
793811/* for debug's sake */
794812Assert (++ counter < 100 );
795813}
@@ -1171,15 +1189,15 @@ get_partition_range(PG_FUNCTION_ARGS)
11711189{
11721190int parent_oid = DatumGetInt32 (PG_GETARG_DATUM (0 ));
11731191int 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 ;
11761196PartRelationInfo * prel ;
11771197RangeRelation * rangerel ;
11781198RangeEntry * ranges ;
11791199TypeCacheEntry * tce ;
11801200ArrayType * arr ;
1181- bool found ;
1182- int i ;
11831201
11841202prel = (PartRelationInfo * )
11851203hash_search (relations , (const void * )& parent_oid ,HASH_FIND ,NULL );