1 <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.35 2006/06/27 19:07:50 momjian Exp $ -->
3 <chapter id="monitoring">
4 <title>Monitoring Database Activity</title>
6 <indexterm zone="monitoring">
7 <primary>monitoring</primary>
8 <secondary>database activity</secondary>
11 <indexterm zone="monitoring">
12 <primary>database activity</primary>
13 <secondary>monitoring</secondary>
17 A database administrator frequently wonders, <quote>What is the system
18 doing right now?</quote>
19 This chapter discusses how to find that out.
23 Several tools are available for monitoring database activity and
24 analyzing performance. Most of this chapter is devoted to describing
25 <productname>PostgreSQL</productname>'s statistics collector,
26 but one should not neglect regular Unix monitoring programs such as
27 <command>ps</>, <command>top</>, <command>iostat</>, and <command>vmstat</>.
28 Also, once one has identified a
29 poorly-performing query, further investigation may be needed using
30 <productname>PostgreSQL</productname>'s <xref linkend="sql-explain"
31 endterm="sql-explain-title"> command.
32 <xref linkend="using-explain"> discusses <command>EXPLAIN</>
33 and other methods for understanding the behavior of an individual
37 <sect1 id="monitoring-ps">
38 <title>Standard Unix Tools</Title>
40 <indexterm zone="monitoring-ps">
42 <secondary>to monitor activity</secondary>
46 On most platforms, <productname>PostgreSQL</productname> modifies its
47 command title as reported by <command>ps</>, so that individual server
48 processes can readily be identified. A sample display is
51 $ ps auxww | grep ^postgres
52 postgres 960 0.0 1.1 6104 1480 pts/1 SN 13:17 0:00 postgres -i
53 postgres 963 0.0 1.1 7084 1472 pts/1 SN 13:17 0:00 postgres: stats buffer process
54 postgres 965 0.0 1.1 6152 1512 pts/1 SN 13:17 0:00 postgres: stats collector process
55 postgres 998 0.0 2.3 6532 2992 pts/1 SN 13:18 0:00 postgres: tgl runbug 127.0.0.1 idle
56 postgres 1003 0.0 2.4 6532 3128 pts/1 SN 13:19 0:00 postgres: tgl regression [local] SELECT waiting
57 postgres 1016 0.1 2.4 6532 3080 pts/1 SN 13:19 0:00 postgres: tgl regression [local] idle in transaction
60 (The appropriate invocation of <command>ps</> varies across different
61 platforms, as do the details of what is shown. This example is from a
62 recent Linux system.) The first process listed here is the
63 the master server process. The command arguments
64 shown for it are the same ones given when it was launched. The next two
65 processes implement the statistics collector, which will be described in
66 detail in the next section. (These will not be present if you have set
67 the system not to start the statistics collector.) Each of the remaining
68 processes is a server process handling one client connection. Each such
69 process sets its command line display in the form
72 postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <replaceable>activity</>
75 The user, database, and connection source host items remain the same for
76 the life of the client connection, but the activity indicator changes.
77 The activity may be <literal>idle</> (i.e., waiting for a client command),
78 <literal>idle in transaction</> (waiting for client inside a <command>BEGIN</> block),
79 or a command type name such as <literal>SELECT</>. Also,
80 <literal>waiting</> is attached if the server process is presently waiting
81 on a lock held by another server process. In the above example we can infer
82 that process 1003 is waiting for process 1016 to complete its transaction and
83 thereby release some lock or other.
88 <productname>Solaris</productname> requires special handling. You must
89 use <command>/usr/ucb/ps</command>, rather than
90 <command>/bin/ps</command>. You also must use two <option>w</option>
91 flags, not just one. In addition, your original invocation of the
92 <command>postgres</command> command must have a shorter
93 <command>ps</command> status display than that provided by each
94 server process. If you fail to do all three things, the <command>ps</>
95 output for each server process will be the original <command>postgres</>
101 <sect1 id="monitoring-stats">
102 <title>The Statistics Collector</Title>
104 <indexterm zone="monitoring-stats">
105 <primary>statistics</primary>
109 <productname>PostgreSQL</productname>'s <firstterm>statistics collector</>
110 is a subsystem that supports collection and reporting of information about
111 server activity. Presently, the collector can count accesses to tables
112 and indexes in both disk-block and individual-row terms.
116 <productname>PostgreSQL</productname> also supports determining the exact
117 command currently being executed by other server processes. This is an
118 independent facility that can be enabled or disabled whether or not
119 block-level and row-level statistics are being collected.
122 <sect2 id="monitoring-stats-setup">
123 <title>Statistics Collection Configuration</Title>
126 Since collection of statistics adds some overhead to query execution,
127 the system can be configured to collect or not collect information.
128 This is controlled by configuration parameters that are normally set in
129 <filename>postgresql.conf</>. (See <xref linkend="runtime-config"> for
130 details about setting configuration parameters.)
134 The parameter <xref linkend="guc-stats-start-collector"> must be
135 set to <literal>true</> for the statistics collector to be launched
136 at all. This is the default and recommended setting, but it may be
137 turned off if you have no interest in statistics and want to
138 squeeze out every last drop of overhead. (The savings is likely to
139 be small, however.) Note that this option cannot be changed while
140 the server is running.
144 The parameters <xref linkend="guc-stats-block-level"> and <xref
145 linkend="guc-stats-row-level"> control how much information is
146 actually sent to the collector and thus determine how much run-time
147 overhead occurs. These respectively determine whether a server
148 process tracks disk-block-level access
149 statistics and row-level access statistics and sends these to the collector.
150 Additionally, per-database transaction commit and abort statistics
151 are collected if either of these parameters are set.
155 The parameter <xref linkend="guc-stats-command-string"> enables monitoring
156 of the current command being executed by any server process.
157 The statistics collector subprocess need not be running to enable this
162 Normally these parameters are set in <filename>postgresql.conf</> so
163 that they apply to all server processes, but it is possible to turn
164 them on or off in individual sessions using the <xref
165 linkend="sql-set" endterm="sql-set-title"> command. (To prevent
166 ordinary users from hiding their activity from the administrator,
167 only superusers are allowed to change these parameters with
173 Since the parameters <varname>stats_block_level</varname>, and
174 <varname>stats_row_level</varname> default to <literal>false</>,
175 very few statistics are collected in the default
176 configuration. Enabling either of these configuration
177 variables will significantly increase the amount of useful data
178 produced by the statistics facilities, at the expense of
179 additional run-time overhead.
185 <sect2 id="monitoring-stats-views">
186 <title>Viewing Collected Statistics</Title>
189 Several predefined views, listed in <xref
190 linkend="monitoring-stats-views-table">, are available to show the results
191 of statistics collection. Alternatively, one can
192 build custom views using the underlying statistics functions.
196 When using the statistics to monitor current activity, it is important
197 to realize that the information does not update instantaneously.
198 Each individual server process transmits new block and row access counts to
199 the collector just before going idle; so a query or transaction still in
200 progress does not affect the displayed totals. Also, the collector itself
201 emits a new report at most once per <varname>PGSTAT_STAT_INTERVAL</varname>
202 milliseconds (500 unless altered while building the server). So the
203 displayed information lags behind actual activity. However, current-query
204 information collected by <varname>stats_command_string</varname> is
209 Another important point is that when a server process is asked to display
210 any of these statistics, it first fetches the most recent report emitted by
211 the collector process and then continues to use this snapshot for all
212 statistical views and functions until the end of its current transaction.
213 So the statistics will appear not to change as long as you continue the
214 current transaction. Similarly, information about the current queries of
215 all processes is collected when any such information is first requested
216 within a transaction, and the same information will be displayed throughout
218 This is a feature, not a bug, because it allows you to perform several
219 queries on the statistics and correlate the results without worrying that
220 the numbers are changing underneath you. But if you want to see new
221 results with each query, be sure to do the queries outside any transaction
225 <table id="monitoring-stats-views-table">
226 <title>Standard Statistics Views</title>
231 <entry>View Name</entry>
232 <entry>Description</entry>
238 <entry><structname>pg_stat_activity</></entry>
239 <entry>One row per server process, showing database OID, database name,
240 process <acronym>ID</>, user OID, user name, current query, time at
241 which the current query began execution, time at which the process
242 was started, and client's address and port number. The columns
243 that report data on the current query are available unless the
244 parameter <varname>stats_command_string</varname> has been
245 turned off. Furthermore, these columns are only visible if the
246 user examining the view is a superuser or the same as the user
247 owning the process being reported on.
252 <entry><structname>pg_stat_database</></entry>
253 <entry>One row per database, showing database OID, database name,
254 number of active server processes connected to that database,
255 number of transactions committed and rolled back in that database,
256 total disk blocks read, and total buffer hits (i.e., block
257 read requests avoided by finding the block already in buffer cache).
262 <entry><structname>pg_stat_all_tables</></entry>
263 <entry>For each table in the current database (including TOAST tables),
264 the table OID, schema and table name, the last time the table was
265 vacuumed by the user and the autovacuum daemon, the last time the table
266 was analyzed by the user and the autovacuum daemon, number of sequential
267 scans initiated, number of live rows fetched by sequential
268 scans, number of index scans initiated (over all indexes
269 belonging to the table), number of live rows fetched by index
271 and numbers of row insertions, updates, and deletions.</entry>
275 <entry><structname>pg_stat_sys_tables</></entry>
276 <entry>Same as <structname>pg_stat_all_tables</>, except that only
277 system tables are shown.</entry>
281 <entry><structname>pg_stat_user_tables</></entry>
282 <entry>Same as <structname>pg_stat_all_tables</>, except that only user
283 tables are shown.</entry>
287 <entry><structname>pg_stat_all_indexes</></entry>
288 <entry>For each index in the current database,
289 the table and index OID, schema, table and index name,
290 number of index scans initiated on that index, number of
291 index entries returned by index scans, and number of live table rows
292 fetched by simple index scans using that index.
297 <entry><structname>pg_stat_sys_indexes</></entry>
298 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
299 indexes on system tables are shown.</entry>
303 <entry><structname>pg_stat_user_indexes</></entry>
304 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
305 indexes on user tables are shown.</entry>
309 <entry><structname>pg_statio_all_tables</></entry>
310 <entry>For each table in the current database (including TOAST tables),
311 the table OID, schema and table name, number of disk
312 blocks read from that table, number of buffer hits, numbers of
313 disk blocks read and buffer hits in all indexes of that table,
314 numbers of disk blocks read and buffer hits from that table's
315 auxiliary TOAST table (if any), and numbers of disk blocks read
316 and buffer hits for the TOAST table's index.
321 <entry><structname>pg_statio_sys_tables</></entry>
322 <entry>Same as <structname>pg_statio_all_tables</>, except that only
323 system tables are shown.</entry>
327 <entry><structname>pg_statio_user_tables</></entry>
328 <entry>Same as <structname>pg_statio_all_tables</>, except that only
329 user tables are shown.</entry>
333 <entry><structname>pg_statio_all_indexes</></entry>
334 <entry>For each index in the current database,
335 the table and index OID, schema, table and index name,
336 numbers of disk blocks read and buffer hits in that index.
341 <entry><structname>pg_statio_sys_indexes</></entry>
342 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
343 indexes on system tables are shown.</entry>
347 <entry><structname>pg_statio_user_indexes</></entry>
348 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
349 indexes on user tables are shown.</entry>
353 <entry><structname>pg_statio_all_sequences</></entry>
354 <entry>For each sequence object in the current database,
355 the sequence OID, schema and sequence name,
356 numbers of disk blocks read and buffer hits in that sequence.
361 <entry><structname>pg_statio_sys_sequences</></entry>
362 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
363 system sequences are shown. (Presently, no system sequences are defined,
364 so this view is always empty.)</entry>
368 <entry><structname>pg_statio_user_sequences</></entry>
369 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
370 user sequences are shown.</entry>
377 The per-index statistics are particularly useful to determine which
378 indexes are being used and how effective they are.
382 Beginning in <productname>PostgreSQL</productname> 8.1, indexes can be
383 used either directly or via <quote>bitmap scans</>. In a bitmap scan
384 the output of several indexes can be combined via AND or OR rules;
385 so it is difficult to associate individual heap row fetches
386 with specific indexes when a bitmap scan is used. Therefore, a bitmap
388 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
389 count(s) for the index(es) it uses, and it increments the
390 <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
391 count for the table, but it does not affect
392 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
397 Before <productname>PostgreSQL</productname> 8.1, the
398 <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
399 were essentially always equal. Now they can be different even without
400 considering bitmap scans, because <structfield>idx_tup_read</> counts
401 index entries retrieved from the index while <structfield>idx_tup_fetch</>
402 counts live rows fetched from the table; the latter will be less if any
403 dead or not-yet-committed rows are fetched using the index.
408 The <structname>pg_statio_</> views are primarily useful to
409 determine the effectiveness of the buffer cache. When the number
410 of actual disk reads is much smaller than the number of buffer
411 hits, then the cache is satisfying most read requests without
412 invoking a kernel call. However, these statistics do not give the
413 entire story: due to the way in which <productname>PostgreSQL</>
414 handles disk I/O, data that is not in the
415 <productname>PostgreSQL</> buffer cache may still reside in the
416 kernel's I/O cache, and may therefore still be fetched without
417 requiring a physical read. Users interested in obtaining more
418 detailed information on <productname>PostgreSQL</> I/O behavior are
419 advised to use the <productname>PostgreSQL</> statistics collector
420 in combination with operating system utilities that allow insight
421 into the kernel's handling of I/O.
425 Other ways of looking at the statistics can be set up by writing
426 queries that use the same underlying statistics access functions as
427 these standard views do. These functions are listed in <xref
428 linkend="monitoring-stats-funcs-table">. The per-database access
429 functions take a database OID as argument to identify which
430 database to report on. The per-table and per-index functions take
431 a table or index OID. (Note that only tables and indexes in the
432 current database can be seen with these functions.) The
433 per-server-process access functions take a server process
434 number, which ranges from one to the number of currently active
438 <table id="monitoring-stats-funcs-table">
439 <title>Statistics Access Functions</title>
444 <entry>Function</entry>
445 <entry>Return Type</entry>
446 <entry>Description</entry>
452 <entry><literal><function>pg_stat_get_db_numbackends</function>(<type>oid</type>)</literal></entry>
453 <entry><type>integer</type></entry>
455 Number of active server processes for database
460 <entry><literal><function>pg_stat_get_db_xact_commit</function>(<type>oid</type>)</literal></entry>
461 <entry><type>bigint</type></entry>
463 Transactions committed in database
468 <entry><literal><function>pg_stat_get_db_xact_rollback</function>(<type>oid</type>)</literal></entry>
469 <entry><type>bigint</type></entry>
471 Transactions rolled back in database
476 <entry><literal><function>pg_stat_get_db_blocks_fetched</function>(<type>oid</type>)</literal></entry>
477 <entry><type>bigint</type></entry>
479 Number of disk block fetch requests for database
484 <entry><literal><function>pg_stat_get_db_blocks_hit</function>(<type>oid</type>)</literal></entry>
485 <entry><type>bigint</type></entry>
487 Number of disk block fetch requests found in cache for database
492 <entry><literal><function>pg_stat_get_numscans</function>(<type>oid</type>)</literal></entry>
493 <entry><type>bigint</type></entry>
495 Number of sequential scans done when argument is a table,
496 or number of index scans done when argument is an index
501 <entry><literal><function>pg_stat_get_tuples_returned</function>(<type>oid</type>)</literal></entry>
502 <entry><type>bigint</type></entry>
504 Number of rows read by sequential scans when argument is a table,
505 or number of index entries returned when argument is an index
510 <entry><literal><function>pg_stat_get_tuples_fetched</function>(<type>oid</type>)</literal></entry>
511 <entry><type>bigint</type></entry>
513 Number of table rows fetched by bitmap scans when argument is a table,
514 or table rows fetched by simple index scans using the index
515 when argument is an index
520 <entry><literal><function>pg_stat_get_tuples_inserted</function>(<type>oid</type>)</literal></entry>
521 <entry><type>bigint</type></entry>
523 Number of rows inserted into table
528 <entry><literal><function>pg_stat_get_tuples_updated</function>(<type>oid</type>)</literal></entry>
529 <entry><type>bigint</type></entry>
531 Number of rows updated in table
536 <entry><literal><function>pg_stat_get_tuples_deleted</function>(<type>oid</type>)</literal></entry>
537 <entry><type>bigint</type></entry>
539 Number of rows deleted from table
544 <entry><literal><function>pg_stat_get_blocks_fetched</function>(<type>oid</type>)</literal></entry>
545 <entry><type>bigint</type></entry>
547 Number of disk block fetch requests for table or index
552 <entry><literal><function>pg_stat_get_blocks_hit</function>(<type>oid</type>)</literal></entry>
553 <entry><type>bigint</type></entry>
555 Number of disk block requests found in cache for table or index
560 <entry><literal><function>pg_stat_get_last_vacuum_time</function>(<type>oid</type>)</literal></entry>
561 <entry><type>timestamptz</type></entry>
563 Time of the last vacuum initiated by the user on this table
568 <entry><literal><function>pg_stat_get_last_autovacuum_time</function>(<type>oid</type>)</literal></entry>
569 <entry><type>timestamptz</type></entry>
571 Time of the last vacuum initiated by the autovacuum daemon on this table
576 <entry><literal><function>pg_stat_get_last_analyze_time</function>(<type>oid</type>)</literal></entry>
577 <entry><type>timestamptz</type></entry>
579 Time of the last analyze initiated by the user on this table
584 <entry><literal><function>pg_stat_get_last_autoanalyze_time</function>(<type>oid</type>)</literal></entry>
585 <entry><type>timestamptz</type></entry>
587 Time of the last analyze initiated by the autovacuum daemon on this
593 <entry><literal><function>pg_stat_get_backend_idset</function>()</literal></entry>
594 <entry><type>setof integer</type></entry>
596 Set of currently active server process numbers (from 1 to the
597 number of active server processes). See usage example in the text
602 <entry><literal><function>pg_backend_pid</function>()</literal></entry>
603 <entry><type>integer</type></entry>
605 Process ID of the server process attached to the current session
610 <entry><literal><function>pg_stat_get_backend_pid</function>(<type>integer</type>)</literal></entry>
611 <entry><type>integer</type></entry>
613 Process ID of the given server process
618 <entry><literal><function>pg_stat_get_backend_dbid</function>(<type>integer</type>)</literal></entry>
619 <entry><type>oid</type></entry>
621 Database ID of the given server process
626 <entry><literal><function>pg_stat_get_backend_userid</function>(<type>integer</type>)</literal></entry>
627 <entry><type>oid</type></entry>
629 User ID of the given server process
634 <entry><literal><function>pg_stat_get_backend_activity</function>(<type>integer</type>)</literal></entry>
635 <entry><type>text</type></entry>
637 Active command of the given server process, but only if the
638 current user is a superuser or the same user as that of
639 the session being queried (and
640 <varname>stats_command_string</varname> is on)
645 <entry><literal><function>pg_stat_get_backend_activity_start</function>(<type>integer</type>)</literal></entry>
646 <entry><type>timestamp with time zone</type></entry>
648 The time at which the given server process' currently
649 executing query was started, but only if the
650 current user is a superuser or the same user as that of
651 the session being queried (and
652 <varname>stats_command_string</varname> is on)
657 <entry><literal><function>pg_stat_get_backend_start</function>(<type>integer</type>)</literal></entry>
658 <entry><type>timestamp with time zone</type></entry>
660 The time at which the given server process was started, or
661 null if the current user is not a superuser nor the same user
662 as that of the session being queried
667 <entry><literal><function>pg_stat_get_backend_client_addr</function>(<type>integer</type>)</literal></entry>
668 <entry><type>inet</type></entry>
670 The IP address of the client connected to the given
671 server process. Null if the connection is over a Unix domain
672 socket. Also null if the current user is not a superuser nor
673 the same user as that of the session being queried
678 <entry><literal><function>pg_stat_get_backend_client_port</function>(<type>integer</type>)</literal></entry>
679 <entry><type>integer</type></entry>
681 The IP port number of the client connected to the given
682 server process. -1 if the connection is over a Unix domain
683 socket. Null if the current user is not a superuser nor the
684 same user as that of the session being queried
689 <entry><literal><function>pg_stat_reset</function>()</literal></entry>
690 <entry><type>boolean</type></entry>
692 Reset all block-level and row-level statistics to zero
701 <function>blocks_fetched</function> minus
702 <function>blocks_hit</function> gives the number of kernel
703 <function>read()</> calls issued for the table, index, or
704 database; but the actual number of physical reads is usually
705 lower due to kernel-level buffering.
710 The function <function>pg_stat_get_backend_idset</function> provides
711 a convenient way to generate one row for each active server process. For
712 example, to show the <acronym>PID</>s and current queries of all server processes:
715 SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
716 pg_stat_get_backend_activity(s.backendid) AS current_query
717 FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
724 <sect1 id="monitoring-locks">
725 <title>Viewing Locks</title>
727 <indexterm zone="monitoring-locks">
728 <primary>lock</primary>
729 <secondary>monitoring</secondary>
733 Another useful tool for monitoring database activity is the
734 <structname>pg_locks</structname> system table. It allows the
735 database administrator to view information about the outstanding
736 locks in the lock manager. For example, this capability can be used
742 View all the locks currently outstanding, all the locks on
743 relations in a particular database, all the locks on a
744 particular relation, or all the locks held by a particular
745 <productname>PostgreSQL</productname> session.
751 Determine the relation in the current database with the most
752 ungranted locks (which might be a source of contention among
759 Determine the effect of lock contention on overall database
760 performance, as well as the extent to which contention varies
761 with overall database traffic.
766 Details of the <structname>pg_locks</structname> view appear in
767 <xref linkend="view-pg-locks">.
768 For more information on locking and managing concurrency with
769 <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
774 <!-- Keep this comment at the end of the file
779 sgml-minimize-attributes:nil
780 sgml-always-quote-attributes:t
783 sgml-parent-document:nil
784 sgml-default-dtd-file:"./reference.ced"
785 sgml-exposed-tags:nil
786 sgml-local-catalogs:("/usr/lib/sgml/catalog")
787 sgml-local-ecat-files:nil