OSDN Git Service

Add test for storing plans
authorKyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Thu, 14 Sep 2017 06:12:14 +0000 (15:12 +0900)
committerKyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Thu, 14 Sep 2017 06:16:35 +0000 (15:16 +0900)
There was no regtest for actual storing behavior. This commit add
that.

Makefile
expected/convert.out [moved from expected/all.out with 100% similarity]
expected/store.out [new file with mode: 0644]
sql/convert.sql [moved from sql/all.sql with 100% similarity]
sql/store.sql [new file with mode: 0644]

index dd9fa61..4308e67 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ OBJS = pg_store_plans.o pgsp_json.o pgsp_json_text.o pgsp_explain.o
 EXTENSION = pg_store_plans
 DATA = pg_store_plans--1.1.sql pg_store_plans--1.0--1.1.sql
 
-REGRESS = all
+REGRESS = convert store
 REGRESS_OPTS = --temp-config=regress.conf
 ifdef USE_PGXS
 PG_CONFIG = pg_config
@@ -56,17 +56,17 @@ rpm95: $(STARBALL95)
 rpm96: $(STARBALL96)
        MAKE_ROOT=`pwd` rpmbuild -bb SPECS/pg_store_plans96.spec
 
-testfiles: all.out all.sql
+testfiles: convert.out convert.sql
 
-all.out: all.sql
-       psql $(DBNAME) -a -q -f all.sql > all.out
+convert.out: convert.sql
+       psql $(DBNAME) -a -q -f convert.sql > $@
 
-all.sql: makeplanfile.sql json2sql.pl
-       psql $(DBNAME) -f makeplanfile.sql |& ./json2sql.pl > all.sql
+convert.sql: makeplanfile.sql json2sql.pl
+       psql $(DBNAME) -f makeplanfile.sql |& ./json2sql.pl > $@
 
 clean-testfiles:
-       rm -f all.out all.sql
+       rm -f convert.out convert.sql
 
 deploy-testfiles: testfiles
-       mv all.sql sql/
-       mv all.out expected/
+       mv convert.sql sql/
+       mv convert.out expected/
similarity index 100%
rename from expected/all.out
rename to expected/convert.out
diff --git a/expected/store.out b/expected/store.out
new file mode 100644 (file)
index 0000000..8e16380
--- /dev/null
@@ -0,0 +1,116 @@
+SET client_min_messages = 'error';
+CREATE EXTENSION IF NOT EXISTS pg_store_plans;
+CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
+SELECT pg_stat_statements_reset();
+ pg_stat_statements_reset 
+--------------------------
+(1 row)
+
+SELECT pg_store_plans_reset();
+ pg_store_plans_reset 
+----------------------
+(1 row)
+
+DROP TABLE IF EXISTS t1;
+CREATE TABLE t1 (a int);
+CREATE INDEX ON t1 (a);
+INSERT INTO t1 (SELECT a FROM generate_series(0, 9999) a);
+RESET enable_seqscan;
+RESET enable_bitmapscan;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+ count 
+-------
+ 10000
+(1 row)
+
+SET enable_seqscan TO false;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+ count 
+-------
+ 10000
+(1 row)
+
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+ count 
+-------
+ 10000
+(1 row)
+
+SET enable_bitmapscan TO false;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+ count 
+-------
+ 10000
+(1 row)
+
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+ count 
+-------
+ 10000
+(1 row)
+
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+ count 
+-------
+ 10000
+(1 row)
+
+RESET enable_seqscan;
+RESET enable_bitmapscan;
+CREATE OR REPLACE FUNCTION test_explain() RETURNS text AS
+$x$
+DECLARE
+    r record;
+    s text;
+    p text;
+    totalrows int;
+    totalcalls int;
+    first bool;
+BEGIN
+    s := '';
+    first = true;
+    SELECT calls, rows INTO totalcalls, totalrows
+    FROM pg_stat_statements
+    WHERE query = 'SELECT count(*) FROM (SELECT * FROM t1) AS x';
+
+    FOR r IN SELECT s.query as q, p.plan as p, p.calls as c, p.rows r
+             FROM pg_stat_statements s
+             JOIN pg_store_plans p ON (s.queryid = p.queryid_stat_statements)
+             WHERE s.query = 'SELECT count(*) FROM (SELECT * FROM t1) AS x'
+             ORDER BY p.calls
+    LOOP
+         IF first then
+        s = r.q || E'\n  totalcalls=' || totalcalls ||
+            ' , totalrows=' || totalrows || E'\n';
+        first := false;
+      END IF;
+      p := regexp_replace(r.p, '=[0-9.]+([^0-9.])', '=xxx\1', 'g');
+      s := s || p || E'\n  calls=' || r.c || ', rows=' || r.r || E'\n';
+    END LOOP;
+
+    RETURN s;
+END
+$x$
+LANGUAGE plpgsql;
+SELECT test_explain();
+                                test_explain                                 
+-----------------------------------------------------------------------------
+ SELECT count(*) FROM (SELECT * FROM t1) AS x                               +
+   totalcalls=6 , totalrows=6                                               +
+ Aggregate  (cost=xxx rows=xxx width=xxx)                                   +
+   ->  Seq Scan on t1  (cost=xxx rows=xxx width=xxx)                        +
+   calls=1, rows=1                                                          +
+ Aggregate  (cost=xxx rows=xxx width=xxx)                                   +
+   ->  Bitmap Heap Scan on t1  (cost=xxx rows=xxx width=xxx)                +
+         ->  Bitmap Index Scan using t1_a_idx  (cost=xxx rows=xxx width=xxx)+
+   calls=2, rows=2                                                          +
+ Aggregate  (cost=xxx rows=xxx width=xxx)                                   +
+   ->  Index Only Scan using t1_a_idx on t1  (cost=xxx rows=xxx width=xxx)  +
+   calls=3, rows=3                                                          +
+(1 row)
+
+DROP FUNCTION test_explain();
+DROP TABLE t1;
similarity index 100%
rename from sql/all.sql
rename to sql/convert.sql
diff --git a/sql/store.sql b/sql/store.sql
new file mode 100644 (file)
index 0000000..f246c57
--- /dev/null
@@ -0,0 +1,62 @@
+SET client_min_messages = 'error';
+CREATE EXTENSION IF NOT EXISTS pg_store_plans;
+CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
+SELECT pg_stat_statements_reset();
+SELECT pg_store_plans_reset();
+
+DROP TABLE IF EXISTS t1;
+CREATE TABLE t1 (a int);
+CREATE INDEX ON t1 (a);
+INSERT INTO t1 (SELECT a FROM generate_series(0, 9999) a);
+RESET enable_seqscan;
+RESET enable_bitmapscan;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+SET enable_seqscan TO false;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+SET enable_bitmapscan TO false;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+SELECT count(*) FROM (SELECT * FROM t1) AS x;
+RESET enable_seqscan;
+RESET enable_bitmapscan;
+
+CREATE OR REPLACE FUNCTION test_explain() RETURNS text AS
+$x$
+DECLARE
+    r record;
+    s text;
+    p text;
+    totalrows int;
+    totalcalls int;
+    first bool;
+BEGIN
+    s := '';
+    first = true;
+    SELECT calls, rows INTO totalcalls, totalrows
+    FROM pg_stat_statements
+    WHERE query = 'SELECT count(*) FROM (SELECT * FROM t1) AS x';
+
+    FOR r IN SELECT s.query as q, p.plan as p, p.calls as c, p.rows r
+             FROM pg_stat_statements s
+             JOIN pg_store_plans p ON (s.queryid = p.queryid_stat_statements)
+             WHERE s.query = 'SELECT count(*) FROM (SELECT * FROM t1) AS x'
+             ORDER BY p.calls
+    LOOP
+         IF first then
+        s = r.q || E'\n  totalcalls=' || totalcalls ||
+            ' , totalrows=' || totalrows || E'\n';
+        first := false;
+      END IF;
+      p := regexp_replace(r.p, '=[0-9.]+([^0-9.])', '=xxx\1', 'g');
+      s := s || p || E'\n  calls=' || r.c || ', rows=' || r.r || E'\n';
+    END LOOP;
+
+    RETURN s;
+END
+$x$
+LANGUAGE plpgsql;
+SELECT test_explain();
+DROP FUNCTION test_explain();
+DROP TABLE t1;
+