pg_top is 'top' for postgresql

27
ptop is 'top' for PostgreSQL Linuxfest Northwest 2008 April 26-27, 2008 Mark Wong PostgreSQL Global Development Group [email protected]

Upload: mark-wong

Post on 17-Dec-2014

4.126 views

Category:

Technology


2 download

DESCRIPTION

pg_top allows you to monitor PostgreSQL processes to view the currently running SQL statement of a process, the query plan of a currently running SELECT statement, locks held by a process, user table statistics, and user index statistics.

TRANSCRIPT

Page 1: pg_top is 'top' for PostgreSQL

ptop is 'top' for PostgreSQL

Linuxfest Northwest 2008April 26-27, 2008

Mark WongPostgreSQL Global Development Group

[email protected]

Page 2: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 2

Oops

'ptop' is the Free Pascal (aka FPK Pascal) source formatter.

Page 3: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 3

pg_top is 'top' for PostgreSQL

Linuxfest Northwest 2008April 26-27, 2008

Mark WongPostgreSQL Global Development Group

[email protected]

Page 4: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 4

Outline

● A little about me● More about pg_top

– PostgreSQL specific functionality– How you might have done it, how pg_top

may help you● Q/A

Page 5: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 5

About my moonlighting

● pg_top● PostgreSQL Virtual Machines● Portland Performance lab

http://wiki.postgresql.org/wiki/Performances_QA_testing

Page 6: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 6

About pg_top

● http://ptop.projects.postgresql.org/● Derived from the Unix Top v3.6.1

– William LeFebvre– http://www.unixtop.org/

● pg_top currently at 3.6.2-beta4

Page 7: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 7

PostgreSQL Specific Features

● Displays only PostgreSQL connection processes

● Show running SQL statements● Show query plans (EXPLAIN or EXPLAIN

ANALYZE)● Show table or index statistics● Show database locks

Page 8: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 8

pg_top

Page 9: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 9

SELECT current_query

SELECT datname, procpid, current_queryFROM pg_stat_activityORDER BY procpid;

datname | procpid | current_query---------+---------+------------------------------------------------------------ dbt3 | 10994 | select sum(l_extendedprice * l_discount) as revenue from dbt3 | 11021 | select s_name, count(*) as numwait from supplier, lineite dbt3 | 11141 | insert into lineitem (select * from tmp_lineitem2); dbt3 | 11155 | : SELECT datname, procpid, current_query : FROM pg_stat_activity : ORDER BY procpid; : (4 rows)

Page 10: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 10

Hit the 'Q' key, enter a pid

Page 11: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 11

'Q' key results

Page 12: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 12

Show a query plan

EXPLAIN select l_returnflag, l_linestatus, sum(l_quantity) as sum_qty, sum(l_extendedprice) as sum_base_price, sum(l_extendedprice * (1 - l_discount)) as sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge, avg(l_quantity) as avg_qty, avg(l_extendedprice) as avg_price, avg(l_discount) as avg_disc, count(*) as count_order from lineitem where l_shipdate <= date'1998-12-01' - interval '105 days' group by l_returnflag, l_linestatus order by l_returnflag, l_linestatus;

QUERY PLAN ------------------------------------------------------------------------------------------ Sort (cost=352306.40..352306.41 rows=6 width=26) Sort Key: l_returnflag, l_linestatus -> HashAggregate (cost=352306.05..352306.32 rows=6 width=26) -> Seq Scan on lineitem (cost=0.00..207292.30 rows=5800550 width=26) Filter: (l_shipdate <= '1998-08-18 00:00:00'::timestamp without time zone)(5 rows)

Page 13: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 13

Hit the 'E' key, or 'A' for 'EXPLAIN ANALYZE', enter a pid

Page 14: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 14

'E' key results

Page 15: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 15

'EXPLAIN' and 'EXPLAIN ANALYZE' caveats

● Neither show what is actually being done● EXPLAIN

– An estimated query plan– Actual plan may differ

● EXPLAIN ANALYZE– Actually executes query– Shows what is actually executed

Page 16: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 16

Query table statistics

SELECT relname, seq_scan, seq_tup_read, n_tup_ins, n_tup_upd, n_tup_delFROM pg_stat_user_tablesORDER BY relname;

relname | seq_scan | seq_tup_read | n_tup_ins | n_tup_upd | n_tup_del-----------------+----------+--------------+-----------+-----------+----------- customer | 64 | 4800000 | 0 | 0 | 0 lineitem | 46 | 144519980 | 17968 | 0 | 0 nation | 1181765 | 29542875 | 0 | 0 | 0 orders | 32 | 24084000 | 4501 | 0 | 0 part | 63 | 6200000 | 0 | 0 | 0 partsupp | 9 | 4000000 | 0 | 0 | 0 region | 10349 | 51665 | 0 | 0 | 0 supplier | 74 | 290000 | 0 | 0 | 0 time_statistics | 105 | 9224 | 105 | 105 | 0(9 rows)

Page 17: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 17

Hit the 'R' key

Page 18: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 18

Some index statisticsSELECT relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetchFROM pg_stat_user_indexesORDER BY relname, indexrelname;

relname | indexrelname | idx_scan | idx_tup_read | idx_tup_fetch----------+-----------------------+----------+--------------+--------------- customer | i_c_nationkey | 0 | 0 | 0 customer | pk_customer | 4 | 600000 | 600000 lineitem | i_l_commitdate | 0 | 0 | 0 lineitem | i_l_orderkey | 1388565 | 26191210 | 26188763 lineitem | i_l_orderkey_quantity | 0 | 0 | 0 lineitem | i_l_partkey | 32033 | 981915 | 187059 lineitem | i_l_receiptdate | 4 | 3648680 | 0 lineitem | i_l_shipdate | 20 | 13120485 | 0 lineitem | i_l_suppkey | 1552 | 934560 | 0 lineitem | i_l_suppkey_partkey | 193876 | 1459950 | 1459669 lineitem | pk_lineitem | 0 | 0 | 0 nation | i_n_regionkey | 0 | 0 | 0 nation | pk_nation | 19984 | 19984 | 19984 orders | i_o_custkey | 76377 | 50962 | 50962 orders | i_o_orderdate | 16 | 3208273 | 0...

Page 19: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 19

Hit the 'X' key

Page 20: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 20

Table and Index Statistics Notes

● Press 't' to toggle between showing cumulative or differential statistics

● Prior to 8.3, some statistics flags need to be enabled, otherwise all numbers are 0 (zero)– stats_start_collector– stats_command_string– stats_block_level– stats_row_level

Page 21: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 21

Looking for locks?

SELECT pid, mode, current_queryFROM pg_locks, pg_stat_activityWHERE granted = false AND locktype = 'transactionid' AND pid = procpidORDER BY pid, granted;

pid | mode | current_query ------+-----------+---------------------------------------------------------- 2047 | ShareLock | update time_statistics set int_time = 3 where task_name =(1 row)

Page 22: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 22

Hit the 'L' key, enter a pid

Page 23: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 23

'L' key results

Page 24: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 24

Tested Platforms

● Linux● FreeBSD● OpenBSD● OS X● Solaris

Page 25: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 25

Some TODO items

● Fix the colors● Support connecting to remote databases● Show elapsed time of SQL statements● Show i/o statistics● Show database statistics: e.g.

connections, reads, fetches, alterations

Page 26: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 26

Thanks!

Page 27: pg_top is 'top' for PostgreSQL

2008 April 26-27 pg_top is 'top' for PostgreSQL 27

License

This work is licensed under the Creative Commons Attribution 3.0 United States

License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.

0/us/; or, (b) send a letter to Creative Commons, 171 2nd Street, Suite 300, San

Francisco, California, 94105, USA.