1 <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.34 2006/06/19 01:51:21 tgl 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_command_string</varname>,
174 <varname>stats_block_level</varname>, and
175 <varname>stats_row_level</varname> default to <literal>false</>,
176 very few statistics are collected in the default
177 configuration. Enabling one or more of these configuration
178 variables will significantly enhance the amount of useful data
179 produced by the statistics facilities, at the expense of
180 additional run-time overhead.
186 <sect2 id="monitoring-stats-views">
187 <title>Viewing Collected Statistics</Title>
190 Several predefined views, listed in <xref
191 linkend="monitoring-stats-views-table">, are available to show the results
192 of statistics collection. Alternatively, one can
193 build custom views using the underlying statistics functions.
197 When using the statistics to monitor current activity, it is important
198 to realize that the information does not update instantaneously.
199 Each individual server process transmits new block and row access counts to
200 the collector just before going idle; so a query or transaction still in
201 progress does not affect the displayed totals. Also, the collector itself
202 emits a new report at most once per <varname>PGSTAT_STAT_INTERVAL</varname>
203 milliseconds (500 unless altered while building the server). So the
204 displayed information lags behind actual activity. However, current-query
205 information collected by <varname>stats_command_string</varname> is
210 Another important point is that when a server process is asked to display
211 any of these statistics, it first fetches the most recent report emitted by
212 the collector process and then continues to use this snapshot for all
213 statistical views and functions until the end of its current transaction.
214 So the statistics will appear not to change as long as you continue the
215 current transaction. Similarly, information about the current queries of
216 all processes is collected when any such information is first requested
217 within a transaction, and the same information will be displayed throughout
219 This is a feature, not a bug, because it allows you to perform several
220 queries on the statistics and correlate the results without worrying that
221 the numbers are changing underneath you. But if you want to see new
222 results with each query, be sure to do the queries outside any transaction
226 <table id="monitoring-stats-views-table">
227 <title>Standard Statistics Views</title>
232 <entry>View Name</entry>
233 <entry>Description</entry>
239 <entry><structname>pg_stat_activity</></entry>
240 <entry>One row per server process, showing database OID, database name,
241 process <acronym>ID</>, user OID, user name, current query, time at
242 which the current query began execution, time at which the process
243 was started, and client's address and port number. The columns
244 that report data on the current query are only available if the
245 parameter <varname>stats_command_string</varname> has been
246 turned on. Furthermore, these columns read as null unless the
247 user examining the view is a superuser or the same as the user
248 owning the process being reported on.
253 <entry><structname>pg_stat_database</></entry>
254 <entry>One row per database, showing database OID, database name,
255 number of active server processes connected to that database,
256 number of transactions committed and rolled back in that database,
257 total disk blocks read, and total buffer hits (i.e., block
258 read requests avoided by finding the block already in buffer cache).
263 <entry><structname>pg_stat_all_tables</></entry>
264 <entry>For each table in the current database (including TOAST tables),
265 the table OID, schema and table name, the last time the table was
266 vacuumed by the user and the autovacuum daemon, the last time the table
267 was analyzed by the user and the autovacuum daemon, number of sequential
268 scans initiated, number of live rows fetched by sequential
269 scans, number of index scans initiated (over all indexes
270 belonging to the table), number of live rows fetched by index
272 and numbers of row insertions, updates, and deletions.</entry>
276 <entry><structname>pg_stat_sys_tables</></entry>
277 <entry>Same as <structname>pg_stat_all_tables</>, except that only
278 system tables are shown.</entry>
282 <entry><structname>pg_stat_user_tables</></entry>
283 <entry>Same as <structname>pg_stat_all_tables</>, except that only user
284 tables are shown.</entry>
288 <entry><structname>pg_stat_all_indexes</></entry>
289 <entry>For each index in the current database,
290 the table and index OID, schema, table and index name,
291 number of index scans initiated on that index, number of
292 index entries returned by index scans, and number of live table rows
293 fetched by simple index scans using that index.
298 <entry><structname>pg_stat_sys_indexes</></entry>
299 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
300 indexes on system tables are shown.</entry>
304 <entry><structname>pg_stat_user_indexes</></entry>
305 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
306 indexes on user tables are shown.</entry>
310 <entry><structname>pg_statio_all_tables</></entry>
311 <entry>For each table in the current database (including TOAST tables),
312 the table OID, schema and table name, number of disk
313 blocks read from that table, number of buffer hits, numbers of
314 disk blocks read and buffer hits in all indexes of that table,
315 numbers of disk blocks read and buffer hits from that table's
316 auxiliary TOAST table (if any), and numbers of disk blocks read
317 and buffer hits for the TOAST table's index.
322 <entry><structname>pg_statio_sys_tables</></entry>
323 <entry>Same as <structname>pg_statio_all_tables</>, except that only
324 system tables are shown.</entry>
328 <entry><structname>pg_statio_user_tables</></entry>
329 <entry>Same as <structname>pg_statio_all_tables</>, except that only
330 user tables are shown.</entry>
334 <entry><structname>pg_statio_all_indexes</></entry>
335 <entry>For each index in the current database,
336 the table and index OID, schema, table and index name,
337 numbers of disk blocks read and buffer hits in that index.
342 <entry><structname>pg_statio_sys_indexes</></entry>
343 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
344 indexes on system tables are shown.</entry>
348 <entry><structname>pg_statio_user_indexes</></entry>
349 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
350 indexes on user tables are shown.</entry>
354 <entry><structname>pg_statio_all_sequences</></entry>
355 <entry>For each sequence object in the current database,
356 the sequence OID, schema and sequence name,
357 numbers of disk blocks read and buffer hits in that sequence.
362 <entry><structname>pg_statio_sys_sequences</></entry>
363 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
364 system sequences are shown. (Presently, no system sequences are defined,
365 so this view is always empty.)</entry>
369 <entry><structname>pg_statio_user_sequences</></entry>
370 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
371 user sequences are shown.</entry>
378 The per-index statistics are particularly useful to determine which
379 indexes are being used and how effective they are.
383 Beginning in <productname>PostgreSQL</productname> 8.1, indexes can be
384 used either directly or via <quote>bitmap scans</>. In a bitmap scan
385 the output of several indexes can be combined via AND or OR rules;
386 so it is difficult to associate individual heap row fetches
387 with specific indexes when a bitmap scan is used. Therefore, a bitmap
389 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
390 count(s) for the index(es) it uses, and it increments the
391 <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
392 count for the table, but it does not affect
393 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
398 Before <productname>PostgreSQL</productname> 8.1, the
399 <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
400 were essentially always equal. Now they can be different even without
401 considering bitmap scans, because <structfield>idx_tup_read</> counts
402 index entries retrieved from the index while <structfield>idx_tup_fetch</>
403 counts live rows fetched from the table; the latter will be less if any
404 dead or not-yet-committed rows are fetched using the index.
409 The <structname>pg_statio_</> views are primarily useful to
410 determine the effectiveness of the buffer cache. When the number
411 of actual disk reads is much smaller than the number of buffer
412 hits, then the cache is satisfying most read requests without
413 invoking a kernel call. However, these statistics do not give the
414 entire story: due to the way in which <productname>PostgreSQL</>
415 handles disk I/O, data that is not in the
416 <productname>PostgreSQL</> buffer cache may still reside in the
417 kernel's I/O cache, and may therefore still be fetched without
418 requiring a physical read. Users interested in obtaining more
419 detailed information on <productname>PostgreSQL</> I/O behavior are
420 advised to use the <productname>PostgreSQL</> statistics collector
421 in combination with operating system utilities that allow insight
422 into the kernel's handling of I/O.
426 Other ways of looking at the statistics can be set up by writing
427 queries that use the same underlying statistics access functions as
428 these standard views do. These functions are listed in <xref
429 linkend="monitoring-stats-funcs-table">. The per-database access
430 functions take a database OID as argument to identify which
431 database to report on. The per-table and per-index functions take
432 a table or index OID. (Note that only tables and indexes in the
433 current database can be seen with these functions.) The
434 per-server-process access functions take a server process
435 number, which ranges from one to the number of currently active
439 <table id="monitoring-stats-funcs-table">
440 <title>Statistics Access Functions</title>
445 <entry>Function</entry>
446 <entry>Return Type</entry>
447 <entry>Description</entry>
453 <entry><literal><function>pg_stat_get_db_numbackends</function>(<type>oid</type>)</literal></entry>
454 <entry><type>integer</type></entry>
456 Number of active server processes for database
461 <entry><literal><function>pg_stat_get_db_xact_commit</function>(<type>oid</type>)</literal></entry>
462 <entry><type>bigint</type></entry>
464 Transactions committed in database
469 <entry><literal><function>pg_stat_get_db_xact_rollback</function>(<type>oid</type>)</literal></entry>
470 <entry><type>bigint</type></entry>
472 Transactions rolled back in database
477 <entry><literal><function>pg_stat_get_db_blocks_fetched</function>(<type>oid</type>)</literal></entry>
478 <entry><type>bigint</type></entry>
480 Number of disk block fetch requests for database
485 <entry><literal><function>pg_stat_get_db_blocks_hit</function>(<type>oid</type>)</literal></entry>
486 <entry><type>bigint</type></entry>
488 Number of disk block fetch requests found in cache for database
493 <entry><literal><function>pg_stat_get_numscans</function>(<type>oid</type>)</literal></entry>
494 <entry><type>bigint</type></entry>
496 Number of sequential scans done when argument is a table,
497 or number of index scans done when argument is an index
502 <entry><literal><function>pg_stat_get_tuples_returned</function>(<type>oid</type>)</literal></entry>
503 <entry><type>bigint</type></entry>
505 Number of rows read by sequential scans when argument is a table,
506 or number of index entries returned when argument is an index
511 <entry><literal><function>pg_stat_get_tuples_fetched</function>(<type>oid</type>)</literal></entry>
512 <entry><type>bigint</type></entry>
514 Number of table rows fetched by bitmap scans when argument is a table,
515 or table rows fetched by simple index scans using the index
516 when argument is an index
521 <entry><literal><function>pg_stat_get_tuples_inserted</function>(<type>oid</type>)</literal></entry>
522 <entry><type>bigint</type></entry>
524 Number of rows inserted into table
529 <entry><literal><function>pg_stat_get_tuples_updated</function>(<type>oid</type>)</literal></entry>
530 <entry><type>bigint</type></entry>
532 Number of rows updated in table
537 <entry><literal><function>pg_stat_get_tuples_deleted</function>(<type>oid</type>)</literal></entry>
538 <entry><type>bigint</type></entry>
540 Number of rows deleted from table
545 <entry><literal><function>pg_stat_get_blocks_fetched</function>(<type>oid</type>)</literal></entry>
546 <entry><type>bigint</type></entry>
548 Number of disk block fetch requests for table or index
553 <entry><literal><function>pg_stat_get_blocks_hit</function>(<type>oid</type>)</literal></entry>
554 <entry><type>bigint</type></entry>
556 Number of disk block requests found in cache for table or index
561 <entry><literal><function>pg_stat_get_last_vacuum_time</function>(<type>oid</type>)</literal></entry>
562 <entry><type>timestamptz</type></entry>
564 Time of the last vacuum initiated by the user on this table
569 <entry><literal><function>pg_stat_get_last_autovacuum_time</function>(<type>oid</type>)</literal></entry>
570 <entry><type>timestamptz</type></entry>
572 Time of the last vacuum initiated by the autovacuum daemon on this table
577 <entry><literal><function>pg_stat_get_last_analyze_time</function>(<type>oid</type>)</literal></entry>
578 <entry><type>timestamptz</type></entry>
580 Time of the last analyze initiated by the user on this table
585 <entry><literal><function>pg_stat_get_last_autoanalyze_time</function>(<type>oid</type>)</literal></entry>
586 <entry><type>timestamptz</type></entry>
588 Time of the last analyze initiated by the autovacuum daemon on this
594 <entry><literal><function>pg_stat_get_backend_idset</function>()</literal></entry>
595 <entry><type>setof integer</type></entry>
597 Set of currently active server process numbers (from 1 to the
598 number of active server processes). See usage example in the text
603 <entry><literal><function>pg_backend_pid</function>()</literal></entry>
604 <entry><type>integer</type></entry>
606 Process ID of the server process attached to the current session
611 <entry><literal><function>pg_stat_get_backend_pid</function>(<type>integer</type>)</literal></entry>
612 <entry><type>integer</type></entry>
614 Process ID of the given server process
619 <entry><literal><function>pg_stat_get_backend_dbid</function>(<type>integer</type>)</literal></entry>
620 <entry><type>oid</type></entry>
622 Database ID of the given server process
627 <entry><literal><function>pg_stat_get_backend_userid</function>(<type>integer</type>)</literal></entry>
628 <entry><type>oid</type></entry>
630 User ID of the given server process
635 <entry><literal><function>pg_stat_get_backend_activity</function>(<type>integer</type>)</literal></entry>
636 <entry><type>text</type></entry>
638 Active command of the given server process (null if the
639 current user is not a superuser nor the same user as that of
640 the session being queried, or
641 <varname>stats_command_string</varname> is not on)
646 <entry><literal><function>pg_stat_get_backend_activity_start</function>(<type>integer</type>)</literal></entry>
647 <entry><type>timestamp with time zone</type></entry>
649 The time at which the given server process' currently
650 executing query was started (null if the
651 current user is not a superuser nor the same user as that of
652 the session being queried, or
653 <varname>stats_command_string</varname> is not on)
658 <entry><literal><function>pg_stat_get_backend_start</function>(<type>integer</type>)</literal></entry>
659 <entry><type>timestamp with time zone</type></entry>
661 The time at which the given server process was started, or
662 null if the current user is not a superuser nor the same user
663 as that of the session being queried
668 <entry><literal><function>pg_stat_get_backend_client_addr</function>(<type>integer</type>)</literal></entry>
669 <entry><type>inet</type></entry>
671 The IP address of the client connected to the given
672 server process. Null if the connection is over a Unix domain
673 socket. Also null if the current user is not a superuser nor
674 the same user as that of the session being queried
679 <entry><literal><function>pg_stat_get_backend_client_port</function>(<type>integer</type>)</literal></entry>
680 <entry><type>integer</type></entry>
682 The IP port number of the client connected to the given
683 server process. -1 if the connection is over a Unix domain
684 socket. Null if the current user is not a superuser nor the
685 same user as that of the session being queried
690 <entry><literal><function>pg_stat_reset</function>()</literal></entry>
691 <entry><type>boolean</type></entry>
693 Reset all block-level and row-level statistics to zero
702 <function>blocks_fetched</function> minus
703 <function>blocks_hit</function> gives the number of kernel
704 <function>read()</> calls issued for the table, index, or
705 database; but the actual number of physical reads is usually
706 lower due to kernel-level buffering.
711 The function <function>pg_stat_get_backend_idset</function> provides
712 a convenient way to generate one row for each active server process. For
713 example, to show the <acronym>PID</>s and current queries of all server processes:
716 SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
717 pg_stat_get_backend_activity(s.backendid) AS current_query
718 FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
725 <sect1 id="monitoring-locks">
726 <title>Viewing Locks</title>
728 <indexterm zone="monitoring-locks">
729 <primary>lock</primary>
730 <secondary>monitoring</secondary>
734 Another useful tool for monitoring database activity is the
735 <structname>pg_locks</structname> system table. It allows the
736 database administrator to view information about the outstanding
737 locks in the lock manager. For example, this capability can be used
743 View all the locks currently outstanding, all the locks on
744 relations in a particular database, all the locks on a
745 particular relation, or all the locks held by a particular
746 <productname>PostgreSQL</productname> session.
752 Determine the relation in the current database with the most
753 ungranted locks (which might be a source of contention among
760 Determine the effect of lock contention on overall database
761 performance, as well as the extent to which contention varies
762 with overall database traffic.
767 Details of the <structname>pg_locks</structname> view appear in
768 <xref linkend="view-pg-locks">.
769 For more information on locking and managing concurrency with
770 <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
775 <!-- Keep this comment at the end of the file
780 sgml-minimize-attributes:nil
781 sgml-always-quote-attributes:t
784 sgml-parent-document:nil
785 sgml-default-dtd-file:"./reference.ced"
786 sgml-exposed-tags:nil
787 sgml-local-catalogs:("/usr/lib/sgml/catalog")
788 sgml-local-ecat-files:nil