@@ -133,22 +133,29 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
133
133
IndexInfo * indexInfo ;
134
134
#endif
135
135
136
- if (sr_plan_write_mode )
136
+ #define call_standard_planner () \
137
+ (srplan_planner_hook_next ? \
138
+ srplan_planner_hook_next(parse, cursorOptions, boundParams) : \
139
+ standard_planner(parse, cursorOptions, boundParams))
140
+
141
+ if (sr_plan_write_mode )
137
142
heap_lock = RowExclusiveLock ;
138
143
139
144
schema_oid = get_sr_plan_schema ();
140
- if (!OidIsValid (schema_oid ))
145
+ if (!OidIsValid (schema_oid ))
141
146
{
142
147
/* Just call standard_planner() if schema doesn't exist. */
143
- return standard_planner ( parse , cursorOptions , boundParams );
148
+ return call_standard_planner ( );
144
149
}
145
150
146
- if (sr_plan_fake_func )
151
+ if (sr_plan_fake_func )
147
152
{
148
153
HeapTuple ftup ;
149
154
ftup = SearchSysCache1 (PROCOID , ObjectIdGetDatum (sr_plan_fake_func ));
150
- if (!HeapTupleIsValid (ftup )) sr_plan_fake_func = 0 ;
151
- else ReleaseSysCache (ftup );
155
+ if (!HeapTupleIsValid (ftup ))
156
+ sr_plan_fake_func = 0 ;
157
+ else
158
+ ReleaseSysCache (ftup );
152
159
}
153
160
154
161
if (!sr_plan_fake_func )
@@ -172,8 +179,7 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
172
179
sr_plans_oid = sr_get_relname_oid (schema_oid , SR_PLANS_TABLE_NAME );
173
180
174
181
if (!OidIsValid (sr_plans_oid ))
175
- /* Just call standard_planner() if table doesn't exist. */
176
- return standard_planner (parse , cursorOptions , boundParams );
182
+ return call_standard_planner ();
177
183
178
184
/* Table "sr_plans" exists */
179
185
sr_plans_heap = heap_open (sr_plans_oid , heap_lock );
@@ -184,7 +190,7 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
184
190
{
185
191
heap_close (sr_plans_heap , heap_lock );
186
192
elog (WARNING , "Not found %s index" , SR_PLANS_TABLE_QUERY_INDEX_NAME );
187
- return standard_planner ( parse , cursorOptions , boundParams );
193
+ return call_standard_planner ( );
188
194
}
189
195
190
196
query_index_rel = index_open (query_index_rel_oid , heap_lock );
@@ -204,9 +210,7 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
204
210
F_INT4EQ ,
205
211
Int32GetDatum (query_hash ));
206
212
207
- index_rescan (query_index_scan ,
208
- & key , 1 ,
209
- NULL , 0 );
213
+ index_rescan (query_index_scan , & key , 1 , NULL , 0 );
210
214
211
215
while ((local_tuple = index_getnext (query_index_scan , ForwardScanDirection )) != NULL )
212
216
{
@@ -235,24 +239,22 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
235
239
if (pl_stmt )
236
240
goto cleanup ;
237
241
242
+ /* Invoke original hook if needed */
243
+ pl_stmt = call_standard_planner ();
244
+
238
245
/* Ok, we supported duplicate query_hash but only if all plans with query_hash disabled.*/
239
246
if (sr_plan_write_mode )
240
247
{
241
- bool not_have_duplicate = true ;
248
+ bool found = false ;
242
249
Datum plan_hash ;
243
250
244
- pl_stmt = standard_planner (parse , cursorOptions , boundParams );
245
251
out_jsonb2 = node_tree_to_jsonb (pl_stmt , 0 , false);
246
252
plan_hash = DirectFunctionCall1 (jsonb_hash , PointerGetDatum (out_jsonb2 ));
247
253
248
254
query_index_scan = index_beginscan (sr_plans_heap ,
249
255
query_index_rel ,
250
- SnapshotSelf ,
251
- 1 ,
252
- 0 );
253
- index_rescan (query_index_scan ,
254
- & key , 1 ,
255
- NULL , 0 );
256
+ SnapshotSelf , 1 , 0 );
257
+ index_rescan (query_index_scan , & key , 1 , NULL , 0 );
256
258
for (;;)
257
259
{
258
260
Datum search_values [6 ];
@@ -270,13 +272,13 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
270
272
/* Detect full plan duplicate */
271
273
if (search_values [1 ] == plan_hash )
272
274
{
273
- not_have_duplicate = false ;
275
+ found = true ;
274
276
break ;
275
277
}
276
278
}
277
279
index_endscan (query_index_scan );
278
280
279
- if (not_have_duplicate )
281
+ if (! found )
280
282
{
281
283
Datum values [6 ];
282
284
bool nulls [6 ];
@@ -291,25 +293,24 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
291
293
292
294
tuple = heap_form_tuple (sr_plans_heap -> rd_att , values , nulls );
293
295
simple_heap_insert (sr_plans_heap , tuple );
296
+ #if PG_VERSION_NUM >= 100000
294
297
index_insert (query_index_rel ,
295
298
values , nulls ,
296
299
& (tuple -> t_self ),
297
300
sr_plans_heap ,
298
- #if PG_VERSION_NUM >= 100000
299
301
UNIQUE_CHECK_NO , indexInfo );
300
302
#else
303
+ index_insert (query_index_rel ,
304
+ values , nulls ,
305
+ & (tuple -> t_self ),
306
+ sr_plans_heap ,
301
307
UNIQUE_CHECK_NO );
302
308
#endif
309
+
310
+ /* Make changes visible */
311
+ CommandCounterIncrement ();
303
312
}
304
313
}
305
- else
306
- {
307
- /* Invoke original hook if needed */
308
- if (srplan_planner_hook_next )
309
- pl_stmt = srplan_planner_hook_next (parse , cursorOptions , boundParams );
310
- else
311
- pl_stmt = standard_planner (parse , cursorOptions , boundParams );
312
- }
313
314
314
315
cleanup :
315
316
index_close (query_index_rel , heap_lock );
@@ -600,8 +601,6 @@ sr_plan_invalid_table(PG_FUNCTION_ARGS)
600
601
/* update existing entry */
601
602
MemSet (search_replaces , 0 , sizeof (search_replaces ));
602
603
603
- search_values [4 ] = BoolGetDatum (false);
604
- search_replaces [4 ] = true;
605
604
search_values [5 ] = BoolGetDatum (false);
606
605
search_replaces [5 ] = true;
607
606
0 commit comments