]> granicus.if.org Git - postgresql/blob - doc/src/sgml/monitoring.sgml
Publish checkpoint timing information to pg_stat_bgwriter.
[postgresql] / doc / src / sgml / monitoring.sgml
1 <!-- doc/src/sgml/monitoring.sgml -->
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 might be needed using
30    <productname>PostgreSQL</productname>'s <xref linkend="sql-explain"> command.
31    <xref linkend="using-explain"> discusses <command>EXPLAIN</>
32    and other methods for understanding the behavior of an individual
33    query.
34   </para>
35
36  <sect1 id="monitoring-ps">
37   <title>Standard Unix Tools</title>
38
39   <indexterm zone="monitoring-ps">
40    <primary>ps</primary>
41    <secondary>to monitor activity</secondary>
42   </indexterm>
43
44   <para>
45    On most Unix platforms, <productname>PostgreSQL</productname> modifies its
46    command title as reported by <command>ps</>, so that individual server
47    processes can readily be identified.  A sample display is
48
49 <screen>
50 $ ps auxww | grep ^postgres
51 postgres   960  0.0  1.1  6104 1480 pts/1    SN   13:17   0:00 postgres -i
52 postgres   963  0.0  1.1  7084 1472 pts/1    SN   13:17   0:00 postgres: writer process
53 postgres   965  0.0  1.1  6152 1512 pts/1    SN   13:17   0:00 postgres: stats collector process
54 postgres   998  0.0  2.3  6532 2992 pts/1    SN   13:18   0:00 postgres: tgl runbug 127.0.0.1 idle
55 postgres  1003  0.0  2.4  6532 3128 pts/1    SN   13:19   0:00 postgres: tgl regression [local] SELECT waiting
56 postgres  1016  0.1  2.4  6532 3080 pts/1    SN   13:19   0:00 postgres: tgl regression [local] idle in transaction
57 </screen>
58
59    (The appropriate invocation of <command>ps</> varies across different
60    platforms, as do the details of what is shown.  This example is from a
61    recent Linux system.)  The first process listed here is the
62    master server process.  The command arguments
63    shown for it are the same ones used when it was launched.  The next two
64    processes are background worker processes automatically launched by the
65    master process.  (The <quote>stats collector</> process will not be present
66    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 (client) host items remain the same for
76   the life of the client connection, but the activity indicator changes.
77   The activity can 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 appended if the server process is presently waiting
81   on a lock held by another session.  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.
84   </para>
85
86   <para>
87    If you have turned off <xref linkend="guc-update-process-title"> then the
88    activity indicator is not updated; the process title is set only once
89    when a new process is launched.  On some platforms this saves a measurable
90    amount of per-command overhead;  on others it's insignificant.
91   </para>
92
93   <tip>
94   <para>
95   <productname>Solaris</productname> requires special handling. You must
96   use <command>/usr/ucb/ps</command>, rather than
97   <command>/bin/ps</command>. You also must use two <option>w</option>
98   flags, not just one. In addition, your original invocation of the
99   <command>postgres</command> command must have a shorter
100   <command>ps</command> status display than that provided by each
101   server process.  If you fail to do all three things, the <command>ps</>
102   output for each server process will be the original <command>postgres</>
103   command line.
104   </para>
105   </tip>
106  </sect1>
107
108  <sect1 id="monitoring-stats">
109   <title>The Statistics Collector</title>
110
111   <indexterm zone="monitoring-stats">
112    <primary>statistics</primary>
113   </indexterm>
114
115   <para>
116    <productname>PostgreSQL</productname>'s <firstterm>statistics collector</>
117    is a subsystem that supports collection and reporting of information about
118    server activity.  Presently, the collector can count accesses to tables
119    and indexes in both disk-block and individual-row terms.  It also tracks
120    the total number of rows in each table, and information about vacuum and
121    analyze actions for each table.  It can also count calls to user-defined
122    functions and the total time spent in each one.
123   </para>
124
125   <para>
126    <productname>PostgreSQL</productname> also supports reporting of the exact
127    command currently being executed by other server processes.  This
128    facility is independent of the collector process.
129   </para>
130
131  <sect2 id="monitoring-stats-setup">
132   <title>Statistics Collection Configuration</title>
133
134   <para>
135    Since collection of statistics adds some overhead to query execution,
136    the system can be configured to collect or not collect information.
137    This is controlled by configuration parameters that are normally set in
138    <filename>postgresql.conf</>.  (See <xref linkend="runtime-config"> for
139    details about setting configuration parameters.)
140   </para>
141
142   <para>
143    The parameter <xref linkend="guc-track-counts"> controls whether
144    statistics are collected about table and index accesses.
145   </para>
146
147   <para>
148    The parameter <xref linkend="guc-track-iotiming"> enables monitoring
149    of block read and write times.
150   </para>
151
152   <para>
153    The parameter <xref linkend="guc-track-functions"> enables tracking of
154    usage of user-defined functions.
155   </para>
156
157   <para>
158    The parameter <xref linkend="guc-track-activities"> enables monitoring
159    of the current command being executed by any server process.
160   </para>
161
162   <para>
163    Normally these parameters are set in <filename>postgresql.conf</> so
164    that they apply to all server processes, but it is possible to turn
165    them on or off in individual sessions using the <xref
166    linkend="sql-set"> command. (To prevent
167    ordinary users from hiding their activity from the administrator,
168    only superusers are allowed to change these parameters with
169    <command>SET</>.)
170   </para>
171
172   <para>
173    The statistics collector transmits the collected
174    information to backends (including autovacuum) through temporary files.
175    These files are stored in the <filename>pg_stat_tmp</filename> subdirectory.
176    When the postmaster shuts down, a permanent copy of the statistics
177    data is stored in the <filename>global</filename> subdirectory. For increased
178    performance, the parameter <xref linkend="guc-stats-temp-directory"> can
179    be pointed at a RAM-based file system, decreasing physical I/O requirements.
180   </para>
181
182  </sect2>
183
184  <sect2 id="monitoring-stats-views">
185   <title>Viewing Collected Statistics</title>
186
187   <para>
188    Several predefined views, listed in <xref
189    linkend="monitoring-stats-views-table">, are available to show the results
190    of statistics collection.  Alternatively, one can
191    build custom views using the underlying statistics functions.
192   </para>
193
194   <para>
195    When using the statistics to monitor current activity, it is important
196    to realize that the information does not update instantaneously.
197    Each individual server process transmits new statistical counts to
198    the collector just before going idle; so a query or transaction still in
199    progress does not affect the displayed totals.  Also, the collector itself
200    emits a new report at most once per <varname>PGSTAT_STAT_INTERVAL</varname>
201    milliseconds (500 unless altered while building the server).  So the
202    displayed information lags behind actual activity.  However, current-query
203    information collected by <varname>track_activities</varname> is
204    always up-to-date.
205   </para>
206
207   <para>
208    Another important point is that when a server process is asked to display
209    any of these statistics, it first fetches the most recent report emitted by
210    the collector process and then continues to use this snapshot for all
211    statistical views and functions until the end of its current transaction.
212    So the statistics will show static information as long as you continue the
213    current transaction.  Similarly, information about the current queries of
214    all sessions is collected when any such information is first requested
215    within a transaction, and the same information will be displayed throughout
216    the transaction.
217    This is a feature, not a bug, because it allows you to perform several
218    queries on the statistics and correlate the results without worrying that
219    the numbers are changing underneath you.  But if you want to see new
220    results with each query, be sure to do the queries outside any transaction
221    block.  Alternatively, you can invoke
222    <function>pg_stat_clear_snapshot</function>(), which will discard the
223    current transaction's statistics snapshot (if any).  The next use of
224    statistical information will cause a new snapshot to be fetched.
225   </para>
226
227   <para>
228    A transaction can also see its own statistics (as yet untransmitted to the
229    collector) in the views <structname>pg_stat_xact_all_tables</>,
230    <structname>pg_stat_xact_sys_tables</>,
231    <structname>pg_stat_xact_user_tables</>, and
232    <structname>pg_stat_xact_user_functions</>, or via these views' underlying
233    functions (named the same as the standard statistics functions but with the
234    prefix <function>pg_stat_get_xact_</function>).  These numbers do not act
235    as stated above; instead they update continuously throughout the transaction.
236   </para>
237
238   <table id="monitoring-stats-views-table">
239    <title>Standard Statistics Views</title>
240
241    <tgroup cols="2">
242     <thead>
243      <row>
244       <entry>View Name</entry>
245       <entry>Description</entry>
246      </row>
247     </thead>
248
249     <tbody>
250      <row>
251       <entry>
252        <structname>pg_stat_activity</structname>
253        <indexterm><primary>pg_stat_activity</primary></indexterm>
254       </entry>
255       <entry>
256        <para>One row per server process, showing information related to
257         each connection to the server. Unless the
258         <xref linkend="guc-track-activities"> parameter has been turned
259          off, it is possible to monitor state and query information of
260          all running processes.
261        </para>
262        <para>
263         See <xref linkend="pg-stat-activity-view"> for more details.
264        </para>
265      </entry>
266      </row>
267
268      <row>
269       <entry><structname>pg_stat_bgwriter</><indexterm><primary>pg_stat_bgwriter</primary></indexterm></entry>
270       <entry>One row only, showing cluster-wide statistics. See
271        <xref linkend="pg-stat-bgwriter-view"> for more details.
272      </entry>
273      </row>
274
275      <row>
276       <entry><structname>pg_stat_database</><indexterm><primary>pg_stat_database</primary></indexterm></entry>
277       <entry>One row per database, showing database-wide statistics. See
278        <xref linkend="pg-stat-database-view"> for more details.
279       </entry>
280      </row>
281
282      <row>
283       <entry><structname>pg_stat_database_conflicts</><indexterm><primary>pg_stat_database_conflicts</primary></indexterm></entry>
284       <entry>
285        One row per database showing database-wide statistics about
286        query cancels due to conflict with recovery on standby servers.
287        Will only contain information on standby servers, since
288        conflicts do not occur on master servers.
289        See <xref linkend="pg-stat-database-conflicts-view"> for more details.
290      </entry>
291      </row>
292
293      <row>
294       <entry><structname>pg_stat_replication</><indexterm><primary>pg_stat_replication</primary></indexterm></entry>
295       <entry>One row per WAL sender process, showing statistics about the
296        replication to this slave. See <xref linkend="pg-stat-replication-view">
297        for more details. Only directly connected standbys are listed; no
298        information about downstream standby servers is recorded.
299      </entry>
300      </row>
301
302      <row>
303       <entry><structname>pg_stat_all_tables</><indexterm><primary>pg_stat_all_tables</primary></indexterm></entry>
304       <entry>
305        One row for each table in the current database (including TOAST
306        tables) with information about accesses to this specific table.
307        See <xref linkend="pg-stat-all-tables-view"> for more details.
308       </entry>
309      </row>
310
311      <row>
312       <entry><structname>pg_stat_sys_tables</><indexterm><primary>pg_stat_sys_tables</primary></indexterm></entry>
313       <entry>Same as <structname>pg_stat_all_tables</>, except that only
314       system tables are shown.</entry>
315      </row>
316
317      <row>
318       <entry><structname>pg_stat_user_tables</><indexterm><primary>pg_stat_user_tables</primary></indexterm></entry>
319       <entry>Same as <structname>pg_stat_all_tables</>, except that only user
320       tables are shown.</entry>
321      </row>
322
323      <row>
324       <entry><structname>pg_stat_xact_all_tables</><indexterm><primary>pg_stat_xact_all_tables</primary></indexterm></entry>
325       <entry>Similar to <structname>pg_stat_all_tables</>, but counts actions
326       taken so far within the current transaction (which are <emphasis>not</>
327       yet included in <structname>pg_stat_all_tables</> and related views).
328       The columns for numbers of live and dead rows and vacuum and
329       analyze actions are not present in this view.</entry>
330      </row>
331
332      <row>
333       <entry><structname>pg_stat_xact_sys_tables</><indexterm><primary>pg_stat_xact_sys_tables</primary></indexterm></entry>
334       <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
335       system tables are shown.</entry>
336      </row>
337
338      <row>
339       <entry><structname>pg_stat_xact_user_tables</><indexterm><primary>pg_stat_xact_user_tables</primary></indexterm></entry>
340       <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
341       user tables are shown.</entry>
342      </row>
343
344      <row>
345       <entry><structname>pg_stat_all_indexes</><indexterm><primary>pg_stat_all_indexes</primary></indexterm></entry>
346       <entry>
347        One row for each index in the current database with information
348        about accesses to this specific index.
349        See <xref linkend="pg-stat-all-indexes-view"> for more details.
350       </entry>
351      </row>
352
353      <row>
354       <entry><structname>pg_stat_sys_indexes</><indexterm><primary>pg_stat_sys_indexes</primary></indexterm></entry>
355       <entry>Same as <structname>pg_stat_all_indexes</>, except that only
356       indexes on system tables are shown.</entry>
357      </row>
358
359      <row>
360       <entry><structname>pg_stat_user_indexes</><indexterm><primary>pg_stat_user_indexes</primary></indexterm></entry>
361       <entry>Same as <structname>pg_stat_all_indexes</>, except that only
362       indexes on user tables are shown.</entry>
363      </row>
364
365      <row>
366       <entry><structname>pg_statio_all_tables</><indexterm><primary>pg_statio_all_tables</primary></indexterm></entry>
367       <entry>
368        One row for each table in the current database (including TOAST
369        tables) with information about I/O on this specific table.
370        See <xref linkend="pg-statio-all-tables-view"> for more details.
371       </entry>
372      </row>
373
374      <row>
375       <entry><structname>pg_statio_sys_tables</><indexterm><primary>pg_statio_sys_tables</primary></indexterm></entry>
376       <entry>Same as <structname>pg_statio_all_tables</>, except that only
377       system tables are shown.</entry>
378      </row>
379
380      <row>
381       <entry><structname>pg_statio_user_tables</><indexterm><primary>pg_statio_user_tables</primary></indexterm></entry>
382       <entry>Same as <structname>pg_statio_all_tables</>, except that only
383       user tables are shown.</entry>
384      </row>
385
386      <row>
387       <entry><structname>pg_statio_all_indexes</><indexterm><primary>pg_statio_all_indexes</primary></indexterm></entry>
388       <entry>
389        One row for each index in the current database
390        with information about I/O on this specific index.
391        See <xref linkend="pg-statio-all-indexes-view"> for more details.
392       </entry>
393      </row>
394
395      <row>
396       <entry><structname>pg_statio_sys_indexes</><indexterm><primary>pg_statio_sys_indexes</primary></indexterm></entry>
397       <entry>Same as <structname>pg_statio_all_indexes</>, except that only
398       indexes on system tables are shown.</entry>
399      </row>
400
401      <row>
402       <entry><structname>pg_statio_user_indexes</><indexterm><primary>pg_statio_user_indexes</primary></indexterm></entry>
403       <entry>Same as <structname>pg_statio_all_indexes</>, except that only
404       indexes on user tables are shown.</entry>
405      </row>
406
407      <row>
408       <entry><structname>pg_statio_all_sequences</><indexterm><primary>pg_statio_all_sequences</primary></indexterm></entry>
409      <entry>
410        One row for each sequence in the current database
411        with information about I/O on this specific sequence.
412        See <xref linkend="pg-statio-all-sequences-view"> for more details.
413      </entry>
414      </row>
415
416      <row>
417       <entry><structname>pg_statio_sys_sequences</><indexterm><primary>pg_statio_sys_sequences</primary></indexterm></entry>
418       <entry>Same as <structname>pg_statio_all_sequences</>, except that only
419       system sequences are shown.  (Presently, no system sequences are defined,
420       so this view is always empty.)</entry>
421      </row>
422
423      <row>
424       <entry><structname>pg_statio_user_sequences</><indexterm><primary>pg_statio_user_sequences</primary></indexterm></entry>
425       <entry>Same as <structname>pg_statio_all_sequences</>, except that only
426       user sequences are shown.</entry>
427      </row>
428
429      <row>
430       <entry><structname>pg_stat_user_functions</><indexterm><primary>pg_stat_user_functions</primary></indexterm></entry>
431       <entry>
432        One row for each tracked function (as specified by the
433        <xref linkend="guc-track-functions"> parameter). See
434        <xref linkend="pg-stat-user-functions-view"> for more details.
435       </entry>
436      </row>
437
438      <row>
439       <entry><structname>pg_stat_xact_user_functions</><indexterm><primary>pg_stat_xact_user_functions</primary></indexterm></entry>
440       <entry>Similar to <structname>pg_stat_user_functions</>, but counts only
441       calls during the current transaction (which are <emphasis>not</>
442       yet included in <structname>pg_stat_user_functions</>).</entry>
443      </row>
444
445     </tbody>
446    </tgroup>
447   </table>
448
449   <para>
450    The per-index statistics are particularly useful to determine which
451    indexes are being used and how effective they are.
452   </para>
453
454   <para>
455    Indexes can be
456    used either directly or via <quote>bitmap scans</>.  In a bitmap scan
457    the output of several indexes can be combined via AND or OR rules;
458    so it is difficult to associate individual heap row fetches
459    with specific indexes when a bitmap scan is used.  Therefore, a bitmap
460    scan increments the
461    <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
462    count(s) for the index(es) it uses, and it increments the
463    <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
464    count for the table, but it does not affect
465    <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
466   </para>
467
468   <note>
469    <para>
470     Before <productname>PostgreSQL</productname> 8.1, the
471     <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
472     were essentially always equal.  Now they can be different even without
473     considering bitmap scans, because <structfield>idx_tup_read</> counts
474     index entries retrieved from the index while <structfield>idx_tup_fetch</>
475     counts live rows fetched from the table; the latter will be less if any
476     dead or not-yet-committed rows are fetched using the index, or if any
477     heap fetches are avoided by means of an index-only scan.
478    </para>
479   </note>
480
481   <para>
482    The <structname>pg_statio_</> views are primarily useful to
483    determine the effectiveness of the buffer cache.  When the number
484    of actual disk reads is much smaller than the number of buffer
485    hits, then the cache is satisfying most read requests without
486    invoking a kernel call. However, these statistics do not give the
487    entire story: due to the way in which <productname>PostgreSQL</>
488    handles disk I/O, data that is not in the
489    <productname>PostgreSQL</> buffer cache might still reside in the
490    kernel's I/O cache, and might therefore still be fetched without
491    requiring a physical read. Users interested in obtaining more
492    detailed information on <productname>PostgreSQL</> I/O behavior are
493    advised to use the <productname>PostgreSQL</> statistics collector
494    in combination with operating system utilities that allow insight
495    into the kernel's handling of I/O.
496   </para>
497
498   <para>
499    Other ways of looking at the statistics can be set up by writing
500    queries that use the same underlying statistics access functions as
501    these standard views do.  These functions are listed in <xref
502    linkend="monitoring-stats-funcs-table">.  The per-database access
503    functions take a database OID as an argument to identify which
504    database to report on.  The per-table and per-index functions take
505    a table or index OID.  The functions for function-call statistics
506    take a function OID.  (Note that only tables, indexes, and functions
507    in the current database can be seen with these functions.)  The
508    per-server-process access functions take a server process
509    number, which ranges from one to the number of currently active
510    server processes.
511   </para>
512
513
514   <table id="pg-stat-activity-view" xreflabel="pg_stat_activity">
515    <title>pg_stat_activity view</title>
516
517    <tgroup cols="3">
518     <thead>
519     <row>
520       <entry>Column</entry>
521       <entry>Type</entry>
522       <entry>Description</entry>
523      </row>
524     </thead>
525
526    <tbody>
527     <row>
528      <entry>datid</entry>
529      <entry><type>oid</></entry>
530      <entry>The OID of the database the backend is connected to.
531      This value can also be returned by directly calling
532      the <function>pg_stat_get_backend_dbid</function> function.</entry>
533     </row>
534     <row>
535      <entry>datname</entry>
536      <entry><type>name</></entry>
537      <entry>The name of the database the backend is connected to.</entry>
538     </row>
539     <row>
540      <entry>pid</entry>
541      <entry><type>integer</></entry>
542      <entry>The process ID of the backend.
543      This value can also be returned by directly calling
544      the <function>pg_stat_get_backend_pid</function>.</entry>
545     </row>
546     <row>
547      <entry>usesysid</entry>
548      <entry><type>oid</></entry>
549      <entry>The id of the user logged into the backend.
550      This value can also be returned by directly calling
551      the <function>pg_stat_get_backend_userid</function>.</entry>
552     </row>
553     <row>
554      <entry>usename</entry>
555      <entry><type>name</></entry>
556      <entry>The name of the user logged into the backend.</entry>
557     </row>
558     <row>
559      <entry>application_name</entry>
560      <entry><type>text</></entry>
561      <entry>The name of the application that has initiated the connection
562       to the backend.</entry>
563     </row>
564     <row>
565      <entry>client_addr</entry>
566      <entry><type>inet</></entry>
567      <entry>The remote IP of the client connected to the backend.
568       If this field is not set, it indicates that the client is either connected
569       via a Unix socket on the server machine or is an internal process such
570       as autovacuum.
571       This value can also be returned by directly calling
572       the <function>pg_stat_get_backend_client_addr</function>.
573      </entry>
574     </row>
575     <row>
576      <entry>client_hostname</entry>
577      <entry><type>text</></entry>
578      <entry>
579       If available, the hostname of the client as reported by a
580       reverse lookup of <structfield>client_addr</>. This field will
581       only be set when <xref linkend="guc-log-hostname"> is enabled.
582      </entry>
583     </row>
584     <row>
585      <entry>client_port</entry>
586      <entry><type>integer</></entry>
587      <entry>
588       The remote TCP port that the client is using for communication
589       to the backend, or <symbol>NULL</> if a unix socket is used.
590       This value can also be returned by directly calling
591       the <function>pg_stat_get_backend_client_port</function>.
592      </entry>
593     </row>
594     <row>
595      <entry>backend_start</entry>
596      <entry><type>timestamp with time zone</></entry>
597      <entry>
598       The time when this process was started,  i.e. when the
599       client connected to the server.
600       This value can also be returned by directly calling
601       the <function>pg_stat_get_backend_start</function>.
602      </entry>
603     </row>
604     <row>
605      <entry>xact_start</entry>
606      <entry><type>timestamp with time zone</></entry>
607      <entry>
608       The time when the current transaction was started. If the client is
609       using autocommit for transactions, this value is equal to the
610       query_start column.
611       This value can also be returned by directly calling
612       the <function>pg_stat_get_backend_xact_start</function>.
613      </entry>
614     </row>
615     <row>
616      <entry>query_start</entry>
617      <entry><type>timestamp with time zone</></entry>
618      <entry>
619       The time when the currently active query started, or if
620       <structfield>state</> is <literal>idle</>, when the last query
621       was started.
622       This value can also be returned by directly calling
623       the <function>pg_stat_get_backend_activity_start</function>.
624      </entry>
625     </row>
626     <row>
627      <entry>state_change</entry>
628      <entry><type>timestamp with time zone</></entry>
629      <entry>The time when the <structfield>state</> was last changed.</entry>
630     </row>
631     <row>
632      <entry>waiting</entry>
633      <entry><type>boolean</></entry>
634      <entry>
635       Boolean indicating if a backend is currently waiting on a lock.
636       This value can also be returned by directly calling
637       the <function>pg_stat_get_backend_waiting</function>.
638      </entry>
639     </row>
640     <row>
641      <entry>state</entry>
642      <entry><type>text</></entry>
643      <entry>
644        The <structfield>state</> of the currently running query.
645        Can be one of:
646        <variablelist>
647          <varlistentry>
648            <term>active</term>
649            <listitem>
650              <para>
651               The backend is executing a query.
652              </para>
653            </listitem>
654            </varlistentry>
655            <varlistentry>
656              <term>idle</term>
657            <listitem>
658              <para>
659                There is no query executing in the backend.
660              </para>
661            </listitem>
662          </varlistentry>
663          <varlistentry>
664            <term>idle in transaction</term>
665            <listitem>
666              <para>
667                The backend is in a transaction, but is currently not currently
668                executing a query.
669              </para>
670            </listitem>
671          </varlistentry>
672          <varlistentry>
673            <term>idle in transaction (aborted)</term>
674            <listitem>
675              <para>
676                This state is similar to <literal>idle in transaction</>,
677                except one of the statements in the transaction caused an error.
678              </para>
679            </listitem>
680          </varlistentry>
681          <varlistentry>
682            <term>fastpath function call</term>
683            <listitem>
684              <para>
685                The backend is executing a fast-path function.
686              </para>
687            </listitem>
688          </varlistentry>
689          <varlistentry>
690            <term>disabled</term>
691            <listitem>
692              <para>
693                This state indicates that <xref linkend="guc-track-activities">
694                is disabled.
695              </para>
696            </listitem>
697          </varlistentry>
698        </variablelist>
699       <note>
700        <para>
701         The <structfield>waiting</> and <structfield>state</> columns are
702         independent.  If a query is in the <literal>active</> state,
703         it may or may not be <literal>waiting</>.  If a query is
704         <literal>active</> and <structfield>waiting</> is true, it means
705         that the query is being executed, but is being blocked by a lock
706         somewhere in the system.
707        </para>
708       </note>
709      </entry>
710     </row>
711     <row>
712      <entry>query</entry>
713      <entry><type>text</></entry>
714      <entry>
715       The most recent query that the backend has executed. If
716       <structfield>state</> is <literal>active</> this means the currently
717       executing query. In all other states, it means the last query that was
718       executed.
719      </entry>
720     </row>
721    </tbody>
722    </tgroup>
723   </table>
724
725   <para>
726    The <structname>pg_stat_activity</structname> view will have one row
727    per server process, showing information related to each connection to
728    the server.
729   </para>
730   <para>
731    All functions used in the view are indexed by backend id number. The
732    function <function>pg_stat_get_backend_idset</function> provides a
733    convenient way to generate one row for each active server process. For
734    example, to show the <acronym>PID</>s and current queries of all server processes:
735
736 <programlisting>
737 SELECT pg_stat_get_backend_pid(s.backendid) AS pid,
738        pg_stat_get_backend_activity(s.backendid) AS query
739     FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
740 </programlisting>
741   </para>
742
743   <table id="pg-stat-bgwriter-view" xreflabel="pg_stat_bgwriter">
744    <title>pg_stat_bgwriter view</title>
745
746    <tgroup cols="3">
747     <thead>
748     <row>
749       <entry>Column</entry>
750       <entry>Type</entry>
751       <entry>Description</entry>
752      </row>
753     </thead>
754
755     <tbody>
756      <row>
757       <entry>checkpoints_timed</entry>
758       <entry><type>bigint</type></entry>
759       <entry>Number of scheduled checkpoints.
760      This value can also be returned by directly calling
761      the <function>pg_stat_get_bgwriter_timed_checkpoints</function> function.</entry>
762      </row>
763      <row>
764       <entry>checkpoints_requested</entry>
765       <entry><type>bigint</type></entry>
766       <entry>Number of requested checkpoints.
767      This value can also be returned by directly calling
768      the <function>pg_stat_get_bgwriter_requested_checkpoints</function> function.</entry>
769      </row>
770      <row>
771       <entry>checkpoint_write_time</entry>
772       <entry><type>bigint</type></entry>
773       <entry>
774         Total amount of time that has been spent in the portion of
775         checkpoint processing where files are written to disk, in milliseconds.
776         This value can also be returned by directly calling the
777         <function>pg_stat_get_checkpoint_write_time</function>
778         function.
779       </entry>
780      </row>
781      <row>
782       <entry>checkpoint_sync_time</entry>
783       <entry><type>bigint</type></entry>
784       <entry>
785         Total amount of time that has been spent in the portion of
786         checkpoint processing where files are synchronized to disk, in
787         milliseconds.  This value can also be returned by directly calling
788         the <function>pg_stat_get_checkpoint_sync_time</function>
789         function.
790       </entry>
791      </row>
792      <row>
793       <entry>buffers_checkpoint</entry>
794       <entry><type>bigint</type></entry>
795       <entry>Number of buffers written during checkpoints.
796      This value can also be returned by directly calling
797      the <function>pg_stat_get_bgwriter_buf_written_checkpoints</function> function.</entry>
798      </row>
799      <row>
800       <entry>buffers_clean</entry>
801       <entry><type>bigint</type></entry>
802       <entry>Number of buffers written by the background writer.
803      This value can also be returned by directly calling
804      the <function>pg_stat_get_bgwriter_buf_written_clean</function> function.</entry>
805      </row>
806      <row>
807       <entry>maxwritten_clean</entry>
808       <entry><type>bigint</type></entry>
809       <entry>Number of times the background writer stopped a cleaning
810       scan because it had written too many buffers.
811      This value can also be returned by directly calling
812      the <function>pg_stat_get_bgwriter_maxwritten_clean</function> function.</entry>
813      </row>
814      <row>
815       <entry>buffers_backend</entry>
816       <entry><type>bigint</type></entry>
817       <entry>Number of buffers written directly by a backend.
818      This value can also be returned by directly calling
819      the <function>pg_stat_get_buf_written_backend</function> function.</entry>
820      </row>
821      <row>
822       <entry>buffers_backend_fsync</entry>
823       <entry><type>bigint</type></entry>
824       <entry>Number of times a backend had to execute its own fsync
825       call (normally the background writer handles those even when the
826       backend does its own write)</entry>
827      </row>
828      <row>
829       <entry>buffers_alloc</entry>
830       <entry><type>bigint</type></entry>
831       <entry>Number of buffers allocated.
832      This value can also be returned by directly calling
833      the <function>pg_stat_get_buf_alloc</function> function.</entry>
834      </row>
835      <row>
836       <entry>stats_reset</entry>
837       <entry><type>bigint</type></entry>
838       <entry>The last time these statistics were reset.
839      This value can also be returned by directly calling
840      the <function>pg_stat_get_bgwriter_stat_reset_time</function> function.</entry>
841      </row>
842     </tbody>
843     </tgroup>
844   </table>
845
846   <para>
847    The <structname>pg_stat_bgwriter</structname> view will always have a
848    single row with global data for the cluster.
849   </para>
850
851   <table id="pg-stat-database-view" xreflabel="pg_stat_database">
852    <title>pg_stat_database view</title>
853    <tgroup cols="3">
854     <thead>
855     <row>
856       <entry>Column</entry>
857       <entry>Type</entry>
858       <entry>Description</entry>
859      </row>
860     </thead>
861
862    <tbody>
863     <row>
864      <entry>datid</entry>
865      <entry><type>oid</></entry>
866      <entry>The OID of the database</entry>
867     </row>
868     <row>
869      <entry>datname</entry>
870      <entry><type>name</></entry>
871      <entry>The name of the database</entry>
872     </row>
873     <row>
874      <entry>numbackends</entry>
875      <entry><type>integer</></entry>
876      <entry>The number of backends currently connected to this database.
877      This is the only column in this view that returns a value for the
878      current state, all other columns return the accumulated values since
879      the last reset. This value can also be returned by directly calling
880      the <function>pg_stat_get_db_numbackends</function> function.</entry>
881     </row>
882     <row>
883      <entry>xact_commit</entry>
884      <entry><type>bigint</></entry>
885      <entry>The number of transactions in this database that have been
886      committed. This value can also be returned by directly calling
887      the <function>pg_stat_get_db_xact_commit</function> function.</entry>
888     </row>
889     <row>
890      <entry>xact_rollback</entry>
891      <entry><type>bigint</></entry>
892      <entry>The number of transactions in this database that have been
893      rolled back. This value can also be returned by directly calling
894      the <function>pg_stat_get_db_xact_rollback</function> function.</entry>
895     </row>
896     <row>
897      <entry>blks_read</entry>
898      <entry><type>bigint</></entry>
899      <entry>The number of disk blocks read in this database.
900      This value can also be returned by directly calling
901      the <function>pg_stat_get_db_blocks_fetched</function> and
902      <function>pg_stat_get_db_blocks_hit</function> functions and
903      subtracting the results.</entry>
904     </row>
905     <row>
906      <entry>blks_hits</entry>
907      <entry><type>bigint</></entry>
908      <entry>The number of disk blocks read from the buffer cache
909      (this only includes hits in the PostgreSQL buffer cache, and not
910      the operating system filesystem cache).
911      This value can also be returned by directly calling
912      the <function>pg_stat_get_db_blocks_hit</function> function.</entry>
913     </row>
914     <row>
915      <entry>tup_returned</entry>
916      <entry><type>bigint</></entry>
917      <entry>The number of rows returned by queries in this database.
918      This value can also be returned by directly calling
919      the <function>pg_stat_get_db_tuples_returned</function> function.</entry>
920     </row>
921     <row>
922      <entry>tup_fetched</entry>
923      <entry><type>bigint</></entry>
924      <entry>The number of rows fetched by queries in this database.
925      This value can also be returned by directly calling
926      the <function>pg_stat_get_db_tuples_fetched</function> function.</entry>
927     </row>
928     <row>
929      <entry>tup_inserted</entry>
930      <entry><type>bigint</></entry>
931      <entry>The number of rows inserted by queries in this database.
932      This value can also be returned by directly calling
933      the <function>pg_stat_get_db_tuples_inserted</function> function.</entry>
934     </row>
935     <row>
936      <entry>tup_updated</entry>
937      <entry><type>bigint</></entry>
938      <entry>The number of rows updated by queries in this database.
939      This value can also be returned by directly calling
940      the <function>pg_stat_get_db_tuples_updated</function> function.</entry>
941     </row>
942     <row>
943      <entry>tup_deleted</entry>
944      <entry><type>bigint</></entry>
945      <entry>The number of rows deleted by queries in this database.
946      This value can also be returned by directly calling
947      the <function>pg_stat_get_db_tuples_deleted</function> function.</entry>
948     </row>
949     <row>
950      <entry>conflicts</entry>
951      <entry><type>bigint</></entry>
952      <entry>
953       The number of queries canceled due to conflict with recovery
954       (on standby servers) in this database. (See
955       <xref linkend="pg-stat-database-conflicts-view"> for more details).
956       This value can also be returned by directly calling
957       the <function>pg_stat_get_db_conflict_all</function> function.
958      </entry>
959     </row>
960     <row>
961      <entry>temp_files</entry>
962      <entry><type>bigint</></entry>
963      <entry>
964       The number of temporary files written by queries in the database.
965       All temporary files are counted, regardless of why the temporary file
966       was created (sorting or hash) or file size, and regardless of the
967       <xref linkend="guc-log-temp-files"> setting.
968       This value can also be returned by directly calling
969       the <function>pg_stat_get_db_temp_files</function> function.
970      </entry>
971     </row>
972     <row>
973      <entry>temp_bytes</entry>
974      <entry><type>bigint</></entry>
975      <entry>
976       The amount of data written to temporary files by queries in
977       the database. All temporary files are counted, regardless of why
978       the temporary file was created (sorting or hash) or file size, and
979       regardless of the <xref linkend="guc-log-temp-files"> setting.
980       This value can also be returned by directly calling
981       the <function>pg_stat_get_db_temp_bytes</function> function.
982      </entry>
983     </row>
984     <row>
985      <entry>deadlocks</entry>
986      <entry><type>bigint</></entry>
987      <entry>Number of deadlocks detected in the database.
988       This value can also be returned by directly calling
989       the <function>pg_stat_get_db_deadlocks</function> function.</entry>
990     </row>
991     <row>
992      <entry>block_read_time</entry>
993      <entry><type>bigint</></entry>
994      <entry>Time spent by backends reading data file blocks, in milliseconds.
995       The same value can be returned in microseconds by directly calling
996       the <function>pg_stat_get_db_block_time_read</function> function.</entry>
997     </row>
998     <row>
999      <entry>block_write_time</entry>
1000      <entry><type>bigint</></entry>
1001      <entry>Time spent by backends writing data file blocks, in milliseconds.
1002       The same value can be returned in microseconds by directly calling
1003       the <function>pg_stat_get_db_block_time_write</function> function.</entry>
1004     </row>
1005     <row>
1006      <entry>stats_reset</entry>
1007      <entry><type>timestamptz</></entry>
1008      <entry>The last time the statistics were reset.
1009       This value can also be returned by directly calling
1010       the <function>pg_stat_get_reset_time</function> function.</entry>
1011     </row>
1012    </tbody>
1013    </tgroup>
1014   </table>
1015
1016   <para>
1017    The <structname>pg_stat_database</structname> view will contain one row
1018    for each database in the cluster showing database-wide statistics.
1019   </para>
1020
1021   <table id="pg-stat-database-conflicts-view" xreflabel="pg_stat_database_conflicts">
1022    <title>pg_stat_database_conflicts view</title>
1023    <tgroup cols="3">
1024     <thead>
1025     <row>
1026       <entry>Column</entry>
1027       <entry>Type</entry>
1028       <entry>Description</entry>
1029      </row>
1030     </thead>
1031
1032    <tbody>
1033     <row>
1034      <entry>datid</entry>
1035      <entry><type>oid</></entry>
1036      <entry>The OID of the database</entry>
1037     </row>
1038     <row>
1039      <entry>datname</entry>
1040      <entry><type>name</></entry>
1041      <entry>The name of the database</entry>
1042     </row>
1043     <row>
1044      <entry>confl_tablespace</entry>
1045      <entry><type>bigint</></entry>
1046      <entry>The number of queries that have been canceled due to
1047       dropped tablespaces. This value can also be returned by directly calling
1048       the <function>pg_stat_get_db_conflict_tablespace</function> function.</entry>
1049     </row>
1050     <row>
1051      <entry>confl_lock</entry>
1052      <entry><type>bigint</></entry>
1053      <entry>The number of queries that have been canceled due to
1054       lock timeouts. This value can also be returned by directly calling
1055       the <function>pg_stat_get_db_conflict_lock</function> function.</entry>
1056     </row>
1057     <row>
1058      <entry>confl_snapshot</entry>
1059      <entry><type>bigint</></entry>
1060      <entry>The number of queries that have been canceled due to
1061       old snapshots. This value can also be returned by directly calling
1062       the <function>pg_stat_get_db_conflict_snapshot</function> function.</entry>
1063     </row>
1064     <row>
1065      <entry>confl_bufferpin</entry>
1066      <entry><type>bigint</></entry>
1067      <entry>The number of queries that have been canceled due to
1068       pinned buffers. This value can also be returned by directly calling
1069       the <function>pg_stat_get_db_conflict_bufferpin</function> function.</entry>
1070     </row>
1071     <row>
1072      <entry>confl_deadlock</entry>
1073      <entry><type>bigint</></entry>
1074      <entry>The number of queries that have been canceled due to
1075       deadlocks. This value can also be returned by directly calling
1076       the <function>pg_stat_get_db_conflict_startup_deadlock</function>
1077       function.</entry>
1078     </row>
1079    </tbody>
1080    </tgroup>
1081   </table>
1082
1083   <para>
1084    The <structname>pg_stat_database_conflicts</structname> view will contain
1085    one row per database showing database-wide statistics about
1086    query cancels due to conflict with recovery on standby servers.
1087    Will only contain information on standby servers, since
1088    conflicts do not occur on master servers.
1089   </para>
1090
1091   <table id="pg-stat-replication-view" xreflabel="pg_stat_replication">
1092    <title>pg_stat_replication view</title>
1093    <tgroup cols="3">
1094     <thead>
1095     <row>
1096       <entry>Column</entry>
1097       <entry>Type</entry>
1098       <entry>Description</entry>
1099      </row>
1100     </thead>
1101
1102    <tbody>
1103     <row>
1104      <entry>pid</entry>
1105      <entry><type>integer</></entry>
1106      <entry>The process id of the WAL sender process</entry>
1107     </row>
1108     <row>
1109      <entry>usesysid</entry>
1110      <entry><type>oid</></entry>
1111      <entry>The OID of the user logged into this WAL sender process</entry>
1112     </row>
1113     <row>
1114      <entry>usename</entry>
1115      <entry><type>name</></entry>
1116      <entry>The name of the user logged into this WAL sender process</entry>
1117     </row>
1118     <row>
1119      <entry>application_name</entry>
1120      <entry><type>text</></entry>
1121      <entry>The name of the application that has initiated the connection
1122       to the WAL sender.</entry>
1123     </row>
1124     <row>
1125      <entry>client_addr</entry>
1126      <entry><type>inet</></entry>
1127      <entry>The remote IP of the client connected to the WAL sender.
1128       If this field is not set, it indicates that the client is
1129       connected via a Unix socket on the server machine.
1130      </entry>
1131     </row>
1132     <row>
1133      <entry>client_hostname</entry>
1134      <entry><type>text</></entry>
1135      <entry>
1136       If available, the hostname of the client as reported by a
1137       reverse lookup of <structfield>client_addr</>. This field will
1138       only be set when <xref linkend="guc-log-hostname"> is enabled.
1139      </entry>
1140     </row>
1141     <row>
1142      <entry>client_port</entry>
1143      <entry><type>integer</></entry>
1144      <entry>
1145       The remote TCP port that the client is using for communication
1146       to the, or <symbol>NULL</> if a unix socket is used.
1147      </entry>
1148     </row>
1149     <row>
1150      <entry>backend_start</entry>
1151      <entry><type>timestamp with time zone</></entry>
1152      <entry>
1153       The time when this process was started,  i.e. when the
1154       client connected to the WAL sender.
1155      </entry>
1156     </row>
1157     <row>
1158      <entry>state</entry>
1159      <entry><type>text</></entry>
1160      <entry>Current WAL sender state</entry>
1161     </row>
1162     <row>
1163      <entry>sent_location</entry>
1164      <entry><type>text</></entry>
1165      <entry>Last transaction log position sent on this connection</entry>
1166     </row>
1167     <row>
1168      <entry>write_location</entry>
1169      <entry><type>text</></entry>
1170      <entry>Last transaction log position written to disk by the slave</entry>
1171     </row>
1172     <row>
1173      <entry>flush_location</entry>
1174      <entry><type>text</></entry>
1175      <entry>Last transaction log position flushed to disk by the slave</entry>
1176     </row>
1177     <row>
1178      <entry>replay_location</entry>
1179      <entry><type>text</></entry>
1180      <entry>Last transaction log position replayed into the database on the slave</entry>
1181     </row>
1182     <row>
1183      <entry>sync_priority</entry>
1184      <entry><type>int</></entry>
1185      <entry>
1186       The priority in the order which this slave will be picked as
1187       the synchronous standby.
1188      </entry>
1189     </row>
1190     <row>
1191      <entry>sync_state</entry>
1192      <entry><type>text</></entry>
1193      <entry>
1194       The synchronous state of this slave.
1195      </entry>
1196     </row>
1197    </tbody>
1198    </tgroup>
1199   </table>
1200
1201   <para>
1202    The <structname>pg_stat_replication</structname> view will contain one row
1203    per WAL sender process, showing statistics about the replication to this
1204    slave. Only directly connected standbys are listed; no information about
1205    downstream standby servers is recorded.
1206   </para>
1207
1208   <table id="pg-stat-all-tables-view" xreflabel="pg_stat_all_tables">
1209    <title>pg_stat_all_tables view</title>
1210    <tgroup cols="3">
1211     <thead>
1212     <row>
1213       <entry>Column</entry>
1214       <entry>Type</entry>
1215       <entry>Description</entry>
1216      </row>
1217     </thead>
1218
1219    <tbody>
1220     <row>
1221      <entry>relid</entry>
1222      <entry><type>oid</></entry>
1223      <entry>The OID of the table</entry>
1224     </row>
1225     <row>
1226      <entry>schemaname</entry>
1227      <entry><type>name</></entry>
1228      <entry>The name of the schema that the table is in</entry>
1229     </row>
1230     <row>
1231      <entry>relname</entry>
1232      <entry><type>name</></entry>
1233      <entry>The name of the table</entry>
1234     </row>
1235     <row>
1236      <entry>seq_scan</entry>
1237      <entry><type>bigint</></entry>
1238      <entry>The number of sequential scans initiated on this table.
1239      This value can also be returned by directly calling
1240      the <function>pg_stat_get_numscans</function> function.</entry>
1241     </row>
1242     <row>
1243      <entry>seq_tup_read</entry>
1244      <entry><type>bigint</></entry>
1245      <entry>The number of live rows fetch by sequential scans.
1246      This value can also be returned by directly calling
1247      the <function>pg_stat_get_tuples_returned</function> function.</entry>
1248     </row>
1249     <row>
1250      <entry>idx_scan</entry>
1251      <entry><type>bigint</></entry>
1252      <entry>The number of index scans initiated on this table</entry>
1253     </row>
1254     <row>
1255      <entry>idx_tup_fetch</entry>
1256      <entry><type>bigint</></entry>
1257      <entry>The number of live rows fetch by index scans.</entry>
1258     </row>
1259     <row>
1260      <entry>n_tup_ins</entry>
1261      <entry><type>bigint</></entry>
1262      <entry>The number of rows inserted.
1263      This value can also be returned by directly calling
1264      the <function>pg_stat_get_tuples_inserted</function> function.</entry>
1265     </row>
1266     <row>
1267      <entry>n_tup_upd</entry>
1268      <entry><type>bigint</></entry>
1269      <entry>The number of rows updated.
1270      This value can also be returned by directly calling
1271      the <function>pg_stat_get_tuples_updated</function> function.</entry>
1272     </row>
1273     <row>
1274      <entry>n_tup_del</entry>
1275      <entry><type>bigint</></entry>
1276      <entry>The number of rows deleted.
1277      This value can also be returned by directly calling
1278      the <function>pg_stat_get_tuples_deleted</function> function.</entry>
1279     </row>
1280     <row>
1281      <entry>n_tup_hot_upd</entry>
1282      <entry><type>bigint</></entry>
1283      <entry>The number of rows HOT (i.e., no separate index update) updated.
1284      This value can also be returned by directly calling
1285      the <function>pg_stat_get_tuples_hot_updated</function> function.</entry>
1286     </row>
1287     <row>
1288      <entry>n_live_tup</entry>
1289      <entry><type>bigint</></entry>
1290      <entry>The number of live rows.
1291      This value can also be returned by directly calling
1292      the <function>pg_stat_get_live_tuples</function> function.</entry>
1293     </row>
1294     <row>
1295      <entry>n_dead_tup</entry>
1296      <entry><type>bigint</></entry>
1297      <entry>The number of dead rows.
1298      This value can also be returned by directly calling
1299      the <function>pg_stat_get_dead_tuples</function> function.</entry>
1300     </row>
1301     <row>
1302      <entry>last_vacuum</entry>
1303      <entry><type>timestamp with time zone</></entry>
1304      <entry>The last time the table was manually non-<option>FULL</> vacuumed.
1305      This value can also be returned by directly calling
1306      the <function>pg_stat_get_last_vacuum_time</function> function.</entry>
1307     </row>
1308     <row>
1309      <entry>last_autovacuum</entry>
1310      <entry><type>timestamp with time zone</></entry>
1311      <entry>The last time the table was vacuumed by the autovacuum daemon.
1312      This value can also be returned by directly calling
1313      the <function>pg_stat_get_last_autovacuum_time</function> function.</entry>
1314     </row>
1315     <row>
1316      <entry>last_analyze</entry>
1317      <entry><type>timestamp with time zone</></entry>
1318      <entry>The last time the table was manually analyzed.
1319      This value can also be returned by directly calling
1320      the <function>pg_stat_get_last_analyze_time</function> function.</entry>
1321     </row>
1322     <row>
1323      <entry>last_autoanalyze</entry>
1324      <entry><type>timestamp with time zone</></entry>
1325      <entry>The last time the table was analyzed by the autovacuum daemon.
1326      This value can also be returned by directly calling
1327      the <function>pg_stat_get_last_autoanalyze_time</function> function.</entry>
1328     </row>
1329     <row>
1330      <entry>vacuum_count</entry>
1331      <entry><type>bigint</></entry>
1332      <entry>The number of times this table has been manually non-<option>FULL</> vacuumed.
1333      This value can also be returned by directly calling
1334      the <function>pg_stat_get_vacuum_count</function> function.</entry>
1335     </row>
1336     <row>
1337      <entry>autovacuum_count</entry>
1338      <entry><type>bigint</></entry>
1339      <entry>The number of times this table has been vacuumed by the autovacuum daemon.
1340      This value can also be returned by directly calling
1341      the <function>pg_stat_get_autovacuum_count</function> function.</entry>
1342     </row>
1343     <row>
1344      <entry>analyze_count</entry>
1345      <entry><type>bigint</></entry>
1346      <entry>The number of times this table has been manually analyzed.
1347      This value can also be returned by directly calling
1348      the <function>pg_stat_get_analyze_count</function> function.</entry>
1349     </row>
1350     <row>
1351      <entry>autoanalyze_count</entry>
1352      <entry><type>bigint</></entry>
1353      <entry>The number of times this table has been analyzed by the autovacuum daemon.
1354      This value can also be returned by directly calling
1355      the <function>pg_stat_get_autoanalyze_count</function> function.</entry>
1356     </row>
1357    </tbody>
1358    </tgroup>
1359   </table>
1360
1361   <para>
1362    The <structname>pg_stat_all_tables</structname> view will contain
1363    one row for each table in the current database (including TOAST
1364    tables) with information about accesses to this specific table. The
1365    <structname>pg_stat_user_tables</structname> and
1366    <structname>pg_stat_sys_tables</structname> contain the same information,
1367    but filtered to only have rows for user and system tables.
1368   </para>
1369
1370   <table id="pg-stat-all-indexes-view" xreflabel="pg_stat_all_indexes">
1371    <title>pg_stat_all_indexes view</title>
1372    <tgroup cols="3">
1373     <thead>
1374     <row>
1375       <entry>Column</entry>
1376       <entry>Type</entry>
1377       <entry>Description</entry>
1378      </row>
1379     </thead>
1380
1381    <tbody>
1382     <row>
1383      <entry>relid</entry>
1384      <entry><type>oid</></entry>
1385      <entry>The OID of the table for this index</entry>
1386     </row>
1387     <row>
1388      <entry>indexrelid</entry>
1389      <entry><type>oid</></entry>
1390      <entry>The OID of the index</entry>
1391     </row>
1392     <row>
1393      <entry>schemaname</entry>
1394      <entry><type>name</></entry>
1395      <entry>The name of the schema the index is in</entry>
1396     </row>
1397     <row>
1398      <entry>relname</entry>
1399      <entry><type>name</></entry>
1400      <entry>The name of the table for this index</entry>
1401     </row>
1402     <row>
1403      <entry>indexrelname</entry>
1404      <entry><type>name</></entry>
1405      <entry>The name of the index</entry>
1406     </row>
1407     <row>
1408      <entry>idx_scan</entry>
1409      <entry><type>bigint</></entry>
1410      <entry>Number of index scans initiated on this index.
1411      This value can also be returned by directly calling
1412      the <function>pg_stat_get_numscans</function> function.</entry>
1413     </row>
1414     <row>
1415      <entry>idx_tup_read</entry>
1416      <entry><type>bigint</></entry>
1417      <entry>Number of index entries returned by scans on this index.
1418      This value can also be returned by directly calling
1419      the <function>pg_stat_get_tuples_returned</function> function.</entry>
1420     </row>
1421     <row>
1422      <entry>idx_tup_fetch</entry>
1423      <entry><type>bigint</></entry>
1424      <entry>Number of live table rows fetched by simple index scans using this index.
1425      This value can also be returned by directly calling
1426      the <function>pg_stat_get_tuples_fetched</function> function.</entry>
1427     </row>
1428    </tbody>
1429    </tgroup>
1430   </table>
1431
1432   <para>
1433    The <structname>pg_stat_all_indexes</structname> view will contain
1434    one row for each index in the current database
1435    with information about accesses to this specific index. The
1436    <structname>pg_stat_user_indexes</structname> and
1437    <structname>pg_stat_sys_indexes</structname> contain the same information,
1438    but filtered to only have rows for user and system indexes.
1439   </para>
1440
1441   <table id="pg-statio-all-tables-view" xreflabel="pg_statio_all_tables">
1442    <title>pg_statio_all_tables view</title>
1443    <tgroup cols="3">
1444     <thead>
1445     <row>
1446       <entry>Column</entry>
1447       <entry>Type</entry>
1448       <entry>Description</entry>
1449      </row>
1450     </thead>
1451
1452    <tbody>
1453     <row>
1454      <entry>relid</entry>
1455      <entry><type>oid</></entry>
1456      <entry>The OID of the table</entry>
1457     </row>
1458     <row>
1459      <entry>schemaname</entry>
1460      <entry><type>name</></entry>
1461      <entry>The name of the schema that the table is in</entry>
1462     </row>
1463     <row>
1464      <entry>relname</entry>
1465      <entry><type>name</></entry>
1466      <entry>The name of the table</entry>
1467     </row>
1468     <row>
1469      <entry>heap_blks_read</entry>
1470      <entry><type>name</></entry>
1471      <entry>Number of disk blocks read from this table.
1472      This value can also be returned by directly calling
1473      the <function>pg_stat_get_blocks_fetched</function> and
1474      <function>pg_stat_get_blocks_hit</function> functions and
1475      subtracting the results.</entry>
1476     </row>
1477     <row>
1478      <entry>heap_blks_hit</entry>
1479      <entry><type>name</></entry>
1480      <entry>Number of buffer hits in this table.
1481      This value can also be returned by directly calling
1482      the <function>pg_stat_get_blocks_hit</function> function.</entry>
1483     </row>
1484     <row>
1485      <entry>idx_blks_read</entry>
1486      <entry><type>name</></entry>
1487      <entry>Number of disk blocks read from all indexes on this table</entry>
1488     </row>
1489     <row>
1490      <entry>idx_blks_hit</entry>
1491      <entry><type>name</></entry>
1492      <entry>Number of buffer hits in all indexes of this table.</entry>
1493     </row>
1494     <row>
1495      <entry>toast_blks_read</entry>
1496      <entry><type>name</></entry>
1497      <entry>Number of disk blocks read from this table's TOAST table (if any)</entry>
1498     </row>
1499     <row>
1500      <entry>toast_blks_hit</entry>
1501      <entry><type>name</></entry>
1502      <entry>Number of buffer hits in this table's TOAST table (if any)</entry>
1503     </row>
1504     <row>
1505      <entry>tidx_blks_read</entry>
1506      <entry><type>name</></entry>
1507      <entry>Number of disk blocks read from this table's TOAST table index (if any)</entry>
1508     </row>
1509     <row>
1510      <entry>tidx_blks_hit</entry>
1511      <entry><type>name</></entry>
1512      <entry>Number of buffer hits in this table's TOAST table index (if any)</entry>
1513     </row>
1514    </tbody>
1515    </tgroup>
1516   </table>
1517
1518   <para>
1519    The <structname>pg_statio_all_tables</structname> view will contain
1520    one row for each table in the current database (including TOAST
1521    tables) with information about I/O on this specific table. The
1522    <structname>pg_statio_user_tables</structname> and
1523    <structname>pg_statio_sys_tables</structname> contain the same information,
1524    but filtered to only have rows for user and system tables.
1525   </para>
1526
1527   <table id="pg-statio-all-indexes-view" xreflabel="pg_statio_all_indexes">
1528    <title>pg_statio_all_indexes view</title>
1529    <tgroup cols="3">
1530     <thead>
1531     <row>
1532       <entry>Column</entry>
1533       <entry>Type</entry>
1534       <entry>Description</entry>
1535      </row>
1536     </thead>
1537
1538    <tbody>
1539     <row>
1540      <entry>relid</entry>
1541      <entry><type>oid</></entry>
1542      <entry>The OID of the table for this index</entry>
1543     </row>
1544     <row>
1545      <entry>indexrelid</entry>
1546      <entry><type>oid</></entry>
1547      <entry>The OID of the index</entry>
1548     </row>
1549     <row>
1550      <entry>schemaname</entry>
1551      <entry><type>name</></entry>
1552      <entry>The name of the schema the index is in</entry>
1553     </row>
1554     <row>
1555      <entry>relname</entry>
1556      <entry><type>name</></entry>
1557      <entry>The name of the table for this index</entry>
1558     </row>
1559     <row>
1560      <entry>indexrelname</entry>
1561      <entry><type>name</></entry>
1562      <entry>The name of the index</entry>
1563     </row>
1564     <row>
1565      <entry>idx_blks_read</entry>
1566      <entry><type>name</></entry>
1567      <entry>Number of disk blocks read from the index.
1568      This value can also be returned by directly calling
1569      the <function>pg_stat_get_blocks_fetched</function> and
1570      <function>pg_stat_get_blocks_hit</function> functions and
1571      subtracting the results.</entry>
1572     </row>
1573     <row>
1574      <entry>idx_blks_hit</entry>
1575      <entry><type>name</></entry>
1576      <entry>Number of buffer hits in the index.
1577      This value can also be returned by directly calling
1578      the <function>pg_stat_get_blocks_hit</function> function.</entry>
1579     </row>
1580    </tbody>
1581    </tgroup>
1582   </table>
1583
1584   <para>
1585    The <structname>pg_statio_all_indexes</structname> view will contain
1586    one row for each index in the current database
1587    with information about I/O on this specific index. The
1588    <structname>pg_statio_user_indexes</structname> and
1589    <structname>pg_statio_sys_indexes</structname> contain the same information,
1590    but filtered to only have rows for user and system indexes.
1591   </para>
1592
1593   <table id="pg-statio-all-sequences-view" xreflabel="pg_statio_all_sequences">
1594    <title>pg_statio_all_sequences view</title>
1595    <tgroup cols="3">
1596     <thead>
1597     <row>
1598       <entry>Column</entry>
1599       <entry>Type</entry>
1600       <entry>Description</entry>
1601      </row>
1602     </thead>
1603
1604    <tbody>
1605     <row>
1606      <entry>relid</entry>
1607      <entry><type>oid</></entry>
1608      <entry>The OID of the sequence</entry>
1609     </row>
1610     <row>
1611      <entry>schemaname</entry>
1612      <entry><type>name</></entry>
1613      <entry>The name of the schema the sequence is in</entry>
1614     </row>
1615     <row>
1616      <entry>relname</entry>
1617      <entry><type>name</></entry>
1618      <entry>The name of the sequence</entry>
1619     </row>
1620     <row>
1621      <entry>blks_read</entry>
1622      <entry><type>name</></entry>
1623      <entry>Number of disk blocks read from the sequence</entry>
1624     </row>
1625     <row>
1626      <entry>blks_hit</entry>
1627      <entry><type>name</></entry>
1628      <entry>Number of buffer hits in the sequence</entry>
1629     </row>
1630    </tbody>
1631    </tgroup>
1632   </table>
1633
1634   <para>
1635    The <structname>pg_statio_all_indexes</structname> view will contain
1636    one row for each sequence in the current database
1637    with information about I/O on this specific sequence.
1638   </para>
1639
1640   <table id="pg-stat-user-functions-view" xreflabel="pg_stat_user_functions">
1641    <title>pg_stat_user_functions view</title>
1642    <tgroup cols="3">
1643     <thead>
1644     <row>
1645       <entry>Column</entry>
1646       <entry>Type</entry>
1647       <entry>Description</entry>
1648      </row>
1649     </thead>
1650
1651    <tbody>
1652     <row>
1653      <entry>funcid</entry>
1654      <entry><type>oid</></entry>
1655      <entry>The OID of the function</entry>
1656     </row>
1657     <row>
1658      <entry>schemaname</entry>
1659      <entry><type>name</></entry>
1660      <entry>The name of the schema the function is in</entry>
1661     </row>
1662     <row>
1663      <entry>funcname</entry>
1664      <entry><type>name</></entry>
1665      <entry>The name of the function</entry>
1666     </row>
1667     <row>
1668      <entry>calls</entry>
1669      <entry><type>bigint</></entry>
1670      <entry>Number of times the function has been called.
1671      This value can also be returned by directly calling
1672      the <function>pg_stat_get_function_calls</function> function.</entry>
1673     </row>
1674     <row>
1675      <entry>total_time</entry>
1676      <entry><type>bigint</></entry>
1677      <entry>Total time spent in this functions and all other functions
1678      called by it, in milliseconds.
1679      The same value can be returned in microseconds by directly calling
1680      the <function>pg_stat_get_function_time</function> function.</entry>
1681     </row>
1682     <row>
1683      <entry>self_time</entry>
1684      <entry><type>bigint</></entry>
1685      <entry>Total time spent in this functions itself but not including
1686      other functions called by it, in milliseconds.
1687      The same value can be returned in microseconds by directly calling
1688      the <function>pg_stat_get_function_self_time</function> function.</entry>
1689     </row>
1690    </tbody>
1691    </tgroup>
1692   </table>
1693
1694   <para>
1695    The <structname>pg_stat_user_functions</structname> view will contain
1696    one row for each tracked function (as specified by the
1697    <xref linkend="guc-track-functions"> parameter).
1698   </para>
1699
1700  <sect3 id="monitoring-stats-functions">
1701   <title>Other Statistics Functions</title>
1702
1703
1704   <table id="monitoring-stats-funcs-table">
1705    <title>Other Statistics Functions</title>
1706
1707    <tgroup cols="3">
1708     <thead>
1709      <row>
1710       <entry>Function</entry>
1711       <entry>Return Type</entry>
1712       <entry>Description</entry>
1713      </row>
1714     </thead>
1715
1716     <tbody>
1717
1718      <row>
1719        <!-- See also the entry for this in func.sgml -->
1720       <entry><literal><function>pg_backend_pid()</function></literal></entry>
1721       <entry><type>integer</type></entry>
1722       <entry>
1723        Process ID of the server process attached to the current session
1724       </entry>
1725      </row>
1726
1727      <row>
1728       <entry><literal><function>pg_stat_get_activity</function>(<type>integer</type>)</literal></entry>
1729       <entry><type>setof record</type></entry>
1730       <entry>
1731        Returns a record of information about the backend with the specified PID, or
1732        one record for each active backend in the system if <symbol>NULL</symbol> is
1733        specified. The fields returned are a subset of those in the
1734        <structname>pg_stat_activity</structname> view.
1735       </entry>
1736      </row>
1737
1738      <row>
1739       <entry><literal><function>pg_stat_get_backend_idset()</function></literal></entry>
1740       <entry><type>setof integer</type></entry>
1741       <entry>
1742        Set of currently active server process numbers (from 1 to the
1743        number of active server processes).  See usage example in the text.
1744       </entry>
1745      </row>
1746
1747
1748      <row>
1749       <entry><literal><function>pg_stat_get_wal_senders()</function></literal></entry>
1750       <entry><type>setof record</type></entry>
1751       <entry>
1752        One record for each active wal sender. The fields returned are a subset
1753        of those in the <structname>pg_stat_replication</structname> view.
1754       </entry>
1755      </row>
1756
1757      <row>
1758       <entry><literal><function>pg_stat_clear_snapshot()</function></literal></entry>
1759       <entry><type>void</type></entry>
1760       <entry>
1761        Discard the current statistics snapshot
1762       </entry>
1763      </row>
1764
1765      <row>
1766       <entry><literal><function>pg_stat_reset()</function></literal></entry>
1767       <entry><type>void</type></entry>
1768       <entry>
1769        Reset all statistics counters for the current database to zero
1770        (requires superuser privileges)
1771       </entry>
1772      </row>
1773
1774      <row>
1775       <entry><literal><function>pg_stat_reset_shared</function>(text)</literal></entry>
1776       <entry><type>void</type></entry>
1777       <entry>
1778        Reset some of the shared statistics counters for the database cluster to
1779        zero (requires superuser privileges).  Calling
1780        <literal>pg_stat_reset_shared('bgwriter')</> will zero all the values shown by
1781        <structname>pg_stat_bgwriter</>.
1782       </entry>
1783      </row>
1784
1785      <row>
1786       <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal></entry>
1787       <entry><type>void</type></entry>
1788       <entry>
1789        Reset statistics for a single table or index in the current database to
1790        zero (requires superuser privileges)
1791       </entry>
1792      </row>
1793
1794      <row>
1795       <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal></entry>
1796       <entry><type>void</type></entry>
1797       <entry>
1798        Reset statistics for a single function in the current database to
1799        zero (requires superuser privileges)
1800       </entry>
1801      </row>
1802     </tbody>
1803    </tgroup>
1804   </table>
1805
1806  </sect3>
1807  </sect2>
1808  </sect1>
1809
1810  <sect1 id="monitoring-locks">
1811   <title>Viewing Locks</title>
1812
1813   <indexterm zone="monitoring-locks">
1814    <primary>lock</primary>
1815    <secondary>monitoring</secondary>
1816   </indexterm>
1817
1818   <para>
1819    Another useful tool for monitoring database activity is the
1820    <structname>pg_locks</structname> system table.  It allows the
1821    database administrator to view information about the outstanding
1822    locks in the lock manager. For example, this capability can be used
1823    to:
1824
1825    <itemizedlist>
1826     <listitem>
1827      <para>
1828       View all the locks currently outstanding, all the locks on
1829       relations in a particular database, all the locks on a
1830       particular relation, or all the locks held by a particular
1831       <productname>PostgreSQL</productname> session.
1832      </para>
1833     </listitem>
1834
1835     <listitem>
1836      <para>
1837       Determine the relation in the current database with the most
1838       ungranted locks (which might be a source of contention among
1839       database clients).
1840      </para>
1841     </listitem>
1842
1843     <listitem>
1844      <para>
1845       Determine the effect of lock contention on overall database
1846       performance, as well as the extent to which contention varies
1847       with overall database traffic.
1848      </para>
1849     </listitem>
1850    </itemizedlist>
1851
1852    Details of the <structname>pg_locks</structname> view appear in
1853    <xref linkend="view-pg-locks">.
1854    For more information on locking and managing concurrency with
1855    <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
1856   </para>
1857  </sect1>
1858
1859  <sect1 id="dynamic-trace">
1860   <title>Dynamic Tracing</title>
1861
1862  <indexterm zone="dynamic-trace">
1863   <primary>DTrace</primary>
1864  </indexterm>
1865
1866   <para>
1867    <productname>PostgreSQL</productname> provides facilities to support
1868    dynamic tracing of the database server. This allows an external
1869    utility to be called at specific points in the code and thereby trace
1870    execution.
1871   </para>
1872
1873   <para>
1874    A number of probes or trace points are already inserted into the source
1875    code. These probes are intended to be used by database developers and
1876    administrators. By default the probes are not compiled into
1877    <productname>PostgreSQL</productname>; the user needs to explicitly tell
1878    the configure script to make the probes available.
1879   </para>
1880
1881   <para>
1882    Currently, only the
1883    <ulink url="http://opensolaris.org/os/community/dtrace/">DTrace</ulink>
1884    utility is supported, which is available
1885    on OpenSolaris, Solaris 10, and Mac OS X Leopard. It is expected that
1886    DTrace will be available in the future on FreeBSD and possibly other
1887    operating systems.  The
1888    <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> project
1889    for Linux also provides a DTrace equivalent.  Supporting other dynamic
1890    tracing utilities is theoretically possible by changing the definitions for
1891    the macros in <filename>src/include/utils/probes.h</>.
1892   </para>
1893
1894   <sect2 id="compiling-for-trace">
1895    <title>Compiling for Dynamic Tracing</title>
1896
1897   <para>
1898    By default, probes are not available, so you will need to
1899    explicitly tell the configure script to make the probes available
1900    in <productname>PostgreSQL</productname>. To include DTrace support
1901    specify <option>--enable-dtrace</> to configure.  See <xref
1902    linkend="install-procedure"> for further information.
1903   </para>
1904   </sect2>
1905
1906   <sect2 id="trace-points">
1907    <title>Built-in Probes</title>
1908
1909   <para>
1910    A number of standard probes are provided in the source code,
1911    as shown in <xref linkend="dtrace-probe-point-table">;
1912    <xref linkend="typedefs-table">
1913    shows the types used in the probes.  More probes can certainly be
1914    added to enhance <productname>PostgreSQL</>'s observability.
1915   </para>
1916
1917  <table id="dtrace-probe-point-table">
1918   <title>Built-in DTrace Probes</title>
1919   <tgroup cols="3">
1920    <thead>
1921     <row>
1922      <entry>Name</entry>
1923      <entry>Parameters</entry>
1924      <entry>Description</entry>
1925     </row>
1926    </thead>
1927
1928    <tbody>
1929
1930     <row>
1931      <entry>transaction-start</entry>
1932      <entry>(LocalTransactionId)</entry>
1933      <entry>Probe that fires at the start of a new transaction.
1934       arg0 is the transaction ID.</entry>
1935     </row>
1936     <row>
1937      <entry>transaction-commit</entry>
1938      <entry>(LocalTransactionId)</entry>
1939      <entry>Probe that fires when a transaction completes successfully.
1940       arg0 is the transaction ID.</entry>
1941     </row>
1942     <row>
1943      <entry>transaction-abort</entry>
1944      <entry>(LocalTransactionId)</entry>
1945      <entry>Probe that fires when a transaction completes unsuccessfully.
1946       arg0 is the transaction ID.</entry>
1947     </row>
1948     <row>
1949      <entry>query-start</entry>
1950      <entry>(const char *)</entry>
1951      <entry>Probe that fires when the processing of a query is started.
1952       arg0 is the query string.</entry>
1953     </row>
1954     <row>
1955      <entry>query-done</entry>
1956      <entry>(const char *)</entry>
1957      <entry>Probe that fires when the processing of a query is complete.
1958       arg0 is the query string.</entry>
1959     </row>
1960     <row>
1961      <entry>query-parse-start</entry>
1962      <entry>(const char *)</entry>
1963      <entry>Probe that fires when the parsing of a query is started.
1964       arg0 is the query string.</entry>
1965     </row>
1966     <row>
1967      <entry>query-parse-done</entry>
1968      <entry>(const char *)</entry>
1969      <entry>Probe that fires when the parsing of a query is complete.
1970       arg0 is the query string.</entry>
1971     </row>
1972     <row>
1973      <entry>query-rewrite-start</entry>
1974      <entry>(const char *)</entry>
1975      <entry>Probe that fires when the rewriting of a query is started.
1976       arg0 is the query string.</entry>
1977     </row>
1978     <row>
1979      <entry>query-rewrite-done</entry>
1980      <entry>(const char *)</entry>
1981      <entry>Probe that fires when the rewriting of a query is complete.
1982       arg0 is the query string.</entry>
1983     </row>
1984     <row>
1985      <entry>query-plan-start</entry>
1986      <entry>()</entry>
1987      <entry>Probe that fires when the planning of a query is started.</entry>
1988     </row>
1989     <row>
1990      <entry>query-plan-done</entry>
1991      <entry>()</entry>
1992      <entry>Probe that fires when the planning of a query is complete.</entry>
1993     </row>
1994     <row>
1995      <entry>query-execute-start</entry>
1996      <entry>()</entry>
1997      <entry>Probe that fires when the execution of a query is started.</entry>
1998     </row>
1999     <row>
2000      <entry>query-execute-done</entry>
2001      <entry>()</entry>
2002      <entry>Probe that fires when the execution of a query is complete.</entry>
2003     </row>
2004     <row>
2005      <entry>statement-status</entry>
2006      <entry>(const char *)</entry>
2007      <entry>Probe that fires anytime the server process updates its
2008       <structname>pg_stat_activity</>.<structfield>status</>.
2009       arg0 is the new status string.</entry>
2010     </row>
2011     <row>
2012      <entry>checkpoint-start</entry>
2013      <entry>(int)</entry>
2014      <entry>Probe that fires when a checkpoint is started.
2015       arg0 holds the bitwise flags used to distinguish different checkpoint
2016       types, such as shutdown, immediate or force.</entry>
2017     </row>
2018     <row>
2019      <entry>checkpoint-done</entry>
2020      <entry>(int, int, int, int, int)</entry>
2021      <entry>Probe that fires when a checkpoint is complete.
2022       (The probes listed next fire in sequence during checkpoint processing.)
2023       arg0 is the number of buffers written. arg1 is the total number of
2024       buffers. arg2, arg3 and arg4 contain the number of xlog file(s) added,
2025       removed and recycled respectively.</entry>
2026     </row>
2027     <row>
2028      <entry>clog-checkpoint-start</entry>
2029      <entry>(bool)</entry>
2030      <entry>Probe that fires when the CLOG portion of a checkpoint is started.
2031       arg0 is true for normal checkpoint, false for shutdown
2032       checkpoint.</entry>
2033     </row>
2034     <row>
2035      <entry>clog-checkpoint-done</entry>
2036      <entry>(bool)</entry>
2037      <entry>Probe that fires when the CLOG portion of a checkpoint is
2038       complete. arg0 has the same meaning as for clog-checkpoint-start.</entry>
2039     </row>
2040     <row>
2041      <entry>subtrans-checkpoint-start</entry>
2042      <entry>(bool)</entry>
2043      <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
2044       started.
2045       arg0 is true for normal checkpoint, false for shutdown
2046       checkpoint.</entry>
2047     </row>
2048     <row>
2049      <entry>subtrans-checkpoint-done</entry>
2050      <entry>(bool)</entry>
2051      <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
2052       complete. arg0 has the same meaning as for
2053       subtrans-checkpoint-start.</entry>
2054     </row>
2055     <row>
2056      <entry>multixact-checkpoint-start</entry>
2057      <entry>(bool)</entry>
2058      <entry>Probe that fires when the MultiXact portion of a checkpoint is
2059       started.
2060       arg0 is true for normal checkpoint, false for shutdown
2061       checkpoint.</entry>
2062     </row>
2063     <row>
2064      <entry>multixact-checkpoint-done</entry>
2065      <entry>(bool)</entry>
2066      <entry>Probe that fires when the MultiXact portion of a checkpoint is
2067       complete. arg0 has the same meaning as for
2068       multixact-checkpoint-start.</entry>
2069     </row>
2070     <row>
2071      <entry>buffer-checkpoint-start</entry>
2072      <entry>(int)</entry>
2073      <entry>Probe that fires when the buffer-writing portion of a checkpoint
2074       is started.
2075       arg0 holds the bitwise flags used to distinguish different checkpoint
2076       types, such as shutdown, immediate or force.</entry>
2077     </row>
2078     <row>
2079      <entry>buffer-sync-start</entry>
2080      <entry>(int, int)</entry>
2081      <entry>Probe that fires when we begin to write dirty buffers during
2082       checkpoint (after identifying which buffers must be written).
2083       arg0 is the total number of buffers.
2084       arg1 is the number that are currently dirty and need to be written.</entry>
2085     </row>
2086     <row>
2087      <entry>buffer-sync-written</entry>
2088      <entry>(int)</entry>
2089      <entry>Probe that fires after each buffer is written during checkpoint.
2090       arg0 is the ID number of the buffer.</entry>
2091     </row>
2092     <row>
2093      <entry>buffer-sync-done</entry>
2094      <entry>(int, int, int)</entry>
2095      <entry>Probe that fires when all dirty buffers have been written.
2096       arg0 is the total number of buffers.
2097       arg1 is the number of buffers actually written by the checkpoint process.
2098       arg2 is the number that were expected to be written (arg1 of
2099       buffer-sync-start); any difference reflects other processes flushing
2100       buffers during the checkpoint.</entry>
2101     </row>
2102     <row>
2103      <entry>buffer-checkpoint-sync-start</entry>
2104      <entry>()</entry>
2105      <entry>Probe that fires after dirty buffers have been written to the
2106       kernel, and before starting to issue fsync requests.</entry>
2107     </row>
2108     <row>
2109      <entry>buffer-checkpoint-done</entry>
2110      <entry>()</entry>
2111      <entry>Probe that fires when syncing of buffers to disk is
2112       complete.</entry>
2113     </row>
2114     <row>
2115      <entry>twophase-checkpoint-start</entry>
2116      <entry>()</entry>
2117      <entry>Probe that fires when the two-phase portion of a checkpoint is
2118       started.</entry>
2119     </row>
2120     <row>
2121      <entry>twophase-checkpoint-done</entry>
2122      <entry>()</entry>
2123      <entry>Probe that fires when the two-phase portion of a checkpoint is
2124       complete.</entry>
2125     </row>
2126     <row>
2127      <entry>buffer-read-start</entry>
2128      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</entry>
2129      <entry>Probe that fires when a buffer read is started.
2130       arg0 and arg1 contain the fork and block numbers of the page (but
2131       arg1 will be -1 if this is a relation extension request).
2132       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2133       identifying the relation.
2134       arg5 is the ID of the backend which created the temporary relation for a
2135       local buffer, or InvalidBackendId (-1) for a shared buffer.
2136       arg6 is true for a relation extension request, false for normal
2137       read.</entry>
2138     </row>
2139     <row>
2140      <entry>buffer-read-done</entry>
2141      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool, bool)</entry>
2142      <entry>Probe that fires when a buffer read is complete.
2143       arg0 and arg1 contain the fork and block numbers of the page (if this
2144       is a relation extension request, arg1 now contains the block number
2145       of the newly added block).
2146       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2147       identifying the relation.
2148       arg5 is the ID of the backend which created the temporary relation for a
2149       local buffer, or InvalidBackendId (-1) for a shared buffer.
2150       arg6 is true for a relation extension request, false for normal
2151       read.
2152       arg7 is true if the buffer was found in the pool, false if not.</entry>
2153     </row>
2154     <row>
2155      <entry>buffer-flush-start</entry>
2156      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2157      <entry>Probe that fires before issuing any write request for a shared
2158       buffer.
2159       arg0 and arg1 contain the fork and block numbers of the page.
2160       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2161       identifying the relation.</entry>
2162     </row>
2163     <row>
2164      <entry>buffer-flush-done</entry>
2165      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2166      <entry>Probe that fires when a write request is complete.  (Note
2167       that this just reflects the time to pass the data to the kernel;
2168       it's typically not actually been written to disk yet.)
2169       The arguments are the same as for buffer-flush-start.</entry>
2170     </row>
2171     <row>
2172      <entry>buffer-write-dirty-start</entry>
2173      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2174      <entry>Probe that fires when a server process begins to write a dirty
2175       buffer.  (If this happens often, it implies that
2176       <xref linkend="guc-shared-buffers"> is too
2177       small or the bgwriter control parameters need adjustment.)
2178       arg0 and arg1 contain the fork and block numbers of the page.
2179       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2180       identifying the relation.</entry>
2181     </row>
2182     <row>
2183      <entry>buffer-write-dirty-done</entry>
2184      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2185      <entry>Probe that fires when a dirty-buffer write is complete.
2186       The arguments are the same as for buffer-write-dirty-start.</entry>
2187     </row>
2188     <row>
2189      <entry>wal-buffer-write-dirty-start</entry>
2190      <entry>()</entry>
2191      <entry>Probe that fires when when a server process begins to write a
2192       dirty WAL buffer because no more WAL buffer space is available.
2193       (If this happens often, it implies that
2194       <xref linkend="guc-wal-buffers"> is too small.)</entry>
2195     </row>
2196     <row>
2197      <entry>wal-buffer-write-dirty-done</entry>
2198      <entry>()</entry>
2199      <entry>Probe that fires when a dirty WAL buffer write is complete.</entry>
2200     </row>
2201     <row>
2202      <entry>xlog-insert</entry>
2203      <entry>(unsigned char, unsigned char)</entry>
2204      <entry>Probe that fires when a WAL record is inserted.
2205       arg0 is the resource manager (rmid) for the record.
2206       arg1 contains the info flags.</entry>
2207     </row>
2208     <row>
2209      <entry>xlog-switch</entry>
2210      <entry>()</entry>
2211      <entry>Probe that fires when a WAL segment switch is requested.</entry>
2212     </row>
2213     <row>
2214      <entry>smgr-md-read-start</entry>
2215      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
2216      <entry>Probe that fires when beginning to read a block from a relation.
2217       arg0 and arg1 contain the fork and block numbers of the page.
2218       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2219       identifying the relation.
2220       arg5 is the ID of the backend which created the temporary relation for a
2221       local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
2222     </row>
2223     <row>
2224      <entry>smgr-md-read-done</entry>
2225      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
2226      <entry>Probe that fires when a block read is complete.
2227       arg0 and arg1 contain the fork and block numbers of the page.
2228       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2229       identifying the relation.
2230       arg5 is the ID of the backend which created the temporary relation for a
2231       local buffer, or InvalidBackendId (-1) for a shared buffer.
2232       arg6 is the number of bytes actually read, while arg7 is the number
2233       requested (if these are different it indicates trouble).</entry>
2234     </row>
2235     <row>
2236      <entry>smgr-md-write-start</entry>
2237      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
2238      <entry>Probe that fires when beginning to write a block to a relation.
2239       arg0 and arg1 contain the fork and block numbers of the page.
2240       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2241       identifying the relation.
2242       arg5 is the ID of the backend which created the temporary relation for a
2243       local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
2244     </row>
2245     <row>
2246      <entry>smgr-md-write-done</entry>
2247      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
2248      <entry>Probe that fires when a block write is complete.
2249       arg0 and arg1 contain the fork and block numbers of the page.
2250       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2251       identifying the relation.
2252       arg5 is the ID of the backend which created the temporary relation for a
2253       local buffer, or InvalidBackendId (-1) for a shared buffer.
2254       arg6 is the number of bytes actually written, while arg7 is the number
2255       requested (if these are different it indicates trouble).</entry>
2256     </row>
2257     <row>
2258      <entry>sort-start</entry>
2259      <entry>(int, bool, int, int, bool)</entry>
2260      <entry>Probe that fires when a sort operation is started.
2261       arg0 indicates heap, index or datum sort.
2262       arg1 is true for unique-value enforcement.
2263       arg2 is the number of key columns.
2264       arg3 is the number of kilobytes of work memory allowed.
2265       arg4 is true if random access to the sort result is required.</entry>
2266     </row>
2267     <row>
2268      <entry>sort-done</entry>
2269      <entry>(bool, long)</entry>
2270      <entry>Probe that fires when a sort is complete.
2271       arg0 is true for external sort, false for internal sort.
2272       arg1 is the number of disk blocks used for an external sort,
2273       or kilobytes of memory used for an internal sort.</entry>
2274     </row>
2275     <row>
2276      <entry>lwlock-acquire</entry>
2277      <entry>(LWLockId, LWLockMode)</entry>
2278      <entry>Probe that fires when an LWLock has been acquired.
2279       arg0 is the LWLock's ID.
2280       arg1 is the requested lock mode, either exclusive or shared.</entry>
2281     </row>
2282     <row>
2283      <entry>lwlock-release</entry>
2284      <entry>(LWLockId)</entry>
2285      <entry>Probe that fires when an LWLock has been released (but note
2286       that any released waiters have not yet been awakened).
2287       arg0 is the LWLock's ID.</entry>
2288     </row>
2289     <row>
2290      <entry>lwlock-wait-start</entry>
2291      <entry>(LWLockId, LWLockMode)</entry>
2292      <entry>Probe that fires when an LWLock was not immediately available and
2293       a server process has begun to wait for the lock to become available.
2294       arg0 is the LWLock's ID.
2295       arg1 is the requested lock mode, either exclusive or shared.</entry>
2296     </row>
2297     <row>
2298      <entry>lwlock-wait-done</entry>
2299      <entry>(LWLockId, LWLockMode)</entry>
2300      <entry>Probe that fires when a server process has been released from its
2301       wait for an LWLock (it does not actually have the lock yet).
2302       arg0 is the LWLock's ID.
2303       arg1 is the requested lock mode, either exclusive or shared.</entry>
2304     </row>
2305     <row>
2306      <entry>lwlock-condacquire</entry>
2307      <entry>(LWLockId, LWLockMode)</entry>
2308      <entry>Probe that fires when an LWLock was successfully acquired when the
2309       caller specified no waiting.
2310       arg0 is the LWLock's ID.
2311       arg1 is the requested lock mode, either exclusive or shared.</entry>
2312     </row>
2313     <row>
2314      <entry>lwlock-condacquire-fail</entry>
2315      <entry>(LWLockId, LWLockMode)</entry>
2316      <entry>Probe that fires when an LWLock was not successfully acquired when
2317       the caller specified no waiting.
2318       arg0 is the LWLock's ID.
2319       arg1 is the requested lock mode, either exclusive or shared.</entry>
2320     </row>
2321     <row>
2322      <entry>lock-wait-start</entry>
2323      <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
2324      <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
2325       has begun to wait because the lock is not available.
2326       arg0 through arg3 are the tag fields identifying the object being
2327       locked.  arg4 indicates the type of object being locked.
2328       arg5 indicates the lock type being requested.</entry>
2329     </row>
2330     <row>
2331      <entry>lock-wait-done</entry>
2332      <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
2333      <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
2334       has finished waiting (i.e., has acquired the lock).
2335       The arguments are the same as for lock-wait-start.</entry>
2336     </row>
2337     <row>
2338      <entry>deadlock-found</entry>
2339      <entry>()</entry>
2340      <entry>Probe that fires when a deadlock is found by the deadlock
2341       detector.</entry>
2342     </row>
2343
2344    </tbody>
2345    </tgroup>
2346   </table>
2347
2348  <table id="typedefs-table">
2349   <title>Defined Types Used in Probe Parameters</title>
2350   <tgroup cols="2">
2351    <thead>
2352     <row>
2353      <entry>Type</entry>
2354      <entry>Definition</entry>
2355     </row>
2356    </thead>
2357
2358    <tbody>
2359
2360     <row>
2361      <entry>LocalTransactionId</entry>
2362      <entry>unsigned int</entry>
2363     </row>
2364     <row>
2365      <entry>LWLockId</entry>
2366      <entry>int</entry>
2367     </row>
2368     <row>
2369      <entry>LWLockMode</entry>
2370      <entry>int</entry>
2371     </row>
2372     <row>
2373      <entry>LOCKMODE</entry>
2374      <entry>int</entry>
2375     </row>
2376     <row>
2377      <entry>BlockNumber</entry>
2378      <entry>unsigned int</entry>
2379     </row>
2380     <row>
2381      <entry>Oid</entry>
2382      <entry>unsigned int</entry>
2383     </row>
2384     <row>
2385      <entry>ForkNumber</entry>
2386      <entry>int</entry>
2387     </row>
2388     <row>
2389      <entry>bool</entry>
2390      <entry>char</entry>
2391     </row>
2392
2393    </tbody>
2394    </tgroup>
2395   </table>
2396
2397
2398   </sect2>
2399
2400   <sect2 id="using-trace-points">
2401    <title>Using Probes</title>
2402
2403   <para>
2404    The example below shows a DTrace script for analyzing transaction
2405    counts in the system, as an alternative to snapshotting
2406    <structname>pg_stat_database</> before and after a performance test:
2407 <programlisting>
2408 #!/usr/sbin/dtrace -qs
2409
2410 postgresql$1:::transaction-start
2411 {
2412       @start["Start"] = count();
2413       self->ts  = timestamp;
2414 }
2415
2416 postgresql$1:::transaction-abort
2417 {
2418       @abort["Abort"] = count();
2419 }
2420
2421 postgresql$1:::transaction-commit
2422 /self->ts/
2423 {
2424       @commit["Commit"] = count();
2425       @time["Total time (ns)"] = sum(timestamp - self->ts);
2426       self->ts=0;
2427 }
2428 </programlisting>
2429    When executed, the example D script gives output such as:
2430 <screen>
2431 # ./txn_count.d `pgrep -n postgres` or ./txn_count.d &lt;PID&gt;
2432 ^C
2433
2434 Start                                          71
2435 Commit                                         70
2436 Total time (ns)                        2312105013
2437 </screen>
2438   </para>
2439
2440   <note>
2441    <para>
2442     SystemTap uses a different notation for trace scripts than DTrace does,
2443     even though the underlying trace points are compatible.  One point worth
2444     noting is that at this writing, SystemTap scripts must reference probe
2445     names using double underscores in place of hyphens.  This is expected to
2446     be fixed in future SystemTap releases.
2447    </para>
2448   </note>
2449
2450   <para>
2451    You should remember that DTrace scripts need to be carefully written and
2452    debugged, otherwise the trace information collected might
2453    be meaningless. In most cases where problems are found it is the
2454    instrumentation that is at fault, not the underlying system. When
2455    discussing information found using dynamic tracing, be sure to enclose
2456    the script used to allow that too to be checked and discussed.
2457   </para>
2458
2459   <para>
2460    More example scripts can be found in the PgFoundry
2461    <ulink url="http://pgfoundry.org/projects/dtrace/">dtrace project</ulink>.
2462   </para>
2463   </sect2>
2464
2465   <sect2 id="defining-trace-points">
2466    <title>Defining New Probes</title>
2467
2468   <para>
2469    New probes can be defined within the code wherever the developer
2470    desires, though this will require a recompilation. Below are the steps
2471    for inserting new probes:
2472   </para>
2473
2474   <procedure>
2475    <step>
2476     <para>
2477      Decide on probe names and data to be made available through the probes
2478     </para>
2479    </step>
2480
2481    <step>
2482     <para>
2483      Add the probe definitions to <filename>src/backend/utils/probes.d</>
2484     </para>
2485    </step>
2486
2487    <step>
2488     <para>
2489      Include <filename>pg_trace.h</> if it is not already present in the
2490      module(s) containing the probe points, and insert
2491      <literal>TRACE_POSTGRESQL</> probe macros at the desired locations
2492      in the source code
2493     </para>
2494    </step>
2495
2496    <step>
2497     <para>
2498      Recompile and verify that the new probes are available
2499     </para>
2500    </step>
2501   </procedure>
2502
2503   <formalpara>
2504    <title>Example:</title>
2505    <para>
2506     Here is an example of how you would add a probe to trace all new
2507     transactions by transaction ID.
2508    </para>
2509   </formalpara>
2510
2511   <procedure>
2512    <step>
2513     <para>
2514      Decide that the probe will be named <literal>transaction-start</> and
2515      requires a parameter of type LocalTransactionId
2516     </para>
2517    </step>
2518
2519    <step>
2520     <para>
2521      Add the probe definition to <filename>src/backend/utils/probes.d</>:
2522 <programlisting>
2523 probe transaction__start(LocalTransactionId);
2524 </programlisting>
2525      Note the use of the double underline in the probe name. In a DTrace
2526      script using the probe, the double underline needs to be replaced with a
2527      hyphen, so <literal>transaction-start</> is the name to document for
2528      users.
2529     </para>
2530    </step>
2531
2532    <step>
2533     <para>
2534      At compile time, <literal>transaction__start</> is converted to a macro
2535      called <literal>TRACE_POSTGRESQL_TRANSACTION_START</> (notice the
2536      underscores are single here), which is available by including
2537      <filename>pg_trace.h</>.  Add the macro call to the appropriate location
2538      in the source code.  In this case, it looks like the following:
2539
2540 <programlisting>
2541 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
2542 </programlisting>
2543     </para>
2544    </step>
2545
2546    <step>
2547     <para>
2548      After recompiling and running the new binary, check that your newly added
2549      probe is available by executing the following DTrace command.  You
2550      should see similar output:
2551 <screen>
2552 # dtrace -ln transaction-start
2553    ID    PROVIDER          MODULE           FUNCTION NAME
2554 18705 postgresql49878     postgres     StartTransactionCommand transaction-start
2555 18755 postgresql49877     postgres     StartTransactionCommand transaction-start
2556 18805 postgresql49876     postgres     StartTransactionCommand transaction-start
2557 18855 postgresql49875     postgres     StartTransactionCommand transaction-start
2558 18986 postgresql49873     postgres     StartTransactionCommand transaction-start
2559 </screen>
2560     </para>
2561    </step>
2562   </procedure>
2563
2564   <para>
2565    There are a few things to be careful about when adding trace macros
2566    to the C code:
2567
2568    <itemizedlist>
2569     <listitem>
2570      <para>
2571       You should take care that the data types specified for a probe's
2572       parameters match the data types of the variables used in the macro.
2573       Otherwise, you will get compilation errors.
2574      </para>
2575     </listitem>
2576
2577
2578     <listitem>
2579      <para>
2580       On most platforms, if <productname>PostgreSQL</productname> is
2581       built with <option>--enable-dtrace</>, the arguments to a trace
2582       macro will be evaluated whenever control passes through the
2583       macro, <emphasis>even if no tracing is being done</>.  This is
2584       usually not worth worrying about if you are just reporting the
2585       values of a few local variables.  But beware of putting expensive
2586       function calls into the arguments.  If you need to do that,
2587       consider protecting the macro with a check to see if the trace
2588       is actually enabled:
2589
2590 <programlisting>
2591 if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
2592     TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
2593 </programlisting>
2594
2595       Each trace macro has a corresponding <literal>ENABLED</> macro.
2596      </para>
2597     </listitem>
2598    </itemizedlist>
2599
2600   </para>
2601
2602   </sect2>
2603
2604  </sect1>
2605
2606 </chapter>