@@ -288,13 +288,13 @@ DECLARE
288
288
r record;
289
289
r2 record;
290
290
cond text;
291
+ idx_ctids tid[];
292
+ ss_ctids tid[];
291
293
count int;
292
- mismatch bool;
293
294
plan_ok bool;
294
295
plan_line text;
295
296
BEGIN
296
297
FOR r IN SELECT colname, oper, typ, value[ordinality], matches[ordinality] FROM brinopers, unnest(op) WITH ORDINALITY AS oper LOOP
297
- mismatch := false;
298
298
299
299
-- prepare the condition
300
300
IF r.value IS NULL THEN
@@ -304,53 +304,46 @@ BEGIN
304
304
END IF;
305
305
306
306
-- run the query using the brin index
307
- CREATE TEMP TABLE brin_result (cid tid);
308
307
SET enable_seqscan = 0;
309
308
SET enable_bitmapscan = 1;
310
309
311
310
plan_ok := false;
312
- FOR plan_line IN EXECUTE format($y$EXPLAIN SELECT ctid FROM brintest WHERE %s $y$, cond) LOOP
313
- IF plan_line LIKE 'Bitmap Heap Scan on brintest%' THEN
311
+ FOR plan_line IN EXECUTE format($y$EXPLAIN SELECT array_agg( ctid) FROM brintest WHERE %s $y$, cond) LOOP
312
+ IF plan_line LIKE '% Bitmap Heap Scan on brintest%' THEN
314
313
plan_ok := true;
315
314
END IF;
316
315
END LOOP;
317
316
IF NOT plan_ok THEN
318
317
RAISE WARNING 'did not get bitmap indexscan plan for %', r;
319
318
END IF;
320
319
321
- EXECUTE format($y$INSERT INTO brin_result SELECT ctid FROM brintest WHERE %s $y$, cond);
320
+ EXECUTE format($y$SELECT array_agg(ctid) FROM brintest WHERE %s $y$, cond)
321
+ INTO idx_ctids;
322
322
323
323
-- run the query using a seqscan
324
- CREATE TEMP TABLE brin_result_ss (cid tid);
325
324
SET enable_seqscan = 1;
326
325
SET enable_bitmapscan = 0;
327
326
328
327
plan_ok := false;
329
- FOR plan_line IN EXECUTE format($y$EXPLAIN SELECT ctid FROM brintest WHERE %s $y$, cond) LOOP
330
- IF plan_line LIKE 'Seq Scan on brintest%' THEN
328
+ FOR plan_line IN EXECUTE format($y$EXPLAIN SELECT array_agg( ctid) FROM brintest WHERE %s $y$, cond) LOOP
329
+ IF plan_line LIKE '% Seq Scan on brintest%' THEN
331
330
plan_ok := true;
332
331
END IF;
333
332
END LOOP;
334
333
IF NOT plan_ok THEN
335
334
RAISE WARNING 'did not get seqscan plan for %', r;
336
335
END IF;
337
336
338
- EXECUTE format($y$INSERT INTO brin_result_ss SELECT ctid FROM brintest WHERE %s $y$, cond);
337
+ EXECUTE format($y$SELECT array_agg(ctid) FROM brintest WHERE %s $y$, cond)
338
+ INTO ss_ctids;
339
339
340
340
-- make sure both return the same results
341
- PERFORM * FROM brin_result EXCEPT ALL SELECT * FROM brin_result_ss;
342
- GET DIAGNOSTICS count = ROW_COUNT;
343
- IF count <> 0 THEN
344
- mismatch = true;
345
- END IF;
346
- PERFORM * FROM brin_result_ss EXCEPT ALL SELECT * FROM brin_result;
347
- GET DIAGNOSTICS count = ROW_COUNT;
348
- IF count <> 0 THEN
349
- mismatch = true;
350
- END IF;
341
+ count := array_length(idx_ctids, 1);
351
342
352
- -- report the results of each scan to make the differences obvious
353
- IF mismatch THEN
343
+ IF NOT (count = array_length(ss_ctids, 1) AND
344
+ idx_ctids @> ss_ctids AND
345
+ idx_ctids <@ ss_ctids) THEN
346
+ -- report the results of each scan to make the differences obvious
354
347
RAISE WARNING 'something not right in %: count %', r, count;
355
348
SET enable_seqscan = 1;
356
349
SET enable_bitmapscan = 0;
@@ -366,12 +359,7 @@ BEGIN
366
359
END IF;
367
360
368
361
-- make sure we found expected number of matches
369
- SELECT count(*) INTO count FROM brin_result;
370
362
IF count != r.matches THEN RAISE WARNING 'unexpected number of results % for %', count, r; END IF;
371
-
372
- -- drop the temporary tables
373
- DROP TABLE brin_result;
374
- DROP TABLE brin_result_ss;
375
363
END LOOP;
376
364
END;
377
365
$x$;
0 commit comments