]> granicus.if.org Git - postgresql/blob - doc/src/sgml/monitoring.sgml
54c6b4f973ab1ffe2c9e3537205164b61b110755
[postgresql] / doc / src / sgml / monitoring.sgml
1 <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.34 2006/06/19 01:51:21 tgl 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_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.
181     </para>
182    </note>
183
184  </sect2>
185
186  <sect2 id="monitoring-stats-views">
187   <title>Viewing Collected Statistics</Title>
188
189   <para>
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.
194   </para>
195
196   <para>
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
206    always up-to-date.
207   </para>
208
209   <para>
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
218    the transaction.
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
223    block.
224   </para>
225
226   <table id="monitoring-stats-views-table">
227    <title>Standard Statistics Views</title>
228
229    <tgroup cols="2">
230     <thead>
231      <row>
232       <entry>View Name</entry>
233       <entry>Description</entry>
234      </row>
235     </thead>
236
237     <tbody>
238      <row>
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.
249      </entry>
250      </row>
251
252      <row>
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).
259      </entry>
260      </row>
261
262      <row>
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
271       scans,
272       and numbers of row insertions, updates, and deletions.</entry>
273      </row>
274
275      <row>
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>
279      </row>
280
281      <row>
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>
285      </row>
286
287      <row>
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.
294       </entry>
295      </row>
296
297      <row>
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>
301      </row>
302
303      <row>
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>
307      </row>
308
309      <row>
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.
318       </entry>
319      </row>
320
321      <row>
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>
325      </row>
326
327      <row>
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>
331      </row>
332
333      <row>
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.
338       </entry>
339      </row>
340
341      <row>
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>
345      </row>
346
347      <row>
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>
351      </row>
352
353      <row>
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.
358       </entry>
359      </row>
360
361      <row>
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>
366      </row>
367
368      <row>
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>
372      </row>
373     </tbody>
374    </tgroup>
375   </table>
376
377   <para>
378    The per-index statistics are particularly useful to determine which
379    indexes are being used and how effective they are.
380   </para>
381
382   <para>
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
388    scan increments the
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</>.
394   </para>
395
396   <note>
397    <para>
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.
405    </para>
406   </note>
407
408   <para>
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.
423   </para>
424
425   <para>
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
436    server processes.
437   </para>
438
439   <table id="monitoring-stats-funcs-table">
440    <title>Statistics Access Functions</title>
441
442    <tgroup cols="3">
443     <thead>
444      <row>
445       <entry>Function</entry>
446       <entry>Return Type</entry>
447       <entry>Description</entry>
448      </row>
449     </thead>
450
451     <tbody>
452      <row>
453       <entry><literal><function>pg_stat_get_db_numbackends</function>(<type>oid</type>)</literal></entry>
454       <entry><type>integer</type></entry>
455       <entry>
456        Number of active server processes for database
457       </entry>
458      </row>
459
460      <row>
461       <entry><literal><function>pg_stat_get_db_xact_commit</function>(<type>oid</type>)</literal></entry>
462       <entry><type>bigint</type></entry>
463       <entry>
464        Transactions committed in database
465       </entry>
466      </row>
467
468      <row>
469       <entry><literal><function>pg_stat_get_db_xact_rollback</function>(<type>oid</type>)</literal></entry>
470       <entry><type>bigint</type></entry>
471       <entry>
472        Transactions rolled back in database
473       </entry>
474      </row>
475
476      <row>
477       <entry><literal><function>pg_stat_get_db_blocks_fetched</function>(<type>oid</type>)</literal></entry>
478       <entry><type>bigint</type></entry>
479       <entry>
480        Number of disk block fetch requests for database
481       </entry>
482      </row>
483
484      <row>
485       <entry><literal><function>pg_stat_get_db_blocks_hit</function>(<type>oid</type>)</literal></entry>
486       <entry><type>bigint</type></entry>
487       <entry>
488        Number of disk block fetch requests found in cache for database
489       </entry>
490      </row>
491
492      <row>
493       <entry><literal><function>pg_stat_get_numscans</function>(<type>oid</type>)</literal></entry>
494       <entry><type>bigint</type></entry>
495       <entry>
496        Number of sequential scans done when argument is a table,
497        or number of index scans done when argument is an index
498       </entry>
499      </row>
500
501      <row>
502       <entry><literal><function>pg_stat_get_tuples_returned</function>(<type>oid</type>)</literal></entry>
503       <entry><type>bigint</type></entry>
504       <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
507       </entry>
508      </row>
509
510      <row>
511       <entry><literal><function>pg_stat_get_tuples_fetched</function>(<type>oid</type>)</literal></entry>
512       <entry><type>bigint</type></entry>
513       <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
517       </entry>
518      </row>
519
520      <row>
521       <entry><literal><function>pg_stat_get_tuples_inserted</function>(<type>oid</type>)</literal></entry>
522       <entry><type>bigint</type></entry>
523       <entry>
524        Number of rows inserted into table
525       </entry>
526      </row>
527
528      <row>
529       <entry><literal><function>pg_stat_get_tuples_updated</function>(<type>oid</type>)</literal></entry>
530       <entry><type>bigint</type></entry>
531       <entry>
532        Number of rows updated in table
533       </entry>
534      </row>
535
536      <row>
537       <entry><literal><function>pg_stat_get_tuples_deleted</function>(<type>oid</type>)</literal></entry>
538       <entry><type>bigint</type></entry>
539       <entry>
540        Number of rows deleted from table
541       </entry>
542      </row>
543
544      <row>
545       <entry><literal><function>pg_stat_get_blocks_fetched</function>(<type>oid</type>)</literal></entry>
546       <entry><type>bigint</type></entry>
547       <entry>
548        Number of disk block fetch requests for table or index
549       </entry>
550      </row>
551
552      <row>
553       <entry><literal><function>pg_stat_get_blocks_hit</function>(<type>oid</type>)</literal></entry>
554       <entry><type>bigint</type></entry>
555       <entry>
556        Number of disk block requests found in cache for table or index
557       </entry>
558      </row>
559
560      <row>
561       <entry><literal><function>pg_stat_get_last_vacuum_time</function>(<type>oid</type>)</literal></entry>
562       <entry><type>timestamptz</type></entry>
563       <entry>
564        Time of the last vacuum initiated by the user on this table
565       </entry>
566      </row>
567
568      <row>
569       <entry><literal><function>pg_stat_get_last_autovacuum_time</function>(<type>oid</type>)</literal></entry>
570       <entry><type>timestamptz</type></entry>
571       <entry>
572        Time of the last vacuum initiated by the autovacuum daemon on this table
573       </entry>
574      </row>
575
576      <row>
577       <entry><literal><function>pg_stat_get_last_analyze_time</function>(<type>oid</type>)</literal></entry>
578       <entry><type>timestamptz</type></entry>
579       <entry>
580        Time of the last analyze initiated by the user on this table
581       </entry>
582      </row>
583
584      <row>
585       <entry><literal><function>pg_stat_get_last_autoanalyze_time</function>(<type>oid</type>)</literal></entry>
586       <entry><type>timestamptz</type></entry>
587       <entry>
588        Time of the last analyze initiated by the autovacuum daemon on this
589        table
590       </entry>
591      </row>
592
593      <row>
594       <entry><literal><function>pg_stat_get_backend_idset</function>()</literal></entry>
595       <entry><type>setof integer</type></entry>
596       <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
599       </entry>
600      </row>
601
602      <row>
603       <entry><literal><function>pg_backend_pid</function>()</literal></entry>
604       <entry><type>integer</type></entry>
605       <entry>
606        Process ID of the server process attached to the current session
607       </entry>
608      </row>
609
610      <row>
611       <entry><literal><function>pg_stat_get_backend_pid</function>(<type>integer</type>)</literal></entry>
612       <entry><type>integer</type></entry>
613       <entry>
614        Process ID of the given server process
615       </entry>
616      </row>
617
618      <row>
619       <entry><literal><function>pg_stat_get_backend_dbid</function>(<type>integer</type>)</literal></entry>
620       <entry><type>oid</type></entry>
621       <entry>
622        Database ID of the given server process
623       </entry>
624      </row>
625
626      <row>
627       <entry><literal><function>pg_stat_get_backend_userid</function>(<type>integer</type>)</literal></entry>
628       <entry><type>oid</type></entry>
629       <entry>
630        User ID of the given server process
631       </entry>
632      </row>
633
634      <row>
635       <entry><literal><function>pg_stat_get_backend_activity</function>(<type>integer</type>)</literal></entry>
636       <entry><type>text</type></entry>
637       <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)
642       </entry>
643      </row>
644
645      <row>
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>
648       <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)
654       </entry>
655      </row>
656
657      <row>
658       <entry><literal><function>pg_stat_get_backend_start</function>(<type>integer</type>)</literal></entry>
659       <entry><type>timestamp with time zone</type></entry>
660       <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
664       </entry>
665      </row>
666
667      <row>
668       <entry><literal><function>pg_stat_get_backend_client_addr</function>(<type>integer</type>)</literal></entry>
669       <entry><type>inet</type></entry>
670       <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
675       </entry>
676      </row>
677
678      <row>
679       <entry><literal><function>pg_stat_get_backend_client_port</function>(<type>integer</type>)</literal></entry>
680       <entry><type>integer</type></entry>
681       <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
686       </entry>
687      </row>
688
689      <row>
690       <entry><literal><function>pg_stat_reset</function>()</literal></entry>
691       <entry><type>boolean</type></entry>
692       <entry>
693        Reset all block-level and row-level statistics to zero
694       </entry>
695      </row>
696     </tbody>
697    </tgroup>
698   </table>
699
700    <note>
701     <para>
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.
707     </para>
708    </note>
709
710   <para>
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:
714
715 <programlisting>
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;
719 </programlisting>
720   </para>
721
722  </sect2>
723  </sect1>
724
725  <sect1 id="monitoring-locks">
726   <title>Viewing Locks</title>
727
728   <indexterm zone="monitoring-locks">
729    <primary>lock</primary>
730    <secondary>monitoring</secondary>
731   </indexterm>
732
733   <para>
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
738    to:
739
740    <itemizedlist>
741     <listitem>
742      <para>
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.
747      </para>
748     </listitem>
749
750     <listitem>
751      <para>
752       Determine the relation in the current database with the most
753       ungranted locks (which might be a source of contention among
754       database clients).
755      </para>
756     </listitem>
757
758     <listitem>
759      <para>
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.
763      </para>
764     </listitem>
765    </itemizedlist>
766
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">.
771   </para>
772  </sect1>
773 </chapter>
774
775 <!-- Keep this comment at the end of the file
776 Local variables:
777 mode:sgml
778 sgml-omittag:nil
779 sgml-shorttag:t
780 sgml-minimize-attributes:nil
781 sgml-always-quote-attributes:t
782 sgml-indent-step:1
783 sgml-indent-data: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
789 End:
790 -->