Skip to content

Instantly share code, notes, and snippets.

@lukaseder
Last active February 22, 2024 09:31
Show Gist options
  • Save lukaseder/370abba98a6ece4aa8876b1114b98ff3 to your computer and use it in GitHub Desktop.
Save lukaseder/370abba98a6ece4aa8876b1114b98ff3 to your computer and use it in GitHub Desktop.
short-circuiting-bool-or.sql
create table t (b boolean);
create table f (b boolean);
insert into t select true from generate_series(1, 1000000);
insert into f select false from generate_series(1, 1000000);
DO $$
DECLARE
v_ts TIMESTAMP;
v_repeat CONSTANT INT := 100;
rec RECORD;
BEGIN
-- Repeat the whole benchmark several times to avoid warmup penalty
FOR r IN 1..5 LOOP
v_ts := clock_timestamp();
FOR i IN 1..v_repeat LOOP
FOR rec IN (
SELECT bool_or(b) FROM t
) LOOP
NULL;
END LOOP;
END LOOP;
RAISE INFO 'Run %, Statement 1: %', r, (clock_timestamp() - v_ts);
v_ts := clock_timestamp();
FOR i IN 1..v_repeat LOOP
FOR rec IN (
SELECT bool_or(b) FROM f
) LOOP
NULL;
END LOOP;
END LOOP;
RAISE INFO 'Run %, Statement 2: %', r, (clock_timestamp() - v_ts);
RAISE INFO '';
END LOOP;
END$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment