Skip to content

Commit 7fad629

Browse files
committed
Increase command counter after insert, could help with duplicates
1 parent e55140c commit 7fad629

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

expected/sr_plan.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ DROP TABLE test_table;
6464
SELECT enable, valid, query FROM sr_plans;
6565
enable | valid | query
6666
--------+-------+-----------------------------------------------------
67-
f | f | SELECT * FROM test_table WHERE test_attr1 = _p(10);
68-
f | f | SELECT * FROM test_table WHERE test_attr1 = 10;
67+
t | f | SELECT * FROM test_table WHERE test_attr1 = _p(10);
68+
t | f | SELECT * FROM test_table WHERE test_attr1 = 10;
6969
(2 rows)
7070

7171
CREATE TABLE test_table(test_attr1 int, test_attr2 int);

sr_plan.c

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,29 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
133133
IndexInfo *indexInfo;
134134
#endif
135135

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)
137142
heap_lock = RowExclusiveLock;
138143

139144
schema_oid = get_sr_plan_schema();
140-
if(!OidIsValid(schema_oid))
145+
if (!OidIsValid(schema_oid))
141146
{
142147
/* Just call standard_planner() if schema doesn't exist. */
143-
return standard_planner(parse, cursorOptions, boundParams);
148+
return call_standard_planner();
144149
}
145150

146-
if(sr_plan_fake_func)
151+
if (sr_plan_fake_func)
147152
{
148153
HeapTuple ftup;
149154
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);
152159
}
153160

154161
if (!sr_plan_fake_func)
@@ -172,8 +179,7 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
172179
sr_plans_oid = sr_get_relname_oid(schema_oid, SR_PLANS_TABLE_NAME);
173180

174181
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();
177183

178184
/* Table "sr_plans" exists */
179185
sr_plans_heap = heap_open(sr_plans_oid, heap_lock);
@@ -184,7 +190,7 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
184190
{
185191
heap_close(sr_plans_heap, heap_lock);
186192
elog(WARNING, "Not found %s index", SR_PLANS_TABLE_QUERY_INDEX_NAME);
187-
return standard_planner(parse, cursorOptions, boundParams);
193+
return call_standard_planner();
188194
}
189195

190196
query_index_rel = index_open(query_index_rel_oid, heap_lock);
@@ -204,9 +210,7 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
204210
F_INT4EQ,
205211
Int32GetDatum(query_hash));
206212

207-
index_rescan(query_index_scan,
208-
&key, 1,
209-
NULL, 0);
213+
index_rescan(query_index_scan, &key, 1, NULL, 0);
210214

211215
while ((local_tuple = index_getnext(query_index_scan, ForwardScanDirection)) != NULL)
212216
{
@@ -235,24 +239,22 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
235239
if (pl_stmt)
236240
goto cleanup;
237241

242+
/* Invoke original hook if needed */
243+
pl_stmt = call_standard_planner();
244+
238245
/* Ok, we supported duplicate query_hash but only if all plans with query_hash disabled.*/
239246
if (sr_plan_write_mode)
240247
{
241-
bool not_have_duplicate = true;
248+
bool found = false;
242249
Datum plan_hash;
243250

244-
pl_stmt = standard_planner(parse, cursorOptions, boundParams);
245251
out_jsonb2 = node_tree_to_jsonb(pl_stmt, 0, false);
246252
plan_hash = DirectFunctionCall1(jsonb_hash, PointerGetDatum(out_jsonb2));
247253

248254
query_index_scan = index_beginscan(sr_plans_heap,
249255
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);
256258
for (;;)
257259
{
258260
Datum search_values[6];
@@ -270,13 +272,13 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
270272
/* Detect full plan duplicate */
271273
if (search_values[1] == plan_hash)
272274
{
273-
not_have_duplicate = false;
275+
found = true;
274276
break;
275277
}
276278
}
277279
index_endscan(query_index_scan);
278280

279-
if (not_have_duplicate)
281+
if (!found)
280282
{
281283
Datum values[6];
282284
bool nulls[6];
@@ -291,25 +293,24 @@ sr_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
291293

292294
tuple = heap_form_tuple(sr_plans_heap->rd_att, values, nulls);
293295
simple_heap_insert(sr_plans_heap, tuple);
296+
#if PG_VERSION_NUM >= 100000
294297
index_insert(query_index_rel,
295298
values, nulls,
296299
&(tuple->t_self),
297300
sr_plans_heap,
298-
#if PG_VERSION_NUM >= 100000
299301
UNIQUE_CHECK_NO, indexInfo);
300302
#else
303+
index_insert(query_index_rel,
304+
values, nulls,
305+
&(tuple->t_self),
306+
sr_plans_heap,
301307
UNIQUE_CHECK_NO);
302308
#endif
309+
310+
/* Make changes visible */
311+
CommandCounterIncrement();
303312
}
304313
}
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-
}
313314

314315
cleanup:
315316
index_close(query_index_rel, heap_lock);
@@ -600,8 +601,6 @@ sr_plan_invalid_table(PG_FUNCTION_ARGS)
600601
/* update existing entry */
601602
MemSet(search_replaces, 0, sizeof(search_replaces));
602603

603-
search_values[4] = BoolGetDatum(false);
604-
search_replaces[4] = true;
605604
search_values[5] = BoolGetDatum(false);
606605
search_replaces[5] = true;
607606

0 commit comments

Comments
 (0)