mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-23 13:10:41 +02:00
415 lines
8.5 KiB
Plaintext
415 lines
8.5 KiB
Plaintext
|
--
|
||
|
-- SELECT statements
|
||
|
--
|
||
|
CREATE EXTENSION pg_stat_statements;
|
||
|
SET pg_stat_statements.track_utility = FALSE;
|
||
|
SET pg_stat_statements.track_planning = TRUE;
|
||
|
SELECT pg_stat_statements_reset();
|
||
|
pg_stat_statements_reset
|
||
|
--------------------------
|
||
|
|
||
|
(1 row)
|
||
|
|
||
|
--
|
||
|
-- simple and compound statements
|
||
|
--
|
||
|
SELECT 1 AS "int";
|
||
|
int
|
||
|
-----
|
||
|
1
|
||
|
(1 row)
|
||
|
|
||
|
SELECT 'hello'
|
||
|
-- multiline
|
||
|
AS "text";
|
||
|
text
|
||
|
-------
|
||
|
hello
|
||
|
(1 row)
|
||
|
|
||
|
SELECT 'world' AS "text";
|
||
|
text
|
||
|
-------
|
||
|
world
|
||
|
(1 row)
|
||
|
|
||
|
-- transaction
|
||
|
BEGIN;
|
||
|
SELECT 1 AS "int";
|
||
|
int
|
||
|
-----
|
||
|
1
|
||
|
(1 row)
|
||
|
|
||
|
SELECT 'hello' AS "text";
|
||
|
text
|
||
|
-------
|
||
|
hello
|
||
|
(1 row)
|
||
|
|
||
|
COMMIT;
|
||
|
-- compound transaction
|
||
|
BEGIN \;
|
||
|
SELECT 2.0 AS "float" \;
|
||
|
SELECT 'world' AS "text" \;
|
||
|
COMMIT;
|
||
|
float
|
||
|
-------
|
||
|
2.0
|
||
|
(1 row)
|
||
|
|
||
|
text
|
||
|
-------
|
||
|
world
|
||
|
(1 row)
|
||
|
|
||
|
-- compound with empty statements and spurious leading spacing
|
||
|
\;\; SELECT 3 + 3 \;\;\; SELECT ' ' || ' !' \;\; SELECT 1 + 4 \;;
|
||
|
?column?
|
||
|
----------
|
||
|
6
|
||
|
(1 row)
|
||
|
|
||
|
?column?
|
||
|
----------
|
||
|
!
|
||
|
(1 row)
|
||
|
|
||
|
?column?
|
||
|
----------
|
||
|
5
|
||
|
(1 row)
|
||
|
|
||
|
-- non ;-terminated statements
|
||
|
SELECT 1 + 1 + 1 AS "add" \gset
|
||
|
SELECT :add + 1 + 1 AS "add" \;
|
||
|
SELECT :add + 1 + 1 AS "add" \gset
|
||
|
add
|
||
|
-----
|
||
|
5
|
||
|
(1 row)
|
||
|
|
||
|
-- set operator
|
||
|
SELECT 1 AS i UNION SELECT 2 ORDER BY i;
|
||
|
i
|
||
|
---
|
||
|
1
|
||
|
2
|
||
|
(2 rows)
|
||
|
|
||
|
-- ? operator
|
||
|
select '{"a":1, "b":2}'::jsonb ? 'b';
|
||
|
?column?
|
||
|
----------
|
||
|
t
|
||
|
(1 row)
|
||
|
|
||
|
-- cte
|
||
|
WITH t(f) AS (
|
||
|
VALUES (1.0), (2.0)
|
||
|
)
|
||
|
SELECT f FROM t ORDER BY f;
|
||
|
f
|
||
|
-----
|
||
|
1.0
|
||
|
2.0
|
||
|
(2 rows)
|
||
|
|
||
|
-- prepared statement with parameter
|
||
|
PREPARE pgss_test (int) AS SELECT $1, 'test' LIMIT 1;
|
||
|
EXECUTE pgss_test(1);
|
||
|
?column? | ?column?
|
||
|
----------+----------
|
||
|
1 | test
|
||
|
(1 row)
|
||
|
|
||
|
DEALLOCATE pgss_test;
|
||
|
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
|
||
|
calls | rows | query
|
||
|
-------+------+------------------------------------------------------------------------------
|
||
|
1 | 1 | PREPARE pgss_test (int) AS SELECT $1, $2 LIMIT $3
|
||
|
4 | 4 | SELECT $1 +
|
||
|
| | -- multiline +
|
||
|
| | AS "text"
|
||
|
2 | 2 | SELECT $1 + $2
|
||
|
3 | 3 | SELECT $1 + $2 + $3 AS "add"
|
||
|
1 | 1 | SELECT $1 AS "float"
|
||
|
2 | 2 | SELECT $1 AS "int"
|
||
|
1 | 2 | SELECT $1 AS i UNION SELECT $2 ORDER BY i
|
||
|
1 | 1 | SELECT $1 || $2
|
||
|
0 | 0 | SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C"
|
||
|
1 | 1 | SELECT pg_stat_statements_reset()
|
||
|
1 | 2 | WITH t(f) AS ( +
|
||
|
| | VALUES ($1), ($2) +
|
||
|
| | ) +
|
||
|
| | SELECT f FROM t ORDER BY f
|
||
|
1 | 1 | select $1::jsonb ? $2
|
||
|
(12 rows)
|
||
|
|
||
|
SELECT pg_stat_statements_reset();
|
||
|
pg_stat_statements_reset
|
||
|
--------------------------
|
||
|
|
||
|
(1 row)
|
||
|
|
||
|
--
|
||
|
-- queries with locking clauses
|
||
|
--
|
||
|
CREATE TABLE pgss_a (id integer PRIMARY KEY);
|
||
|
CREATE TABLE pgss_b (id integer PRIMARY KEY, a_id integer REFERENCES pgss_a);
|
||
|
SELECT pg_stat_statements_reset();
|
||
|
pg_stat_statements_reset
|
||
|
--------------------------
|
||
|
|
||
|
(1 row)
|
||
|
|
||
|
-- control query
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
-- test range tables
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_a;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_b;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_a, pgss_b; -- matches plain "FOR UPDATE"
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_b, pgss_a;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
-- test strengths
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR NO KEY UPDATE;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR SHARE;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR KEY SHARE;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
-- test wait policies
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE NOWAIT;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE SKIP LOCKED;
|
||
|
id | id | a_id
|
||
|
----+----+------
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT calls, query FROM pg_stat_statements ORDER BY query COLLATE "C";
|
||
|
calls | query
|
||
|
-------+------------------------------------------------------------------------------------------
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR KEY SHARE
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR NO KEY UPDATE
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR SHARE
|
||
|
2 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE NOWAIT
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_a
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_b
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE OF pgss_b, pgss_a
|
||
|
1 | SELECT * FROM pgss_a JOIN pgss_b ON pgss_b.a_id = pgss_a.id FOR UPDATE SKIP LOCKED
|
||
|
0 | SELECT calls, query FROM pg_stat_statements ORDER BY query COLLATE "C"
|
||
|
1 | SELECT pg_stat_statements_reset()
|
||
|
(12 rows)
|
||
|
|
||
|
DROP TABLE pgss_a, pgss_b CASCADE;
|
||
|
--
|
||
|
-- access to pg_stat_statements_info view
|
||
|
--
|
||
|
SELECT pg_stat_statements_reset();
|
||
|
pg_stat_statements_reset
|
||
|
--------------------------
|
||
|
|
||
|
(1 row)
|
||
|
|
||
|
SELECT dealloc FROM pg_stat_statements_info;
|
||
|
dealloc
|
||
|
---------
|
||
|
0
|
||
|
(1 row)
|
||
|
|
||
|
-- FROM [ONLY]
|
||
|
CREATE TABLE tbl_inh(id integer);
|
||
|
CREATE TABLE tbl_inh_1() INHERITS (tbl_inh);
|
||
|
INSERT INTO tbl_inh_1 SELECT 1;
|
||
|
SELECT * FROM tbl_inh;
|
||
|
id
|
||
|
----
|
||
|
1
|
||
|
(1 row)
|
||
|
|
||
|
SELECT * FROM ONLY tbl_inh;
|
||
|
id
|
||
|
----
|
||
|
(0 rows)
|
||
|
|
||
|
SELECT COUNT(*) FROM pg_stat_statements WHERE query LIKE '%FROM%tbl_inh%';
|
||
|
count
|
||
|
-------
|
||
|
2
|
||
|
(1 row)
|
||
|
|
||
|
-- WITH TIES
|
||
|
CREATE TABLE limitoption AS SELECT 0 AS val FROM generate_series(1, 10);
|
||
|
SELECT *
|
||
|
FROM limitoption
|
||
|
WHERE val < 2
|
||
|
ORDER BY val
|
||
|
FETCH FIRST 2 ROWS WITH TIES;
|
||
|
val
|
||
|
-----
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
(10 rows)
|
||
|
|
||
|
SELECT *
|
||
|
FROM limitoption
|
||
|
WHERE val < 2
|
||
|
ORDER BY val
|
||
|
FETCH FIRST 2 ROW ONLY;
|
||
|
val
|
||
|
-----
|
||
|
0
|
||
|
0
|
||
|
(2 rows)
|
||
|
|
||
|
SELECT COUNT(*) FROM pg_stat_statements WHERE query LIKE '%FETCH FIRST%';
|
||
|
count
|
||
|
-------
|
||
|
2
|
||
|
(1 row)
|
||
|
|
||
|
-- GROUP BY [DISTINCT]
|
||
|
SELECT a, b, c
|
||
|
FROM (VALUES (1, 2, 3), (4, NULL, 6), (7, 8, 9)) AS t (a, b, c)
|
||
|
GROUP BY ROLLUP(a, b), rollup(a, c)
|
||
|
ORDER BY a, b, c;
|
||
|
a | b | c
|
||
|
---+---+---
|
||
|
1 | 2 | 3
|
||
|
1 | 2 |
|
||
|
1 | 2 |
|
||
|
1 | | 3
|
||
|
1 | | 3
|
||
|
1 | |
|
||
|
1 | |
|
||
|
1 | |
|
||
|
4 | | 6
|
||
|
4 | | 6
|
||
|
4 | | 6
|
||
|
4 | |
|
||
|
4 | |
|
||
|
4 | |
|
||
|
4 | |
|
||
|
4 | |
|
||
|
7 | 8 | 9
|
||
|
7 | 8 |
|
||
|
7 | 8 |
|
||
|
7 | | 9
|
||
|
7 | | 9
|
||
|
7 | |
|
||
|
7 | |
|
||
|
7 | |
|
||
|
| |
|
||
|
(25 rows)
|
||
|
|
||
|
SELECT a, b, c
|
||
|
FROM (VALUES (1, 2, 3), (4, NULL, 6), (7, 8, 9)) AS t (a, b, c)
|
||
|
GROUP BY DISTINCT ROLLUP(a, b), rollup(a, c)
|
||
|
ORDER BY a, b, c;
|
||
|
a | b | c
|
||
|
---+---+---
|
||
|
1 | 2 | 3
|
||
|
1 | 2 |
|
||
|
1 | | 3
|
||
|
1 | |
|
||
|
4 | | 6
|
||
|
4 | | 6
|
||
|
4 | |
|
||
|
4 | |
|
||
|
7 | 8 | 9
|
||
|
7 | 8 |
|
||
|
7 | | 9
|
||
|
7 | |
|
||
|
| |
|
||
|
(13 rows)
|
||
|
|
||
|
SELECT COUNT(*) FROM pg_stat_statements WHERE query LIKE '%GROUP BY%ROLLUP%';
|
||
|
count
|
||
|
-------
|
||
|
2
|
||
|
(1 row)
|
||
|
|
||
|
-- GROUPING SET agglevelsup
|
||
|
SELECT (
|
||
|
SELECT (
|
||
|
SELECT GROUPING(a,b) FROM (VALUES (1)) v2(c)
|
||
|
) FROM (VALUES (1,2)) v1(a,b) GROUP BY (a,b)
|
||
|
) FROM (VALUES(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
|
||
|
grouping
|
||
|
----------
|
||
|
0
|
||
|
0
|
||
|
0
|
||
|
(3 rows)
|
||
|
|
||
|
SELECT (
|
||
|
SELECT (
|
||
|
SELECT GROUPING(e,f) FROM (VALUES (1)) v2(c)
|
||
|
) FROM (VALUES (1,2)) v1(a,b) GROUP BY (a,b)
|
||
|
) FROM (VALUES(6,7)) v3(e,f) GROUP BY ROLLUP(e,f);
|
||
|
grouping
|
||
|
----------
|
||
|
3
|
||
|
0
|
||
|
1
|
||
|
(3 rows)
|
||
|
|
||
|
SELECT COUNT(*) FROM pg_stat_statements WHERE query LIKE '%SELECT GROUPING%';
|
||
|
count
|
||
|
-------
|
||
|
2
|
||
|
(1 row)
|
||
|
|
||
|
SELECT pg_stat_statements_reset();
|
||
|
pg_stat_statements_reset
|
||
|
--------------------------
|
||
|
|
||
|
(1 row)
|
||
|
|