PG-186: Add support to monitor query execution plan.

This requires refactoring of code to add this functionality. Along with
that this patch contains regression test cases.
pull/79/head
Ibrar Ahmed 2021-03-12 18:55:12 +00:00
parent 8f69daa5dd
commit 066162c3f6
34 changed files with 10234 additions and 1173 deletions

View File

@ -11,7 +11,7 @@ PGFILEDESC = "pg_stat_monitor - execution statistics of SQL statements"
LDFLAGS_SL += $(filter -lm, $(LIBS))
REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_stat_monitor/pg_stat_monitor.conf --inputdir=regression
REGRESS = guc relations basic pg_stat_monitor test_version
REGRESS = basic version guc user counters relations database top_query application_name cmd_type error rows
# Disabled because these tests require "shared_preload_libraries=pg_stat_statements",
# which typical installcheck users do not have (e.g. buildfarm clients).

View File

@ -400,3 +400,53 @@ postgres=# SELECT queryid, top_queryid, query, top_query FROM pg_stat_monitor;
762B99349F6C7F31 | 3408CA84B2353094 | SELECT (select $1 + $2) | select add2($1,$2)
(3 rows)
```
#### Monitor Query Execution Plan.
```sql
postgres=# SELECT substr(query,0,50), query_plan from pg_stat_monitor limit 10;
substr | query_plan
---------------------------------------------------+---------------------------------------------------------------------------------------------------------------
select o.n, p.partstrat, pg_catalog.count(i.inhpa | Limit +
| -> GroupAggregate +
| Group Key: (array_position(current_schemas(true), n.nspname)), p.partstrat +
| -> Sort +
| Sort Key: (array_position(current_schemas(true), n.nspname)), p.partstrat +
| -> Nested Loop Left Join +
| -> Nested Loop Left Join +
| -> Nested Loop +
| Join Filter: (c.relnamespace = n.oid) +
| -> Index Scan using pg_class_relname_nsp_index on pg_class c +
| Index Cond: (relname = 'pgbench_accounts'::name) +
| -> Seq Scan on pg_namespace n +
| Filter: (array_position(current_schemas(true), nspname) IS NOT NULL) +
| -> Index Scan using pg_partitioned_table_partrelid_index on pg_partitioned_table p+
| Index Cond: (partrelid = c.oid) +
| -> Bitmap Heap Scan on pg_inherits i +
| R
SELECT abalance FROM pgbench_accounts WHERE aid = | Index Scan using pgbench_accounts_pkey on pgbench_accounts +
| Index Cond: (aid = 102232)
BEGIN; |
END; |
SELECT substr(query,$1,$2), query_plan from pg_st |
SELECT substr(query,$1,$2),calls, planid,query_pl | Limit +
| -> Subquery Scan on pg_stat_monitor +
| -> Result +
| -> Sort +
| Sort Key: p.bucket_start_time +
| -> Hash Join +
| Hash Cond: (p.dbid = d.oid) +
| -> Function Scan on pg_stat_monitor_internal p +
| -> Hash +
| -> Seq Scan on pg_database d +
| SubPlan 1 +
| -> Function Scan on pg_stat_monitor_internal s +
| Filter: (queryid = p.top_queryid)
select count(*) from pgbench_branches | Aggregate +
| -> Seq Scan on pgbench_branches
UPDATE pgbench_tellers SET tbalance = tbalance + |
vacuum pgbench_tellers |
UPDATE pgbench_accounts SET abalance = abalance + |
(10 rows)
```

13
guc.c
View File

@ -173,6 +173,19 @@ init_guc(void)
};
DefineIntGUC(&conf[i++]);
conf[i] = (GucVariable) {
.guc_name = "pg_stat_monitor.pgsm_enable_query_plan",
.guc_desc = "Enable/Disable query plan monitoring",
.guc_default = 0,
.guc_min = 0,
.guc_max = 0,
.guc_restart = false,
.guc_unit = 0,
.guc_value = &PGSM_QUERY_PLAN
};
DefineBoolGUC(&conf[i++]);
#if PG_VERSION_NUM >= 130000
conf[i] = (GucVariable) {
.guc_name = "pg_stat_monitor.pgsm_track_planning",

View File

@ -21,8 +21,7 @@
static pgssSharedState *pgss;
static HTAB *pgss_hash;
static HTAB *pgss_query_hash;
static HTAB *pgss_plan_hash;
static HTAB* hash_init(const char *hash_name, int key_size, int entry_size, int hash_size);
@ -73,6 +72,7 @@ pgss_startup(void)
pgss_hash = hash_init("pg_stat_monitor: bucket hashtable", sizeof(pgssHashKey), sizeof(pgssEntry), MAX_BUCKET_ENTRIES);
pgss_query_hash = hash_init("pg_stat_monitor: query hashtable", sizeof(pgssQueryHashKey), sizeof(pgssQueryEntry),500000);
pgss_plan_hash = hash_init("pg_stat_monitor: plan hashtable", sizeof(pgssPlanHashKey), sizeof(pgssPlanEntry), MAX_BUCKET_ENTRIES);
LWLockRelease(AddinShmemInitLock);
@ -83,6 +83,12 @@ pgss_startup(void)
on_shmem_exit(pgss_shmem_shutdown, (Datum) 0);
}
HTAB*
pgsm_get_plan_hash(void)
{
return pgss_plan_hash;
}
pgssSharedState*
pgsm_get_ss(void)
{
@ -122,11 +128,32 @@ hash_memsize(void)
size = MAXALIGN(sizeof(pgssSharedState));
size += MAXALIGN(MAX_QUERY_BUF);
size = add_size(size, hash_estimate_size(MAX_BUCKET_ENTRIES, sizeof(pgssEntry)));
size = add_size(size, hash_estimate_size(MAX_BUCKET_ENTRIES, sizeof(pgssPlanEntry)));
size = add_size(size, hash_estimate_size(500000, sizeof(pgssQueryEntry)));
return size;
}
pgssPlanEntry *
hash_plan_entry_alloc(pgssSharedState *pgss, pgssPlanHashKey *key)
{
pgssPlanEntry *entry = NULL;
bool found = false;
if (hash_get_num_entries(pgss_plan_hash) >= MAX_BUCKET_ENTRIES)
return NULL;
/* Find or create an entry with desired hash code */
entry = (pgssPlanEntry *) hash_search(pgss_plan_hash, key, HASH_ENTER, &found);
if (!found)
{
memset(&entry->plan_info, 0, sizeof(PlanInfo));
SpinLockInit(&entry->mutex);
}
if (entry == NULL)
elog(FATAL, "%s", "pg_stat_monitor: out of memory");
return entry;
}
pgssEntry *
hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key,int encoding)
@ -135,8 +162,10 @@ hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key,int encoding)
bool found = false;
if (hash_get_num_entries(pgss_hash) >= MAX_BUCKET_ENTRIES)
{
elog(DEBUG2, "%s", "pg_stat_monitor: out of memory");
return NULL;
}
/* Find or create an entry with desired hash code */
entry = (pgssEntry *) hash_search(pgss_hash, key, HASH_ENTER, &found);
if (!found)

View File

@ -24,32 +24,27 @@ RETURNS text[] AS $$
SELECT string_to_array(get_histogram_timings(), ',');
$$ LANGUAGE SQL;
CREATE FUNCTION pg_stat_monitor(IN showtext boolean,
OUT bucket int,
CREATE FUNCTION pg_stat_monitor_internal(IN showtext boolean,
OUT bucket int, -- 0
OUT userid oid,
OUT dbid oid,
OUT client_ip int8,
OUT queryid text,
OUT queryid text, -- 4
OUT planid text,
OUT top_queryid text,
OUT query text,
OUT query_plan text,
OUT application_name text,
OUT relations text,
OUT relations text, -- 10
OUT cmd_type int,
OUT elevel int,
OUT sqlcode TEXT,
OUT message text,
OUT bucket_start_time text,
OUT plans int8,
OUT plan_total_time float8,
OUT plan_min_time float8,
OUT plan_max_time float8,
OUT plan_mean_time float8,
OUT plan_stddev_time float8,
OUT plan_rows int8,
OUT calls int8,
OUT calls int8, -- 16
OUT total_time float8,
OUT min_time float8,
OUT max_time float8,
@ -57,7 +52,13 @@ CREATE FUNCTION pg_stat_monitor(IN showtext boolean,
OUT stddev_time float8,
OUT rows int8,
OUT shared_blks_hit int8,
OUT plans_calls int8, -- 23
OUT plan_total_time float8,
OUT plan_min_time float8,
OUT plan_max_time float8,
OUT plan_mean_time float8,
OUT shared_blks_hit int8, -- 28
OUT shared_blks_read int8,
OUT shared_blks_dirtied int8,
OUT shared_blks_written int8,
@ -128,7 +129,9 @@ CREATE VIEW pg_stat_monitor AS SELECT
queryid,
top_queryid,
query,
(SELECT query from pg_stat_monitor(true) s where s.queryid = p.top_queryid) AS top_query,
planid,
query_plan,
(SELECT query from pg_stat_monitor_internal(true) s where s.queryid = p.top_queryid) AS top_query,
application_name,
string_to_array(relations, ',') AS relations,
cmd_type,
@ -136,12 +139,6 @@ CREATE VIEW pg_stat_monitor AS SELECT
elevel,
sqlcode,
message,
plans,
round( CAST(plan_total_time as numeric), 4)::float8 as plan_total_time,
round( CAST(plan_min_time as numeric), 4)::float8 as plan_min_time,
round( CAST(plan_max_time as numeric), 4)::float8 as plan_max_time,
round( CAST(plan_mean_time as numeric), 4)::float8 as plan_mean_time,
round( CAST(plan_stddev_time as numeric), 4)::float8 as plan_stddev_time,
calls,
round( CAST(total_time as numeric), 4)::float8 as total_time,
round( CAST(min_time as numeric), 4)::float8 as min_time,
@ -149,6 +146,12 @@ CREATE VIEW pg_stat_monitor AS SELECT
round( CAST(mean_time as numeric), 4)::float8 as mean_time,
round( CAST(stddev_time as numeric), 4)::float8 as stddev_time,
rows,
plans_calls,
round( CAST(plan_total_time as numeric), 4)::float8 as plan_total_time,
round( CAST(plan_min_time as numeric), 4)::float8 as plan_min_time,
round( CAST(plan_max_time as numeric), 4)::float8 as plan_max_time,
round( CAST(plan_mean_time as numeric), 4)::float8 as plan_mean_time,
shared_blks_hit,
shared_blks_read,
shared_blks_dirtied,
@ -167,7 +170,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
wal_records,
wal_fpi,
wal_bytes
FROM pg_stat_monitor(TRUE) p, pg_database d WHERE dbid = oid
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
ORDER BY bucket_start_time;
CREATE FUNCTION decode_error_level(elevel int)

File diff suppressed because it is too large Load Diff

View File

@ -79,7 +79,7 @@
#define CMD_LEN 20
#define APPLICATIONNAME_LEN 100
#define PGSM_OVER_FLOW_MAX 10
#define PLAN_TEXT_LEN 1024
/* the assumption of query max nested level */
#define DEFAULT_MAX_NESTED_LEVEL 10
@ -94,9 +94,9 @@
#define SQLCODE_LEN 20
#if PG_VERSION_NUM >= 130000
#define MAX_SETTINGS 13
#define MAX_SETTINGS 14
#else
#define MAX_SETTINGS 12
#define MAX_SETTINGS 13
#endif
typedef struct GucVariables
@ -149,6 +149,15 @@ typedef enum AGG_KEY
#define MAX_QUERY_LEN 1024
/* shared nenory storage for the query */
typedef struct CallTime
{
double total_time; /* total execution time, in msec */
double min_time; /* minimum execution time in msec */
double max_time; /* maximum execution time in msec */
double mean_time; /* mean execution time in msec */
double sum_var_time; /* sum of variances in execution time in msec */
} CallTime;
typedef struct pgssQueryHashKey
{
uint64 queryid; /* query identifier */
@ -161,6 +170,27 @@ typedef struct pgssQueryEntry
uint64 pos; /* bucket number */
} pgssQueryEntry;
typedef struct PlanInfo
{
uint64 planid; /* plan identifier */
uint64 plans; /* how many times plan */
CallTime time;
char plan_text[PLAN_TEXT_LEN]; /* plan text */
} PlanInfo;
/* shared nenory storage for the query */
typedef struct pgssPlanHashKey
{
uint64 query_hash; /* query reference identifier */
} pgssPlanHashKey;
typedef struct pgssPlanEntry
{
pgssPlanHashKey key; /* hash key of entry - MUST BE FIRST */
PlanInfo plan_info; /* planning information */
slock_t mutex; /* protects the counters only */
} pgssPlanEntry;
typedef struct pgssHashKey
{
uint64 bucket_id; /* bucket number */
@ -198,14 +228,6 @@ typedef struct Calls
double usage; /* usage factor */
} Calls;
typedef struct CallTime
{
double total_time; /* total execution time, in msec */
double min_time; /* minimum execution time in msec */
double max_time; /* maximum execution time in msec */
double mean_time; /* mean execution time in msec */
double sum_var_time; /* sum of variances in execution time in msec */
} CallTime;
typedef struct Blocks
{
@ -235,15 +257,14 @@ typedef struct Wal_Usage
int64 wal_fpi; /* # of WAL full page images generated */
uint64 wal_bytes; /* total amount of WAL bytes generated */
} Wal_Usage;
/*
* The actual stats counters kept within pgssEntry.
*/
typedef struct Counters
{
uint64 bucket_id; /* bucket id */
Calls calls[PGSS_NUMKIND];
Calls calls;
QueryInfo info;
CallTime time[PGSS_NUMKIND];
PlanInfo plan_info;
CallTime time;
Blocks blocks;
SysInfo sysinfo;
ErrorInfo error;
@ -340,18 +361,19 @@ void pgss_shmem_startup(void);
void pgss_shmem_shutdown(int code, Datum arg);
int pgsm_get_bucket_size(void);
pgssSharedState* pgsm_get_ss(void);
HTAB* pgsm_get_hash(void);
HTAB *pgsm_get_plan_hash(void);
HTAB *pgsm_get_hash(void);
HTAB *pgsm_get_plan_hash(void);
HTAB* pgsm_get_query_hash(void);
void hash_entry_reset(void);
void hash_query_entry_dealloc(int bucket);
void hash_entry_dealloc(int bucket);
pgssPlanEntry *hash_plan_entry_alloc(pgssSharedState *pgss, pgssPlanHashKey *key);
pgssEntry* hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key, int encoding);
Size hash_memsize(void);
bool hash_find_query_entry(uint64 bucket_id, uint64 queryid);
bool hash_create_query_entry(uint64 bucket_id, uint64 queryid);
/* hash_query.c */
void pgss_startup(void);
void set_qbuf(int i, unsigned char *);
@ -369,7 +391,8 @@ void pgss_startup(void);
#define PGSM_HISTOGRAM_MAX get_conf(8)->guc_variable
#define PGSM_HISTOGRAM_BUCKETS get_conf(9)->guc_variable
#define PGSM_QUERY_SHARED_BUFFER get_conf(10)->guc_variable
#define PGSM_TRACK_PLANNING get_conf(11)->guc_variable
#define PGSM_OVERFLOW_TARGET get_conf(12)->guc_variable
#define PGSM_QUERY_PLAN get_conf(12)->guc_variable
#define PGSM_TRACK_PLANNING get_conf(13)->guc_variable
#endif

View File

@ -0,0 +1,28 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT 1 AS num;
num
-----
1
(1 row)
SELECT query,application_name FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | application_name
--------------------------------------------------------------------------------+-----------------------------
SELECT $1 AS num | pg_regress/application_name
SELECT pg_stat_monitor_reset(); | pg_regress/application_name
SELECT query,application_name FROM pg_stat_monitor ORDER BY query COLLATE "C"; |
(3 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
DROP EXTENSION pg_stat_monitor;

View File

@ -11,19 +11,20 @@ select pg_sleep(.5);
(1 row)
SELECT 1;
?column?
----------
SELECT 1 AS num;
num
-----
1
(1 row)
SELECT query FROM pg_stat_monitor ORDER BY query COLLATE "C";
query
--------------------------------
SELECT $1
SELECT pg_stat_monitor_reset()
---------------------------------------------------------------
SELECT $1 AS num
SELECT pg_stat_monitor_reset();
SELECT query FROM pg_stat_monitor ORDER BY query COLLATE "C";
select pg_sleep($1)
(3 rows)
(4 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset

View File

@ -0,0 +1,45 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
CREATE TABLE t1 (a INTEGER);
INSERT INTO t1 VALUES(1);
SELECT a FROM t1;
a
---
1
(1 row)
UPDATE t1 SET a = 2;
DELETE FROM t1;
SELECT a FROM t1 FOR UPDATE;
a
---
(0 rows)
TRUNCATE t1;
DROP TABLE t1;
SELECT query, cmd_type, cmd_type_text FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | cmd_type | cmd_type_text
-----------------------------------------------------------------------------------------+----------+---------------
CREATE TABLE t1 (a INTEGER); | 0 |
DELETE FROM t1; | 4 | DELETE
DROP TABLE t1; | 0 |
INSERT INTO t1 VALUES($1) | 3 | INSERT
SELECT a FROM t1; | 1 | SELECT
SELECT pg_stat_monitor_reset(); | 1 | SELECT
SELECT query, cmd_type, cmd_type_text FROM pg_stat_monitor ORDER BY query COLLATE "C"; | 0 |
TRUNCATE t1; | 0 |
UPDATE t1 SET a = $1 | 2 | UPDATE
(9 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,93 @@
CREATE EXTENSION pg_stat_monitor;
CREATE TABLE t1 (a INTEGER);
CREATE TABLE t2 (b INTEGER);
CREATE TABLE t3 (c INTEGER);
CREATE TABLE t4 (d INTEGER);
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
a | b | c | d
---+---+---+---
(0 rows)
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
a | b | c | d
---+---+---+---
(0 rows)
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
a | b | c | d
---+---+---+---
(0 rows)
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
a | b | c | d
---+---+---+---
(0 rows)
SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls
----------------------------------------------------------------------------------+-------
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a; | 4
SELECT pg_stat_monitor_reset(); | 1
SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C"; | 1
(3 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
do $$
declare
n integer:= 1;
begin
loop
PERFORM a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
exit when n = 1000;
n := n + 1;
end loop;
end $$;
SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls
---------------------------------------------------------------------------------------------------+-------
SELECT $1 AS num | 1
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a; | 1000
SELECT n + $3 | 1
SELECT n = $3 | 1
SELECT pg_stat_monitor_reset(); | 1
SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C"; | 1
do $$ +| 1
declare +|
n integer:= 1; +|
begin +|
loop +|
PERFORM a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;+|
exit when n = 1000; +|
n := n + 1; +|
end loop; +|
end $$; |
(7 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
DROP TABLE t1;
DROP TABLE t2;
DROP TABLE t3;
DROP TABLE t4;
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,54 @@
CREATE EXTENSION pg_stat_monitor;
CREATE DATABASE db1;
CREATE DATABASE db2;
\c db1
CREATE TABLE t1 (a int);
CREATE TABLE t2 (b int);
\c db2
CREATE TABLE t3 (c int);
CREATE TABLE t4 (d int);
\c contrib_regression
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
\c db1
SELECT * FROM t1,t2 WHERE t1.a = t2.b;
a | b
---+---
(0 rows)
\c db2
SELECT * FROM t3,t4 WHERE t3.c = t4.d;
c | d
---+---
(0 rows)
\c contrib_regression
SELECT datname, query FROM pg_stat_monitor ORDER BY query COLLATE "C";
datname | query
--------------------+------------------------------------------------------------------------
db1 | SELECT * FROM t1,t2 WHERE t1.a = t2.b;
db2 | SELECT * FROM t3,t4 WHERE t3.c = t4.d;
contrib_regression | SELECT datname, query FROM pg_stat_monitor ORDER BY query COLLATE "C";
contrib_regression | SELECT pg_stat_monitor_reset();
(4 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
\c db1
DROP TABLE t1;
DROP TABLE t2;
\c db2
DROP TABLE t3;
DROP TABLE t4;
\c contrib_regression
DROP DATABASE db1;
DROP DATABASE db2;
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,44 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT 1/0; -- divide by zero
ERROR: division by zero
SELECT * FROM unknown; -- unknown table
ERROR: relation "unknown" does not exist
LINE 1: SELECT * FROM unknown;
^
ELECET * FROM unknown; -- syntax error
ERROR: syntax error at or near "ELECET"
LINE 1: ELECET * FROM unknown;
^
do $$
BEGIN
RAISE WARNING 'warning message';
END $$;
WARNING: warning message
SELECT query, elevel,sqlcode, message FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | elevel | sqlcode | message
----------------------------------------------------------------------------------------+--------+---------+-----------------------------------
ELECET * FROM unknown; | 20 | 42601 | syntax error at or near "ELECET"
SELECT $1/$2 | 0 | |
SELECT * FROM unknown; | 20 | 42P01 | relation "unknown" does not exist
SELECT 1/0; | 20 | 22012 | division by zero
SELECT pg_stat_monitor_reset(); | 0 | |
SELECT query, elevel,sqlcode, message FROM pg_stat_monitor ORDER BY query COLLATE "C"; | 0 | |
do $$ +| 19 | 01000 | warning message
BEGIN +| | |
RAISE WARNING 'warning message'; +| | |
END $$; | | |
(7 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
DROP EXTENSION pg_stat_monitor;

View File

@ -16,6 +16,7 @@ SELECT * FROM pg_stat_monitor_settings ORDER BY name COLLATE "C";
------------------------------------------+--------+---------------+----------------------------------------------------------------------------------------------------------+---------+------------+---------
pg_stat_monitor.pgsm_bucket_time | 300 | 300 | Sets the time in seconds per bucket. | 1 | 2147483647 | 1
pg_stat_monitor.pgsm_enable | 1 | 1 | Enable/Disable statistics collector. | 0 | 0 | 0
pg_stat_monitor.pgsm_enable_query_plan | 0 | 0 | Enable/Disable query plan monitoring | 0 | 0 | 0
pg_stat_monitor.pgsm_histogram_buckets | 10 | 10 | Sets the maximum number of histogram buckets | 2 | 2147483647 | 1
pg_stat_monitor.pgsm_histogram_max | 100000 | 100000 | Sets the time in millisecond. | 10 | 2147483647 | 1
pg_stat_monitor.pgsm_histogram_min | 0 | 0 | Sets the time in millisecond. | 0 | 2147483647 | 1
@ -25,8 +26,9 @@ SELECT * FROM pg_stat_monitor_settings ORDER BY name COLLATE "C";
pg_stat_monitor.pgsm_overflow_target | 0 | 1 | Sets the overflow target for pg_stat_monitor | 0 | 1 | 1
pg_stat_monitor.pgsm_query_max_len | 1024 | 1024 | Sets the maximum length of query. | 1024 | 2147483647 | 1
pg_stat_monitor.pgsm_query_shared_buffer | 20 | 20 | Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor. | 1 | 10000 | 1
pg_stat_monitor.pgsm_track_planning | 1 | 1 | Selects whether planning statistics are tracked. | 0 | 0 | 0
pg_stat_monitor.pgsm_track_utility | 1 | 1 | Selects whether utility commands are tracked. | 0 | 0 | 0
(12 rows)
(14 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset

View File

View File

@ -1,436 +0,0 @@
CREATE EXTENSION pg_stat_monitor;
--
-- simple and compound statements
--
SET pg_stat_monitor.track_utility = FALSE;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
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;
-- compound with empty statements and spurious leading spacing
\;\; SELECT 3 + 3 \;\;\; SELECT ' ' || ' !' \;\; SELECT 1 + 4 \;;
?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
-- 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 query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | rows
---------------------------------------------------+-------+------
BEGIN | 2 | 0
COMMIT | 2 | 0
PREPARE pgss_test (int) AS SELECT $1, $2 LIMIT $3 | 1 | 1
SELECT $1 | 2 | 2
SELECT $1 +| 4 | 4
+| |
AS "text" | |
SELECT $1 + $2 | 2 | 2
SELECT $1 + $2 + $3 AS "add" | 3 | 3
SELECT $1 AS "float" | 1 | 1
SELECT $1 AS i UNION SELECT $2 ORDER BY i | 1 | 2
SELECT $1 || $2 | 1 | 1
SELECT pg_stat_monitor_reset() | 1 | 1
WITH t(f) AS ( +| 1 | 2
VALUES ($1), ($2) +| |
) +| |
SELECT f FROM t ORDER BY f | |
select $1::jsonb ? $2 | 1 | 1
(13 rows)
--
-- CRUD: INSERT SELECT UPDATE DELETE on test table
--
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
-- utility "create table" should not be shown
CREATE TEMP TABLE test (a int, b char(20));
INSERT INTO test VALUES(generate_series(1, 10), 'aaa');
UPDATE test SET b = 'bbb' WHERE a > 7;
DELETE FROM test WHERE a > 9;
-- explicit transaction
BEGIN;
UPDATE test SET b = '111' WHERE a = 1 ;
COMMIT;
BEGIN \;
UPDATE test SET b = '222' WHERE a = 2 \;
COMMIT ;
UPDATE test SET b = '333' WHERE a = 3 \;
UPDATE test SET b = '444' WHERE a = 4 ;
BEGIN \;
UPDATE test SET b = '555' WHERE a = 5 \;
UPDATE test SET b = '666' WHERE a = 6 \;
COMMIT ;
-- many INSERT values
INSERT INTO test (a, b) VALUES (1, 'a'), (2, 'b'), (3, 'c');
-- SELECT with constants
SELECT * FROM test WHERE a > 5 ORDER BY a ;
a | b
---+----------------------
6 | 666
7 | aaa
8 | bbb
9 | bbb
(4 rows)
SELECT *
FROM test
WHERE a > 9
ORDER BY a ;
a | b
---+---
(0 rows)
-- SELECT without constants
SELECT * FROM test ORDER BY a;
a | b
---+----------------------
1 | a
1 | 111
2 | b
2 | 222
3 | c
3 | 333
4 | 444
5 | 555
6 | 666
7 | aaa
8 | bbb
9 | bbb
(12 rows)
-- SELECT with IN clause
SELECT * FROM test WHERE a IN (1, 2, 3, 4, 5);
a | b
---+----------------------
1 | 111
2 | 222
3 | 333
4 | 444
5 | 555
1 | a
2 | b
3 | c
(8 rows)
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | rows
-------------------------------------------------------------+-------+------
BEGIN | 3 | 0
COMMIT | 3 | 0
CREATE TEMP TABLE test (a int, b char(20)) | 1 | 0
DELETE FROM test WHERE a > $1 | 1 | 1
INSERT INTO test (a, b) VALUES ($1, $2), ($3, $4), ($5, $6) | 1 | 3
INSERT INTO test VALUES(generate_series($1, $2), $3) | 1 | 10
SELECT * FROM test ORDER BY a | 1 | 12
SELECT * FROM test WHERE a > $1 ORDER BY a | 2 | 4
SELECT * FROM test WHERE a IN ($1, $2, $3, $4, $5) | 1 | 8
SELECT pg_stat_monitor_reset() | 1 | 1
UPDATE test SET b = $1 WHERE a = $2 | 6 | 6
UPDATE test SET b = $1 WHERE a > $2 | 1 | 3
(12 rows)
--
-- pg_stat_monitor.track = none
--
SET pg_stat_monitor.track = 'none';
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT 1 AS "one";
one
-----
1
(1 row)
SELECT 1 + 1 AS "two";
two
-----
2
(1 row)
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | rows
--------------------------------+-------+------
SELECT $1 | 1 | 1
SELECT $1 + $2 | 1 | 1
SELECT pg_stat_monitor_reset() | 1 | 1
(3 rows)
--
-- pg_stat_monitor.track = top
--
SET pg_stat_monitor.track = 'top';
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
DO LANGUAGE plpgsql $$
BEGIN
-- this is a SELECT
PERFORM 'hello world'::TEXT;
END;
$$;
-- PL/pgSQL function
CREATE FUNCTION PLUS_TWO(i INTEGER) RETURNS INTEGER AS $$
DECLARE
r INTEGER;
BEGIN
SELECT (i + 1 + 1.0)::INTEGER INTO r;
RETURN r;
END; $$ LANGUAGE plpgsql;
SELECT PLUS_TWO(3);
plus_two
----------
5
(1 row)
SELECT PLUS_TWO(7);
plus_two
----------
9
(1 row)
-- SQL function --- use LIMIT to keep it from being inlined
CREATE FUNCTION PLUS_ONE(i INTEGER) RETURNS INTEGER AS
$$ SELECT (i + 1.0)::INTEGER LIMIT 1 $$ LANGUAGE SQL;
SELECT PLUS_ONE(8);
plus_one
----------
9
(1 row)
SELECT PLUS_ONE(10);
plus_one
----------
11
(1 row)
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | rows
-----------------------------------------------------------+-------+------
CREATE FUNCTION PLUS_ONE(i INTEGER) RETURNS INTEGER AS +| 1 | 0
$$ SELECT (i + 1.0)::INTEGER LIMIT 1 $$ LANGUAGE SQL | |
CREATE FUNCTION PLUS_TWO(i INTEGER) RETURNS INTEGER AS $$+| 1 | 0
DECLARE +| |
r INTEGER; +| |
BEGIN +| |
SELECT (i + 1 + 1.0)::INTEGER INTO r; +| |
RETURN r; +| |
END; $$ LANGUAGE plpgsql | |
DO LANGUAGE plpgsql $$ +| 1 | 0
BEGIN +| |
-- this is a SELECT +| |
PERFORM 'hello world'::TEXT; +| |
END; +| |
$$ | |
SELECT $1 +| 1 | 1
+| |
AS "text" | |
SELECT (i + $2 + $3)::INTEGER | 2 | 2
SELECT (i + $2)::INTEGER LIMIT $3 | 2 | 2
SELECT PLUS_ONE($1) | 2 | 2
SELECT PLUS_TWO($1) | 2 | 2
SELECT pg_stat_monitor_reset() | 1 | 1
(9 rows)
--
-- pg_stat_monitor.track = all
--
SET pg_stat_monitor.track = 'all';
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
-- we drop and recreate the functions to avoid any caching funnies
DROP FUNCTION PLUS_ONE(INTEGER);
DROP FUNCTION PLUS_TWO(INTEGER);
-- PL/pgSQL function
CREATE FUNCTION PLUS_TWO(i INTEGER) RETURNS INTEGER AS $$
DECLARE
r INTEGER;
BEGIN
SELECT (i + 1 + 1.0)::INTEGER INTO r;
RETURN r;
END; $$ LANGUAGE plpgsql;
SELECT PLUS_TWO(-1);
plus_two
----------
1
(1 row)
SELECT PLUS_TWO(2);
plus_two
----------
4
(1 row)
-- SQL function --- use LIMIT to keep it from being inlined
CREATE FUNCTION PLUS_ONE(i INTEGER) RETURNS INTEGER AS
$$ SELECT (i + 1.0)::INTEGER LIMIT 1 $$ LANGUAGE SQL;
SELECT PLUS_ONE(3);
plus_one
----------
4
(1 row)
SELECT PLUS_ONE(1);
plus_one
----------
2
(1 row)
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | rows
-----------------------------------------------------------+-------+------
CREATE FUNCTION PLUS_ONE(i INTEGER) RETURNS INTEGER AS +| 1 | 0
$$ SELECT (i + 1.0)::INTEGER LIMIT 1 $$ LANGUAGE SQL | |
CREATE FUNCTION PLUS_TWO(i INTEGER) RETURNS INTEGER AS $$+| 1 | 0
DECLARE +| |
r INTEGER; +| |
BEGIN +| |
SELECT (i + 1 + 1.0)::INTEGER INTO r; +| |
RETURN r; +| |
END; $$ LANGUAGE plpgsql | |
DROP FUNCTION PLUS_ONE(INTEGER) | 1 | 0
DROP FUNCTION PLUS_TWO(INTEGER) | 1 | 0
SELECT (i + $2 + $3)::INTEGER | 2 | 2
SELECT (i + $2)::INTEGER LIMIT $3 | 2 | 2
SELECT PLUS_ONE($1) | 2 | 2
SELECT PLUS_TWO($1) | 2 | 2
SELECT pg_stat_monitor_reset() | 1 | 1
(9 rows)
--
-- utility commands
--
SET pg_stat_monitor.track_utility = TRUE;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT 1;
?column?
----------
1
(1 row)
CREATE INDEX test_b ON test(b);
DROP TABLE test \;
DROP TABLE IF EXISTS test \;
DROP FUNCTION PLUS_ONE(INTEGER);
NOTICE: table "test" does not exist, skipping
DROP TABLE IF EXISTS test \;
DROP TABLE IF EXISTS test \;
DROP FUNCTION IF EXISTS PLUS_ONE(INTEGER);
NOTICE: table "test" does not exist, skipping
NOTICE: table "test" does not exist, skipping
NOTICE: function plus_one(pg_catalog.int4) does not exist, skipping
DROP FUNCTION PLUS_TWO(INTEGER);
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | rows
-------------------------------------------+-------+------
CREATE INDEX test_b ON test(b) | 1 | 0
DROP FUNCTION IF EXISTS PLUS_ONE(INTEGER) | 1 | 0
DROP FUNCTION PLUS_ONE(INTEGER) | 1 | 0
DROP FUNCTION PLUS_TWO(INTEGER) | 1 | 0
DROP TABLE IF EXISTS test | 3 | 0
DROP TABLE test | 1 | 0
SELECT $1 | 1 | 1
SELECT pg_stat_monitor_reset() | 1 | 1
(8 rows)
DROP EXTENSION pg_stat_monitor;

View File

@ -6,26 +6,46 @@ SELECT pg_stat_monitor_reset();
(1 row)
CREATE TABLE foo1(a int);
CREATE TABLE foo2(a int);
CREATE TABLE foo3(a int);
CREATE TABLE foo4(a int);
CREATE TABLE foo2(b int);
CREATE TABLE foo3(c int);
CREATE TABLE foo4(d int);
-- test the simple table names
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT * FROM foo1;
a
---
(0 rows)
SELECT * FROM foo1, foo2;
a | b
---+---
(0 rows)
SELECT * FROM foo1, foo2, foo3;
a | b | c
---+---+---
(0 rows)
SELECT * FROM foo1, foo2, foo3, foo4;
a | a | a | a
a | b | c | d
---+---+---+---
(0 rows)
SELECT query, relations from pg_stat_monitor ORDER BY query;
query | relations
--------------------------------------+---------------------------------------------------
SELECT * FROM foo1, foo2, foo3, foo4 | {public.foo1,public.foo2,public.foo3,public.foo4}
SELECT pg_stat_monitor_reset() |
(2 rows)
--------------------------------------------------------------+---------------------------------------------------
SELECT * FROM foo1, foo2, foo3, foo4; | {public.foo1,public.foo2,public.foo3,public.foo4}
SELECT * FROM foo1, foo2, foo3; | {public.foo1,public.foo2,public.foo3}
SELECT * FROM foo1, foo2; | {public.foo1,public.foo2}
SELECT * FROM foo1; | {public.foo1}
SELECT pg_stat_monitor_reset(); |
SELECT query, relations from pg_stat_monitor ORDER BY query; |
(6 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
@ -33,8 +53,151 @@ SELECT pg_stat_monitor_reset();
(1 row)
-- test the schema qualified table
CREATE schema sch1;
CREATE schema sch2;
CREATE schema sch3;
CREATE schema sch4;
CREATE TABLE sch1.foo1(a int);
CREATE TABLE sch2.foo2(b int);
CREATE TABLE sch3.foo3(c int);
CREATE TABLE sch4.foo4(d int);
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT * FROM sch1.foo1;
a
---
(0 rows)
SELECT * FROM sch1.foo1, sch2.foo2;
a | b
---+---
(0 rows)
SELECT * FROM sch1.foo1, sch2.foo2, sch3.foo3;
a | b | c
---+---+---
(0 rows)
SELECT * FROM sch1.foo1, sch2.foo2, sch3.foo3, sch4.foo4;
a | b | c | d
---+---+---+---
(0 rows)
SELECT query, relations from pg_stat_monitor ORDER BY query;
query | relations
--------------------------------------------------------------+-------------------------------------------
SELECT * FROM sch1.foo1, sch2.foo2, sch3.foo3, sch4.foo4; | {sch1.foo1,sch2.foo2,sch3.foo3,sch4.foo4}
SELECT * FROM sch1.foo1, sch2.foo2, sch3.foo3; | {sch1.foo1,sch2.foo2,sch3.foo3}
SELECT * FROM sch1.foo1, sch2.foo2; | {sch1.foo1,sch2.foo2}
SELECT * FROM sch1.foo1; | {sch1.foo1}
SELECT pg_stat_monitor_reset(); |
SELECT query, relations from pg_stat_monitor ORDER BY query; |
(6 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT * FROM sch1.foo1, foo1;
a | a
---+---
(0 rows)
SELECT * FROM sch1.foo1, sch2.foo2, foo1, foo2;
a | b | a | b
---+---+---+---
(0 rows)
SELECT query, relations from pg_stat_monitor ORDER BY query;
query | relations
--------------------------------------------------------------+-----------------------------------------------
SELECT * FROM sch1.foo1, foo1; | {sch1.foo1,public.foo1}
SELECT * FROM sch1.foo1, sch2.foo2, foo1, foo2; | {sch1.foo1,sch2.foo2,public.foo1,public.foo2}
SELECT pg_stat_monitor_reset(); |
SELECT query, relations from pg_stat_monitor ORDER BY query; |
(4 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
-- test the view
CREATE VIEW v1 AS SELECT * from foo1;
CREATE VIEW v2 AS SELECT * from foo1,foo2;
CREATE VIEW v3 AS SELECT * from foo1,foo2,foo3;
CREATE VIEW v4 AS SELECT * from foo1,foo2,foo3,foo4;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT * FROM v1;
a
---
(0 rows)
SELECT * FROM v1,v2;
a | a | b
---+---+---
(0 rows)
SELECT * FROM v1,v2,v3;
a | a | b | a | b | c
---+---+---+---+---+---
(0 rows)
SELECT * FROM v1,v2,v3,v4;
a | a | b | a | b | c | a | b | c | d
---+---+---+---+---+---+---+---+---+---
(0 rows)
SELECT query, relations from pg_stat_monitor ORDER BY query;
query | relations
--------------------------------------------------------------+-----------------------------------------------------------------------------------------------
SELECT * FROM v1,v2,v3,v4; | {public.v1*,public.foo1,public.v2*,public.foo2,public.v3*,public.foo3,public.v4*,public.foo4}
SELECT * FROM v1,v2,v3; | {public.v1*,public.foo1,public.v2*,public.foo2,public.v3*,public.foo3}
SELECT * FROM v1,v2; | {public.v1*,public.foo1,public.v2*,public.foo2}
SELECT * FROM v1; | {public.v1*,public.foo1}
SELECT pg_stat_monitor_reset(); |
SELECT query, relations from pg_stat_monitor ORDER BY query; |
(6 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
DROP VIEW v1;
DROP VIEW v2;
DROP VIEW v3;
DROP VIEW v4;
DROP TABLE foo1;
DROP TABLE foo2;
DROP TABLE foo3;
DROP TABLE foo4;
DROP TABLE sch1.foo1;
DROP TABLE sch2.foo2;
DROP TABLE sch3.foo3;
DROP TABLE sch4.foo4;
DROP SCHEMA sch1;
DROP SCHEMA sch2;
DROP SCHEMA sch3;
DROP SCHEMA sch4;
DROP EXTENSION pg_stat_monitor;

8560
regression/expected/rows.out Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,52 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
CREATE OR REPLACE FUNCTION add(int, int) RETURNS INTEGER AS
$$
BEGIN
return (select $1 + $2);
END; $$ language plpgsql;
CREATE OR REPLACE function add2(int, int) RETURNS int as
$$
BEGIN
return add($1,$2);
END;
$$ language plpgsql;
SELECT add2(1,2);
add2
------
3
(1 row)
SELECT query, top_query FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | top_query
--------------------------------------------------------------------------+--------------------
CREATE OR REPLACE FUNCTION add(int, int) RETURNS INTEGER AS +|
$$ +|
BEGIN +|
return (select $1 + $2); +|
END; $$ language plpgsql; |
CREATE OR REPLACE function add2(int, int) RETURNS int as +|
$$ +|
BEGIN +|
return add($1,$2); +|
END; +|
$$ language plpgsql; |
SELECT (select $1 + $2) | SELECT add2($1,$2)
SELECT add($1,$2) |
SELECT add2($1,$2) |
SELECT pg_stat_monitor_reset(); |
SELECT query, top_query FROM pg_stat_monitor ORDER BY query COLLATE "C"; |
(7 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,53 @@
DROP USER IF EXISTS su;
CREATE USER su WITH SUPERUSER;
SET ROLE su;
CREATE EXTENSION pg_stat_monitor;
CREATE USER u1;
CREATE USER u2;
SET ROLE su;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SET ROLE u1;
CREATE TABLE t1 (a int);
SELECT * FROM t1;
a
---
(0 rows)
SET ROLE u2;
CREATE TABLE t2 (a int);
SELECT * FROM t2;
a
---
(0 rows)
SET ROLE su;
SELECT userid, query FROM pg_stat_monitor ORDER BY query COLLATE "C";
userid | query
--------+-----------------------------------------------------------------------
u1 | CREATE TABLE t1 (a int);
u2 | CREATE TABLE t2 (a int);
u1 | SELECT * FROM t1;
u2 | SELECT * FROM t2;
su | SELECT pg_stat_monitor_reset();
su | SELECT userid, query FROM pg_stat_monitor ORDER BY query COLLATE "C";
su | SET ROLE su;
u1 | SET ROLE u1;
u2 | SET ROLE u2;
(9 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
DROP TABLE t1;
DROP TABLE t2;
DROP USER u1;
DROP USER u2;
DROP EXTENSION pg_stat_monitor;

View File

@ -2,7 +2,7 @@ CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_version();
pg_stat_monitor_version
-------------------------
0.7.0
devel
(1 row)
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,6 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
SELECT 1 AS num;
SELECT query,application_name FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
DROP EXTENSION pg_stat_monitor;

View File

@ -1,7 +1,7 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
select pg_sleep(.5);
SELECT 1;
SELECT 1 AS num;
SELECT query FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,15 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
CREATE TABLE t1 (a INTEGER);
INSERT INTO t1 VALUES(1);
SELECT a FROM t1;
UPDATE t1 SET a = 2;
DELETE FROM t1;
SELECT a FROM t1 FOR UPDATE;
TRUNCATE t1;
DROP TABLE t1;
SELECT query, cmd_type, cmd_type_text FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,35 @@
CREATE EXTENSION pg_stat_monitor;
CREATE TABLE t1 (a INTEGER);
CREATE TABLE t2 (b INTEGER);
CREATE TABLE t3 (c INTEGER);
CREATE TABLE t4 (d INTEGER);
SELECT pg_stat_monitor_reset();
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
SELECT pg_stat_monitor_reset();
do $$
declare
n integer:= 1;
begin
loop
PERFORM a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a;
exit when n = 1000;
n := n + 1;
end loop;
end $$;
SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
DROP TABLE t1;
DROP TABLE t2;
DROP TABLE t3;
DROP TABLE t4;
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,37 @@
CREATE EXTENSION pg_stat_monitor;
CREATE DATABASE db1;
CREATE DATABASE db2;
\c db1
CREATE TABLE t1 (a int);
CREATE TABLE t2 (b int);
\c db2
CREATE TABLE t3 (c int);
CREATE TABLE t4 (d int);
\c contrib_regression
SELECT pg_stat_monitor_reset();
\c db1
SELECT * FROM t1,t2 WHERE t1.a = t2.b;
\c db2
SELECT * FROM t3,t4 WHERE t3.c = t4.d;
\c contrib_regression
SELECT datname, query FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
\c db1
DROP TABLE t1;
DROP TABLE t2;
\c db2
DROP TABLE t3;
DROP TABLE t4;
\c contrib_regression
DROP DATABASE db1;
DROP DATABASE db2;
DROP EXTENSION pg_stat_monitor;

14
regression/sql/error.sql Normal file
View File

@ -0,0 +1,14 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
SELECT 1/0; -- divide by zero
SELECT * FROM unknown; -- unknown table
ELECET * FROM unknown; -- syntax error
do $$
BEGIN
RAISE WARNING 'warning message';
END $$;
SELECT query, elevel,sqlcode, message FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,28 @@
CREATE EXTENSION pg_stat_monitor;
CREATE TABLE t1(a int);
SELECT pg_stat_monitor_reset();
INSERT INTO t1 VALUES(generate_series(1,10));
ANALYZE t1;
SELECT count(*) FROM t1;
INSERT INTO t1 VALUES(generate_series(1,10000));
ANALYZE t1;
SELECT count(*) FROM t1;;
INSERT INTO t1 VALUES(generate_series(1,1000000));
ANALYZE t1;
SELECT count(*) FROM t1;
INSERT INTO t1 VALUES(generate_series(1,10000000));
ANALYZE t1;
SELECT count(*) FROM t1;
SELECT query, calls, min_time, max_time, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT * FROM histogram(0, 'F44CD1B4B33A47AF') AS a(range TEXT, freq INT, bar TEXT);
DROP TABLE t1;
SELECT pg_stat_monitor_reset();
DROP EXTENSION pg_stat_monitor;

View File

@ -1,198 +0,0 @@
CREATE EXTENSION pg_stat_monitor;
--
-- simple and compound statements
--
SET pg_stat_monitor.track_utility = FALSE;
SELECT pg_stat_monitor_reset();
SELECT 1 AS "int";
SELECT 'hello'
-- multiline
AS "text";
SELECT 'world' AS "text";
-- transaction
BEGIN;
SELECT 1 AS "int";
SELECT 'hello' AS "text";
COMMIT;
-- compound transaction
BEGIN \;
SELECT 2.0 AS "float" \;
SELECT 'world' AS "text" \;
COMMIT;
-- compound with empty statements and spurious leading spacing
\;\; SELECT 3 + 3 \;\;\; SELECT ' ' || ' !' \;\; SELECT 1 + 4 \;;
-- non ;-terminated statements
SELECT 1 + 1 + 1 AS "add" \gset
SELECT :add + 1 + 1 AS "add" \;
SELECT :add + 1 + 1 AS "add" \gset
-- set operator
SELECT 1 AS i UNION SELECT 2 ORDER BY i;
-- ? operator
select '{"a":1, "b":2}'::jsonb ? 'b';
-- cte
WITH t(f) AS (
VALUES (1.0), (2.0)
)
SELECT f FROM t ORDER BY f;
-- prepared statement with parameter
PREPARE pgss_test (int) AS SELECT $1, 'test' LIMIT 1;
EXECUTE pgss_test(1);
DEALLOCATE pgss_test;
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
--
-- CRUD: INSERT SELECT UPDATE DELETE on test table
--
SELECT pg_stat_monitor_reset();
-- utility "create table" should not be shown
CREATE TEMP TABLE test (a int, b char(20));
INSERT INTO test VALUES(generate_series(1, 10), 'aaa');
UPDATE test SET b = 'bbb' WHERE a > 7;
DELETE FROM test WHERE a > 9;
-- explicit transaction
BEGIN;
UPDATE test SET b = '111' WHERE a = 1 ;
COMMIT;
BEGIN \;
UPDATE test SET b = '222' WHERE a = 2 \;
COMMIT ;
UPDATE test SET b = '333' WHERE a = 3 \;
UPDATE test SET b = '444' WHERE a = 4 ;
BEGIN \;
UPDATE test SET b = '555' WHERE a = 5 \;
UPDATE test SET b = '666' WHERE a = 6 \;
COMMIT ;
-- many INSERT values
INSERT INTO test (a, b) VALUES (1, 'a'), (2, 'b'), (3, 'c');
-- SELECT with constants
SELECT * FROM test WHERE a > 5 ORDER BY a ;
SELECT *
FROM test
WHERE a > 9
ORDER BY a ;
-- SELECT without constants
SELECT * FROM test ORDER BY a;
-- SELECT with IN clause
SELECT * FROM test WHERE a IN (1, 2, 3, 4, 5);
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
--
-- pg_stat_monitor.track = none
--
SET pg_stat_monitor.track = 'none';
SELECT pg_stat_monitor_reset();
SELECT 1 AS "one";
SELECT 1 + 1 AS "two";
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
--
-- pg_stat_monitor.track = top
--
SET pg_stat_monitor.track = 'top';
SELECT pg_stat_monitor_reset();
DO LANGUAGE plpgsql $$
BEGIN
-- this is a SELECT
PERFORM 'hello world'::TEXT;
END;
$$;
-- PL/pgSQL function
CREATE FUNCTION PLUS_TWO(i INTEGER) RETURNS INTEGER AS $$
DECLARE
r INTEGER;
BEGIN
SELECT (i + 1 + 1.0)::INTEGER INTO r;
RETURN r;
END; $$ LANGUAGE plpgsql;
SELECT PLUS_TWO(3);
SELECT PLUS_TWO(7);
-- SQL function --- use LIMIT to keep it from being inlined
CREATE FUNCTION PLUS_ONE(i INTEGER) RETURNS INTEGER AS
$$ SELECT (i + 1.0)::INTEGER LIMIT 1 $$ LANGUAGE SQL;
SELECT PLUS_ONE(8);
SELECT PLUS_ONE(10);
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
--
-- pg_stat_monitor.track = all
--
SET pg_stat_monitor.track = 'all';
SELECT pg_stat_monitor_reset();
-- we drop and recreate the functions to avoid any caching funnies
DROP FUNCTION PLUS_ONE(INTEGER);
DROP FUNCTION PLUS_TWO(INTEGER);
-- PL/pgSQL function
CREATE FUNCTION PLUS_TWO(i INTEGER) RETURNS INTEGER AS $$
DECLARE
r INTEGER;
BEGIN
SELECT (i + 1 + 1.0)::INTEGER INTO r;
RETURN r;
END; $$ LANGUAGE plpgsql;
SELECT PLUS_TWO(-1);
SELECT PLUS_TWO(2);
-- SQL function --- use LIMIT to keep it from being inlined
CREATE FUNCTION PLUS_ONE(i INTEGER) RETURNS INTEGER AS
$$ SELECT (i + 1.0)::INTEGER LIMIT 1 $$ LANGUAGE SQL;
SELECT PLUS_ONE(3);
SELECT PLUS_ONE(1);
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
--
-- utility commands
--
SET pg_stat_monitor.track_utility = TRUE;
SELECT pg_stat_monitor_reset();
SELECT 1;
CREATE INDEX test_b ON test(b);
DROP TABLE test \;
DROP TABLE IF EXISTS test \;
DROP FUNCTION PLUS_ONE(INTEGER);
DROP TABLE IF EXISTS test \;
DROP TABLE IF EXISTS test \;
DROP FUNCTION IF EXISTS PLUS_ONE(INTEGER);
DROP FUNCTION PLUS_TWO(INTEGER);
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
DROP EXTENSION pg_stat_monitor;

View File

@ -1,15 +1,79 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
CREATE TABLE foo1(a int);
CREATE TABLE foo2(a int);
CREATE TABLE foo3(a int);
CREATE TABLE foo4(a int);
CREATE TABLE foo2(b int);
CREATE TABLE foo3(c int);
CREATE TABLE foo4(d int);
-- test the simple table names
SELECT pg_stat_monitor_reset();
SELECT * FROM foo1;
SELECT * FROM foo1, foo2;
SELECT * FROM foo1, foo2, foo3;
SELECT * FROM foo1, foo2, foo3, foo4;
SELECT query, relations from pg_stat_monitor ORDER BY query;
SELECT pg_stat_monitor_reset();
-- test the schema qualified table
CREATE schema sch1;
CREATE schema sch2;
CREATE schema sch3;
CREATE schema sch4;
CREATE TABLE sch1.foo1(a int);
CREATE TABLE sch2.foo2(b int);
CREATE TABLE sch3.foo3(c int);
CREATE TABLE sch4.foo4(d int);
SELECT pg_stat_monitor_reset();
SELECT * FROM sch1.foo1;
SELECT * FROM sch1.foo1, sch2.foo2;
SELECT * FROM sch1.foo1, sch2.foo2, sch3.foo3;
SELECT * FROM sch1.foo1, sch2.foo2, sch3.foo3, sch4.foo4;
SELECT query, relations from pg_stat_monitor ORDER BY query;
SELECT pg_stat_monitor_reset();
SELECT pg_stat_monitor_reset();
SELECT * FROM sch1.foo1, foo1;
SELECT * FROM sch1.foo1, sch2.foo2, foo1, foo2;
SELECT query, relations from pg_stat_monitor ORDER BY query;
SELECT pg_stat_monitor_reset();
-- test the view
CREATE VIEW v1 AS SELECT * from foo1;
CREATE VIEW v2 AS SELECT * from foo1,foo2;
CREATE VIEW v3 AS SELECT * from foo1,foo2,foo3;
CREATE VIEW v4 AS SELECT * from foo1,foo2,foo3,foo4;
SELECT pg_stat_monitor_reset();
SELECT * FROM v1;
SELECT * FROM v1,v2;
SELECT * FROM v1,v2,v3;
SELECT * FROM v1,v2,v3,v4;
SELECT query, relations from pg_stat_monitor ORDER BY query;
SELECT pg_stat_monitor_reset();
DROP VIEW v1;
DROP VIEW v2;
DROP VIEW v3;
DROP VIEW v4;
DROP TABLE foo1;
DROP TABLE foo2;
DROP TABLE foo3;
DROP TABLE foo4;
DROP TABLE sch1.foo1;
DROP TABLE sch2.foo2;
DROP TABLE sch3.foo3;
DROP TABLE sch4.foo4;
DROP SCHEMA sch1;
DROP SCHEMA sch2;
DROP SCHEMA sch3;
DROP SCHEMA sch4;
DROP EXTENSION pg_stat_monitor;

19
regression/sql/rows.sql Normal file
View File

@ -0,0 +1,19 @@
CREATE EXTENSION pg_stat_monitor;
CREATE TABLE t1(a int);
CREATE TABLE t2(b int);
INSERT INTO t1 VALUES(generate_series(1,1000));
INSERT INTO t2 VALUES(generate_series(1,5000));
SELECT pg_stat_monitor_reset();
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t1 LIMIT 10;
SELECt * FROM t2 WHERE b % 2 = 0;
SELECT query, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
DROP TABLE t1;
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,19 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
CREATE OR REPLACE FUNCTION add(int, int) RETURNS INTEGER AS
$$
BEGIN
return (select $1 + $2);
END; $$ language plpgsql;
CREATE OR REPLACE function add2(int, int) RETURNS int as
$$
BEGIN
return add($1,$2);
END;
$$ language plpgsql;
SELECT add2(1,2);
SELECT query, top_query FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
DROP EXTENSION pg_stat_monitor;

31
regression/sql/user.sql Normal file
View File

@ -0,0 +1,31 @@
DROP USER IF EXISTS su;
CREATE USER su WITH SUPERUSER;
SET ROLE su;
CREATE EXTENSION pg_stat_monitor;
CREATE USER u1;
CREATE USER u2;
SET ROLE su;
SELECT pg_stat_monitor_reset();
SET ROLE u1;
CREATE TABLE t1 (a int);
SELECT * FROM t1;
SET ROLE u2;
CREATE TABLE t2 (a int);
SELECT * FROM t2;
SET ROLE su;
SELECT userid, query FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();
DROP TABLE t1;
DROP TABLE t2;
DROP USER u1;
DROP USER u2;
DROP EXTENSION pg_stat_monitor;