@@ -278,3 +278,80 @@ drop table pc_list_part_1;
278
278
execute pstmt_def_insert(1);
279
279
drop table pc_list_parted, pc_list_part_null;
280
280
deallocate pstmt_def_insert;
281
+ -- Test plan_cache_mode
282
+ create table test_mode (a int);
283
+ insert into test_mode select 1 from generate_series(1,1000) union all select 2;
284
+ create index on test_mode (a);
285
+ analyze test_mode;
286
+ prepare test_mode_pp (int) as select count(*) from test_mode where a = $1;
287
+ -- up to 5 executions, custom plan is used
288
+ explain (costs off) execute test_mode_pp(2);
289
+ QUERY PLAN
290
+ ----------------------------------------------------------
291
+ Aggregate
292
+ -> Index Only Scan using test_mode_a_idx on test_mode
293
+ Index Cond: (a = 2)
294
+ (3 rows)
295
+
296
+ -- force generic plan
297
+ set plan_cache_mode to force_generic_plan;
298
+ explain (costs off) execute test_mode_pp(2);
299
+ QUERY PLAN
300
+ -----------------------------
301
+ Aggregate
302
+ -> Seq Scan on test_mode
303
+ Filter: (a = $1)
304
+ (3 rows)
305
+
306
+ -- get to generic plan by 5 executions
307
+ set plan_cache_mode to auto;
308
+ execute test_mode_pp(1); -- 1x
309
+ count
310
+ -------
311
+ 1000
312
+ (1 row)
313
+
314
+ execute test_mode_pp(1); -- 2x
315
+ count
316
+ -------
317
+ 1000
318
+ (1 row)
319
+
320
+ execute test_mode_pp(1); -- 3x
321
+ count
322
+ -------
323
+ 1000
324
+ (1 row)
325
+
326
+ execute test_mode_pp(1); -- 4x
327
+ count
328
+ -------
329
+ 1000
330
+ (1 row)
331
+
332
+ execute test_mode_pp(1); -- 5x
333
+ count
334
+ -------
335
+ 1000
336
+ (1 row)
337
+
338
+ -- we should now get a really bad plan
339
+ explain (costs off) execute test_mode_pp(2);
340
+ QUERY PLAN
341
+ -----------------------------
342
+ Aggregate
343
+ -> Seq Scan on test_mode
344
+ Filter: (a = $1)
345
+ (3 rows)
346
+
347
+ -- but we can force a custom plan
348
+ set plan_cache_mode to force_custom_plan;
349
+ explain (costs off) execute test_mode_pp(2);
350
+ QUERY PLAN
351
+ ----------------------------------------------------------
352
+ Aggregate
353
+ -> Index Only Scan using test_mode_a_idx on test_mode
354
+ Index Cond: (a = 2)
355
+ (3 rows)
356
+
357
+ drop table test_mode;
0 commit comments