]> granicus.if.org Git - postgresql/blob - doc/src/sgml/monitoring.sgml
Default stats_command_string to 'on', now that its overhead is minimal.
[postgresql] / doc / src / sgml / monitoring.sgml
1 <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.35 2006/06/27 19:07:50 momjian Exp $ -->
2
3 <chapter id="monitoring">
4  <title>Monitoring Database Activity</title>
5
6  <indexterm zone="monitoring">
7   <primary>monitoring</primary>
8   <secondary>database activity</secondary>
9  </indexterm>
10
11  <indexterm zone="monitoring">
12   <primary>database activity</primary>
13   <secondary>monitoring</secondary>
14  </indexterm>
15
16  <para>
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.
20  </para>
21
22   <para>
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
34    query.
35   </para>
36
37  <sect1 id="monitoring-ps">
38   <title>Standard Unix Tools</Title>
39
40   <indexterm zone="monitoring-ps">
41    <primary>ps</primary>
42    <secondary>to monitor activity</secondary>
43   </indexterm>
44
45   <para>
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
49
50 <screen>
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
58 </screen>
59
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
70
71 <screen>
72 postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <replaceable>activity</>
73 </screen>
74
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.
84   </para>
85
86   <tip>
87   <para>
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</>
96   command line.
97   </para>
98   </tip>
99  </sect1>
100
101  <sect1 id="monitoring-stats">
102   <title>The Statistics Collector</Title>
103
104   <indexterm zone="monitoring-stats">
105    <primary>statistics</primary>
106   </indexterm>
107
108   <para>
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.
113   </para>
114
115   <para>
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.
120   </para>
121
122  <sect2 id="monitoring-stats-setup">
123   <title>Statistics Collection Configuration</Title>
124
125   <para>
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.)
131   </para>
132
133   <para>
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.
141   </para>
142
143   <para>
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.
152   </para>
153
154   <para>
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
158    feature.
159   </para>
160   
161   <para>
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
168    <command>SET</>.)
169   </para>
170
171    <note>
172     <para>
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.
180     </para>
181    </note>
182
183  </sect2>
184
185  <sect2 id="monitoring-stats-views">
186   <title>Viewing Collected Statistics</Title>
187
188   <para>
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.
193   </para>
194
195   <para>
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
205    always up-to-date.
206   </para>
207
208   <para>
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
217    the transaction.
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
222    block.
223   </para>
224
225   <table id="monitoring-stats-views-table">
226    <title>Standard Statistics Views</title>
227
228    <tgroup cols="2">
229     <thead>
230      <row>
231       <entry>View Name</entry>
232       <entry>Description</entry>
233      </row>
234     </thead>
235
236     <tbody>
237      <row>
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.
248      </entry>
249      </row>
250
251      <row>
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).
258      </entry>
259      </row>
260
261      <row>
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
270       scans,
271       and numbers of row insertions, updates, and deletions.</entry>
272      </row>
273
274      <row>
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>
278      </row>
279
280      <row>
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>
284      </row>
285
286      <row>
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.
293       </entry>
294      </row>
295
296      <row>
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>
300      </row>
301
302      <row>
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>
306      </row>
307
308      <row>
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.
317       </entry>
318      </row>
319
320      <row>
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>
324      </row>
325
326      <row>
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>
330      </row>
331
332      <row>
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.
337       </entry>
338      </row>
339
340      <row>
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>
344      </row>
345
346      <row>
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>
350      </row>
351
352      <row>
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.
357       </entry>
358      </row>
359
360      <row>
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>
365      </row>
366
367      <row>
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>
371      </row>
372     </tbody>
373    </tgroup>
374   </table>
375
376   <para>
377    The per-index statistics are particularly useful to determine which
378    indexes are being used and how effective they are.
379   </para>
380
381   <para>
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
387    scan increments the
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</>.
393   </para>
394
395   <note>
396    <para>
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.
404    </para>
405   </note>
406
407   <para>
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.
422   </para>
423
424   <para>
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
435    server processes.
436   </para>
437
438   <table id="monitoring-stats-funcs-table">
439    <title>Statistics Access Functions</title>
440
441    <tgroup cols="3">
442     <thead>
443      <row>
444       <entry>Function</entry>
445       <entry>Return Type</entry>
446       <entry>Description</entry>
447      </row>
448     </thead>
449
450     <tbody>
451      <row>
452       <entry><literal><function>pg_stat_get_db_numbackends</function>(<type>oid</type>)</literal></entry>
453       <entry><type>integer</type></entry>
454       <entry>
455        Number of active server processes for database
456       </entry>
457      </row>
458
459      <row>
460       <entry><literal><function>pg_stat_get_db_xact_commit</function>(<type>oid</type>)</literal></entry>
461       <entry><type>bigint</type></entry>
462       <entry>
463        Transactions committed in database
464       </entry>
465      </row>
466
467      <row>
468       <entry><literal><function>pg_stat_get_db_xact_rollback</function>(<type>oid</type>)</literal></entry>
469       <entry><type>bigint</type></entry>
470       <entry>
471        Transactions rolled back in database
472       </entry>
473      </row>
474
475      <row>
476       <entry><literal><function>pg_stat_get_db_blocks_fetched</function>(<type>oid</type>)</literal></entry>
477       <entry><type>bigint</type></entry>
478       <entry>
479        Number of disk block fetch requests for database
480       </entry>
481      </row>
482
483      <row>
484       <entry><literal><function>pg_stat_get_db_blocks_hit</function>(<type>oid</type>)</literal></entry>
485       <entry><type>bigint</type></entry>
486       <entry>
487        Number of disk block fetch requests found in cache for database
488       </entry>
489      </row>
490
491      <row>
492       <entry><literal><function>pg_stat_get_numscans</function>(<type>oid</type>)</literal></entry>
493       <entry><type>bigint</type></entry>
494       <entry>
495        Number of sequential scans done when argument is a table,
496        or number of index scans done when argument is an index
497       </entry>
498      </row>
499
500      <row>
501       <entry><literal><function>pg_stat_get_tuples_returned</function>(<type>oid</type>)</literal></entry>
502       <entry><type>bigint</type></entry>
503       <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
506       </entry>
507      </row>
508
509      <row>
510       <entry><literal><function>pg_stat_get_tuples_fetched</function>(<type>oid</type>)</literal></entry>
511       <entry><type>bigint</type></entry>
512       <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
516       </entry>
517      </row>
518
519      <row>
520       <entry><literal><function>pg_stat_get_tuples_inserted</function>(<type>oid</type>)</literal></entry>
521       <entry><type>bigint</type></entry>
522       <entry>
523        Number of rows inserted into table
524       </entry>
525      </row>
526
527      <row>
528       <entry><literal><function>pg_stat_get_tuples_updated</function>(<type>oid</type>)</literal></entry>
529       <entry><type>bigint</type></entry>
530       <entry>
531        Number of rows updated in table
532       </entry>
533      </row>
534
535      <row>
536       <entry><literal><function>pg_stat_get_tuples_deleted</function>(<type>oid</type>)</literal></entry>
537       <entry><type>bigint</type></entry>
538       <entry>
539        Number of rows deleted from table
540       </entry>
541      </row>
542
543      <row>
544       <entry><literal><function>pg_stat_get_blocks_fetched</function>(<type>oid</type>)</literal></entry>
545       <entry><type>bigint</type></entry>
546       <entry>
547        Number of disk block fetch requests for table or index
548       </entry>
549      </row>
550
551      <row>
552       <entry><literal><function>pg_stat_get_blocks_hit</function>(<type>oid</type>)</literal></entry>
553       <entry><type>bigint</type></entry>
554       <entry>
555        Number of disk block requests found in cache for table or index
556       </entry>
557      </row>
558
559      <row>
560       <entry><literal><function>pg_stat_get_last_vacuum_time</function>(<type>oid</type>)</literal></entry>
561       <entry><type>timestamptz</type></entry>
562       <entry>
563        Time of the last vacuum initiated by the user on this table
564       </entry>
565      </row>
566
567      <row>
568       <entry><literal><function>pg_stat_get_last_autovacuum_time</function>(<type>oid</type>)</literal></entry>
569       <entry><type>timestamptz</type></entry>
570       <entry>
571        Time of the last vacuum initiated by the autovacuum daemon on this table
572       </entry>
573      </row>
574
575      <row>
576       <entry><literal><function>pg_stat_get_last_analyze_time</function>(<type>oid</type>)</literal></entry>
577       <entry><type>timestamptz</type></entry>
578       <entry>
579        Time of the last analyze initiated by the user on this table
580       </entry>
581      </row>
582
583      <row>
584       <entry><literal><function>pg_stat_get_last_autoanalyze_time</function>(<type>oid</type>)</literal></entry>
585       <entry><type>timestamptz</type></entry>
586       <entry>
587        Time of the last analyze initiated by the autovacuum daemon on this
588        table
589       </entry>
590      </row>
591
592      <row>
593       <entry><literal><function>pg_stat_get_backend_idset</function>()</literal></entry>
594       <entry><type>setof integer</type></entry>
595       <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
598       </entry>
599      </row>
600
601      <row>
602       <entry><literal><function>pg_backend_pid</function>()</literal></entry>
603       <entry><type>integer</type></entry>
604       <entry>
605        Process ID of the server process attached to the current session
606       </entry>
607      </row>
608
609      <row>
610       <entry><literal><function>pg_stat_get_backend_pid</function>(<type>integer</type>)</literal></entry>
611       <entry><type>integer</type></entry>
612       <entry>
613        Process ID of the given server process
614       </entry>
615      </row>
616
617      <row>
618       <entry><literal><function>pg_stat_get_backend_dbid</function>(<type>integer</type>)</literal></entry>
619       <entry><type>oid</type></entry>
620       <entry>
621        Database ID of the given server process
622       </entry>
623      </row>
624
625      <row>
626       <entry><literal><function>pg_stat_get_backend_userid</function>(<type>integer</type>)</literal></entry>
627       <entry><type>oid</type></entry>
628       <entry>
629        User ID of the given server process
630       </entry>
631      </row>
632
633      <row>
634       <entry><literal><function>pg_stat_get_backend_activity</function>(<type>integer</type>)</literal></entry>
635       <entry><type>text</type></entry>
636       <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)
641       </entry>
642      </row>
643
644      <row>
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>
647       <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)
653       </entry>
654      </row>
655
656      <row>
657       <entry><literal><function>pg_stat_get_backend_start</function>(<type>integer</type>)</literal></entry>
658       <entry><type>timestamp with time zone</type></entry>
659       <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
663       </entry>
664      </row>
665
666      <row>
667       <entry><literal><function>pg_stat_get_backend_client_addr</function>(<type>integer</type>)</literal></entry>
668       <entry><type>inet</type></entry>
669       <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
674       </entry>
675      </row>
676
677      <row>
678       <entry><literal><function>pg_stat_get_backend_client_port</function>(<type>integer</type>)</literal></entry>
679       <entry><type>integer</type></entry>
680       <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
685       </entry>
686      </row>
687
688      <row>
689       <entry><literal><function>pg_stat_reset</function>()</literal></entry>
690       <entry><type>boolean</type></entry>
691       <entry>
692        Reset all block-level and row-level statistics to zero
693       </entry>
694      </row>
695     </tbody>
696    </tgroup>
697   </table>
698
699    <note>
700     <para>
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.
706     </para>
707    </note>
708
709   <para>
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:
713
714 <programlisting>
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;
718 </programlisting>
719   </para>
720
721  </sect2>
722  </sect1>
723
724  <sect1 id="monitoring-locks">
725   <title>Viewing Locks</title>
726
727   <indexterm zone="monitoring-locks">
728    <primary>lock</primary>
729    <secondary>monitoring</secondary>
730   </indexterm>
731
732   <para>
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
737    to:
738
739    <itemizedlist>
740     <listitem>
741      <para>
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.
746      </para>
747     </listitem>
748
749     <listitem>
750      <para>
751       Determine the relation in the current database with the most
752       ungranted locks (which might be a source of contention among
753       database clients).
754      </para>
755     </listitem>
756
757     <listitem>
758      <para>
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.
762      </para>
763     </listitem>
764    </itemizedlist>
765
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">.
770   </para>
771  </sect1>
772 </chapter>
773
774 <!-- Keep this comment at the end of the file
775 Local variables:
776 mode:sgml
777 sgml-omittag:nil
778 sgml-shorttag:t
779 sgml-minimize-attributes:nil
780 sgml-always-quote-attributes:t
781 sgml-indent-step:1
782 sgml-indent-data: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
788 End:
789 -->