]> granicus.if.org Git - postgresql/blob - doc/src/sgml/monitoring.sgml
Improve documentation about pg_stat_replication view.
[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  15551  0.0  0.1  57536  7132 pts/0    S    18:02   0:00 postgres -i
52 postgres  15554  0.0  0.0  57536  1184 ?        Ss   18:02   0:00 postgres: writer process
53 postgres  15555  0.0  0.0  57536   916 ?        Ss   18:02   0:00 postgres: checkpointer process
54 postgres  15556  0.0  0.0  57536   916 ?        Ss   18:02   0:00 postgres: wal writer process
55 postgres  15557  0.0  0.0  58504  2244 ?        Ss   18:02   0:00 postgres: autovacuum launcher process
56 postgres  15558  0.0  0.0  17512  1068 ?        Ss   18:02   0:00 postgres: stats collector process
57 postgres  15582  0.0  0.0  58772  3080 ?        Ss   18:04   0:00 postgres: joe runbug 127.0.0.1 idle
58 postgres  15606  0.0  0.0  58772  3052 ?        Ss   18:07   0:00 postgres: tgl regression [local] SELECT waiting
59 postgres  15610  0.0  0.0  58772  3056 ?        Ss   18:07   0:00 postgres: tgl regression [local] idle in transaction
60 </screen>
61
62    (The appropriate invocation of <command>ps</> varies across different
63    platforms, as do the details of what is shown.  This example is from a
64    recent Linux system.)  The first process listed here is the
65    master server process.  The command arguments
66    shown for it are the same ones used when it was launched.  The next five
67    processes are background worker processes automatically launched by the
68    master process.  (The <quote>stats collector</> process will not be present
69    if you have set the system not to start the statistics collector; likewise
70    the <quote>autovacuum launcher</> process can be disabled.)
71    Each of the remaining
72    processes is a server process handling one client connection.  Each such
73    process sets its command line display in the form
74
75 <screen>
76 postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <replaceable>activity</>
77 </screen>
78
79   The user, database, and (client) host items remain the same for
80   the life of the client connection, but the activity indicator changes.
81   The activity can be <literal>idle</> (i.e., waiting for a client command),
82   <literal>idle in transaction</> (waiting for client inside a <command>BEGIN</> block),
83   or a command type name such as <literal>SELECT</>.  Also,
84   <literal>waiting</> is appended if the server process is presently waiting
85   on a lock held by another session.  In the above example we can infer
86   that process 15606 is waiting for process 15610 to complete its transaction
87   and thereby release some lock.  (Process 15610 must be the blocker, because
88   there is no other active session.  In more complicated cases it would be
89   necessary to look into the
90   <link linkend="view-pg-locks"><structname>pg_locks</structname></link>
91   system view to determine who is blocking whom.)
92   </para>
93
94   <para>
95    If <xref linkend="guc-cluster-name"> has been configured the
96    cluster name will also be shown in <command>ps</> output:
97 <screen>
98 $ psql -c 'SHOW cluster_name'
99  cluster_name
100 --------------
101  server1
102 (1 row)
103
104 $ ps aux|grep server1
105 postgres   27093  0.0  0.0  30096  2752 ?        Ss   11:34   0:00 postgres: server1: writer process
106 ...
107 </screen>
108   </para>
109
110   <para>
111    If you have turned off <xref linkend="guc-update-process-title"> then the
112    activity indicator is not updated; the process title is set only once
113    when a new process is launched.  On some platforms this saves a measurable
114    amount of per-command overhead;  on others it's insignificant.
115   </para>
116
117   <tip>
118   <para>
119   <productname>Solaris</productname> requires special handling. You must
120   use <command>/usr/ucb/ps</command>, rather than
121   <command>/bin/ps</command>. You also must use two <option>w</option>
122   flags, not just one. In addition, your original invocation of the
123   <command>postgres</command> command must have a shorter
124   <command>ps</command> status display than that provided by each
125   server process.  If you fail to do all three things, the <command>ps</>
126   output for each server process will be the original <command>postgres</>
127   command line.
128   </para>
129   </tip>
130  </sect1>
131
132  <sect1 id="monitoring-stats">
133   <title>The Statistics Collector</title>
134
135   <indexterm zone="monitoring-stats">
136    <primary>statistics</primary>
137   </indexterm>
138
139   <para>
140    <productname>PostgreSQL</productname>'s <firstterm>statistics collector</>
141    is a subsystem that supports collection and reporting of information about
142    server activity.  Presently, the collector can count accesses to tables
143    and indexes in both disk-block and individual-row terms.  It also tracks
144    the total number of rows in each table, and information about vacuum and
145    analyze actions for each table.  It can also count calls to user-defined
146    functions and the total time spent in each one.
147   </para>
148
149   <para>
150    <productname>PostgreSQL</productname> also supports reporting dynamic
151    information about exactly what is going on in the system right now, such as
152    the exact command currently being executed by other server processes, and
153    which other connections exist in the system.  This facility is independent
154    of the collector process.
155   </para>
156
157  <sect2 id="monitoring-stats-setup">
158   <title>Statistics Collection Configuration</title>
159
160   <para>
161    Since collection of statistics adds some overhead to query execution,
162    the system can be configured to collect or not collect information.
163    This is controlled by configuration parameters that are normally set in
164    <filename>postgresql.conf</>.  (See <xref linkend="runtime-config"> for
165    details about setting configuration parameters.)
166   </para>
167
168   <para>
169    The parameter <xref linkend="guc-track-activities"> enables monitoring
170    of the current command being executed by any server process.
171   </para>
172
173   <para>
174    The parameter <xref linkend="guc-track-counts"> controls whether
175    statistics are collected about table and index accesses.
176   </para>
177
178   <para>
179    The parameter <xref linkend="guc-track-functions"> enables tracking of
180    usage of user-defined functions.
181   </para>
182
183   <para>
184    The parameter <xref linkend="guc-track-io-timing"> enables monitoring
185    of block read and write times.
186   </para>
187
188   <para>
189    Normally these parameters are set in <filename>postgresql.conf</> so
190    that they apply to all server processes, but it is possible to turn
191    them on or off in individual sessions using the <xref
192    linkend="sql-set"> command. (To prevent
193    ordinary users from hiding their activity from the administrator,
194    only superusers are allowed to change these parameters with
195    <command>SET</>.)
196   </para>
197
198   <para>
199    The statistics collector transmits the collected information to other
200    <productname>PostgreSQL</productname> processes through temporary files.
201    These files are stored in the directory named by the
202    <xref linkend="guc-stats-temp-directory"> parameter,
203    <filename>pg_stat_tmp</filename> by default.
204    For better performance, <varname>stats_temp_directory</> can be
205    pointed at a RAM-based file system, decreasing physical I/O requirements.
206    When the server shuts down cleanly, a permanent copy of the statistics
207    data is stored in the <filename>pg_stat</filename> subdirectory, so that
208    statistics can be retained across server restarts.  When recovery is
209    performed at server start (e.g. after immediate shutdown, server crash,
210    and point-in-time recovery), all statistics counters are reset.
211   </para>
212
213  </sect2>
214
215  <sect2 id="monitoring-stats-views">
216   <title>Viewing Statistics</title>
217
218   <para>
219    Several predefined views, listed in <xref
220    linkend="monitoring-stats-dynamic-views-table">, are available to show
221    the current state of the system. There are also several other
222    views, listed in <xref
223    linkend="monitoring-stats-views-table">, available to show the results
224    of statistics collection.  Alternatively, one can
225    build custom views using the underlying statistics functions, as discussed
226    in <xref linkend="monitoring-stats-functions">.
227   </para>
228
229   <para>
230    When using the statistics to monitor collected data, it is important
231    to realize that the information does not update instantaneously.
232    Each individual server process transmits new statistical counts to
233    the collector just before going idle; so a query or transaction still in
234    progress does not affect the displayed totals.  Also, the collector itself
235    emits a new report at most once per <varname>PGSTAT_STAT_INTERVAL</varname>
236    milliseconds (500 ms unless altered while building the server).  So the
237    displayed information lags behind actual activity.  However, current-query
238    information collected by <varname>track_activities</varname> is
239    always up-to-date.
240   </para>
241
242   <para>
243    Another important point is that when a server process is asked to display
244    any of these statistics, it first fetches the most recent report emitted by
245    the collector process and then continues to use this snapshot for all
246    statistical views and functions until the end of its current transaction.
247    So the statistics will show static information as long as you continue the
248    current transaction.  Similarly, information about the current queries of
249    all sessions is collected when any such information is first requested
250    within a transaction, and the same information will be displayed throughout
251    the transaction.
252    This is a feature, not a bug, because it allows you to perform several
253    queries on the statistics and correlate the results without worrying that
254    the numbers are changing underneath you.  But if you want to see new
255    results with each query, be sure to do the queries outside any transaction
256    block.  Alternatively, you can invoke
257    <function>pg_stat_clear_snapshot</function>(), which will discard the
258    current transaction's statistics snapshot (if any).  The next use of
259    statistical information will cause a new snapshot to be fetched.
260   </para>
261
262   <para>
263    A transaction can also see its own statistics (as yet untransmitted to the
264    collector) in the views <structname>pg_stat_xact_all_tables</>,
265    <structname>pg_stat_xact_sys_tables</>,
266    <structname>pg_stat_xact_user_tables</>, and
267    <structname>pg_stat_xact_user_functions</>.  These numbers do not act as
268    stated above; instead they update continuously throughout the transaction.
269   </para>
270
271   <table id="monitoring-stats-dynamic-views-table">
272    <title>Dynamic Statistics Views</title>
273
274    <tgroup cols="2">
275     <thead>
276      <row>
277       <entry>View Name</entry>
278       <entry>Description</entry>
279      </row>
280     </thead>
281
282     <tbody>
283      <row>
284       <entry>
285        <structname>pg_stat_activity</structname>
286        <indexterm><primary>pg_stat_activity</primary></indexterm>
287       </entry>
288       <entry>
289        One row per server process, showing information related to
290        the current activity of that process, such as state and current query.
291        See <xref linkend="pg-stat-activity-view"> for details.
292       </entry>
293      </row>
294
295      <row>
296       <entry><structname>pg_stat_replication</><indexterm><primary>pg_stat_replication</primary></indexterm></entry>
297       <entry>One row per WAL sender process, showing statistics about
298        replication to that sender's connected standby server.
299        See <xref linkend="pg-stat-replication-view"> for details.
300       </entry>
301      </row>
302
303      <row>
304       <entry><structname>pg_stat_wal_receiver</><indexterm><primary>pg_stat_wal_receiver</primary></indexterm></entry>
305       <entry>Only one row, showing statistics about the WAL receiver from
306        that receiver's connected server.
307        See <xref linkend="pg-stat-wal-receiver-view"> for details.
308       </entry>
309      </row>
310
311      <row>
312       <entry><structname>pg_stat_ssl</><indexterm><primary>pg_stat_ssl</primary></indexterm></entry>
313       <entry>One row per connection (regular and replication), showing information about
314        SSL used on this connection.
315        See <xref linkend="pg-stat-ssl-view"> for details.
316       </entry>
317      </row>
318
319     </tbody>
320    </tgroup>
321   </table>
322
323   <table id="monitoring-stats-views-table">
324    <title>Collected Statistics Views</title>
325
326    <tgroup cols="2">
327     <thead>
328      <row>
329       <entry>View Name</entry>
330       <entry>Description</entry>
331      </row>
332     </thead>
333
334     <tbody>
335      <row>
336       <entry><structname>pg_stat_archiver</><indexterm><primary>pg_stat_archiver</primary></indexterm></entry>
337       <entry>One row only, showing statistics about the
338        WAL archiver process's activity. See
339        <xref linkend="pg-stat-archiver-view"> for details.
340       </entry>
341      </row>
342
343      <row>
344       <entry><structname>pg_stat_bgwriter</><indexterm><primary>pg_stat_bgwriter</primary></indexterm></entry>
345       <entry>One row only, showing statistics about the
346        background writer process's activity. See
347        <xref linkend="pg-stat-bgwriter-view"> for details.
348      </entry>
349      </row>
350
351      <row>
352       <entry><structname>pg_stat_database</><indexterm><primary>pg_stat_database</primary></indexterm></entry>
353       <entry>One row per database, showing database-wide statistics. See
354        <xref linkend="pg-stat-database-view"> for details.
355       </entry>
356      </row>
357
358      <row>
359       <entry><structname>pg_stat_database_conflicts</><indexterm><primary>pg_stat_database_conflicts</primary></indexterm></entry>
360       <entry>
361        One row per database, showing database-wide statistics about
362        query cancels due to conflict with recovery on standby servers.
363        See <xref linkend="pg-stat-database-conflicts-view"> for details.
364       </entry>
365      </row>
366
367      <row>
368       <entry><structname>pg_stat_all_tables</><indexterm><primary>pg_stat_all_tables</primary></indexterm></entry>
369       <entry>
370        One row for each table in the current database, showing statistics
371        about accesses to that specific table.
372        See <xref linkend="pg-stat-all-tables-view"> for details.
373       </entry>
374      </row>
375
376      <row>
377       <entry><structname>pg_stat_sys_tables</><indexterm><primary>pg_stat_sys_tables</primary></indexterm></entry>
378       <entry>Same as <structname>pg_stat_all_tables</>, except that only
379       system tables are shown.</entry>
380      </row>
381
382      <row>
383       <entry><structname>pg_stat_user_tables</><indexterm><primary>pg_stat_user_tables</primary></indexterm></entry>
384       <entry>Same as <structname>pg_stat_all_tables</>, except that only user
385       tables are shown.</entry>
386      </row>
387
388      <row>
389       <entry><structname>pg_stat_xact_all_tables</><indexterm><primary>pg_stat_xact_all_tables</primary></indexterm></entry>
390       <entry>Similar to <structname>pg_stat_all_tables</>, but counts actions
391       taken so far within the current transaction (which are <emphasis>not</>
392       yet included in <structname>pg_stat_all_tables</> and related views).
393       The columns for numbers of live and dead rows and vacuum and
394       analyze actions are not present in this view.</entry>
395      </row>
396
397      <row>
398       <entry><structname>pg_stat_xact_sys_tables</><indexterm><primary>pg_stat_xact_sys_tables</primary></indexterm></entry>
399       <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
400       system tables are shown.</entry>
401      </row>
402
403      <row>
404       <entry><structname>pg_stat_xact_user_tables</><indexterm><primary>pg_stat_xact_user_tables</primary></indexterm></entry>
405       <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
406       user tables are shown.</entry>
407      </row>
408
409      <row>
410       <entry><structname>pg_stat_all_indexes</><indexterm><primary>pg_stat_all_indexes</primary></indexterm></entry>
411       <entry>
412        One row for each index in the current database, showing statistics
413        about accesses to that specific index.
414        See <xref linkend="pg-stat-all-indexes-view"> for details.
415       </entry>
416      </row>
417
418      <row>
419       <entry><structname>pg_stat_sys_indexes</><indexterm><primary>pg_stat_sys_indexes</primary></indexterm></entry>
420       <entry>Same as <structname>pg_stat_all_indexes</>, except that only
421       indexes on system tables are shown.</entry>
422      </row>
423
424      <row>
425       <entry><structname>pg_stat_user_indexes</><indexterm><primary>pg_stat_user_indexes</primary></indexterm></entry>
426       <entry>Same as <structname>pg_stat_all_indexes</>, except that only
427       indexes on user tables are shown.</entry>
428      </row>
429
430      <row>
431       <entry><structname>pg_statio_all_tables</><indexterm><primary>pg_statio_all_tables</primary></indexterm></entry>
432       <entry>
433        One row for each table in the current database, showing statistics
434        about I/O on that specific table.
435        See <xref linkend="pg-statio-all-tables-view"> for details.
436       </entry>
437      </row>
438
439      <row>
440       <entry><structname>pg_statio_sys_tables</><indexterm><primary>pg_statio_sys_tables</primary></indexterm></entry>
441       <entry>Same as <structname>pg_statio_all_tables</>, except that only
442       system tables are shown.</entry>
443      </row>
444
445      <row>
446       <entry><structname>pg_statio_user_tables</><indexterm><primary>pg_statio_user_tables</primary></indexterm></entry>
447       <entry>Same as <structname>pg_statio_all_tables</>, except that only
448       user tables are shown.</entry>
449      </row>
450
451      <row>
452       <entry><structname>pg_statio_all_indexes</><indexterm><primary>pg_statio_all_indexes</primary></indexterm></entry>
453       <entry>
454        One row for each index in the current database,
455        showing statistics about I/O on that specific index.
456        See <xref linkend="pg-statio-all-indexes-view"> for details.
457       </entry>
458      </row>
459
460      <row>
461       <entry><structname>pg_statio_sys_indexes</><indexterm><primary>pg_statio_sys_indexes</primary></indexterm></entry>
462       <entry>Same as <structname>pg_statio_all_indexes</>, except that only
463       indexes on system tables are shown.</entry>
464      </row>
465
466      <row>
467       <entry><structname>pg_statio_user_indexes</><indexterm><primary>pg_statio_user_indexes</primary></indexterm></entry>
468       <entry>Same as <structname>pg_statio_all_indexes</>, except that only
469       indexes on user tables are shown.</entry>
470      </row>
471
472      <row>
473       <entry><structname>pg_statio_all_sequences</><indexterm><primary>pg_statio_all_sequences</primary></indexterm></entry>
474      <entry>
475        One row for each sequence in the current database,
476        showing statistics about I/O on that specific sequence.
477        See <xref linkend="pg-statio-all-sequences-view"> for details.
478      </entry>
479      </row>
480
481      <row>
482       <entry><structname>pg_statio_sys_sequences</><indexterm><primary>pg_statio_sys_sequences</primary></indexterm></entry>
483       <entry>Same as <structname>pg_statio_all_sequences</>, except that only
484       system sequences are shown.  (Presently, no system sequences are defined,
485       so this view is always empty.)</entry>
486      </row>
487
488      <row>
489       <entry><structname>pg_statio_user_sequences</><indexterm><primary>pg_statio_user_sequences</primary></indexterm></entry>
490       <entry>Same as <structname>pg_statio_all_sequences</>, except that only
491       user sequences are shown.</entry>
492      </row>
493
494      <row>
495       <entry><structname>pg_stat_user_functions</><indexterm><primary>pg_stat_user_functions</primary></indexterm></entry>
496       <entry>
497        One row for each tracked function, showing statistics
498        about executions of that function. See
499        <xref linkend="pg-stat-user-functions-view"> for details.
500       </entry>
501      </row>
502
503      <row>
504       <entry><structname>pg_stat_xact_user_functions</><indexterm><primary>pg_stat_xact_user_functions</primary></indexterm></entry>
505       <entry>Similar to <structname>pg_stat_user_functions</>, but counts only
506       calls during the current transaction (which are <emphasis>not</>
507       yet included in <structname>pg_stat_user_functions</>).</entry>
508      </row>
509
510      <row>
511       <entry><structname>pg_stat_progress_vacuum</><indexterm><primary>pg_stat_progress_vacuum</primary></indexterm></entry>
512       <entry>One row for each backend (including autovacuum worker processes) running
513       <command>VACUUM</>, showing current progress.
514       See <xref linkend='vacuum-progress-reporting'>.</entry>
515      </row>
516     </tbody>
517    </tgroup>
518   </table>
519
520   <para>
521    The per-index statistics are particularly useful to determine which
522    indexes are being used and how effective they are.
523   </para>
524
525   <para>
526    The <structname>pg_statio_</> views are primarily useful to
527    determine the effectiveness of the buffer cache.  When the number
528    of actual disk reads is much smaller than the number of buffer
529    hits, then the cache is satisfying most read requests without
530    invoking a kernel call. However, these statistics do not give the
531    entire story: due to the way in which <productname>PostgreSQL</>
532    handles disk I/O, data that is not in the
533    <productname>PostgreSQL</> buffer cache might still reside in the
534    kernel's I/O cache, and might therefore still be fetched without
535    requiring a physical read. Users interested in obtaining more
536    detailed information on <productname>PostgreSQL</> I/O behavior are
537    advised to use the <productname>PostgreSQL</> statistics collector
538    in combination with operating system utilities that allow insight
539    into the kernel's handling of I/O.
540   </para>
541
542
543   <table id="pg-stat-activity-view" xreflabel="pg_stat_activity">
544    <title><structname>pg_stat_activity</structname> View</title>
545
546    <tgroup cols="3">
547     <thead>
548     <row>
549       <entry>Column</entry>
550       <entry>Type</entry>
551       <entry>Description</entry>
552      </row>
553     </thead>
554
555    <tbody>
556     <row>
557      <entry><structfield>datid</></entry>
558      <entry><type>oid</></entry>
559      <entry>OID of the database this backend is connected to</entry>
560     </row>
561     <row>
562      <entry><structfield>datname</></entry>
563      <entry><type>name</></entry>
564      <entry>Name of the database this backend is connected to</entry>
565     </row>
566     <row>
567      <entry><structfield>pid</></entry>
568      <entry><type>integer</></entry>
569      <entry>Process ID of this backend</entry>
570     </row>
571     <row>
572      <entry><structfield>usesysid</></entry>
573      <entry><type>oid</></entry>
574      <entry>OID of the user logged into this backend</entry>
575     </row>
576     <row>
577      <entry><structfield>usename</></entry>
578      <entry><type>name</></entry>
579      <entry>Name of the user logged into this backend</entry>
580     </row>
581     <row>
582      <entry><structfield>application_name</></entry>
583      <entry><type>text</></entry>
584      <entry>Name of the application that is connected
585       to this backend</entry>
586     </row>
587     <row>
588      <entry><structfield>client_addr</></entry>
589      <entry><type>inet</></entry>
590      <entry>IP address of the client connected to this backend.
591       If this field is null, it indicates either that the client is
592       connected via a Unix socket on the server machine or that this is an
593       internal process such as autovacuum.
594      </entry>
595     </row>
596     <row>
597      <entry><structfield>client_hostname</></entry>
598      <entry><type>text</></entry>
599      <entry>Host name of the connected client, as reported by a
600       reverse DNS lookup of <structfield>client_addr</>. This field will
601       only be non-null for IP connections, and only when <xref
602       linkend="guc-log-hostname"> is enabled.
603      </entry>
604     </row>
605     <row>
606      <entry><structfield>client_port</></entry>
607      <entry><type>integer</></entry>
608      <entry>TCP port number that the client is using for communication
609       with this backend, or <literal>-1</> if a Unix socket is used
610      </entry>
611     </row>
612     <row>
613      <entry><structfield>backend_start</></entry>
614      <entry><type>timestamp with time zone</></entry>
615      <entry>Time when this process was started, i.e., when the
616       client connected to the server
617      </entry>
618     </row>
619     <row>
620      <entry><structfield>xact_start</></entry>
621      <entry><type>timestamp with time zone</></entry>
622      <entry>Time when this process' current transaction was started, or null
623       if no transaction is active. If the current
624       query is the first of its transaction, this column is equal to the
625       <structfield>query_start</> column.
626      </entry>
627     </row>
628     <row>
629      <entry><structfield>query_start</></entry>
630      <entry><type>timestamp with time zone</></entry>
631      <entry>Time when the currently active query was started, or if
632       <structfield>state</> is not <literal>active</>, when the last query
633       was started
634      </entry>
635     </row>
636     <row>
637      <entry><structfield>state_change</></entry>
638      <entry><type>timestamp with time zone</></entry>
639      <entry>Time when the <structfield>state</> was last changed</entry>
640     </row>
641      <row>
642       <entry><structfield>wait_event_type</></entry>
643       <entry><type>text</></entry>
644       <entry>The type of event for which the backend is waiting, if any;
645        otherwise NULL. Possible values are:
646        <itemizedlist>
647         <listitem>
648          <para>
649           <literal>LWLockNamed</>: The backend is waiting for a specific named
650           lightweight lock.  Each such lock protects a particular data
651           structure in shared memory.  <literal>wait_event</> will contain
652           the name of the lightweight lock.
653          </para>
654         </listitem>
655         <listitem>
656          <para>
657           <literal>LWLockTranche</>: The backend is waiting for one of a
658           group of related lightweight locks.  All locks in the group perform
659           a similar function; <literal>wait_event</> will identify the general
660           purpose of locks in that group.
661          </para>
662         </listitem>
663         <listitem>
664          <para>
665           <literal>Lock</>: The backend is waiting for a heavyweight lock.
666           Heavyweight locks, also known as lock manager locks or simply locks,
667           primarily protect SQL-visible objects such as tables.  However,
668           they are also used to ensure mutual exclusion for certain internal
669           operations such as relation extension.  <literal>wait_event</> will
670           identify the type of lock awaited.
671          </para>
672         </listitem>
673         <listitem>
674          <para>
675           <literal>BufferPin</>: The server process is waiting to access to
676           a data buffer during a period when no other process can be
677           examining that buffer.  Buffer pin waits can be protracted if
678           another process holds an open cursor which last read data from the
679           buffer in question.
680          </para>
681         </listitem>
682         <listitem>
683          <para>
684           <literal>Activity</>: The server process is idle.  This is used by
685           system processes waiting for activity in their main processing loop.
686           <literal>wait_event</> will identify the specific wait point.
687          </para>
688         </listitem>
689         <listitem>
690          <para>
691           <literal>Extension</>: The server process is waiting for activity
692           in an extension module.  This category is useful for modules to
693           track custom waiting points.
694          </para>
695         </listitem>
696         <listitem>
697          <para>
698           <literal>Client</>: The server process is waiting for some activity
699           on a socket from user applications, and that the server expects
700           something to happen that is independent from its internal processes.
701           <literal>wait_event</> will identify the specific wait point.
702          </para>
703         </listitem>
704         <listitem>
705          <para>
706           <literal>IPC</>: The server process is waiting for some activity
707           from another process in the server.  <literal>wait_event</> will
708           identify the specific wait point.
709          </para>
710         </listitem>
711         <listitem>
712          <para>
713           <literal>Timeout</>: The server process is waiting for a timeout
714           to expire.  <literal>wait_event</> will identify the specific wait
715           point.
716          </para>
717         </listitem>
718        </itemizedlist>
719       </entry>
720      </row>
721     <row>
722      <entry><structfield>wait_event</></entry>
723      <entry><type>text</></entry>
724      <entry>Wait event name if backend is currently waiting, otherwise NULL.
725      See <xref linkend="wait-event-table"> for details.
726      </entry>
727     </row>
728     <row>
729      <entry><structfield>state</></entry>
730      <entry><type>text</></entry>
731      <entry>Current overall state of this backend.
732        Possible values are:
733        <itemizedlist>
734          <listitem>
735           <para>
736            <literal>active</>: The backend is executing a query.
737           </para>
738          </listitem>
739          <listitem>
740           <para>
741            <literal>idle</>: The backend is waiting for a new client command.
742           </para>
743          </listitem>
744          <listitem>
745           <para>
746            <literal>idle in transaction</>: The backend is in a transaction,
747            but is not currently executing a query.
748           </para>
749          </listitem>
750          <listitem>
751           <para>
752            <literal>idle in transaction (aborted)</>: This state is similar to
753            <literal>idle in transaction</>, except one of the statements in
754            the transaction caused an error.
755           </para>
756          </listitem>
757          <listitem>
758           <para>
759            <literal>fastpath function call</>: The backend is executing a
760            fast-path function.
761           </para>
762          </listitem>
763          <listitem>
764            <para>
765            <literal>disabled</>: This state is reported if <xref
766            linkend="guc-track-activities"> is disabled in this backend.
767           </para>
768          </listitem>
769        </itemizedlist>
770      </entry>
771     </row>
772     <row>
773      <entry><structfield>backend_xid</structfield></entry>
774      <entry><type>xid</type></entry>
775      <entry>Top-level transaction identifier of this backend, if any.</entry>
776     </row>
777     <row>
778      <entry><structfield>backend_xmin</structfield></entry>
779      <entry><type>xid</type></entry>
780      <entry>The current backend's <literal>xmin</> horizon.</entry>
781     </row>
782     <row>
783      <entry><structfield>query</></entry>
784      <entry><type>text</></entry>
785      <entry>Text of this backend's most recent query. If
786       <structfield>state</> is <literal>active</> this field shows the
787       currently executing query. In all other states, it shows the last query
788       that was executed. By default the query text is truncated at 1024
789       characters; this value can be changed via the parameter
790       <xref linkend="guc-track-activity-query-size">.
791      </entry>
792     </row>
793    </tbody>
794    </tgroup>
795   </table>
796
797   <para>
798    The <structname>pg_stat_activity</structname> view will have one row
799    per server process, showing information related to
800    the current activity of that process.
801   </para>
802
803   <note>
804    <para>
805     The <structfield>wait_event</> and <structfield>state</> columns are
806     independent.  If a backend is in the <literal>active</> state,
807     it may or may not be <literal>waiting</> on some event.  If the state
808     is <literal>active</> and <structfield>wait_event</> is non-null, it
809     means that a query is being executed, but is being blocked somewhere
810     in the system.
811    </para>
812   </note>
813
814   <table id="wait-event-table">
815    <title><structname>wait_event</structname> Description</title>
816
817     <tgroup cols="3">
818       <thead>
819        <row>
820         <entry>Wait Event Type</entry>
821         <entry>Wait Event Name</entry>
822         <entry>Description</entry>
823        </row>
824       </thead>
825
826       <tbody>
827        <row>
828         <entry morerows="41"><literal>LWLockNamed</></entry>
829         <entry><literal>ShmemIndexLock</></entry>
830         <entry>Waiting to find or allocate space in shared memory.</entry>
831        </row>
832        <row>
833         <entry><literal>OidGenLock</></entry>
834         <entry>Waiting to allocate or assign an OID.</entry>
835        </row>
836         <row>
837          <entry><literal>XidGenLock</></entry>
838          <entry>Waiting to allocate or assign a transaction id.</entry>
839         </row>
840         <row>
841          <entry><literal>ProcArrayLock</></entry>
842          <entry>Waiting to get a snapshot or clearing a transaction id at
843          transaction end.</entry>
844         </row>
845         <row>
846          <entry><literal>SInvalReadLock</></entry>
847          <entry>Waiting to retrieve or remove messages from shared invalidation
848          queue.</entry>
849         </row>
850         <row>
851          <entry><literal>SInvalWriteLock</></entry>
852          <entry>Waiting to add a message in shared invalidation queue.</entry>
853         </row>
854         <row>
855          <entry><literal>WALBufMappingLock</></entry>
856          <entry>Waiting to replace a page in WAL buffers.</entry>
857         </row>
858         <row>
859          <entry><literal>WALWriteLock</></entry>
860          <entry>Waiting for WAL buffers to be written to disk.</entry>
861         </row>
862         <row>
863          <entry><literal>ControlFileLock</></entry>
864          <entry>Waiting to read or update the control file or creation of a
865          new WAL file.</entry>
866         </row>
867         <row>
868          <entry><literal>CheckpointLock</></entry>
869          <entry>Waiting to perform checkpoint.</entry>
870         </row>
871         <row>
872          <entry><literal>CLogControlLock</></entry>
873          <entry>Waiting to read or update transaction status.</entry>
874         </row>
875         <row>
876          <entry><literal>SubtransControlLock</></entry>
877          <entry>Waiting to read or update sub-transaction information.</entry>
878         </row>
879         <row>
880          <entry><literal>MultiXactGenLock</></entry>
881          <entry>Waiting to read or update shared multixact state.</entry>
882         </row>
883         <row>
884          <entry><literal>MultiXactOffsetControlLock</></entry>
885          <entry>Waiting to read or update multixact offset mappings.</entry>
886         </row>
887         <row>
888          <entry><literal>MultiXactMemberControlLock</></entry>
889          <entry>Waiting to read or update multixact member mappings.</entry>
890         </row>
891         <row>
892          <entry><literal>RelCacheInitLock</></entry>
893          <entry>Waiting to read or write relation cache initialization
894          file.</entry>
895         </row>
896         <row>
897          <entry><literal>CheckpointerCommLock</></entry>
898          <entry>Waiting to manage fsync requests.</entry>
899         </row>
900         <row>
901          <entry><literal>TwoPhaseStateLock</></entry>
902          <entry>Waiting to read or update the state of prepared transactions.</entry>
903         </row>
904         <row>
905          <entry><literal>TablespaceCreateLock</></entry>
906          <entry>Waiting to create or drop the tablespace.</entry>
907         </row>
908         <row>
909          <entry><literal>BtreeVacuumLock</></entry>
910           <entry>Waiting to read or update vacuum-related information for a
911           B-tree index.</entry>
912         </row>
913         <row>
914          <entry><literal>AddinShmemInitLock</></entry>
915          <entry>Waiting to manage space allocation in shared memory.</entry>
916         </row>
917         <row>
918          <entry><literal>AutovacuumLock</></entry>
919          <entry>Autovacuum worker or launcher waiting to update or
920          read the current state of autovacuum workers.</entry>
921         </row>
922         <row>
923          <entry><literal>AutovacuumScheduleLock</></entry>
924          <entry>Waiting to ensure that the table it has selected for a vacuum
925          still needs vacuuming.
926          </entry>
927         </row>
928         <row>
929          <entry><literal>SyncScanLock</></entry>
930          <entry>Waiting to get the start location of a scan on a table for
931          synchronized scans.</entry>
932         </row>
933         <row>
934          <entry><literal>RelationMappingLock</></entry>
935          <entry>Waiting to update the relation map file used to store catalog
936          to filenode mapping.
937          </entry>
938         </row>
939         <row>
940          <entry><literal>AsyncCtlLock</></entry>
941          <entry>Waiting to read or update shared notification state.</entry>
942         </row>
943         <row>
944          <entry><literal>AsyncQueueLock</></entry>
945           <entry>Waiting to read or update notification messages.</entry>
946         </row>
947         <row>
948          <entry><literal>SerializableXactHashLock</></entry>
949          <entry>Waiting to retrieve or store information about serializable
950          transactions.</entry>
951         </row>
952         <row>
953          <entry><literal>SerializableFinishedListLock</></entry>
954          <entry>Waiting to access the list of finished serializable
955          transactions.</entry>
956         </row>
957         <row>
958          <entry><literal>SerializablePredicateLockListLock</></entry>
959          <entry>Waiting to perform an operation on a list of locks held by
960          serializable transactions.</entry>
961         </row>
962         <row>
963          <entry><literal>OldSerXidLock</></entry>
964          <entry>Waiting to read or record conflicting serializable
965          transactions.</entry>
966         </row>
967         <row>
968          <entry><literal>SyncRepLock</></entry>
969          <entry>Waiting to read or update information about synchronous
970          replicas.</entry>
971         </row>
972         <row>
973          <entry><literal>BackgroundWorkerLock</></entry>
974          <entry>Waiting to read or update background worker state.</entry>
975         </row>
976         <row>
977          <entry><literal>DynamicSharedMemoryControlLock</></entry>
978          <entry>Waiting to read or update dynamic shared memory state.</entry>
979         </row>
980         <row>
981          <entry><literal>AutoFileLock</></entry>
982          <entry>Waiting to update the <filename>postgresql.auto.conf</> file.</entry>
983         </row>
984         <row>
985          <entry><literal>ReplicationSlotAllocationLock</></entry>
986          <entry>Waiting to allocate or free a replication slot.</entry>
987         </row>
988         <row>
989          <entry><literal>ReplicationSlotControlLock</></entry>
990          <entry>Waiting to read or update replication slot state.</entry>
991         </row>
992         <row>
993          <entry><literal>CommitTsControlLock</></entry>
994          <entry>Waiting to read or update transaction commit timestamps.</entry>
995         </row>
996         <row>
997          <entry><literal>CommitTsLock</></entry>
998          <entry>Waiting to read or update the last value set for the
999          transaction timestamp.</entry>
1000         </row>
1001         <row>
1002          <entry><literal>ReplicationOriginLock</></entry>
1003          <entry>Waiting to setup, drop or use replication origin.</entry>
1004         </row>
1005         <row>
1006          <entry><literal>MultiXactTruncationLock</></entry>
1007          <entry>Waiting to read or truncate multixact information.</entry>
1008         </row>
1009         <row>
1010          <entry><literal>OldSnapshotTimeMapLock</></entry>
1011          <entry>Waiting to read or update old snapshot control information.</entry>
1012         </row>
1013         <row>
1014          <entry morerows="15"><literal>LWLockTranche</></entry>
1015          <entry><literal>clog</></entry>
1016          <entry>Waiting for I/O on a clog (transaction status) buffer.</entry>
1017         </row>
1018         <row>
1019          <entry><literal>commit_timestamp</></entry>
1020          <entry>Waiting for I/O on commit timestamp buffer.</entry>
1021         </row>
1022         <row>
1023          <entry><literal>subtrans</></entry>
1024          <entry>Waiting for I/O a subtransaction buffer.</entry>
1025         </row>
1026         <row>
1027          <entry><literal>multixact_offset</></entry>
1028          <entry>Waiting for I/O on a multixact offset buffer.</entry>
1029         </row>
1030         <row>
1031          <entry><literal>multixact_member</></entry>
1032          <entry>Waiting for I/O on a multixact_member buffer.</entry>
1033         </row>
1034         <row>
1035          <entry><literal>async</></entry>
1036          <entry>Waiting for I/O on an async (notify) buffer.</entry>
1037         </row>
1038         <row>
1039          <entry><literal>oldserxid</></entry>
1040          <entry>Waiting to I/O on an oldserxid buffer.</entry>
1041         </row>
1042         <row>
1043          <entry><literal>wal_insert</></entry>
1044          <entry>Waiting to insert WAL into a memory buffer.</entry>
1045         </row>
1046         <row>
1047          <entry><literal>buffer_content</></entry>
1048          <entry>Waiting to read or write a data page in memory.</entry>
1049         </row>
1050         <row>
1051          <entry><literal>buffer_io</></entry>
1052          <entry>Waiting for I/O on a data page.</entry>
1053         </row>
1054         <row>
1055          <entry><literal>replication_origin</></entry>
1056          <entry>Waiting to read or update the replication progress.</entry>
1057         </row>
1058         <row>
1059          <entry><literal>replication_slot_io</></entry>
1060          <entry>Waiting for I/O on a replication slot.</entry>
1061         </row>
1062         <row>
1063          <entry><literal>proc</></entry>
1064          <entry>Waiting to read or update the fast-path lock information.</entry>
1065         </row>
1066         <row>
1067          <entry><literal>buffer_mapping</></entry>
1068          <entry>Waiting to associate a data block with a buffer in the buffer
1069          pool.</entry>
1070         </row>
1071         <row>
1072          <entry><literal>lock_manager</></entry>
1073          <entry>Waiting to add or examine locks for backends, or waiting to
1074          join or exit a locking group (used by parallel query).</entry>
1075         </row>
1076         <row>
1077          <entry><literal>predicate_lock_manager</></entry>
1078          <entry>Waiting to add or examine predicate lock information.</entry>
1079         </row>
1080         <row>
1081          <entry morerows="9"><literal>Lock</></entry>
1082          <entry><literal>relation</></entry>
1083          <entry>Waiting to acquire a lock on a relation.</entry>
1084         </row>
1085         <row>
1086          <entry><literal>extend</></entry>
1087          <entry>Waiting to extend a relation.</entry>
1088         </row>
1089         <row>
1090          <entry><literal>page</></entry>
1091          <entry>Waiting to acquire a lock on page of a relation.</entry>
1092         </row>
1093         <row>
1094          <entry><literal>tuple</></entry>
1095          <entry>Waiting to acquire a lock on a tuple.</entry>
1096         </row>
1097         <row>
1098          <entry><literal>transactionid</></entry>
1099          <entry>Waiting for a transaction to finish.</entry>
1100         </row>
1101         <row>
1102          <entry><literal>virtualxid</></entry>
1103          <entry>Waiting to acquire a virtual xid lock.</entry>
1104         </row>
1105         <row>
1106          <entry><literal>speculative token</></entry>
1107          <entry>Waiting to acquire a speculative insertion lock.</entry>
1108         </row>
1109         <row>
1110          <entry><literal>object</></entry>
1111          <entry>Waiting to acquire a lock on a non-relation database object.</entry>
1112         </row>
1113         <row>
1114          <entry><literal>userlock</></entry>
1115          <entry>Waiting to acquire a userlock.</entry>
1116         </row>
1117         <row>
1118          <entry><literal>advisory</></entry>
1119          <entry>Waiting to acquire an advisory user lock.</entry>
1120         </row>
1121         <row>
1122          <entry><literal>BufferPin</></entry>
1123          <entry><literal>BufferPin</></entry>
1124          <entry>Waiting to acquire a pin on a buffer.</entry>
1125         </row>
1126         <row>
1127          <entry morerows="11"><literal>Activity</></entry>
1128          <entry><literal>ArchiverMain</></entry>
1129          <entry>Waiting in main loop of the archiver process.</entry>
1130         </row>
1131         <row>
1132          <entry><literal>AutoVacuumMain</></entry>
1133          <entry>Waiting in main loop of autovacuum launcher process.</entry>
1134         </row>
1135         <row>
1136          <entry><literal>BgWriterHibernate</></entry>
1137          <entry>Waiting in background writer process, hibernating.</entry>
1138         </row>
1139         <row>
1140          <entry><literal>BgWriterMain</></entry>
1141          <entry>Waiting in main loop of background writer process background worker.</entry>
1142         </row>
1143         <row>
1144          <entry><literal>CheckpointerMain</></entry>
1145          <entry>Waiting in main loop of checkpointer process.</entry>
1146         </row>
1147         <row>
1148          <entry><literal>PgStatMain</></entry>
1149          <entry>Waiting in main loop of the statistics collector process.</entry>
1150         </row>
1151         <row>
1152          <entry><literal>RecoveryWalAll</></entry>
1153          <entry>Waiting for WAL from any kind of source (local, archive or stream) at recovery.</entry>
1154         </row>
1155         <row>
1156          <entry><literal>RecoveryWalStream</></entry>
1157          <entry>Waiting for WAL from a stream at recovery.</entry>
1158         </row>
1159         <row>
1160          <entry><literal>SysLoggerMain</></entry>
1161          <entry>Waiting in main loop of syslogger process.</entry>
1162         </row>
1163         <row>
1164          <entry><literal>WalReceiverMain</></entry>
1165          <entry>Waiting in main loop of WAL receiver process.</entry>
1166         </row>
1167         <row>
1168          <entry><literal>WalSenderMain</></entry>
1169          <entry>Waiting in main loop of WAL sender process.</entry>
1170         </row>
1171         <row>
1172          <entry><literal>WalWriterMain</></entry>
1173          <entry>Waiting in main loop of WAL writer process.</entry>
1174         </row>
1175         <row>
1176          <entry morerows="5"><literal>Client</></entry>
1177          <entry><literal>ClientRead</></entry>
1178          <entry>Waiting to read data from the client.</entry>
1179         </row>
1180         <row>
1181          <entry><literal>ClientWrite</></entry>
1182          <entry>Waiting to write data from the client.</entry>
1183         </row>
1184         <row>
1185          <entry><literal>SSLOpenServer</></entry>
1186          <entry>Waiting for SSL while attempting connection.</entry>
1187         </row>
1188         <row>
1189          <entry><literal>WalReceiverWaitStart</></entry>
1190          <entry>Waiting for startup process to send initial data for streaming replication.</entry>
1191         </row>
1192         <row>
1193          <entry><literal>WalSenderWaitForWAL</></entry>
1194          <entry>Waiting for WAL to be flushed in WAL sender process.</entry>
1195         </row>
1196         <row>
1197          <entry><literal>WalSenderWriteData</></entry>
1198          <entry>Waiting for any activity when processing replies from WAL receiver in WAL sender process.</entry>
1199         </row>
1200         <row>
1201          <entry><literal>Extension</></entry>
1202          <entry><literal>Extension</></entry>
1203          <entry>Waiting in an extension.</entry>
1204         </row>
1205         <row>
1206          <entry morerows="9"><literal>IPC</></entry>
1207          <entry><literal>BgWorkerShutdown</></entry>
1208          <entry>Waiting for background worker to shut down.</entry>
1209         </row>
1210         <row>
1211          <entry><literal>BgWorkerStartup</></entry>
1212          <entry>Waiting for background worker to start up.</entry>
1213         </row>
1214         <row>
1215          <entry><literal>ExecuteGather</></entry>
1216          <entry>Waiting for activity from child process when executing <literal>Gather</> node.</entry>
1217         </row>
1218         <row>
1219          <entry><literal>MessageQueueInternal</></entry>
1220          <entry>Waiting for other process to be attached in shared message queue.</entry>
1221         </row>
1222         <row>
1223          <entry><literal>MessageQueuePutMessage</></entry>
1224          <entry>Waiting to write a protoocol message to a shared message queue.</entry>
1225         </row>
1226         <row>
1227          <entry><literal>MessageQueueReceive</></entry>
1228          <entry>Waiting to receive bytes from a shared message queue.</entry>
1229         </row>
1230         <row>
1231          <entry><literal>MessageQueueSend</></entry>
1232          <entry>Waiting to send bytes to a shared message queue.</entry>
1233         </row>
1234         <row>
1235          <entry><literal>ParallelFinish</></entry>
1236          <entry>Waiting for parallel workers to finish computing.</entry>
1237         </row>
1238         <row>
1239          <entry><literal>SafeSnapshot</></entry>
1240          <entry>Waiting for a snapshot for a <literal>READ ONLY DEFERRABLE</> transaction.</entry>
1241         </row>
1242         <row>
1243          <entry><literal>SyncRep</></entry>
1244          <entry>Waiting for confirmation from remote server during synchronous replication.</entry>
1245         </row>
1246         <row>
1247          <entry morerows="2"><literal>Timeout</></entry>
1248          <entry><literal>BaseBackupThrottle</></entry>
1249          <entry>Waiting during base backup when throttling activity.</entry>
1250         </row>
1251         <row>
1252          <entry><literal>PgSleep</></entry>
1253          <entry>Waiting in process that called <function>pg_sleep</>.</entry>
1254         </row>
1255         <row>
1256          <entry><literal>RecoveryApplyDelay</></entry>
1257          <entry>Waiting to apply WAL at recovery because it is delayed.</entry>
1258         </row>
1259       </tbody>
1260      </tgroup>
1261     </table>
1262
1263    <note>
1264     <para>
1265      For tranches registered by extensions, the name is specified by extension
1266      and this will be displayed as <structfield>wait_event</>.  It is quite
1267      possible that user has registered the tranche in one of the backends (by
1268      having allocation in dynamic shared memory) in which case other backends
1269      won't have that information, so we display <literal>extension</> for such
1270      cases.
1271     </para>
1272    </note>
1273
1274    <para>
1275      Here is an example of how wait events can be viewed
1276
1277 <programlisting>
1278 SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event is NOT NULL;
1279  pid  | wait_event_type |  wait_event
1280 ------+-----------------+---------------
1281  2540 | Lock            | relation
1282  6644 | LWLockNamed     | ProcArrayLock
1283 (2 rows)
1284 </programlisting>
1285    </para>
1286
1287   <table id="pg-stat-replication-view" xreflabel="pg_stat_replication">
1288    <title><structname>pg_stat_replication</structname> View</title>
1289    <tgroup cols="3">
1290     <thead>
1291     <row>
1292       <entry>Column</entry>
1293       <entry>Type</entry>
1294       <entry>Description</entry>
1295      </row>
1296     </thead>
1297
1298    <tbody>
1299     <row>
1300      <entry><structfield>pid</></entry>
1301      <entry><type>integer</></entry>
1302      <entry>Process ID of a WAL sender process</entry>
1303     </row>
1304     <row>
1305      <entry><structfield>usesysid</></entry>
1306      <entry><type>oid</></entry>
1307      <entry>OID of the user logged into this WAL sender process</entry>
1308     </row>
1309     <row>
1310      <entry><structfield>usename</></entry>
1311      <entry><type>name</></entry>
1312      <entry>Name of the user logged into this WAL sender process</entry>
1313     </row>
1314     <row>
1315      <entry><structfield>application_name</></entry>
1316      <entry><type>text</></entry>
1317      <entry>Name of the application that is connected
1318       to this WAL sender</entry>
1319     </row>
1320     <row>
1321      <entry><structfield>client_addr</></entry>
1322      <entry><type>inet</></entry>
1323      <entry>IP address of the client connected to this WAL sender.
1324       If this field is null, it indicates that the client is
1325       connected via a Unix socket on the server machine.
1326      </entry>
1327     </row>
1328     <row>
1329      <entry><structfield>client_hostname</></entry>
1330      <entry><type>text</></entry>
1331      <entry>Host name of the connected client, as reported by a
1332       reverse DNS lookup of <structfield>client_addr</>. This field will
1333       only be non-null for IP connections, and only when <xref
1334       linkend="guc-log-hostname"> is enabled.
1335      </entry>
1336     </row>
1337     <row>
1338      <entry><structfield>client_port</></entry>
1339      <entry><type>integer</></entry>
1340      <entry>TCP port number that the client is using for communication
1341       with this WAL sender, or <literal>-1</> if a Unix socket is used
1342      </entry>
1343     </row>
1344     <row>
1345      <entry><structfield>backend_start</></entry>
1346      <entry><type>timestamp with time zone</></entry>
1347      <entry>Time when this process was started, i.e., when the
1348       client connected to this WAL sender
1349      </entry>
1350     </row>
1351     <row>
1352      <entry><structfield>backend_xmin</structfield></entry>
1353      <entry><type>xid</type></entry>
1354      <entry>This standby's <literal>xmin</> horizon reported
1355      by <xref linkend="guc-hot-standby-feedback">.</entry>
1356     </row>
1357     <row>
1358      <entry><structfield>state</></entry>
1359      <entry><type>text</></entry>
1360      <entry>Current WAL sender state.
1361        Possible values are:
1362        <itemizedlist>
1363          <listitem>
1364           <para>
1365            <literal>startup</>: This WAL sender is starting up.
1366           </para>
1367          </listitem>
1368          <listitem>
1369           <para>
1370            <literal>catchup</>: This WAL sender's connected standby is
1371            catching up with the primary.
1372           </para>
1373          </listitem>
1374          <listitem>
1375           <para>
1376            <literal>streaming</>: This WAL sender is streaming changes
1377            after its connected standby server has caught up with the primary.
1378           </para>
1379          </listitem>
1380          <listitem>
1381           <para>
1382            <literal>backup</>: This WAL sender is sending a backup.
1383           </para>
1384          </listitem>
1385        </itemizedlist>
1386      </entry>
1387     </row>
1388     <row>
1389      <entry><structfield>sent_location</></entry>
1390      <entry><type>pg_lsn</></entry>
1391      <entry>Last transaction log position sent on this connection</entry>
1392     </row>
1393     <row>
1394      <entry><structfield>write_location</></entry>
1395      <entry><type>pg_lsn</></entry>
1396      <entry>Last transaction log position written to disk by this standby
1397       server</entry>
1398     </row>
1399     <row>
1400      <entry><structfield>flush_location</></entry>
1401      <entry><type>pg_lsn</></entry>
1402      <entry>Last transaction log position flushed to disk by this standby
1403       server</entry>
1404     </row>
1405     <row>
1406      <entry><structfield>replay_location</></entry>
1407      <entry><type>pg_lsn</></entry>
1408      <entry>Last transaction log position replayed into the database on this
1409       standby server</entry>
1410     </row>
1411     <row>
1412      <entry><structfield>sync_priority</></entry>
1413      <entry><type>integer</></entry>
1414      <entry>Priority of this standby server for being chosen as the
1415       synchronous standby</entry>
1416     </row>
1417     <row>
1418      <entry><structfield>sync_state</></entry>
1419      <entry><type>text</></entry>
1420      <entry>Synchronous state of this standby server.
1421        Possible values are:
1422        <itemizedlist>
1423          <listitem>
1424           <para>
1425            <literal>async</>: This standby server is asynchronous.
1426           </para>
1427          </listitem>
1428          <listitem>
1429           <para>
1430            <literal>potential</>: This standby server is now asynchronous,
1431            but can potentially become synchronous if one of current
1432            synchronous ones fails.
1433           </para>
1434          </listitem>
1435          <listitem>
1436           <para>
1437            <literal>sync</>: This standby server is synchronous.
1438           </para>
1439          </listitem>
1440        </itemizedlist>
1441      </entry>
1442     </row>
1443    </tbody>
1444    </tgroup>
1445   </table>
1446
1447   <para>
1448    The <structname>pg_stat_replication</structname> view will contain one row
1449    per WAL sender process, showing statistics about replication to that
1450    sender's connected standby server.  Only directly connected standbys are
1451    listed; no information is available about downstream standby servers.
1452   </para>
1453
1454   <table id="pg-stat-wal-receiver-view" xreflabel="pg_stat_wal_receiver">
1455    <title><structname>pg_stat_wal_receiver</structname> View</title>
1456    <tgroup cols="3">
1457     <thead>
1458     <row>
1459       <entry>Column</entry>
1460       <entry>Type</entry>
1461       <entry>Description</entry>
1462      </row>
1463     </thead>
1464
1465    <tbody>
1466     <row>
1467      <entry><structfield>pid</></entry>
1468      <entry><type>integer</></entry>
1469      <entry>Process ID of the WAL receiver process</entry>
1470     </row>
1471     <row>
1472      <entry><structfield>status</></entry>
1473      <entry><type>text</></entry>
1474      <entry>Activity status of the WAL receiver process</entry>
1475     </row>
1476     <row>
1477      <entry><structfield>receive_start_lsn</></entry>
1478      <entry><type>pg_lsn</></entry>
1479      <entry>First transaction log position used when WAL receiver is
1480       started</entry>
1481     </row>
1482     <row>
1483      <entry><structfield>receive_start_tli</></entry>
1484      <entry><type>integer</></entry>
1485      <entry>First timeline number used when WAL receiver is started</entry>
1486     </row>
1487     <row>
1488      <entry><structfield>received_lsn</></entry>
1489      <entry><type>pg_lsn</></entry>
1490      <entry>Last transaction log position already received and flushed to
1491       disk, the initial value of this field being the first log position used
1492       when WAL receiver is started</entry>
1493     </row>
1494     <row>
1495      <entry><structfield>received_tli</></entry>
1496      <entry><type>integer</></entry>
1497      <entry>Timeline number of last transaction log position received and
1498       flushed to disk, the initial value of this field being the timeline
1499       number of the first log position used when WAL receiver is started
1500      </entry>
1501     </row>
1502     <row>
1503      <entry><structfield>last_msg_send_time</></entry>
1504      <entry><type>timestamp with time zone</></entry>
1505      <entry>Send time of last message received from origin WAL sender</entry>
1506     </row>
1507     <row>
1508      <entry><structfield>last_msg_receipt_time</></entry>
1509      <entry><type>timestamp with time zone</></entry>
1510      <entry>Receipt time of last message received from origin WAL sender</entry>
1511     </row>
1512     <row>
1513      <entry><structfield>latest_end_lsn</></entry>
1514      <entry><type>pg_lsn</></entry>
1515      <entry>Last transaction log position reported to origin WAL sender</entry>
1516     </row>
1517     <row>
1518      <entry><structfield>latest_end_time</></entry>
1519      <entry><type>timestamp with time zone</></entry>
1520      <entry>Time of last transaction log position reported to origin WAL sender</entry>
1521     </row>
1522     <row>
1523      <entry><structfield>slot_name</></entry>
1524      <entry><type>text</></entry>
1525      <entry>Replication slot name used by this WAL receiver</entry>
1526     </row>
1527     <row>
1528      <entry><structfield>conninfo</></entry>
1529      <entry><type>text</></entry>
1530      <entry>
1531       Connection string used by this WAL receiver,
1532       with security-sensitive fields obfuscated.
1533      </entry>
1534     </row>
1535    </tbody>
1536    </tgroup>
1537   </table>
1538
1539   <para>
1540    The <structname>pg_stat_wal_receiver</structname> view will contain only
1541    one row, showing statistics about the WAL receiver from that receiver's
1542    connected server.
1543   </para>
1544
1545   <table id="pg-stat-ssl-view" xreflabel="pg_stat_ssl">
1546    <title><structname>pg_stat_ssl</structname> View</title>
1547    <tgroup cols="3">
1548     <thead>
1549     <row>
1550       <entry>Column</entry>
1551       <entry>Type</entry>
1552       <entry>Description</entry>
1553      </row>
1554     </thead>
1555
1556    <tbody>
1557     <row>
1558      <entry><structfield>pid</></entry>
1559      <entry><type>integer</></entry>
1560      <entry>Process ID of a backend or WAL sender process</entry>
1561     </row>
1562     <row>
1563      <entry><structfield>ssl</></entry>
1564      <entry><type>boolean</></entry>
1565      <entry>True if SSL is used on this connection</entry>
1566     </row>
1567     <row>
1568      <entry><structfield>version</></entry>
1569      <entry><type>text</></entry>
1570      <entry>Version of SSL in use, or NULL if SSL is not in use
1571       on this connection</entry>
1572     </row>
1573     <row>
1574      <entry><structfield>cipher</></entry>
1575      <entry><type>text</></entry>
1576      <entry>Name of SSL cipher in use, or NULL if SSL is not in use
1577       on this connection</entry>
1578     </row>
1579     <row>
1580      <entry><structfield>bits</></entry>
1581      <entry><type>integer</></entry>
1582      <entry>Number of bits in the encryption algorithm used, or NULL
1583      if SSL is not used on this connection</entry>
1584     </row>
1585     <row>
1586      <entry><structfield>compression</></entry>
1587      <entry><type>boolean</></entry>
1588      <entry>True if SSL compression is in use, false if not,
1589       or NULL if SSL is not in use on this connection</entry>
1590     </row>
1591     <row>
1592      <entry><structfield>clientdn</></entry>
1593      <entry><type>text</></entry>
1594      <entry>Distinguished Name (DN) field from the client certificate
1595       used, or NULL if no client certificate was supplied or if SSL
1596       is not in use on this connection. This field is truncated if the
1597       DN field is longer than <symbol>NAMEDATALEN</symbol> (64 characters
1598       in a standard build)
1599      </entry>
1600     </row>
1601    </tbody>
1602    </tgroup>
1603   </table>
1604
1605   <para>
1606    The <structname>pg_stat_ssl</structname> view will contain one row per
1607    backend or WAL sender process, showing statistics about SSL usage on
1608    this connection. It can be joined to <structname>pg_stat_activity</structname>
1609    or <structname>pg_stat_replication</structname> on the
1610    <structfield>pid</structfield> column to get more details about the
1611    connection.
1612   </para>
1613
1614
1615   <table id="pg-stat-archiver-view" xreflabel="pg_stat_archiver">
1616    <title><structname>pg_stat_archiver</structname> View</title>
1617
1618    <tgroup cols="3">
1619     <thead>
1620      <row>
1621       <entry>Column</entry>
1622       <entry>Type</entry>
1623       <entry>Description</entry>
1624      </row>
1625     </thead>
1626
1627     <tbody>
1628      <row>
1629       <entry><structfield>archived_count</></entry>
1630       <entry><type>bigint</type></entry>
1631       <entry>Number of WAL files that have been successfully archived</entry>
1632      </row>
1633      <row>
1634       <entry><structfield>last_archived_wal</></entry>
1635       <entry><type>text</type></entry>
1636       <entry>Name of the last WAL file successfully archived</entry>
1637      </row>
1638      <row>
1639       <entry><structfield>last_archived_time</></entry>
1640       <entry><type>timestamp with time zone</type></entry>
1641       <entry>Time of the last successful archive operation</entry>
1642      </row>
1643      <row>
1644       <entry><structfield>failed_count</></entry>
1645       <entry><type>bigint</type></entry>
1646       <entry>Number of failed attempts for archiving WAL files</entry>
1647      </row>
1648      <row>
1649       <entry><structfield>last_failed_wal</></entry>
1650       <entry><type>text</type></entry>
1651       <entry>Name of the WAL file of the last failed archival operation</entry>
1652      </row>
1653      <row>
1654       <entry><structfield>last_failed_time</></entry>
1655       <entry><type>timestamp with time zone</type></entry>
1656       <entry>Time of the last failed archival operation</entry>
1657      </row>
1658      <row>
1659       <entry><structfield>stats_reset</></entry>
1660       <entry><type>timestamp with time zone</type></entry>
1661       <entry>Time at which these statistics were last reset</entry>
1662      </row>
1663     </tbody>
1664    </tgroup>
1665   </table>
1666
1667   <para>
1668    The <structname>pg_stat_archiver</structname> view will always have a
1669    single row, containing data about the archiver process of the cluster.
1670   </para>
1671
1672   <table id="pg-stat-bgwriter-view" xreflabel="pg_stat_bgwriter">
1673    <title><structname>pg_stat_bgwriter</structname> View</title>
1674
1675    <tgroup cols="3">
1676     <thead>
1677     <row>
1678       <entry>Column</entry>
1679       <entry>Type</entry>
1680       <entry>Description</entry>
1681      </row>
1682     </thead>
1683
1684     <tbody>
1685      <row>
1686       <entry><structfield>checkpoints_timed</></entry>
1687       <entry><type>bigint</type></entry>
1688       <entry>Number of scheduled checkpoints that have been performed</entry>
1689      </row>
1690      <row>
1691       <entry><structfield>checkpoints_req</></entry>
1692       <entry><type>bigint</type></entry>
1693       <entry>Number of requested checkpoints that have been performed</entry>
1694      </row>
1695      <row>
1696       <entry><structfield>checkpoint_write_time</></entry>
1697       <entry><type>double precision</type></entry>
1698       <entry>
1699         Total amount of time that has been spent in the portion of
1700         checkpoint processing where files are written to disk, in milliseconds
1701       </entry>
1702      </row>
1703      <row>
1704       <entry><structfield>checkpoint_sync_time</></entry>
1705       <entry><type>double precision</type></entry>
1706       <entry>
1707         Total amount of time that has been spent in the portion of
1708         checkpoint processing where files are synchronized to disk, in
1709         milliseconds
1710       </entry>
1711      </row>
1712      <row>
1713       <entry><structfield>buffers_checkpoint</></entry>
1714       <entry><type>bigint</type></entry>
1715       <entry>Number of buffers written during checkpoints</entry>
1716      </row>
1717      <row>
1718       <entry><structfield>buffers_clean</></entry>
1719       <entry><type>bigint</type></entry>
1720       <entry>Number of buffers written by the background writer</entry>
1721      </row>
1722      <row>
1723       <entry><structfield>maxwritten_clean</></entry>
1724       <entry><type>bigint</type></entry>
1725       <entry>Number of times the background writer stopped a cleaning
1726        scan because it had written too many buffers</entry>
1727      </row>
1728      <row>
1729       <entry><structfield>buffers_backend</></entry>
1730       <entry><type>bigint</type></entry>
1731       <entry>Number of buffers written directly by a backend</entry>
1732      </row>
1733      <row>
1734       <entry><structfield>buffers_backend_fsync</></entry>
1735       <entry><type>bigint</type></entry>
1736       <entry>Number of times a backend had to execute its own
1737        <function>fsync</> call (normally the background writer handles those
1738        even when the backend does its own write)</entry>
1739      </row>
1740      <row>
1741       <entry><structfield>buffers_alloc</></entry>
1742       <entry><type>bigint</type></entry>
1743       <entry>Number of buffers allocated</entry>
1744      </row>
1745      <row>
1746       <entry><structfield>stats_reset</></entry>
1747       <entry><type>timestamp with time zone</type></entry>
1748       <entry>Time at which these statistics were last reset</entry>
1749      </row>
1750     </tbody>
1751     </tgroup>
1752   </table>
1753
1754   <para>
1755    The <structname>pg_stat_bgwriter</structname> view will always have a
1756    single row, containing global data for the cluster.
1757   </para>
1758
1759   <table id="pg-stat-database-view" xreflabel="pg_stat_database">
1760    <title><structname>pg_stat_database</structname> View</title>
1761    <tgroup cols="3">
1762     <thead>
1763     <row>
1764       <entry>Column</entry>
1765       <entry>Type</entry>
1766       <entry>Description</entry>
1767      </row>
1768     </thead>
1769
1770    <tbody>
1771     <row>
1772      <entry><structfield>datid</></entry>
1773      <entry><type>oid</></entry>
1774      <entry>OID of a database</entry>
1775     </row>
1776     <row>
1777      <entry><structfield>datname</></entry>
1778      <entry><type>name</></entry>
1779      <entry>Name of this database</entry>
1780     </row>
1781     <row>
1782      <entry><structfield>numbackends</></entry>
1783      <entry><type>integer</></entry>
1784      <entry>Number of backends currently connected to this database.
1785      This is the only column in this view that returns a value reflecting
1786      current state; all other columns return the accumulated values since
1787      the last reset.</entry>
1788     </row>
1789     <row>
1790      <entry><structfield>xact_commit</></entry>
1791      <entry><type>bigint</></entry>
1792      <entry>Number of transactions in this database that have been
1793       committed</entry>
1794     </row>
1795     <row>
1796      <entry><structfield>xact_rollback</></entry>
1797      <entry><type>bigint</></entry>
1798      <entry>Number of transactions in this database that have been
1799       rolled back</entry>
1800     </row>
1801     <row>
1802      <entry><structfield>blks_read</></entry>
1803      <entry><type>bigint</></entry>
1804      <entry>Number of disk blocks read in this database</entry>
1805     </row>
1806     <row>
1807      <entry><structfield>blks_hit</></entry>
1808      <entry><type>bigint</></entry>
1809      <entry>Number of times disk blocks were found already in the buffer
1810       cache, so that a read was not necessary (this only includes hits in the
1811       PostgreSQL buffer cache, not the operating system's file system cache)
1812      </entry>
1813     </row>
1814     <row>
1815      <entry><structfield>tup_returned</></entry>
1816      <entry><type>bigint</></entry>
1817      <entry>Number of rows returned by queries in this database</entry>
1818     </row>
1819     <row>
1820      <entry><structfield>tup_fetched</></entry>
1821      <entry><type>bigint</></entry>
1822      <entry>Number of rows fetched by queries in this database</entry>
1823     </row>
1824     <row>
1825      <entry><structfield>tup_inserted</></entry>
1826      <entry><type>bigint</></entry>
1827      <entry>Number of rows inserted by queries in this database</entry>
1828     </row>
1829     <row>
1830      <entry><structfield>tup_updated</></entry>
1831      <entry><type>bigint</></entry>
1832      <entry>Number of rows updated by queries in this database</entry>
1833     </row>
1834     <row>
1835      <entry><structfield>tup_deleted</></entry>
1836      <entry><type>bigint</></entry>
1837      <entry>Number of rows deleted by queries in this database</entry>
1838     </row>
1839     <row>
1840      <entry><structfield>conflicts</></entry>
1841      <entry><type>bigint</></entry>
1842      <entry>Number of queries canceled due to conflicts with recovery
1843       in this database. (Conflicts occur only on standby servers; see
1844       <xref linkend="pg-stat-database-conflicts-view"> for details.)
1845      </entry>
1846     </row>
1847     <row>
1848      <entry><structfield>temp_files</></entry>
1849      <entry><type>bigint</></entry>
1850      <entry>Number of temporary files created by queries in this database.
1851       All temporary files are counted, regardless of why the temporary file
1852       was created (e.g., sorting or hashing), and regardless of the
1853       <xref linkend="guc-log-temp-files"> setting.
1854      </entry>
1855     </row>
1856     <row>
1857      <entry><structfield>temp_bytes</></entry>
1858      <entry><type>bigint</></entry>
1859      <entry>Total amount of data written to temporary files by queries in
1860       this database. All temporary files are counted, regardless of why
1861       the temporary file was created, and
1862       regardless of the <xref linkend="guc-log-temp-files"> setting.
1863      </entry>
1864     </row>
1865     <row>
1866      <entry><structfield>deadlocks</></entry>
1867      <entry><type>bigint</></entry>
1868      <entry>Number of deadlocks detected in this database</entry>
1869     </row>
1870     <row>
1871      <entry><structfield>blk_read_time</></entry>
1872      <entry><type>double precision</></entry>
1873      <entry>Time spent reading data file blocks by backends in this database,
1874       in milliseconds</entry>
1875     </row>
1876     <row>
1877      <entry><structfield>blk_write_time</></entry>
1878      <entry><type>double precision</></entry>
1879      <entry>Time spent writing data file blocks by backends in this database,
1880       in milliseconds</entry>
1881     </row>
1882     <row>
1883      <entry><structfield>stats_reset</></entry>
1884      <entry><type>timestamp with time zone</></entry>
1885      <entry>Time at which these statistics were last reset</entry>
1886     </row>
1887    </tbody>
1888    </tgroup>
1889   </table>
1890
1891   <para>
1892    The <structname>pg_stat_database</structname> view will contain one row
1893    for each database in the cluster, showing database-wide statistics.
1894   </para>
1895
1896   <table id="pg-stat-database-conflicts-view" xreflabel="pg_stat_database_conflicts">
1897    <title><structname>pg_stat_database_conflicts</structname> View</title>
1898    <tgroup cols="3">
1899     <thead>
1900     <row>
1901       <entry>Column</entry>
1902       <entry>Type</entry>
1903       <entry>Description</entry>
1904      </row>
1905     </thead>
1906
1907    <tbody>
1908     <row>
1909      <entry><structfield>datid</></entry>
1910      <entry><type>oid</></entry>
1911      <entry>OID of a database</entry>
1912     </row>
1913     <row>
1914      <entry><structfield>datname</></entry>
1915      <entry><type>name</></entry>
1916      <entry>Name of this database</entry>
1917     </row>
1918     <row>
1919      <entry><structfield>confl_tablespace</></entry>
1920      <entry><type>bigint</></entry>
1921      <entry>Number of queries in this database that have been canceled due to
1922       dropped tablespaces</entry>
1923     </row>
1924     <row>
1925      <entry><structfield>confl_lock</></entry>
1926      <entry><type>bigint</></entry>
1927      <entry>Number of queries in this database that have been canceled due to
1928       lock timeouts</entry>
1929     </row>
1930     <row>
1931      <entry><structfield>confl_snapshot</></entry>
1932      <entry><type>bigint</></entry>
1933      <entry>Number of queries in this database that have been canceled due to
1934       old snapshots</entry>
1935     </row>
1936     <row>
1937      <entry><structfield>confl_bufferpin</></entry>
1938      <entry><type>bigint</></entry>
1939      <entry>Number of queries in this database that have been canceled due to
1940       pinned buffers</entry>
1941     </row>
1942     <row>
1943      <entry><structfield>confl_deadlock</></entry>
1944      <entry><type>bigint</></entry>
1945      <entry>Number of queries in this database that have been canceled due to
1946       deadlocks</entry>
1947     </row>
1948    </tbody>
1949    </tgroup>
1950   </table>
1951
1952   <para>
1953    The <structname>pg_stat_database_conflicts</structname> view will contain
1954    one row per database, showing database-wide statistics about
1955    query cancels occurring due to conflicts with recovery on standby servers.
1956    This view will only contain information on standby servers, since
1957    conflicts do not occur on master servers.
1958   </para>
1959
1960   <table id="pg-stat-all-tables-view" xreflabel="pg_stat_all_tables">
1961    <title><structname>pg_stat_all_tables</structname> View</title>
1962    <tgroup cols="3">
1963     <thead>
1964     <row>
1965       <entry>Column</entry>
1966       <entry>Type</entry>
1967       <entry>Description</entry>
1968      </row>
1969     </thead>
1970
1971    <tbody>
1972     <row>
1973      <entry><structfield>relid</></entry>
1974      <entry><type>oid</></entry>
1975      <entry>OID of a table</entry>
1976     </row>
1977     <row>
1978      <entry><structfield>schemaname</></entry>
1979      <entry><type>name</></entry>
1980      <entry>Name of the schema that this table is in</entry>
1981     </row>
1982     <row>
1983      <entry><structfield>relname</></entry>
1984      <entry><type>name</></entry>
1985      <entry>Name of this table</entry>
1986     </row>
1987     <row>
1988      <entry><structfield>seq_scan</></entry>
1989      <entry><type>bigint</></entry>
1990      <entry>Number of sequential scans initiated on this table</entry>
1991     </row>
1992     <row>
1993      <entry><structfield>seq_tup_read</></entry>
1994      <entry><type>bigint</></entry>
1995      <entry>Number of live rows fetched by sequential scans</entry>
1996     </row>
1997     <row>
1998      <entry><structfield>idx_scan</></entry>
1999      <entry><type>bigint</></entry>
2000      <entry>Number of index scans initiated on this table</entry>
2001     </row>
2002     <row>
2003      <entry><structfield>idx_tup_fetch</></entry>
2004      <entry><type>bigint</></entry>
2005      <entry>Number of live rows fetched by index scans</entry>
2006     </row>
2007     <row>
2008      <entry><structfield>n_tup_ins</></entry>
2009      <entry><type>bigint</></entry>
2010      <entry>Number of rows inserted</entry>
2011     </row>
2012     <row>
2013      <entry><structfield>n_tup_upd</></entry>
2014      <entry><type>bigint</></entry>
2015      <entry>Number of rows updated (includes HOT updated rows)</entry>
2016     </row>
2017     <row>
2018      <entry><structfield>n_tup_del</></entry>
2019      <entry><type>bigint</></entry>
2020      <entry>Number of rows deleted</entry>
2021     </row>
2022     <row>
2023      <entry><structfield>n_tup_hot_upd</></entry>
2024      <entry><type>bigint</></entry>
2025      <entry>Number of rows HOT updated (i.e., with no separate index
2026       update required)</entry>
2027     </row>
2028     <row>
2029      <entry><structfield>n_live_tup</></entry>
2030      <entry><type>bigint</></entry>
2031      <entry>Estimated number of live rows</entry>
2032     </row>
2033     <row>
2034      <entry><structfield>n_dead_tup</></entry>
2035      <entry><type>bigint</></entry>
2036      <entry>Estimated number of dead rows</entry>
2037     </row>
2038     <row>
2039      <entry><structfield>n_mod_since_analyze</></entry>
2040      <entry><type>bigint</></entry>
2041      <entry>Estimated number of rows modified since this table was last analyzed</entry>
2042     </row>
2043     <row>
2044      <entry><structfield>last_vacuum</></entry>
2045      <entry><type>timestamp with time zone</></entry>
2046      <entry>Last time at which this table was manually vacuumed
2047       (not counting <command>VACUUM FULL</>)</entry>
2048     </row>
2049     <row>
2050      <entry><structfield>last_autovacuum</></entry>
2051      <entry><type>timestamp with time zone</></entry>
2052      <entry>Last time at which this table was vacuumed by the autovacuum
2053       daemon</entry>
2054     </row>
2055     <row>
2056      <entry><structfield>last_analyze</></entry>
2057      <entry><type>timestamp with time zone</></entry>
2058      <entry>Last time at which this table was manually analyzed</entry>
2059     </row>
2060     <row>
2061      <entry><structfield>last_autoanalyze</></entry>
2062      <entry><type>timestamp with time zone</></entry>
2063      <entry>Last time at which this table was analyzed by the autovacuum
2064       daemon</entry>
2065     </row>
2066     <row>
2067      <entry><structfield>vacuum_count</></entry>
2068      <entry><type>bigint</></entry>
2069      <entry>Number of times this table has been manually vacuumed
2070       (not counting <command>VACUUM FULL</>)</entry>
2071     </row>
2072     <row>
2073      <entry><structfield>autovacuum_count</></entry>
2074      <entry><type>bigint</></entry>
2075      <entry>Number of times this table has been vacuumed by the autovacuum
2076       daemon</entry>
2077     </row>
2078     <row>
2079      <entry><structfield>analyze_count</></entry>
2080      <entry><type>bigint</></entry>
2081      <entry>Number of times this table has been manually analyzed</entry>
2082     </row>
2083     <row>
2084      <entry><structfield>autoanalyze_count</></entry>
2085      <entry><type>bigint</></entry>
2086      <entry>Number of times this table has been analyzed by the autovacuum
2087       daemon</entry>
2088     </row>
2089    </tbody>
2090    </tgroup>
2091   </table>
2092
2093   <para>
2094    The <structname>pg_stat_all_tables</structname> view will contain
2095    one row for each table in the current database (including TOAST
2096    tables), showing statistics about accesses to that specific table. The
2097    <structname>pg_stat_user_tables</structname> and
2098    <structname>pg_stat_sys_tables</structname> views
2099    contain the same information,
2100    but filtered to only show user and system tables respectively.
2101   </para>
2102
2103   <table id="pg-stat-all-indexes-view" xreflabel="pg_stat_all_indexes">
2104    <title><structname>pg_stat_all_indexes</structname> View</title>
2105    <tgroup cols="3">
2106     <thead>
2107     <row>
2108       <entry>Column</entry>
2109       <entry>Type</entry>
2110       <entry>Description</entry>
2111      </row>
2112     </thead>
2113
2114    <tbody>
2115     <row>
2116      <entry><structfield>relid</></entry>
2117      <entry><type>oid</></entry>
2118      <entry>OID of the table for this index</entry>
2119     </row>
2120     <row>
2121      <entry><structfield>indexrelid</></entry>
2122      <entry><type>oid</></entry>
2123      <entry>OID of this index</entry>
2124     </row>
2125     <row>
2126      <entry><structfield>schemaname</></entry>
2127      <entry><type>name</></entry>
2128      <entry>Name of the schema this index is in</entry>
2129     </row>
2130     <row>
2131      <entry><structfield>relname</></entry>
2132      <entry><type>name</></entry>
2133      <entry>Name of the table for this index</entry>
2134     </row>
2135     <row>
2136      <entry><structfield>indexrelname</></entry>
2137      <entry><type>name</></entry>
2138      <entry>Name of this index</entry>
2139     </row>
2140     <row>
2141      <entry><structfield>idx_scan</></entry>
2142      <entry><type>bigint</></entry>
2143      <entry>Number of index scans initiated on this index</entry>
2144     </row>
2145     <row>
2146      <entry><structfield>idx_tup_read</></entry>
2147      <entry><type>bigint</></entry>
2148      <entry>Number of index entries returned by scans on this index</entry>
2149     </row>
2150     <row>
2151      <entry><structfield>idx_tup_fetch</></entry>
2152      <entry><type>bigint</></entry>
2153      <entry>Number of live table rows fetched by simple index scans using this
2154       index</entry>
2155     </row>
2156    </tbody>
2157    </tgroup>
2158   </table>
2159
2160   <para>
2161    The <structname>pg_stat_all_indexes</structname> view will contain
2162    one row for each index in the current database,
2163    showing statistics about accesses to that specific index. The
2164    <structname>pg_stat_user_indexes</structname> and
2165    <structname>pg_stat_sys_indexes</structname> views
2166    contain the same information,
2167    but filtered to only show user and system indexes respectively.
2168   </para>
2169
2170   <para>
2171    Indexes can be used by simple index scans, <quote>bitmap</> index scans,
2172    and the optimizer.  In a bitmap scan
2173    the output of several indexes can be combined via AND or OR rules,
2174    so it is difficult to associate individual heap row fetches
2175    with specific indexes when a bitmap scan is used.  Therefore, a bitmap
2176    scan increments the
2177    <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
2178    count(s) for the index(es) it uses, and it increments the
2179    <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
2180    count for the table, but it does not affect
2181    <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
2182    The optimizer also accesses indexes to check for supplied constants
2183    whose values are outside the recorded range of the optimizer statistics
2184    because the optimizer statistics might be stale.
2185   </para>
2186
2187   <note>
2188    <para>
2189     The <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
2190     can be different even without any use of bitmap scans,
2191     because <structfield>idx_tup_read</> counts
2192     index entries retrieved from the index while <structfield>idx_tup_fetch</>
2193     counts live rows fetched from the table.  The latter will be less if any
2194     dead or not-yet-committed rows are fetched using the index, or if any
2195     heap fetches are avoided by means of an index-only scan.
2196    </para>
2197   </note>
2198
2199   <table id="pg-statio-all-tables-view" xreflabel="pg_statio_all_tables">
2200    <title><structname>pg_statio_all_tables</structname> View</title>
2201    <tgroup cols="3">
2202     <thead>
2203     <row>
2204       <entry>Column</entry>
2205       <entry>Type</entry>
2206       <entry>Description</entry>
2207      </row>
2208     </thead>
2209
2210    <tbody>
2211     <row>
2212      <entry><structfield>relid</></entry>
2213      <entry><type>oid</></entry>
2214      <entry>OID of a table</entry>
2215     </row>
2216     <row>
2217      <entry><structfield>schemaname</></entry>
2218      <entry><type>name</></entry>
2219      <entry>Name of the schema that this table is in</entry>
2220     </row>
2221     <row>
2222      <entry><structfield>relname</></entry>
2223      <entry><type>name</></entry>
2224      <entry>Name of this table</entry>
2225     </row>
2226     <row>
2227      <entry><structfield>heap_blks_read</></entry>
2228      <entry><type>bigint</></entry>
2229      <entry>Number of disk blocks read from this table</entry>
2230     </row>
2231     <row>
2232      <entry><structfield>heap_blks_hit</></entry>
2233      <entry><type>bigint</></entry>
2234      <entry>Number of buffer hits in this table</entry>
2235     </row>
2236     <row>
2237      <entry><structfield>idx_blks_read</></entry>
2238      <entry><type>bigint</></entry>
2239      <entry>Number of disk blocks read from all indexes on this table</entry>
2240     </row>
2241     <row>
2242      <entry><structfield>idx_blks_hit</></entry>
2243      <entry><type>bigint</></entry>
2244      <entry>Number of buffer hits in all indexes on this table</entry>
2245     </row>
2246     <row>
2247      <entry><structfield>toast_blks_read</></entry>
2248      <entry><type>bigint</></entry>
2249      <entry>Number of disk blocks read from this table's TOAST table (if any)</entry>
2250     </row>
2251     <row>
2252      <entry><structfield>toast_blks_hit</></entry>
2253      <entry><type>bigint</></entry>
2254      <entry>Number of buffer hits in this table's TOAST table (if any)</entry>
2255     </row>
2256     <row>
2257      <entry><structfield>tidx_blks_read</></entry>
2258      <entry><type>bigint</></entry>
2259      <entry>Number of disk blocks read from this table's TOAST table indexes (if any)</entry>
2260     </row>
2261     <row>
2262      <entry><structfield>tidx_blks_hit</></entry>
2263      <entry><type>bigint</></entry>
2264      <entry>Number of buffer hits in this table's TOAST table indexes (if any)</entry>
2265     </row>
2266    </tbody>
2267    </tgroup>
2268   </table>
2269
2270   <para>
2271    The <structname>pg_statio_all_tables</structname> view will contain
2272    one row for each table in the current database (including TOAST
2273    tables), showing statistics about I/O on that specific table. The
2274    <structname>pg_statio_user_tables</structname> and
2275    <structname>pg_statio_sys_tables</structname> views
2276    contain the same information,
2277    but filtered to only show user and system tables respectively.
2278   </para>
2279
2280   <table id="pg-statio-all-indexes-view" xreflabel="pg_statio_all_indexes">
2281    <title><structname>pg_statio_all_indexes</structname> View</title>
2282    <tgroup cols="3">
2283     <thead>
2284     <row>
2285       <entry>Column</entry>
2286       <entry>Type</entry>
2287       <entry>Description</entry>
2288      </row>
2289     </thead>
2290
2291    <tbody>
2292     <row>
2293      <entry><structfield>relid</></entry>
2294      <entry><type>oid</></entry>
2295      <entry>OID of the table for this index</entry>
2296     </row>
2297     <row>
2298      <entry><structfield>indexrelid</></entry>
2299      <entry><type>oid</></entry>
2300      <entry>OID of this index</entry>
2301     </row>
2302     <row>
2303      <entry><structfield>schemaname</></entry>
2304      <entry><type>name</></entry>
2305      <entry>Name of the schema this index is in</entry>
2306     </row>
2307     <row>
2308      <entry><structfield>relname</></entry>
2309      <entry><type>name</></entry>
2310      <entry>Name of the table for this index</entry>
2311     </row>
2312     <row>
2313      <entry><structfield>indexrelname</></entry>
2314      <entry><type>name</></entry>
2315      <entry>Name of this index</entry>
2316     </row>
2317     <row>
2318      <entry><structfield>idx_blks_read</></entry>
2319      <entry><type>bigint</></entry>
2320      <entry>Number of disk blocks read from this index</entry>
2321     </row>
2322     <row>
2323      <entry><structfield>idx_blks_hit</></entry>
2324      <entry><type>bigint</></entry>
2325      <entry>Number of buffer hits in this index</entry>
2326     </row>
2327    </tbody>
2328    </tgroup>
2329   </table>
2330
2331   <para>
2332    The <structname>pg_statio_all_indexes</structname> view will contain
2333    one row for each index in the current database,
2334    showing statistics about I/O on that specific index. The
2335    <structname>pg_statio_user_indexes</structname> and
2336    <structname>pg_statio_sys_indexes</structname> views
2337    contain the same information,
2338    but filtered to only show user and system indexes respectively.
2339   </para>
2340
2341   <table id="pg-statio-all-sequences-view" xreflabel="pg_statio_all_sequences">
2342    <title><structname>pg_statio_all_sequences</structname> View</title>
2343    <tgroup cols="3">
2344     <thead>
2345     <row>
2346       <entry>Column</entry>
2347       <entry>Type</entry>
2348       <entry>Description</entry>
2349      </row>
2350     </thead>
2351
2352    <tbody>
2353     <row>
2354      <entry><structfield>relid</></entry>
2355      <entry><type>oid</></entry>
2356      <entry>OID of a sequence</entry>
2357     </row>
2358     <row>
2359      <entry><structfield>schemaname</></entry>
2360      <entry><type>name</></entry>
2361      <entry>Name of the schema this sequence is in</entry>
2362     </row>
2363     <row>
2364      <entry><structfield>relname</></entry>
2365      <entry><type>name</></entry>
2366      <entry>Name of this sequence</entry>
2367     </row>
2368     <row>
2369      <entry><structfield>blks_read</></entry>
2370      <entry><type>bigint</></entry>
2371      <entry>Number of disk blocks read from this sequence</entry>
2372     </row>
2373     <row>
2374      <entry><structfield>blks_hit</></entry>
2375      <entry><type>bigint</></entry>
2376      <entry>Number of buffer hits in this sequence</entry>
2377     </row>
2378    </tbody>
2379    </tgroup>
2380   </table>
2381
2382   <para>
2383    The <structname>pg_statio_all_sequences</structname> view will contain
2384    one row for each sequence in the current database,
2385    showing statistics about I/O on that specific sequence.
2386   </para>
2387
2388   <table id="pg-stat-user-functions-view" xreflabel="pg_stat_user_functions">
2389    <title><structname>pg_stat_user_functions</structname> View</title>
2390    <tgroup cols="3">
2391     <thead>
2392     <row>
2393       <entry>Column</entry>
2394       <entry>Type</entry>
2395       <entry>Description</entry>
2396      </row>
2397     </thead>
2398
2399    <tbody>
2400     <row>
2401      <entry><structfield>funcid</></entry>
2402      <entry><type>oid</></entry>
2403      <entry>OID of a function</entry>
2404     </row>
2405     <row>
2406      <entry><structfield>schemaname</></entry>
2407      <entry><type>name</></entry>
2408      <entry>Name of the schema this function is in</entry>
2409     </row>
2410     <row>
2411      <entry><structfield>funcname</></entry>
2412      <entry><type>name</></entry>
2413      <entry>Name of this function</entry>
2414     </row>
2415     <row>
2416      <entry><structfield>calls</></entry>
2417      <entry><type>bigint</></entry>
2418      <entry>Number of times this function has been called</entry>
2419     </row>
2420     <row>
2421      <entry><structfield>total_time</></entry>
2422      <entry><type>double precision</></entry>
2423      <entry>Total time spent in this function and all other functions
2424      called by it, in milliseconds</entry>
2425     </row>
2426     <row>
2427      <entry><structfield>self_time</></entry>
2428      <entry><type>double precision</></entry>
2429      <entry>Total time spent in this function itself, not including
2430      other functions called by it, in milliseconds</entry>
2431     </row>
2432    </tbody>
2433    </tgroup>
2434   </table>
2435
2436   <para>
2437    The <structname>pg_stat_user_functions</structname> view will contain
2438    one row for each tracked function, showing statistics about executions of
2439    that function.  The <xref linkend="guc-track-functions"> parameter
2440    controls exactly which functions are tracked.
2441   </para>
2442
2443  </sect2>
2444
2445  <sect2 id="monitoring-stats-functions">
2446   <title>Statistics Functions</title>
2447
2448   <para>
2449    Other ways of looking at the statistics can be set up by writing
2450    queries that use the same underlying statistics access functions used by
2451    the standard views shown above.  For details such as the functions' names,
2452    consult the definitions of the standard views.  (For example, in
2453    <application>psql</> you could issue <literal>\d+ pg_stat_activity</>.)
2454    The access functions for per-database statistics take a database OID as an
2455    argument to identify which database to report on.
2456    The per-table and per-index functions take a table or index OID.
2457    The functions for per-function statistics take a function OID.
2458    Note that only tables, indexes, and functions in the current database
2459    can be seen with these functions.
2460   </para>
2461
2462   <para>
2463    Additional functions related to statistics collection are listed in <xref
2464    linkend="monitoring-stats-funcs-table">.
2465   </para>
2466
2467   <table id="monitoring-stats-funcs-table">
2468    <title>Additional Statistics Functions</title>
2469
2470    <tgroup cols="3">
2471     <thead>
2472      <row>
2473       <entry>Function</entry>
2474       <entry>Return Type</entry>
2475       <entry>Description</entry>
2476      </row>
2477     </thead>
2478
2479     <tbody>
2480
2481      <row>
2482        <!-- See also the entry for this in func.sgml -->
2483       <entry><literal><function>pg_backend_pid()</function></literal></entry>
2484       <entry><type>integer</type></entry>
2485       <entry>
2486        Process ID of the server process handling the current session
2487       </entry>
2488      </row>
2489
2490      <row>
2491       <entry><literal><function>pg_stat_get_activity</function>(<type>integer</type>)</literal><indexterm><primary>pg_stat_get_activity</primary></indexterm></entry>
2492       <entry><type>setof record</type></entry>
2493       <entry>
2494        Returns a record of information about the backend with the specified PID, or
2495        one record for each active backend in the system if <symbol>NULL</symbol> is
2496        specified. The fields returned are a subset of those in the
2497        <structname>pg_stat_activity</structname> view.
2498       </entry>
2499      </row>
2500
2501      <row>
2502       <entry><literal><function>pg_stat_get_snapshot_timestamp()</function></literal><indexterm><primary>pg_stat_get_snapshot_timestamp</primary></indexterm></entry>
2503       <entry><type>timestamp with time zone</type></entry>
2504       <entry>
2505        Returns the timestamp of the current statistics snapshot
2506       </entry>
2507      </row>
2508
2509      <row>
2510       <entry><literal><function>pg_stat_clear_snapshot()</function></literal><indexterm><primary>pg_stat_clear_snapshot</primary></indexterm></entry>
2511       <entry><type>void</type></entry>
2512       <entry>
2513        Discard the current statistics snapshot
2514       </entry>
2515      </row>
2516
2517      <row>
2518       <entry><literal><function>pg_stat_reset()</function></literal><indexterm><primary>pg_stat_reset</primary></indexterm></entry>
2519       <entry><type>void</type></entry>
2520       <entry>
2521        Reset all statistics counters for the current database to zero
2522        (requires superuser privileges by default, but EXECUTE for this
2523        function can be granted to others.)
2524       </entry>
2525      </row>
2526
2527      <row>
2528       <entry><literal><function>pg_stat_reset_shared</function>(text)</literal><indexterm><primary>pg_stat_reset_shared</primary></indexterm></entry>
2529       <entry><type>void</type></entry>
2530       <entry>
2531        Reset some cluster-wide statistics counters to zero, depending on the
2532        argument (requires superuser privileges by default, but EXECUTE for
2533        this function can be granted to others).
2534        Calling <literal>pg_stat_reset_shared('bgwriter')</> will zero all the
2535        counters shown in the <structname>pg_stat_bgwriter</> view.
2536        Calling <literal>pg_stat_reset_shared('archiver')</> will zero all the
2537        counters shown in the <structname>pg_stat_archiver</> view.
2538       </entry>
2539      </row>
2540
2541      <row>
2542       <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal><indexterm><primary>pg_stat_reset_single_table_counters</primary></indexterm></entry>
2543       <entry><type>void</type></entry>
2544       <entry>
2545        Reset statistics for a single table or index in the current database to
2546        zero (requires superuser privileges by default, but EXECUTE for this
2547        function can be granted to others)
2548       </entry>
2549      </row>
2550
2551      <row>
2552       <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal><indexterm><primary>pg_stat_reset_single_function_counters</primary></indexterm></entry>
2553       <entry><type>void</type></entry>
2554       <entry>
2555        Reset statistics for a single function in the current database to
2556        zero (requires superuser privileges by default, but EXECUTE for this
2557        function can be granted to others)
2558       </entry>
2559      </row>
2560     </tbody>
2561    </tgroup>
2562   </table>
2563
2564   <para>
2565    <function>pg_stat_get_activity</function>, the underlying function of
2566    the <structname>pg_stat_activity</> view, returns a set of records
2567    containing all the available information about each backend process.
2568    Sometimes it may be more convenient to obtain just a subset of this
2569    information.  In such cases, an older set of per-backend statistics
2570    access functions can be used; these are shown in <xref
2571    linkend="monitoring-stats-backend-funcs-table">.
2572    These access functions use a backend ID number, which ranges from one
2573    to the number of currently active backends.
2574    The function <function>pg_stat_get_backend_idset</function> provides a
2575    convenient way to generate one row for each active backend for
2576    invoking these functions.  For example, to show the <acronym>PID</>s and
2577    current queries of all backends:
2578
2579 <programlisting>
2580 SELECT pg_stat_get_backend_pid(s.backendid) AS pid,
2581        pg_stat_get_backend_activity(s.backendid) AS query
2582     FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
2583 </programlisting>
2584   </para>
2585
2586   <table id="monitoring-stats-backend-funcs-table">
2587    <title>Per-Backend Statistics Functions</title>
2588
2589    <tgroup cols="3">
2590     <thead>
2591      <row>
2592       <entry>Function</entry>
2593       <entry>Return Type</entry>
2594       <entry>Description</entry>
2595      </row>
2596     </thead>
2597
2598     <tbody>
2599
2600      <row>
2601       <entry><literal><function>pg_stat_get_backend_idset()</function></literal></entry>
2602       <entry><type>setof integer</type></entry>
2603       <entry>Set of currently active backend ID numbers (from 1 to the
2604        number of active backends)</entry>
2605      </row>
2606
2607      <row>
2608       <entry><literal><function>pg_stat_get_backend_activity(integer)</function></literal></entry>
2609       <entry><type>text</type></entry>
2610       <entry>Text of this backend's most recent query</>
2611      </row>
2612
2613      <row>
2614       <entry><literal><function>pg_stat_get_backend_activity_start(integer)</function></literal></entry>
2615       <entry><type>timestamp with time zone</type></entry>
2616       <entry>Time when the most recent query was started</entry>
2617      </row>
2618
2619      <row>
2620       <entry><literal><function>pg_stat_get_backend_client_addr(integer)</function></literal></entry>
2621       <entry><type>inet</type></entry>
2622       <entry>IP address of the client connected to this backend</entry>
2623      </row>
2624
2625      <row>
2626       <entry><literal><function>pg_stat_get_backend_client_port(integer)</function></literal></entry>
2627       <entry><type>integer</type></entry>
2628       <entry>TCP port number that the client is using for communication</entry>
2629      </row>
2630
2631      <row>
2632       <entry><literal><function>pg_stat_get_backend_dbid(integer)</function></literal></entry>
2633       <entry><type>oid</type></entry>
2634       <entry>OID of the database this backend is connected to</entry>
2635      </row>
2636
2637      <row>
2638       <entry><literal><function>pg_stat_get_backend_pid(integer)</function></literal></entry>
2639       <entry><type>integer</type></entry>
2640       <entry>Process ID of this backend</entry>
2641      </row>
2642
2643      <row>
2644       <entry><literal><function>pg_stat_get_backend_start(integer)</function></literal></entry>
2645       <entry><type>timestamp with time zone</type></entry>
2646       <entry>Time when this process was started</entry>
2647      </row>
2648
2649      <row>
2650       <entry><literal><function>pg_stat_get_backend_userid(integer)</function></literal></entry>
2651       <entry><type>oid</type></entry>
2652       <entry>OID of the user logged into this backend</entry>
2653      </row>
2654
2655       <row>
2656        <entry><literal><function>pg_stat_get_backend_wait_event_type(integer)</function></literal></entry>
2657        <entry><type>text</type></entry>
2658         <entry>Wait event type name if backend is currently waiting, otherwise NULL.
2659         See <xref linkend="wait-event-table"> for details.
2660         </entry>
2661       </row>
2662
2663      <row>
2664       <entry><literal><function>pg_stat_get_backend_wait_event(integer)</function></literal></entry>
2665       <entry><type>text</type></entry>
2666        <entry>Wait event name if backend is currently waiting, otherwise NULL.
2667        See <xref linkend="wait-event-table"> for details.
2668        </entry>
2669      </row>
2670
2671      <row>
2672       <entry><literal><function>pg_stat_get_backend_xact_start(integer)</function></literal></entry>
2673       <entry><type>timestamp with time zone</type></entry>
2674       <entry>Time when the current transaction was started</entry>
2675      </row>
2676
2677     </tbody>
2678    </tgroup>
2679   </table>
2680
2681  </sect2>
2682  </sect1>
2683
2684  <sect1 id="monitoring-locks">
2685   <title>Viewing Locks</title>
2686
2687   <indexterm zone="monitoring-locks">
2688    <primary>lock</primary>
2689    <secondary>monitoring</secondary>
2690   </indexterm>
2691
2692   <para>
2693    Another useful tool for monitoring database activity is the
2694    <structname>pg_locks</structname> system table.  It allows the
2695    database administrator to view information about the outstanding
2696    locks in the lock manager. For example, this capability can be used
2697    to:
2698
2699    <itemizedlist>
2700     <listitem>
2701      <para>
2702       View all the locks currently outstanding, all the locks on
2703       relations in a particular database, all the locks on a
2704       particular relation, or all the locks held by a particular
2705       <productname>PostgreSQL</productname> session.
2706      </para>
2707     </listitem>
2708
2709     <listitem>
2710      <para>
2711       Determine the relation in the current database with the most
2712       ungranted locks (which might be a source of contention among
2713       database clients).
2714      </para>
2715     </listitem>
2716
2717     <listitem>
2718      <para>
2719       Determine the effect of lock contention on overall database
2720       performance, as well as the extent to which contention varies
2721       with overall database traffic.
2722      </para>
2723     </listitem>
2724    </itemizedlist>
2725
2726    Details of the <structname>pg_locks</structname> view appear in
2727    <xref linkend="view-pg-locks">.
2728    For more information on locking and managing concurrency with
2729    <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
2730   </para>
2731  </sect1>
2732
2733  <sect1 id="progress-reporting">
2734   <title>Progress Reporting</title>
2735
2736   <para>
2737    <productname>PostgreSQL</> has the ability to report the progress of
2738    certain commands during command execution.  Currently, the only command
2739    which supports progress reporting is <command>VACUUM</>.  This may be
2740    expanded in the future.
2741   </para>
2742
2743  <sect2 id="vacuum-progress-reporting">
2744   <title>VACUUM Progress Reporting</title>
2745
2746   <para>
2747    Whenever <command>VACUUM</> is running, the
2748    <structname>pg_stat_progress_vacuum</structname> view will contain
2749    one row for each backend (including autovacuum worker processes) that is
2750    currently vacuuming.  The tables below describe the information
2751    that will be reported and provide information about how to interpret it.
2752    Progress reporting is not currently supported for <command>VACUUM FULL</>
2753    and backends running <command>VACUUM FULL</> will not be listed in this
2754    view.
2755   </para>
2756
2757   <table id="pg-stat-progress-vacuum-view" xreflabel="pg_stat_progress_vacuum">
2758    <title><structname>pg_stat_progress_vacuum</structname> View</title>
2759    <tgroup cols="3">
2760     <thead>
2761     <row>
2762       <entry>Column</entry>
2763       <entry>Type</entry>
2764       <entry>Description</entry>
2765      </row>
2766     </thead>
2767
2768    <tbody>
2769     <row>
2770      <entry><structfield>pid</></entry>
2771      <entry><type>integer</></entry>
2772      <entry>Process ID of backend.</entry>
2773     </row>
2774     <row>
2775      <entry><structfield>datid</></entry>
2776      <entry><type>oid</></entry>
2777      <entry>OID of the database to which this backend is connected.</entry>
2778     </row>
2779     <row>
2780      <entry><structfield>datname</></entry>
2781      <entry><type>name</></entry>
2782      <entry>Name of the database to which this backend is connected.</entry>
2783     </row>
2784     <row>
2785      <entry><structfield>relid</></entry>
2786      <entry><type>oid</></entry>
2787      <entry>OID of the table being vacuumed.</entry>
2788     </row>
2789     <row>
2790      <entry><structfield>phase</></entry>
2791      <entry><type>text</></entry>
2792      <entry>
2793        Current processing phase of vacuum.  See <xref linkend='vacuum-phases'>.
2794      </entry>
2795     </row>
2796     <row>
2797      <entry><structfield>heap_blks_total</></entry>
2798      <entry><type>bigint</></entry>
2799      <entry>
2800        Total number of heap blocks in the table.  This number is reported
2801        as of the beginning of the scan; blocks added later will not be (and
2802        need not be) visited by this <command>VACUUM</>.
2803      </entry>
2804     </row>
2805     <row>
2806      <entry><structfield>heap_blks_scanned</></entry>
2807      <entry><type>bigint</></entry>
2808      <entry>
2809        Number of heap blocks scanned.  Because the
2810        <link linkend="storage-vm">visibility map</> is used to optimize scans,
2811        some blocks will be skipped without inspection; skipped blocks are
2812        included in this total, so that this number will eventually become
2813        equal to <structfield>heap_blks_total</> when the vacuum is complete.
2814        This counter only advances when the phase is <literal>scanning heap</>.
2815      </entry>
2816     </row>
2817     <row>
2818      <entry><structfield>heap_blks_vacuumed</></entry>
2819      <entry><type>bigint</></entry>
2820      <entry>
2821        Number of heap blocks vacuumed.  Unless the table has no indexes, this
2822        counter only advances when the phase is <literal>vacuuming heap</>.
2823        Blocks that contain no dead tuples are skipped, so the counter may
2824        sometimes skip forward in large increments.
2825      </entry>
2826     </row>
2827     <row>
2828      <entry><structfield>index_vacuum_count</></entry>
2829      <entry><type>bigint</></entry>
2830      <entry>
2831        Number of completed index vacuum cycles.
2832      </entry>
2833     </row>
2834     <row>
2835      <entry><structfield>max_dead_tuples</></entry>
2836      <entry><type>bigint</></entry>
2837      <entry>
2838       Number of dead tuples that we can store before needing to perform
2839       an index vacuum cycle, based on
2840       <xref linkend="guc-maintenance-work-mem">.
2841      </entry>
2842     </row>
2843     <row>
2844      <entry><structfield>num_dead_tuples</></entry>
2845      <entry><type>bigint</></entry>
2846      <entry>
2847        Number of dead tuples collected since the last index vacuum cycle.
2848      </entry>
2849     </row>
2850    </tbody>
2851    </tgroup>
2852   </table>
2853
2854   <table id="vacuum-phases">
2855    <title>VACUUM phases</title>
2856    <tgroup cols="2">
2857     <thead>
2858     <row>
2859       <entry>Phase</entry>
2860       <entry>Description</entry>
2861      </row>
2862     </thead>
2863
2864    <tbody>
2865     <row>
2866      <entry><literal>initializing</literal></entry>
2867      <entry>
2868        <command>VACUUM</> is preparing to begin scanning the heap.  This
2869        phase is expected to be very brief.
2870      </entry>
2871     </row>
2872     <row>
2873      <entry><literal>scanning heap</literal></entry>
2874      <entry>
2875        <command>VACUUM</> is currently scanning the heap.  It will prune and
2876        defragment each page if required, and possibly perform freezing
2877        activity.  The <structfield>heap_blks_scanned</> column can be used
2878        to monitor the progress of the scan.
2879      </entry>
2880     </row>
2881     <row>
2882      <entry><literal>vacuuming indexes</literal></entry>
2883      <entry>
2884        <command>VACUUM</> is currently vacuuming the indexes.  If a table has
2885        any indexes, this will happen at least once per vacuum, after the heap
2886        has been completely scanned.  It may happen multiple times per vacuum
2887        if <xref linkend="guc-maintenance-work-mem"> is insufficient to
2888        store the number of dead tuples found.
2889      </entry>
2890     </row>
2891     <row>
2892      <entry><literal>vacuuming heap</literal></entry>
2893      <entry>
2894        <command>VACUUM</> is currently vacuuming the heap.  Vacuuming the heap
2895        is distinct from scanning the heap, and occurs after each instance of
2896        vacuuming indexes.  If <structfield>heap_blks_scanned</> is less than
2897        <structfield>heap_blks_total</>, the system will return to scanning
2898        the heap after this phase is completed; otherwise, it will begin
2899        cleaning up indexes after this phase is completed.
2900      </entry>
2901     </row>
2902     <row>
2903      <entry><literal>cleaning up indexes</literal></entry>
2904      <entry>
2905        <command>VACUUM</> is currently cleaning up indexes.  This occurs after
2906        the heap has been completely scanned and all vacuuming of the indexes
2907        and the heap has been completed.
2908      </entry>
2909     </row>
2910     <row>
2911      <entry><literal>truncating heap</literal></entry>
2912      <entry>
2913        <command>VACUUM</> is currently truncating the heap so as to return
2914        empty pages at the end of the relation to the operating system.  This
2915        occurs after cleaning up indexes.
2916      </entry>
2917     </row>
2918     <row>
2919      <entry><literal>performing final cleanup</literal></entry>
2920      <entry>
2921        <command>VACUUM</> is performing final cleanup.  During this phase,
2922        <command>VACUUM</> will vacuum the free space map, update statistics
2923        in <literal>pg_class</>, and report statistics to the statistics
2924        collector.  When this phase is completed, <command>VACUUM</> will end.
2925      </entry>
2926     </row>
2927    </tbody>
2928    </tgroup>
2929   </table>
2930
2931  </sect2>
2932  </sect1>
2933
2934  <sect1 id="dynamic-trace">
2935   <title>Dynamic Tracing</title>
2936
2937  <indexterm zone="dynamic-trace">
2938   <primary>DTrace</primary>
2939  </indexterm>
2940
2941   <para>
2942    <productname>PostgreSQL</productname> provides facilities to support
2943    dynamic tracing of the database server. This allows an external
2944    utility to be called at specific points in the code and thereby trace
2945    execution.
2946   </para>
2947
2948   <para>
2949    A number of probes or trace points are already inserted into the source
2950    code. These probes are intended to be used by database developers and
2951    administrators. By default the probes are not compiled into
2952    <productname>PostgreSQL</productname>; the user needs to explicitly tell
2953    the configure script to make the probes available.
2954   </para>
2955
2956   <para>
2957    Currently, the
2958    <ulink url="https://en.wikipedia.org/wiki/DTrace">DTrace</ulink>
2959    utility is supported, which, at the time of this writing, is available
2960    on Solaris, macOS, FreeBSD, NetBSD, and Oracle Linux.  The
2961    <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> project
2962    for Linux provides a DTrace equivalent and can also be used.  Supporting other dynamic
2963    tracing utilities is theoretically possible by changing the definitions for
2964    the macros in <filename>src/include/utils/probes.h</>.
2965   </para>
2966
2967   <sect2 id="compiling-for-trace">
2968    <title>Compiling for Dynamic Tracing</title>
2969
2970   <para>
2971    By default, probes are not available, so you will need to
2972    explicitly tell the configure script to make the probes available
2973    in <productname>PostgreSQL</productname>. To include DTrace support
2974    specify <option>--enable-dtrace</> to configure.  See <xref
2975    linkend="install-procedure"> for further information.
2976   </para>
2977   </sect2>
2978
2979   <sect2 id="trace-points">
2980    <title>Built-in Probes</title>
2981
2982   <para>
2983    A number of standard probes are provided in the source code,
2984    as shown in <xref linkend="dtrace-probe-point-table">;
2985    <xref linkend="typedefs-table">
2986    shows the types used in the probes.  More probes can certainly be
2987    added to enhance <productname>PostgreSQL</>'s observability.
2988   </para>
2989
2990  <table id="dtrace-probe-point-table">
2991   <title>Built-in DTrace Probes</title>
2992   <tgroup cols="3">
2993    <thead>
2994     <row>
2995      <entry>Name</entry>
2996      <entry>Parameters</entry>
2997      <entry>Description</entry>
2998     </row>
2999    </thead>
3000
3001    <tbody>
3002
3003     <row>
3004      <entry><literal>transaction-start</literal></entry>
3005      <entry><literal>(LocalTransactionId)</literal></entry>
3006      <entry>Probe that fires at the start of a new transaction.
3007       arg0 is the transaction ID.</entry>
3008     </row>
3009     <row>
3010      <entry><literal>transaction-commit</literal></entry>
3011      <entry><literal>(LocalTransactionId)</literal></entry>
3012      <entry>Probe that fires when a transaction completes successfully.
3013       arg0 is the transaction ID.</entry>
3014     </row>
3015     <row>
3016      <entry><literal>transaction-abort</literal></entry>
3017      <entry><literal>(LocalTransactionId)</literal></entry>
3018      <entry>Probe that fires when a transaction completes unsuccessfully.
3019       arg0 is the transaction ID.</entry>
3020     </row>
3021     <row>
3022      <entry><literal>query-start</literal></entry>
3023      <entry><literal>(const char *)</literal></entry>
3024      <entry>Probe that fires when the processing of a query is started.
3025       arg0 is the query string.</entry>
3026     </row>
3027     <row>
3028      <entry><literal>query-done</literal></entry>
3029      <entry><literal>(const char *)</literal></entry>
3030      <entry>Probe that fires when the processing of a query is complete.
3031       arg0 is the query string.</entry>
3032     </row>
3033     <row>
3034      <entry><literal>query-parse-start</literal></entry>
3035      <entry><literal>(const char *)</literal></entry>
3036      <entry>Probe that fires when the parsing of a query is started.
3037       arg0 is the query string.</entry>
3038     </row>
3039     <row>
3040      <entry><literal>query-parse-done</literal></entry>
3041      <entry><literal>(const char *)</literal></entry>
3042      <entry>Probe that fires when the parsing of a query is complete.
3043       arg0 is the query string.</entry>
3044     </row>
3045     <row>
3046      <entry><literal>query-rewrite-start</literal></entry>
3047      <entry><literal>(const char *)</literal></entry>
3048      <entry>Probe that fires when the rewriting of a query is started.
3049       arg0 is the query string.</entry>
3050     </row>
3051     <row>
3052      <entry><literal>query-rewrite-done</literal></entry>
3053      <entry><literal>(const char *)</literal></entry>
3054      <entry>Probe that fires when the rewriting of a query is complete.
3055       arg0 is the query string.</entry>
3056     </row>
3057     <row>
3058      <entry><literal>query-plan-start</literal></entry>
3059      <entry><literal>()</literal></entry>
3060      <entry>Probe that fires when the planning of a query is started.</entry>
3061     </row>
3062     <row>
3063      <entry><literal>query-plan-done</literal></entry>
3064      <entry><literal>()</literal></entry>
3065      <entry>Probe that fires when the planning of a query is complete.</entry>
3066     </row>
3067     <row>
3068      <entry><literal>query-execute-start</literal></entry>
3069      <entry><literal>()</literal></entry>
3070      <entry>Probe that fires when the execution of a query is started.</entry>
3071     </row>
3072     <row>
3073      <entry><literal>query-execute-done</literal></entry>
3074      <entry><literal>()</literal></entry>
3075      <entry>Probe that fires when the execution of a query is complete.</entry>
3076     </row>
3077     <row>
3078      <entry><literal>statement-status</literal></entry>
3079      <entry><literal>(const char *)</literal></entry>
3080      <entry>Probe that fires anytime the server process updates its
3081       <structname>pg_stat_activity</>.<structfield>status</>.
3082       arg0 is the new status string.</entry>
3083     </row>
3084     <row>
3085      <entry><literal>checkpoint-start</literal></entry>
3086      <entry><literal>(int)</literal></entry>
3087      <entry>Probe that fires when a checkpoint is started.
3088       arg0 holds the bitwise flags used to distinguish different checkpoint
3089       types, such as shutdown, immediate or force.</entry>
3090     </row>
3091     <row>
3092      <entry><literal>checkpoint-done</literal></entry>
3093      <entry><literal>(int, int, int, int, int)</literal></entry>
3094      <entry>Probe that fires when a checkpoint is complete.
3095       (The probes listed next fire in sequence during checkpoint processing.)
3096       arg0 is the number of buffers written. arg1 is the total number of
3097       buffers. arg2, arg3 and arg4 contain the number of WAL files added,
3098       removed and recycled respectively.</entry>
3099     </row>
3100     <row>
3101      <entry><literal>clog-checkpoint-start</literal></entry>
3102      <entry><literal>(bool)</literal></entry>
3103      <entry>Probe that fires when the CLOG portion of a checkpoint is started.
3104       arg0 is true for normal checkpoint, false for shutdown
3105       checkpoint.</entry>
3106     </row>
3107     <row>
3108      <entry><literal>clog-checkpoint-done</literal></entry>
3109      <entry><literal>(bool)</literal></entry>
3110      <entry>Probe that fires when the CLOG portion of a checkpoint is
3111       complete. arg0 has the same meaning as for <literal>clog-checkpoint-start</literal>.</entry>
3112     </row>
3113     <row>
3114      <entry><literal>subtrans-checkpoint-start</literal></entry>
3115      <entry><literal>(bool)</literal></entry>
3116      <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
3117       started.
3118       arg0 is true for normal checkpoint, false for shutdown
3119       checkpoint.</entry>
3120     </row>
3121     <row>
3122      <entry><literal>subtrans-checkpoint-done</literal></entry>
3123      <entry><literal>(bool)</literal></entry>
3124      <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
3125       complete. arg0 has the same meaning as for
3126       <literal>subtrans-checkpoint-start</literal>.</entry>
3127     </row>
3128     <row>
3129      <entry><literal>multixact-checkpoint-start</literal></entry>
3130      <entry><literal>(bool)</literal></entry>
3131      <entry>Probe that fires when the MultiXact portion of a checkpoint is
3132       started.
3133       arg0 is true for normal checkpoint, false for shutdown
3134       checkpoint.</entry>
3135     </row>
3136     <row>
3137      <entry><literal>multixact-checkpoint-done</literal></entry>
3138      <entry><literal>(bool)</literal></entry>
3139      <entry>Probe that fires when the MultiXact portion of a checkpoint is
3140       complete. arg0 has the same meaning as for
3141       <literal>multixact-checkpoint-start</literal>.</entry>
3142     </row>
3143     <row>
3144      <entry><literal>buffer-checkpoint-start</literal></entry>
3145      <entry><literal>(int)</literal></entry>
3146      <entry>Probe that fires when the buffer-writing portion of a checkpoint
3147       is started.
3148       arg0 holds the bitwise flags used to distinguish different checkpoint
3149       types, such as shutdown, immediate or force.</entry>
3150     </row>
3151     <row>
3152      <entry><literal>buffer-sync-start</literal></entry>
3153      <entry><literal>(int, int)</literal></entry>
3154      <entry>Probe that fires when we begin to write dirty buffers during
3155       checkpoint (after identifying which buffers must be written).
3156       arg0 is the total number of buffers.
3157       arg1 is the number that are currently dirty and need to be written.</entry>
3158     </row>
3159     <row>
3160      <entry><literal>buffer-sync-written</literal></entry>
3161      <entry><literal>(int)</literal></entry>
3162      <entry>Probe that fires after each buffer is written during checkpoint.
3163       arg0 is the ID number of the buffer.</entry>
3164     </row>
3165     <row>
3166      <entry><literal>buffer-sync-done</literal></entry>
3167      <entry><literal>(int, int, int)</literal></entry>
3168      <entry>Probe that fires when all dirty buffers have been written.
3169       arg0 is the total number of buffers.
3170       arg1 is the number of buffers actually written by the checkpoint process.
3171       arg2 is the number that were expected to be written (arg1 of
3172       <literal>buffer-sync-start</literal>); any difference reflects other processes flushing
3173       buffers during the checkpoint.</entry>
3174     </row>
3175     <row>
3176      <entry><literal>buffer-checkpoint-sync-start</literal></entry>
3177      <entry><literal>()</literal></entry>
3178      <entry>Probe that fires after dirty buffers have been written to the
3179       kernel, and before starting to issue fsync requests.</entry>
3180     </row>
3181     <row>
3182      <entry><literal>buffer-checkpoint-done</literal></entry>
3183      <entry><literal>()</literal></entry>
3184      <entry>Probe that fires when syncing of buffers to disk is
3185       complete.</entry>
3186     </row>
3187     <row>
3188      <entry><literal>twophase-checkpoint-start</literal></entry>
3189      <entry><literal>()</literal></entry>
3190      <entry>Probe that fires when the two-phase portion of a checkpoint is
3191       started.</entry>
3192     </row>
3193     <row>
3194      <entry><literal>twophase-checkpoint-done</literal></entry>
3195      <entry><literal>()</literal></entry>
3196      <entry>Probe that fires when the two-phase portion of a checkpoint is
3197       complete.</entry>
3198     </row>
3199     <row>
3200      <entry><literal>buffer-read-start</literal></entry>
3201      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</literal></entry>
3202      <entry>Probe that fires when a buffer read is started.
3203       arg0 and arg1 contain the fork and block numbers of the page (but
3204       arg1 will be -1 if this is a relation extension request).
3205       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
3206       identifying the relation.
3207       arg5 is the ID of the backend which created the temporary relation for a
3208       local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared buffer.
3209       arg6 is true for a relation extension request, false for normal
3210       read.</entry>
3211     </row>
3212     <row>
3213      <entry><literal>buffer-read-done</literal></entry>
3214      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool, bool)</literal></entry>
3215      <entry>Probe that fires when a buffer read is complete.
3216       arg0 and arg1 contain the fork and block numbers of the page (if this
3217       is a relation extension request, arg1 now contains the block number
3218       of the newly added block).
3219       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
3220       identifying the relation.
3221       arg5 is the ID of the backend which created the temporary relation for a
3222       local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared buffer.
3223       arg6 is true for a relation extension request, false for normal
3224       read.
3225       arg7 is true if the buffer was found in the pool, false if not.</entry>
3226     </row>
3227     <row>
3228      <entry><literal>buffer-flush-start</literal></entry>
3229      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid)</literal></entry>
3230      <entry>Probe that fires before issuing any write request for a shared
3231       buffer.
3232       arg0 and arg1 contain the fork and block numbers of the page.
3233       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
3234       identifying the relation.</entry>
3235     </row>
3236     <row>
3237      <entry><literal>buffer-flush-done</literal></entry>
3238      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid)</literal></entry>
3239      <entry>Probe that fires when a write request is complete.  (Note
3240       that this just reflects the time to pass the data to the kernel;
3241       it's typically not actually been written to disk yet.)
3242       The arguments are the same as for <literal>buffer-flush-start</literal>.</entry>
3243     </row>
3244     <row>
3245      <entry><literal>buffer-write-dirty-start</literal></entry>
3246      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid)</literal></entry>
3247      <entry>Probe that fires when a server process begins to write a dirty
3248       buffer.  (If this happens often, it implies that
3249       <xref linkend="guc-shared-buffers"> is too
3250       small or the background writer control parameters need adjustment.)
3251       arg0 and arg1 contain the fork and block numbers of the page.
3252       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
3253       identifying the relation.</entry>
3254     </row>
3255     <row>
3256      <entry><literal>buffer-write-dirty-done</literal></entry>
3257      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid)</literal></entry>
3258      <entry>Probe that fires when a dirty-buffer write is complete.
3259       The arguments are the same as for <literal>buffer-write-dirty-start</literal>.</entry>
3260     </row>
3261     <row>
3262      <entry><literal>wal-buffer-write-dirty-start</literal></entry>
3263      <entry><literal>()</literal></entry>
3264      <entry>Probe that fires when a server process begins to write a
3265       dirty WAL buffer because no more WAL buffer space is available.
3266       (If this happens often, it implies that
3267       <xref linkend="guc-wal-buffers"> is too small.)</entry>
3268     </row>
3269     <row>
3270      <entry><literal>wal-buffer-write-dirty-done</literal></entry>
3271      <entry><literal>()</literal></entry>
3272      <entry>Probe that fires when a dirty WAL buffer write is complete.</entry>
3273     </row>
3274     <row>
3275      <entry><literal>xlog-insert</literal></entry>
3276      <entry><literal>(unsigned char, unsigned char)</literal></entry>
3277      <entry>Probe that fires when a WAL record is inserted.
3278       arg0 is the resource manager (rmid) for the record.
3279       arg1 contains the info flags.</entry>
3280     </row>
3281     <row>
3282      <entry><literal>xlog-switch</literal></entry>
3283      <entry><literal>()</literal></entry>
3284      <entry>Probe that fires when a WAL segment switch is requested.</entry>
3285     </row>
3286     <row>
3287      <entry><literal>smgr-md-read-start</literal></entry>
3288      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</literal></entry>
3289      <entry>Probe that fires when beginning to read a block from a relation.
3290       arg0 and arg1 contain the fork and block numbers of the page.
3291       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
3292       identifying the relation.
3293       arg5 is the ID of the backend which created the temporary relation for a
3294       local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared buffer.</entry>
3295     </row>
3296     <row>
3297      <entry><literal>smgr-md-read-done</literal></entry>
3298      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</literal></entry>
3299      <entry>Probe that fires when a block read is complete.
3300       arg0 and arg1 contain the fork and block numbers of the page.
3301       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
3302       identifying the relation.
3303       arg5 is the ID of the backend which created the temporary relation for a
3304       local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared buffer.
3305       arg6 is the number of bytes actually read, while arg7 is the number
3306       requested (if these are different it indicates trouble).</entry>
3307     </row>
3308     <row>
3309      <entry><literal>smgr-md-write-start</literal></entry>
3310      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</literal></entry>
3311      <entry>Probe that fires when beginning to write a block to a relation.
3312       arg0 and arg1 contain the fork and block numbers of the page.
3313       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
3314       identifying the relation.
3315       arg5 is the ID of the backend which created the temporary relation for a
3316       local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared buffer.</entry>
3317     </row>
3318     <row>
3319      <entry><literal>smgr-md-write-done</literal></entry>
3320      <entry><literal>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</literal></entry>
3321      <entry>Probe that fires when a block write is complete.
3322       arg0 and arg1 contain the fork and block numbers of the page.
3323       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
3324       identifying the relation.
3325       arg5 is the ID of the backend which created the temporary relation for a
3326       local buffer, or <symbol>InvalidBackendId</symbol> (-1) for a shared buffer.
3327       arg6 is the number of bytes actually written, while arg7 is the number
3328       requested (if these are different it indicates trouble).</entry>
3329     </row>
3330     <row>
3331      <entry><literal>sort-start</literal></entry>
3332      <entry><literal>(int, bool, int, int, bool)</literal></entry>
3333      <entry>Probe that fires when a sort operation is started.
3334       arg0 indicates heap, index or datum sort.
3335       arg1 is true for unique-value enforcement.
3336       arg2 is the number of key columns.
3337       arg3 is the number of kilobytes of work memory allowed.
3338       arg4 is true if random access to the sort result is required.</entry>
3339     </row>
3340     <row>
3341      <entry><literal>sort-done</literal></entry>
3342      <entry><literal>(bool, long)</literal></entry>
3343      <entry>Probe that fires when a sort is complete.
3344       arg0 is true for external sort, false for internal sort.
3345       arg1 is the number of disk blocks used for an external sort,
3346       or kilobytes of memory used for an internal sort.</entry>
3347     </row>
3348     <row>
3349      <entry><literal>lwlock-acquire</literal></entry>
3350      <entry><literal>(char *, int, LWLockMode)</literal></entry>
3351      <entry>Probe that fires when an LWLock has been acquired.
3352       arg0 is the LWLock's tranche.
3353       arg1 is the LWLock's offset within its tranche.
3354       arg2 is the requested lock mode, either exclusive or shared.</entry>
3355     </row>
3356     <row>
3357      <entry><literal>lwlock-release</literal></entry>
3358      <entry><literal>(char *, int)</literal></entry>
3359      <entry>Probe that fires when an LWLock has been released (but note
3360       that any released waiters have not yet been awakened).
3361       arg0 is the LWLock's tranche.
3362       arg1 is the LWLock's offset within its tranche.</entry>
3363     </row>
3364     <row>
3365      <entry><literal>lwlock-wait-start</literal></entry>
3366      <entry><literal>(char *, int, LWLockMode)</literal></entry>
3367      <entry>Probe that fires when an LWLock was not immediately available and
3368       a server process has begun to wait for the lock to become available.
3369       arg0 is the LWLock's tranche.
3370       arg1 is the LWLock's offset within its tranche.
3371       arg2 is the requested lock mode, either exclusive or shared.</entry>
3372     </row>
3373     <row>
3374      <entry><literal>lwlock-wait-done</literal></entry>
3375      <entry><literal>(char *, int, LWLockMode)</literal></entry>
3376      <entry>Probe that fires when a server process has been released from its
3377       wait for an LWLock (it does not actually have the lock yet).
3378       arg0 is the LWLock's tranche.
3379       arg1 is the LWLock's offset within its tranche.
3380       arg2 is the requested lock mode, either exclusive or shared.</entry>
3381     </row>
3382     <row>
3383      <entry><literal>lwlock-condacquire</literal></entry>
3384      <entry><literal>(char *, int, LWLockMode)</literal></entry>
3385      <entry>Probe that fires when an LWLock was successfully acquired when the
3386       caller specified no waiting.
3387       arg0 is the LWLock's tranche.
3388       arg1 is the LWLock's offset within its tranche.
3389       arg2 is the requested lock mode, either exclusive or shared.</entry>
3390     </row>
3391     <row>
3392      <entry><literal>lwlock-condacquire-fail</literal></entry>
3393      <entry><literal>(char *, int, LWLockMode)</literal></entry>
3394      <entry>Probe that fires when an LWLock was not successfully acquired when
3395       the caller specified no waiting.
3396       arg0 is the LWLock's tranche.
3397       arg1 is the LWLock's offset within its tranche.
3398       arg2 is the requested lock mode, either exclusive or shared.</entry>
3399     </row>
3400     <row>
3401      <entry><literal>lock-wait-start</literal></entry>
3402      <entry><literal>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</literal></entry>
3403      <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
3404       has begun to wait because the lock is not available.
3405       arg0 through arg3 are the tag fields identifying the object being
3406       locked.  arg4 indicates the type of object being locked.
3407       arg5 indicates the lock type being requested.</entry>
3408     </row>
3409     <row>
3410      <entry><literal>lock-wait-done</literal></entry>
3411      <entry><literal>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</literal></entry>
3412      <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
3413       has finished waiting (i.e., has acquired the lock).
3414       The arguments are the same as for <literal>lock-wait-start</literal>.</entry>
3415     </row>
3416     <row>
3417      <entry><literal>deadlock-found</literal></entry>
3418      <entry><literal>()</literal></entry>
3419      <entry>Probe that fires when a deadlock is found by the deadlock
3420       detector.</entry>
3421     </row>
3422
3423    </tbody>
3424    </tgroup>
3425   </table>
3426
3427  <table id="typedefs-table">
3428   <title>Defined Types Used in Probe Parameters</title>
3429   <tgroup cols="2">
3430    <thead>
3431     <row>
3432      <entry>Type</entry>
3433      <entry>Definition</entry>
3434     </row>
3435    </thead>
3436
3437    <tbody>
3438
3439     <row>
3440      <entry><type>LocalTransactionId</type></entry>
3441      <entry><type>unsigned int</type></entry>
3442     </row>
3443     <row>
3444      <entry><type>LWLockMode</type></entry>
3445      <entry><type>int</type></entry>
3446     </row>
3447     <row>
3448      <entry><type>LOCKMODE</type></entry>
3449      <entry><type>int</type></entry>
3450     </row>
3451     <row>
3452      <entry><type>BlockNumber</type></entry>
3453      <entry><type>unsigned int</type></entry>
3454     </row>
3455     <row>
3456      <entry><type>Oid</type></entry>
3457      <entry><type>unsigned int</type></entry>
3458     </row>
3459     <row>
3460      <entry><type>ForkNumber</type></entry>
3461      <entry><type>int</type></entry>
3462     </row>
3463     <row>
3464      <entry><type>bool</type></entry>
3465      <entry><type>char</type></entry>
3466     </row>
3467
3468    </tbody>
3469    </tgroup>
3470   </table>
3471
3472
3473   </sect2>
3474
3475   <sect2 id="using-trace-points">
3476    <title>Using Probes</title>
3477
3478   <para>
3479    The example below shows a DTrace script for analyzing transaction
3480    counts in the system, as an alternative to snapshotting
3481    <structname>pg_stat_database</> before and after a performance test:
3482 <programlisting>
3483 #!/usr/sbin/dtrace -qs
3484
3485 postgresql$1:::transaction-start
3486 {
3487       @start["Start"] = count();
3488       self->ts  = timestamp;
3489 }
3490
3491 postgresql$1:::transaction-abort
3492 {
3493       @abort["Abort"] = count();
3494 }
3495
3496 postgresql$1:::transaction-commit
3497 /self->ts/
3498 {
3499       @commit["Commit"] = count();
3500       @time["Total time (ns)"] = sum(timestamp - self->ts);
3501       self->ts=0;
3502 }
3503 </programlisting>
3504    When executed, the example D script gives output such as:
3505 <screen>
3506 # ./txn_count.d `pgrep -n postgres` or ./txn_count.d &lt;PID&gt;
3507 ^C
3508
3509 Start                                          71
3510 Commit                                         70
3511 Total time (ns)                        2312105013
3512 </screen>
3513   </para>
3514
3515   <note>
3516    <para>
3517     SystemTap uses a different notation for trace scripts than DTrace does,
3518     even though the underlying trace points are compatible.  One point worth
3519     noting is that at this writing, SystemTap scripts must reference probe
3520     names using double underscores in place of hyphens.  This is expected to
3521     be fixed in future SystemTap releases.
3522    </para>
3523   </note>
3524
3525   <para>
3526    You should remember that DTrace scripts need to be carefully written and
3527    debugged, otherwise the trace information collected might
3528    be meaningless. In most cases where problems are found it is the
3529    instrumentation that is at fault, not the underlying system. When
3530    discussing information found using dynamic tracing, be sure to enclose
3531    the script used to allow that too to be checked and discussed.
3532   </para>
3533   </sect2>
3534
3535   <sect2 id="defining-trace-points">
3536    <title>Defining New Probes</title>
3537
3538   <para>
3539    New probes can be defined within the code wherever the developer
3540    desires, though this will require a recompilation. Below are the steps
3541    for inserting new probes:
3542   </para>
3543
3544   <procedure>
3545    <step>
3546     <para>
3547      Decide on probe names and data to be made available through the probes
3548     </para>
3549    </step>
3550
3551    <step>
3552     <para>
3553      Add the probe definitions to <filename>src/backend/utils/probes.d</>
3554     </para>
3555    </step>
3556
3557    <step>
3558     <para>
3559      Include <filename>pg_trace.h</> if it is not already present in the
3560      module(s) containing the probe points, and insert
3561      <literal>TRACE_POSTGRESQL</> probe macros at the desired locations
3562      in the source code
3563     </para>
3564    </step>
3565
3566    <step>
3567     <para>
3568      Recompile and verify that the new probes are available
3569     </para>
3570    </step>
3571   </procedure>
3572
3573   <formalpara>
3574    <title>Example:</title>
3575    <para>
3576     Here is an example of how you would add a probe to trace all new
3577     transactions by transaction ID.
3578    </para>
3579   </formalpara>
3580
3581   <procedure>
3582    <step>
3583     <para>
3584      Decide that the probe will be named <literal>transaction-start</> and
3585      requires a parameter of type <type>LocalTransactionId</type>
3586     </para>
3587    </step>
3588
3589    <step>
3590     <para>
3591      Add the probe definition to <filename>src/backend/utils/probes.d</>:
3592 <programlisting>
3593 probe transaction__start(LocalTransactionId);
3594 </programlisting>
3595      Note the use of the double underline in the probe name. In a DTrace
3596      script using the probe, the double underline needs to be replaced with a
3597      hyphen, so <literal>transaction-start</> is the name to document for
3598      users.
3599     </para>
3600    </step>
3601
3602    <step>
3603     <para>
3604      At compile time, <literal>transaction__start</> is converted to a macro
3605      called <literal>TRACE_POSTGRESQL_TRANSACTION_START</> (notice the
3606      underscores are single here), which is available by including
3607      <filename>pg_trace.h</>.  Add the macro call to the appropriate location
3608      in the source code.  In this case, it looks like the following:
3609
3610 <programlisting>
3611 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
3612 </programlisting>
3613     </para>
3614    </step>
3615
3616    <step>
3617     <para>
3618      After recompiling and running the new binary, check that your newly added
3619      probe is available by executing the following DTrace command.  You
3620      should see similar output:
3621 <screen>
3622 # dtrace -ln transaction-start
3623    ID    PROVIDER          MODULE           FUNCTION NAME
3624 18705 postgresql49878     postgres     StartTransactionCommand transaction-start
3625 18755 postgresql49877     postgres     StartTransactionCommand transaction-start
3626 18805 postgresql49876     postgres     StartTransactionCommand transaction-start
3627 18855 postgresql49875     postgres     StartTransactionCommand transaction-start
3628 18986 postgresql49873     postgres     StartTransactionCommand transaction-start
3629 </screen>
3630     </para>
3631    </step>
3632   </procedure>
3633
3634   <para>
3635    There are a few things to be careful about when adding trace macros
3636    to the C code:
3637
3638    <itemizedlist>
3639     <listitem>
3640      <para>
3641       You should take care that the data types specified for a probe's
3642       parameters match the data types of the variables used in the macro.
3643       Otherwise, you will get compilation errors.
3644      </para>
3645     </listitem>
3646
3647
3648     <listitem>
3649      <para>
3650       On most platforms, if <productname>PostgreSQL</productname> is
3651       built with <option>--enable-dtrace</>, the arguments to a trace
3652       macro will be evaluated whenever control passes through the
3653       macro, <emphasis>even if no tracing is being done</>.  This is
3654       usually not worth worrying about if you are just reporting the
3655       values of a few local variables.  But beware of putting expensive
3656       function calls into the arguments.  If you need to do that,
3657       consider protecting the macro with a check to see if the trace
3658       is actually enabled:
3659
3660 <programlisting>
3661 if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
3662     TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
3663 </programlisting>
3664
3665       Each trace macro has a corresponding <literal>ENABLED</> macro.
3666      </para>
3667     </listitem>
3668    </itemizedlist>
3669
3670   </para>
3671
3672   </sect2>
3673
3674  </sect1>
3675
3676 </chapter>