]> granicus.if.org Git - postgresql/blob - doc/src/sgml/monitoring.sgml
Add vacuum and analyze counters to pg_stat_*_tables views.
[postgresql] / doc / src / sgml / monitoring.sgml
1 <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.85 2010/08/21 10:59:17 mha Exp $ -->
2
3 <chapter id="monitoring">
4  <title>Monitoring Database Activity</title>
5
6  <indexterm zone="monitoring">
7   <primary>monitoring</primary>
8   <secondary>database activity</secondary>
9  </indexterm>
10
11  <indexterm zone="monitoring">
12   <primary>database activity</primary>
13   <secondary>monitoring</secondary>
14  </indexterm>
15
16  <para>
17   A database administrator frequently wonders, <quote>What is the system
18   doing right now?</quote>
19   This chapter discusses how to find that out.
20  </para>
21
22   <para>
23    Several tools are available for monitoring database activity and
24    analyzing performance.  Most of this chapter is devoted to describing
25    <productname>PostgreSQL</productname>'s statistics collector,
26    but one should not neglect regular Unix monitoring programs such as
27    <command>ps</>, <command>top</>, <command>iostat</>, and <command>vmstat</>.
28    Also, once one has identified a
29    poorly-performing query, further investigation might be needed using
30    <productname>PostgreSQL</productname>'s <xref linkend="sql-explain"> command.
31    <xref linkend="using-explain"> discusses <command>EXPLAIN</>
32    and other methods for understanding the behavior of an individual
33    query.
34   </para>
35
36  <sect1 id="monitoring-ps">
37   <title>Standard Unix Tools</Title>
38
39   <indexterm zone="monitoring-ps">
40    <primary>ps</primary>
41    <secondary>to monitor activity</secondary>
42   </indexterm>
43
44   <para>
45    On most Unix platforms, <productname>PostgreSQL</productname> modifies its
46    command title as reported by <command>ps</>, so that individual server
47    processes can readily be identified.  A sample display is
48
49 <screen>
50 $ ps auxww | grep ^postgres
51 postgres   960  0.0  1.1  6104 1480 pts/1    SN   13:17   0:00 postgres -i
52 postgres   963  0.0  1.1  7084 1472 pts/1    SN   13:17   0:00 postgres: writer process
53 postgres   965  0.0  1.1  6152 1512 pts/1    SN   13:17   0:00 postgres: stats collector process
54 postgres   998  0.0  2.3  6532 2992 pts/1    SN   13:18   0:00 postgres: tgl runbug 127.0.0.1 idle
55 postgres  1003  0.0  2.4  6532 3128 pts/1    SN   13:19   0:00 postgres: tgl regression [local] SELECT waiting
56 postgres  1016  0.1  2.4  6532 3080 pts/1    SN   13:19   0:00 postgres: tgl regression [local] idle in transaction
57 </screen>
58
59    (The appropriate invocation of <command>ps</> varies across different
60    platforms, as do the details of what is shown.  This example is from a
61    recent Linux system.)  The first process listed here is the
62    master server process.  The command arguments
63    shown for it are the same ones used when it was launched.  The next two
64    processes are background worker processes automatically launched by the
65    master process.  (The <quote>stats collector</> process will not be present
66    if you have set
67    the system not to start the statistics collector.)  Each of the remaining
68    processes is a server process handling one client connection.  Each such
69    process sets its command line display in the form
70
71 <screen>
72 postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <replaceable>activity</>
73 </screen>
74
75   The user, database, and (client) host items remain the same for
76   the life of the client connection, but the activity indicator changes.
77   The activity can be <literal>idle</> (i.e., waiting for a client command),
78   <literal>idle in transaction</> (waiting for client inside a <command>BEGIN</> block),
79   or a command type name such as <literal>SELECT</>.  Also,
80   <literal>waiting</> is appended if the server process is presently waiting
81   on a lock held by another session.  In the above example we can infer
82   that process 1003 is waiting for process 1016 to complete its transaction and
83   thereby release some lock.
84   </para>
85
86   <para>
87    If you have turned off <xref linkend="guc-update-process-title"> then the
88    activity indicator is not updated; the process title is set only once
89    when a new process is launched.  On some platforms this saves a measurable
90    amount of per-command overhead;  on others it's insignificant.
91   </para>
92
93   <tip>
94   <para>
95   <productname>Solaris</productname> requires special handling. You must
96   use <command>/usr/ucb/ps</command>, rather than
97   <command>/bin/ps</command>. You also must use two <option>w</option>
98   flags, not just one. In addition, your original invocation of the
99   <command>postgres</command> command must have a shorter
100   <command>ps</command> status display than that provided by each
101   server process.  If you fail to do all three things, the <command>ps</>
102   output for each server process will be the original <command>postgres</>
103   command line.
104   </para>
105   </tip>
106  </sect1>
107
108  <sect1 id="monitoring-stats">
109   <title>The Statistics Collector</Title>
110
111   <indexterm zone="monitoring-stats">
112    <primary>statistics</primary>
113   </indexterm>
114
115   <para>
116    <productname>PostgreSQL</productname>'s <firstterm>statistics collector</>
117    is a subsystem that supports collection and reporting of information about
118    server activity.  Presently, the collector can count accesses to tables
119    and indexes in both disk-block and individual-row terms.  It also tracks
120    the total number of rows in each table, and information about vacuum and
121    analyze actions for each table.  It can also count calls to user-defined
122    functions and the total time spent in each one.
123   </para>
124
125   <para>
126    <productname>PostgreSQL</productname> also supports reporting of the exact
127    command currently being executed by other server processes.  This
128    facility is independent of the collector process.
129   </para>
130
131  <sect2 id="monitoring-stats-setup">
132   <title>Statistics Collection Configuration</Title>
133
134   <para>
135    Since collection of statistics adds some overhead to query execution,
136    the system can be configured to collect or not collect information.
137    This is controlled by configuration parameters that are normally set in
138    <filename>postgresql.conf</>.  (See <xref linkend="runtime-config"> for
139    details about setting configuration parameters.)
140   </para>
141
142   <para>
143    The parameter <xref linkend="guc-track-counts"> controls whether
144    statistics are collected about table and index accesses.
145   </para>
146
147   <para>
148    The parameter <xref linkend="guc-track-functions"> enables tracking of
149    usage of user-defined functions.
150   </para>
151
152   <para>
153    The parameter <xref linkend="guc-track-activities"> enables monitoring
154    of the current command being executed by any server process.
155   </para>
156
157   <para>
158    Normally these parameters are set in <filename>postgresql.conf</> so
159    that they apply to all server processes, but it is possible to turn
160    them on or off in individual sessions using the <xref
161    linkend="sql-set"> command. (To prevent
162    ordinary users from hiding their activity from the administrator,
163    only superusers are allowed to change these parameters with
164    <command>SET</>.)
165   </para>
166
167   <para>
168    The statistics collector transmits the collected
169    information to backends (including autovacuum) through temporary files.
170    These files are stored in the <filename>pg_stat_tmp</filename> subdirectory.
171    When the postmaster shuts down, a permanent copy of the statistics
172    data is stored in the <filename>global</filename> subdirectory. For increased
173    performance, the parameter <xref linkend="guc-stats-temp-directory"> can
174    be pointed at a RAM-based file system, decreasing physical I/O requirements.
175   </para>
176
177  </sect2>
178
179  <sect2 id="monitoring-stats-views">
180   <title>Viewing Collected Statistics</Title>
181
182   <para>
183    Several predefined views, listed in <xref
184    linkend="monitoring-stats-views-table">, are available to show the results
185    of statistics collection.  Alternatively, one can
186    build custom views using the underlying statistics functions.
187   </para>
188
189   <para>
190    When using the statistics to monitor current activity, it is important
191    to realize that the information does not update instantaneously.
192    Each individual server process transmits new statistical counts to
193    the collector just before going idle; so a query or transaction still in
194    progress does not affect the displayed totals.  Also, the collector itself
195    emits a new report at most once per <varname>PGSTAT_STAT_INTERVAL</varname>
196    milliseconds (500 unless altered while building the server).  So the
197    displayed information lags behind actual activity.  However, current-query
198    information collected by <varname>track_activities</varname> is
199    always up-to-date.
200   </para>
201
202   <para>
203    Another important point is that when a server process is asked to display
204    any of these statistics, it first fetches the most recent report emitted by
205    the collector process and then continues to use this snapshot for all
206    statistical views and functions until the end of its current transaction.
207    So the statistics will show static information as long as you continue the
208    current transaction.  Similarly, information about the current queries of
209    all sessions is collected when any such information is first requested
210    within a transaction, and the same information will be displayed throughout
211    the transaction.
212    This is a feature, not a bug, because it allows you to perform several
213    queries on the statistics and correlate the results without worrying that
214    the numbers are changing underneath you.  But if you want to see new
215    results with each query, be sure to do the queries outside any transaction
216    block.  Alternatively, you can invoke
217    <function>pg_stat_clear_snapshot</function>(), which will discard the
218    current transaction's statistics snapshot (if any).  The next use of
219    statistical information will cause a new snapshot to be fetched.
220   </para>
221
222   <para>
223    A transaction can also see its own statistics (as yet untransmitted to the
224    collector) in the views <structname>pg_stat_xact_all_tables</>,
225    <structname>pg_stat_xact_sys_tables</>,
226    <structname>pg_stat_xact_user_tables</>, and
227    <structname>pg_stat_xact_user_functions</>, or via these views' underlying
228    functions.  These numbers do not act as stated above; instead they update
229    continuously throughout the transaction.
230   </para>
231
232   <table id="monitoring-stats-views-table">
233    <title>Standard Statistics Views</title>
234
235    <tgroup cols="2">
236     <thead>
237      <row>
238       <entry>View Name</entry>
239       <entry>Description</entry>
240      </row>
241     </thead>
242
243     <tbody>
244      <row>
245       <entry><structname>pg_stat_activity</></entry>
246       <entry>One row per server process, showing database OID, database
247       name, process <acronym>ID</>, user OID, user name, application name,
248       client's address and port number, times at which the server process,
249       current transaction, and current query began execution, process's waiting
250       status, and text of the current query.
251       The columns that report data on the current query are available unless
252       the parameter <varname>track_activities</varname> has been turned off.
253       Furthermore, these columns are only visible if the user examining
254       the view is a superuser or the same as the user owning the process
255       being reported on.
256      </entry>
257      </row>
258
259      <row>
260       <entry><structname>pg_stat_bgwriter</></entry>
261       <entry>One row only, showing cluster-wide statistics from the
262       background writer: number of scheduled checkpoints, requested
263       checkpoints, buffers written by checkpoints and cleaning scans,
264       and the number of times the background writer stopped a cleaning scan
265       because it had written too many buffers.  Also includes
266       statistics about the shared buffer pool, including buffers written
267       by backends (that is, not by the background writer) and total buffers
268       allocated.
269      </entry>
270      </row>
271
272      <row>
273       <entry><structname>pg_stat_database</></entry>
274       <entry>One row per database, showing database OID, database name,
275       number of active server processes connected to that database,
276       number of transactions committed and rolled back in that database,
277       total disk blocks read, total buffer hits (i.e., block
278       read requests avoided by finding the block already in buffer cache),
279       number of rows returned, fetched, inserted, updated and deleted.
280      </entry>
281      </row>
282
283      <row>
284       <entry><structname>pg_stat_all_tables</></entry>
285       <entry>For each table in the current database (including TOAST tables),
286       the table OID, schema and table name, number of sequential
287       scans initiated, number of live rows fetched by sequential
288       scans, number of index scans initiated (over all indexes
289       belonging to the table), number of live rows fetched by index
290       scans, numbers of row insertions, updates, and deletions,
291       number of row updates that were HOT (i.e., no separate index update),
292       numbers of live and dead rows,
293       the last time the table was vacuumed manually,
294       the last time it was vacuumed by the autovacuum daemon,
295       the last time it was analyzed manually,
296       the last time it was analyzed by the autovacuum daemon,
297       number of times it has been vacuumed manually,
298       number of times it has been vacuumed by the autovacuum daemon,
299       number of times it has been analyzed manually,
300       and the number of times it has been analyzed by the autovacuum daemon.
301       </entry>
302      </row>
303
304      <row>
305       <entry><structname>pg_stat_sys_tables</></entry>
306       <entry>Same as <structname>pg_stat_all_tables</>, except that only
307       system tables are shown.</entry>
308      </row>
309
310      <row>
311       <entry><structname>pg_stat_user_tables</></entry>
312       <entry>Same as <structname>pg_stat_all_tables</>, except that only user
313       tables are shown.</entry>
314      </row>
315
316      <row>
317       <entry><structname>pg_stat_xact_all_tables</></entry>
318       <entry>Similar to <structname>pg_stat_all_tables</>, but counts actions
319       taken so far within the current transaction (which are <emphasis>not</>
320       yet included in <structname>pg_stat_all_tables</> and related views).
321       The columns for numbers of live and dead rows and vacuum and
322       analyze actions are not present in this view.</entry>
323      </row>
324
325      <row>
326       <entry><structname>pg_stat_xact_sys_tables</></entry>
327       <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
328       system tables are shown.</entry>
329      </row>
330
331      <row>
332       <entry><structname>pg_stat_xact_user_tables</></entry>
333       <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
334       user tables are shown.</entry>
335      </row>
336
337      <row>
338       <entry><structname>pg_stat_all_indexes</></entry>
339       <entry>For each index in the current database,
340       the table and index OID, schema, table and index name,
341       number of index scans initiated on that index, number of
342       index entries returned by index scans, and number of live table rows
343       fetched by simple index scans using that index.
344       </entry>
345      </row>
346
347      <row>
348       <entry><structname>pg_stat_sys_indexes</></entry>
349       <entry>Same as <structname>pg_stat_all_indexes</>, except that only
350       indexes on system tables are shown.</entry>
351      </row>
352
353      <row>
354       <entry><structname>pg_stat_user_indexes</></entry>
355       <entry>Same as <structname>pg_stat_all_indexes</>, except that only
356       indexes on user tables are shown.</entry>
357      </row>
358
359      <row>
360       <entry><structname>pg_statio_all_tables</></entry>
361       <entry>For each table in the current database (including TOAST tables),
362       the table OID, schema and table name, number of disk
363       blocks read from that table, number of buffer hits, numbers of
364       disk blocks read and buffer hits in all indexes of that table,
365       numbers of disk blocks read and buffer hits from that table's
366       auxiliary TOAST table (if any), and numbers of disk blocks read
367       and buffer hits for the TOAST table's index.
368       </entry>
369      </row>
370
371      <row>
372       <entry><structname>pg_statio_sys_tables</></entry>
373       <entry>Same as <structname>pg_statio_all_tables</>, except that only
374       system tables are shown.</entry>
375      </row>
376
377      <row>
378       <entry><structname>pg_statio_user_tables</></entry>
379       <entry>Same as <structname>pg_statio_all_tables</>, except that only
380       user tables are shown.</entry>
381      </row>
382
383      <row>
384       <entry><structname>pg_statio_all_indexes</></entry>
385       <entry>For each index in the current database,
386       the table and index OID, schema, table and index name,
387       numbers of disk blocks read and buffer hits in that index.
388       </entry>
389      </row>
390
391      <row>
392       <entry><structname>pg_statio_sys_indexes</></entry>
393       <entry>Same as <structname>pg_statio_all_indexes</>, except that only
394       indexes on system tables are shown.</entry>
395      </row>
396
397      <row>
398       <entry><structname>pg_statio_user_indexes</></entry>
399       <entry>Same as <structname>pg_statio_all_indexes</>, except that only
400       indexes on user tables are shown.</entry>
401      </row>
402
403      <row>
404       <entry><structname>pg_statio_all_sequences</></entry>
405       <entry>For each sequence object in the current database,
406       the sequence OID, schema and sequence name,
407       numbers of disk blocks read and buffer hits in that sequence.
408       </entry>
409      </row>
410
411      <row>
412       <entry><structname>pg_statio_sys_sequences</></entry>
413       <entry>Same as <structname>pg_statio_all_sequences</>, except that only
414       system sequences are shown.  (Presently, no system sequences are defined,
415       so this view is always empty.)</entry>
416      </row>
417
418      <row>
419       <entry><structname>pg_statio_user_sequences</></entry>
420       <entry>Same as <structname>pg_statio_all_sequences</>, except that only
421       user sequences are shown.</entry>
422      </row>
423
424      <row>
425       <entry><structname>pg_stat_user_functions</></entry>
426       <entry>For all tracked functions, function OID, schema, name, number
427       of calls, total time, and self time.  Self time is the
428       amount of time spent in the function itself, total time includes the
429       time spent in functions it called. Time values are in milliseconds.
430      </entry>
431      </row>
432
433      <row>
434       <entry><structname>pg_stat_xact_user_functions</></entry>
435       <entry>Similar to <structname>pg_stat_user_functions</>, but counts only
436       calls during the current transaction (which are <emphasis>not</>
437       yet included in <structname>pg_stat_user_functions</>).</entry>
438      </row>
439
440     </tbody>
441    </tgroup>
442   </table>
443
444   <para>
445    The per-index statistics are particularly useful to determine which
446    indexes are being used and how effective they are.
447   </para>
448
449   <para>
450    Indexes can be
451    used either directly or via <quote>bitmap scans</>.  In a bitmap scan
452    the output of several indexes can be combined via AND or OR rules;
453    so it is difficult to associate individual heap row fetches
454    with specific indexes when a bitmap scan is used.  Therefore, a bitmap
455    scan increments the
456    <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
457    count(s) for the index(es) it uses, and it increments the
458    <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
459    count for the table, but it does not affect
460    <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
461   </para>
462
463   <note>
464    <para>
465     Before <productname>PostgreSQL</productname> 8.1, the
466     <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
467     were essentially always equal.  Now they can be different even without
468     considering bitmap scans, because <structfield>idx_tup_read</> counts
469     index entries retrieved from the index while <structfield>idx_tup_fetch</>
470     counts live rows fetched from the table; the latter will be less if any
471     dead or not-yet-committed rows are fetched using the index.
472    </para>
473   </note>
474
475   <para>
476    The <structname>pg_statio_</> views are primarily useful to
477    determine the effectiveness of the buffer cache.  When the number
478    of actual disk reads is much smaller than the number of buffer
479    hits, then the cache is satisfying most read requests without
480    invoking a kernel call. However, these statistics do not give the
481    entire story: due to the way in which <productname>PostgreSQL</>
482    handles disk I/O, data that is not in the
483    <productname>PostgreSQL</> buffer cache might still reside in the
484    kernel's I/O cache, and might therefore still be fetched without
485    requiring a physical read. Users interested in obtaining more
486    detailed information on <productname>PostgreSQL</> I/O behavior are
487    advised to use the <productname>PostgreSQL</> statistics collector
488    in combination with operating system utilities that allow insight
489    into the kernel's handling of I/O.
490   </para>
491
492   <para>
493    Other ways of looking at the statistics can be set up by writing
494    queries that use the same underlying statistics access functions as
495    these standard views do.  These functions are listed in <xref
496    linkend="monitoring-stats-funcs-table">.  The per-database access
497    functions take a database OID as argument to identify which
498    database to report on.  The per-table and per-index functions take
499    a table or index OID.  The functions for function-call statistics
500    take a function OID.  (Note that only tables, indexes, and functions
501    in the current database can be seen with these functions.)  The
502    per-server-process access functions take a server process
503    number, which ranges from one to the number of currently active
504    server processes.
505   </para>
506
507   <table id="monitoring-stats-funcs-table">
508    <title>Statistics Access Functions</title>
509
510    <tgroup cols="3">
511     <thead>
512      <row>
513       <entry>Function</entry>
514       <entry>Return Type</entry>
515       <entry>Description</entry>
516      </row>
517     </thead>
518
519     <tbody>
520      <row>
521       <entry><literal><function>pg_stat_get_db_numbackends</function>(<type>oid</type>)</literal></entry>
522       <entry><type>integer</type></entry>
523       <entry>
524        Number of active server processes for database
525       </entry>
526      </row>
527
528      <row>
529       <entry><literal><function>pg_stat_get_db_xact_commit</function>(<type>oid</type>)</literal></entry>
530       <entry><type>bigint</type></entry>
531       <entry>
532        Number of transactions committed in database
533       </entry>
534      </row>
535
536      <row>
537       <entry><literal><function>pg_stat_get_db_xact_rollback</function>(<type>oid</type>)</literal></entry>
538       <entry><type>bigint</type></entry>
539       <entry>
540        Number of transactions rolled back in database
541       </entry>
542      </row>
543
544      <row>
545       <entry><literal><function>pg_stat_get_db_blocks_fetched</function>(<type>oid</type>)</literal></entry>
546       <entry><type>bigint</type></entry>
547       <entry>
548        Number of disk block fetch requests for database
549       </entry>
550      </row>
551
552      <row>
553       <entry><literal><function>pg_stat_get_db_blocks_hit</function>(<type>oid</type>)</literal></entry>
554       <entry><type>bigint</type></entry>
555       <entry>
556        Number of disk block fetch requests found in cache for database
557       </entry>
558      </row>
559
560      <row>
561       <entry><literal><function>pg_stat_get_db_tuples_returned</function>(<type>oid</type>)</literal></entry>
562       <entry><type>bigint</type></entry>
563       <entry>
564        Number of tuples returned for database
565       </entry>
566      </row>
567
568      <row>
569       <entry><literal><function>pg_stat_get_db_tuples_fetched</function>(<type>oid</type>)</literal></entry>
570       <entry><type>bigint</type></entry>
571       <entry>
572        Number of tuples fetched for database
573       </entry>
574      </row>
575
576      <row>
577       <entry><literal><function>pg_stat_get_db_tuples_inserted</function>(<type>oid</type>)</literal></entry>
578       <entry><type>bigint</type></entry>
579       <entry>
580        Number of tuples inserted in database
581       </entry>
582      </row>
583
584      <row>
585       <entry><literal><function>pg_stat_get_db_tuples_updated</function>(<type>oid</type>)</literal></entry>
586       <entry><type>bigint</type></entry>
587       <entry>
588        Number of tuples updated in database
589       </entry>
590      </row>
591
592      <row>
593       <entry><literal><function>pg_stat_get_db_tuples_deleted</function>(<type>oid</type>)</literal></entry>
594       <entry><type>bigint</type></entry>
595       <entry>
596        Number of tuples deleted in database
597       </entry>
598      </row>
599
600      <row>
601       <entry><literal><function>pg_stat_get_numscans</function>(<type>oid</type>)</literal></entry>
602       <entry><type>bigint</type></entry>
603       <entry>
604        Number of sequential scans done when argument is a table,
605        or number of index scans done when argument is an index
606       </entry>
607      </row>
608
609      <row>
610       <entry><literal><function>pg_stat_get_tuples_returned</function>(<type>oid</type>)</literal></entry>
611       <entry><type>bigint</type></entry>
612       <entry>
613        Number of rows read by sequential scans when argument is a table,
614        or number of index entries returned when argument is an index
615       </entry>
616      </row>
617
618      <row>
619       <entry><literal><function>pg_stat_get_tuples_fetched</function>(<type>oid</type>)</literal></entry>
620       <entry><type>bigint</type></entry>
621       <entry>
622        Number of table rows fetched by bitmap scans when argument is a table,
623        or table rows fetched by simple index scans using the index
624        when argument is an index
625       </entry>
626      </row>
627
628      <row>
629       <entry><literal><function>pg_stat_get_tuples_inserted</function>(<type>oid</type>)</literal></entry>
630       <entry><type>bigint</type></entry>
631       <entry>
632        Number of rows inserted into table
633       </entry>
634      </row>
635
636      <row>
637       <entry><literal><function>pg_stat_get_tuples_updated</function>(<type>oid</type>)</literal></entry>
638       <entry><type>bigint</type></entry>
639       <entry>
640        Number of rows updated in table (includes HOT updates)
641       </entry>
642      </row>
643
644      <row>
645       <entry><literal><function>pg_stat_get_tuples_deleted</function>(<type>oid</type>)</literal></entry>
646       <entry><type>bigint</type></entry>
647       <entry>
648        Number of rows deleted from table
649       </entry>
650      </row>
651
652      <row>
653       <entry><literal><function>pg_stat_get_tuples_hot_updated</function>(<type>oid</type>)</literal></entry>
654       <entry><type>bigint</type></entry>
655       <entry>
656        Number of rows HOT-updated in table
657       </entry>
658      </row>
659
660      <row>
661       <entry><literal><function>pg_stat_get_live_tuples</function>(<type>oid</type>)</literal></entry>
662       <entry><type>bigint</type></entry>
663       <entry>
664        Number of live rows in table
665       </entry>
666      </row>
667
668      <row>
669       <entry><literal><function>pg_stat_get_dead_tuples</function>(<type>oid</type>)</literal></entry>
670       <entry><type>bigint</type></entry>
671       <entry>
672        Number of dead rows in table
673       </entry>
674      </row>
675
676      <row>
677       <entry><literal><function>pg_stat_get_blocks_fetched</function>(<type>oid</type>)</literal></entry>
678       <entry><type>bigint</type></entry>
679       <entry>
680        Number of disk block fetch requests for table or index
681       </entry>
682      </row>
683
684      <row>
685       <entry><literal><function>pg_stat_get_blocks_hit</function>(<type>oid</type>)</literal></entry>
686       <entry><type>bigint</type></entry>
687       <entry>
688        Number of disk block requests found in cache for table or index
689       </entry>
690      </row>
691
692      <row>
693       <entry><literal><function>pg_stat_get_last_vacuum_time</function>(<type>oid</type>)</literal></entry>
694       <entry><type>timestamptz</type></entry>
695       <entry>
696        Time of the last vacuum initiated by the user on this table
697       </entry>
698      </row>
699
700      <row>
701       <entry><literal><function>pg_stat_get_last_autovacuum_time</function>(<type>oid</type>)</literal></entry>
702       <entry><type>timestamptz</type></entry>
703       <entry>
704        Time of the last vacuum initiated by the autovacuum daemon on this table
705       </entry>
706      </row>
707
708      <row>
709       <entry><literal><function>pg_stat_get_last_analyze_time</function>(<type>oid</type>)</literal></entry>
710       <entry><type>timestamptz</type></entry>
711       <entry>
712        Time of the last analyze initiated by the user on this table
713       </entry>
714      </row>
715
716      <row>
717       <entry><literal><function>pg_stat_get_last_autoanalyze_time</function>(<type>oid</type>)</literal></entry>
718       <entry><type>timestamptz</type></entry>
719       <entry>
720        Time of the last analyze initiated by the autovacuum daemon on this
721        table
722       </entry>
723      </row>
724
725      <row>
726       <entry><literal><function>pg_stat_get_vacuum_count</function>(<type>oid</type>)</literal></entry>
727       <entry><type>bigint</type></entry>
728       <entry>
729        The number of times this table has been vacuumed manually
730       </entry>
731      </row>
732
733      <row>
734       <entry><literal><function>pg_stat_get_autovacuum_count</function>(<type>oid</type>)</literal></entry>
735       <entry><type>bigint</type></entry>
736       <entry>
737        The number of times this table has been vacuumed by the autovacuum daemon
738       </entry>
739      </row>
740
741      <row>
742       <entry><literal><function>pg_stat_get_analyze_count</function>(<type>oid</type>)</literal></entry>
743       <entry><type>bigint</type></entry>
744       <entry>
745        The number of times this table has been analyzed manually
746       </entry>
747      </row>
748
749      <row>
750       <entry><literal><function>pg_stat_get_autoanalyze_count</function>(<type>oid</type>)</literal></entry>
751       <entry><type>bigint</type></entry>
752       <entry>
753        The number of times this table has been analyzed by the autovacuum daemon
754       </entry>
755      </row>
756
757      <row>
758       <entry><literal><function>pg_stat_get_xact_numscans</function>(<type>oid</type>)</literal></entry>
759       <entry><type>bigint</type></entry>
760       <entry>
761        Number of sequential scans done when argument is a table,
762        or number of index scans done when argument is an index, in the current transaction
763       </entry>
764      </row>
765
766      <row>
767       <entry><literal><function>pg_stat_get_xact_tuples_returned</function>(<type>oid</type>)</literal></entry>
768       <entry><type>bigint</type></entry>
769       <entry>
770        Number of rows read by sequential scans when argument is a table,
771        or number of index entries returned when argument is an index, in the current transaction
772       </entry>
773      </row>
774
775      <row>
776       <entry><literal><function>pg_stat_get_xact_tuples_fetched</function>(<type>oid</type>)</literal></entry>
777       <entry><type>bigint</type></entry>
778       <entry>
779        Number of table rows fetched by bitmap scans when argument is a table,
780        or table rows fetched by simple index scans using the index
781        when argument is an index, in the current transaction
782       </entry>
783      </row>
784
785      <row>
786       <entry><literal><function>pg_stat_get_xact_tuples_inserted</function>(<type>oid</type>)</literal></entry>
787       <entry><type>bigint</type></entry>
788       <entry>
789        Number of rows inserted into table, in the current transaction
790       </entry>
791      </row>
792
793      <row>
794       <entry><literal><function>pg_stat_get_xact_tuples_updated</function>(<type>oid</type>)</literal></entry>
795       <entry><type>bigint</type></entry>
796       <entry>
797        Number of rows updated in table (includes HOT updates), in the current transaction
798       </entry>
799      </row>
800
801      <row>
802       <entry><literal><function>pg_stat_get_xact_tuples_deleted</function>(<type>oid</type>)</literal></entry>
803       <entry><type>bigint</type></entry>
804       <entry>
805        Number of rows deleted from table, in the current transaction
806       </entry>
807      </row>
808
809      <row>
810       <entry><literal><function>pg_stat_get_xact_tuples_hot_updated</function>(<type>oid</type>)</literal></entry>
811       <entry><type>bigint</type></entry>
812       <entry>
813        Number of rows HOT-updated in table, in the current transaction
814       </entry>
815      </row>
816
817      <row>
818       <entry><literal><function>pg_stat_get_xact_blocks_fetched</function>(<type>oid</type>)</literal></entry>
819       <entry><type>bigint</type></entry>
820       <entry>
821        Number of disk block fetch requests for table or index, in the current transaction
822       </entry>
823      </row>
824
825      <row>
826       <entry><literal><function>pg_stat_get_xact_blocks_hit</function>(<type>oid</type>)</literal></entry>
827       <entry><type>bigint</type></entry>
828       <entry>
829        Number of disk block requests found in cache for table or index, in the current transaction
830       </entry>
831      </row>
832
833      <row>
834        <!-- See also the entry for this in func.sgml -->
835       <entry><literal><function>pg_backend_pid</function>()</literal></entry>
836       <entry><type>integer</type></entry>
837       <entry>
838        Process ID of the server process attached to the current session
839       </entry>
840      </row>
841
842      <row>
843       <entry><literal><function>pg_stat_get_activity</function>(<type>integer</type>)</literal></entry>
844       <entry><type>setof record</type></entry>
845       <entry>
846        Returns a record of information about the backend with the specified PID, or
847        one record for each active backend in the system if <symbol>NULL</symbol> is
848        specified. The fields returned are a subset of those in the
849        <structname>pg_stat_activity</structname> view.
850       </entry>
851      </row>
852
853      <row>
854       <entry><literal><function>pg_stat_get_function_calls</function>(<type>oid</type>)</literal></entry>
855       <entry><type>bigint</type></entry>
856       <entry>
857        Number of times the function has been called
858       </entry>
859      </row>
860
861      <row>
862       <entry><literal><function>pg_stat_get_function_time</function>(<type>oid</type>)</literal></entry>
863       <entry><type>bigint</type></entry>
864       <entry>
865        Total wall clock time spent in the function, in microseconds.  Includes
866        the time spent in functions called by this one.
867       </entry>
868      </row>
869
870      <row>
871       <entry><literal><function>pg_stat_get_function_self_time</function>(<type>oid</type>)</literal></entry>
872       <entry><type>bigint</type></entry>
873       <entry>
874        Time spent in only this function. Time spent in called functions
875        is excluded.
876       </entry>
877      </row>
878
879      <row>
880       <entry><literal><function>pg_stat_get_xact_function_calls</function>(<type>oid</type>)</literal></entry>
881       <entry><type>bigint</type></entry>
882       <entry>
883        Number of times the function has been called, in the current transaction.
884       </entry>
885      </row>
886
887      <row>
888       <entry><literal><function>pg_stat_get_xact_function_time</function>(<type>oid</type>)</literal></entry>
889       <entry><type>bigint</type></entry>
890       <entry>
891        Total wall clock time spent in the function, in microseconds, in the
892        current transaction.  Includes the time spent in functions called by
893        this one.
894       </entry>
895      </row>
896
897      <row>
898       <entry><literal><function>pg_stat_get_xact_function_self_time</function>(<type>oid</type>)</literal></entry>
899       <entry><type>bigint</type></entry>
900       <entry>
901        Time spent in only this function, in the current transaction. Time
902        spent in called functions is excluded.
903       </entry>
904      </row>
905
906      <row>
907       <entry><literal><function>pg_stat_get_backend_idset</function>()</literal></entry>
908       <entry><type>setof integer</type></entry>
909       <entry>
910        Set of currently active server process numbers (from 1 to the
911        number of active server processes).  See usage example in the text.
912       </entry>
913      </row>
914
915      <row>
916       <entry><literal><function>pg_stat_get_backend_pid</function>(<type>integer</type>)</literal></entry>
917       <entry><type>integer</type></entry>
918       <entry>
919        Process ID of the given server process
920       </entry>
921      </row>
922
923      <row>
924       <entry><literal><function>pg_stat_get_backend_dbid</function>(<type>integer</type>)</literal></entry>
925       <entry><type>oid</type></entry>
926       <entry>
927        Database ID of the given server process
928       </entry>
929      </row>
930
931      <row>
932       <entry><literal><function>pg_stat_get_backend_userid</function>(<type>integer</type>)</literal></entry>
933       <entry><type>oid</type></entry>
934       <entry>
935        User ID of the given server process
936       </entry>
937      </row>
938
939      <row>
940       <entry><literal><function>pg_stat_get_backend_activity</function>(<type>integer</type>)</literal></entry>
941       <entry><type>text</type></entry>
942       <entry>
943        Active command of the given server process, but only if the
944        current user is a superuser or the same user as that of
945        the session being queried (and
946        <varname>track_activities</varname> is on)
947       </entry>
948      </row>
949
950      <row>
951       <entry><literal><function>pg_stat_get_backend_waiting</function>(<type>integer</type>)</literal></entry>
952       <entry><type>boolean</type></entry>
953       <entry>
954        True if the given server process is waiting for a lock,
955        but only if the current user is a superuser or the same user as that of
956        the session being queried (and
957        <varname>track_activities</varname> is on)
958       </entry>
959      </row>
960
961      <row>
962       <entry><literal><function>pg_stat_get_backend_activity_start</function>(<type>integer</type>)</literal></entry>
963       <entry><type>timestamp with time zone</type></entry>
964       <entry>
965        The time at which the given server process' currently
966        executing query was started, but only if the
967        current user is a superuser or the same user as that of
968        the session being queried (and
969        <varname>track_activities</varname> is on)
970       </entry>
971      </row>
972
973      <row>
974       <entry><literal><function>pg_stat_get_backend_xact_start</function>(<type>integer</type>)</literal></entry>
975       <entry><type>timestamp with time zone</type></entry>
976       <entry>
977        The time at which the given server process' currently
978        executing transaction was started, but only if the
979        current user is a superuser or the same user as that of
980        the session being queried (and
981        <varname>track_activities</varname> is on)
982       </entry>
983      </row>
984
985      <row>
986       <entry><literal><function>pg_stat_get_backend_start</function>(<type>integer</type>)</literal></entry>
987       <entry><type>timestamp with time zone</type></entry>
988       <entry>
989        The time at which the given server process was started, or
990        null if the current user is not a superuser nor the same user
991        as that of the session being queried
992       </entry>
993      </row>
994
995      <row>
996       <entry><literal><function>pg_stat_get_backend_client_addr</function>(<type>integer</type>)</literal></entry>
997       <entry><type>inet</type></entry>
998       <entry>
999        The IP address of the client connected to the given
1000        server process; null if the connection is over a Unix domain
1001        socket, also null if the current user is not a superuser nor
1002        the same user as that of the session being queried
1003       </entry>
1004      </row>
1005
1006      <row>
1007       <entry><literal><function>pg_stat_get_backend_client_port</function>(<type>integer</type>)</literal></entry>
1008       <entry><type>integer</type></entry>
1009       <entry>
1010        The TCP port number of the client connected to the given
1011        server process; -1 if the connection is over a Unix domain
1012        socket, null if the current user is not a superuser nor the
1013        same user as that of the session being queried
1014       </entry>
1015      </row>
1016
1017      <row>
1018       <entry><literal><function>pg_stat_get_bgwriter_timed_checkpoints</function>()</literal></entry>
1019        <entry><type>bigint</type></entry>
1020        <entry>
1021         Number of times the background writer has started timed checkpoints
1022         (because the <varname>checkpoint_timeout</varname> time has expired)
1023        </entry>
1024      </row>
1025
1026      <row>
1027       <entry><literal><function>pg_stat_get_bgwriter_requested_checkpoints</function>()</literal></entry>
1028       <entry><type>bigint</type></entry>
1029       <entry>
1030        Number of times the background writer has started checkpoints based
1031        on requests from backends because the <varname>checkpoint_segments</varname>
1032        has been exceeded or because the <command>CHECKPOINT</command>
1033        command has been issued
1034       </entry>
1035      </row>
1036
1037      <row>
1038       <entry><literal><function>pg_stat_get_bgwriter_buf_written_checkpoints</function>()</literal></entry>
1039       <entry><type>bigint</type></entry>
1040       <entry>
1041        Number of buffers written by the background writer during checkpoints
1042       </entry>
1043      </row>
1044
1045      <row>
1046       <entry><literal><function>pg_stat_get_bgwriter_buf_written_clean</function>()</literal></entry>
1047       <entry><type>bigint</type></entry>
1048       <entry>
1049        Number of buffers written by the background writer for routine cleaning of
1050        dirty pages
1051       </entry>
1052      </row>
1053
1054      <row>
1055       <entry><literal><function>pg_stat_get_bgwriter_maxwritten_clean</function>()</literal></entry>
1056       <entry><type>bigint</type></entry>
1057       <entry>
1058        Number of times the background writer has stopped its cleaning scan because
1059        it has written more buffers than specified in the
1060        <varname>bgwriter_lru_maxpages</varname> parameter
1061       </entry>
1062      </row>
1063
1064      <row>
1065       <entry><literal><function>pg_stat_get_buf_written_backend</function>()</literal></entry>
1066       <entry><type>bigint</type></entry>
1067       <entry>
1068        Number of buffers written by backends because they needed
1069        to allocate a new buffer
1070       </entry>
1071      </row>
1072
1073      <row>
1074       <entry><literal><function>pg_stat_get_buf_alloc</function>()</literal></entry>
1075       <entry><type>bigint</type></entry>
1076       <entry>
1077        Total number of buffer allocations
1078       </entry>
1079      </row>
1080
1081      <row>
1082       <entry><literal><function>pg_stat_clear_snapshot</function>()</literal></entry>
1083       <entry><type>void</type></entry>
1084       <entry>
1085        Discard the current statistics snapshot
1086       </entry>
1087      </row>
1088
1089      <row>
1090       <entry><literal><function>pg_stat_reset</function>()</literal></entry>
1091       <entry><type>void</type></entry>
1092       <entry>
1093        Reset all statistics counters for the current database to zero
1094        (requires superuser privileges)
1095       </entry>
1096      </row>
1097
1098      <row>
1099       <entry><literal><function>pg_stat_reset_shared</function>(text)</literal></entry>
1100       <entry><type>void</type></entry>
1101       <entry>
1102        Reset some of the shared statistics counters for the database cluster to
1103        zero (requires superuser privileges).  Calling
1104        <literal>pg_stat_reset_shared('bgwriter')</> will zero all the values shown by
1105        <structname>pg_stat_bgwriter</>.
1106       </entry>
1107      </row>
1108
1109      <row>
1110       <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal></entry>
1111       <entry><type>void</type></entry>
1112       <entry>
1113        Reset statistics for a single table or index in the current database to
1114        zero (requires superuser privileges)
1115       </entry>
1116      </row>
1117
1118      <row>
1119       <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal></entry>
1120       <entry><type>void</type></entry>
1121       <entry>
1122        Reset statistics for a single function in the current database to
1123        zero (requires superuser privileges)
1124       </entry>
1125      </row>
1126     </tbody>
1127    </tgroup>
1128   </table>
1129
1130    <note>
1131     <para>
1132      <function>pg_stat_get_blocks_fetched</function> minus
1133      <function>pg_stat_get_blocks_hit</function> gives the number of kernel
1134      <function>read()</> calls issued for the table, index, or
1135      database; the number of actual physical reads is usually
1136      lower due to kernel-level buffering.  The <literal>*_blks_read</>
1137      statistics columns use this subtraction, i.e., fetched minus hit.
1138     </para>
1139    </note>
1140
1141   <para>
1142    All functions to access information about backends are indexed by backend id
1143    number, except <function>pg_stat_get_activity</function> which is indexed by PID.
1144    The function <function>pg_stat_get_backend_idset</function> provides
1145    a convenient way to generate one row for each active server process.  For
1146    example, to show the <acronym>PID</>s and current queries of all server processes:
1147
1148 <programlisting>
1149 SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
1150        pg_stat_get_backend_activity(s.backendid) AS current_query
1151     FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
1152 </programlisting>
1153   </para>
1154
1155  </sect2>
1156  </sect1>
1157
1158  <sect1 id="monitoring-locks">
1159   <title>Viewing Locks</title>
1160
1161   <indexterm zone="monitoring-locks">
1162    <primary>lock</primary>
1163    <secondary>monitoring</secondary>
1164   </indexterm>
1165
1166   <para>
1167    Another useful tool for monitoring database activity is the
1168    <structname>pg_locks</structname> system table.  It allows the
1169    database administrator to view information about the outstanding
1170    locks in the lock manager. For example, this capability can be used
1171    to:
1172
1173    <itemizedlist>
1174     <listitem>
1175      <para>
1176       View all the locks currently outstanding, all the locks on
1177       relations in a particular database, all the locks on a
1178       particular relation, or all the locks held by a particular
1179       <productname>PostgreSQL</productname> session.
1180      </para>
1181     </listitem>
1182
1183     <listitem>
1184      <para>
1185       Determine the relation in the current database with the most
1186       ungranted locks (which might be a source of contention among
1187       database clients).
1188      </para>
1189     </listitem>
1190
1191     <listitem>
1192      <para>
1193       Determine the effect of lock contention on overall database
1194       performance, as well as the extent to which contention varies
1195       with overall database traffic.
1196      </para>
1197     </listitem>
1198    </itemizedlist>
1199
1200    Details of the <structname>pg_locks</structname> view appear in
1201    <xref linkend="view-pg-locks">.
1202    For more information on locking and managing concurrency with
1203    <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
1204   </para>
1205  </sect1>
1206
1207  <sect1 id="dynamic-trace">
1208   <title>Dynamic Tracing</title>
1209
1210  <indexterm zone="dynamic-trace">
1211   <primary>DTrace</primary>
1212  </indexterm>
1213
1214   <para>
1215    <productname>PostgreSQL</productname> provides facilities to support
1216    dynamic tracing of the database server. This allows an external
1217    utility to be called at specific points in the code and thereby trace
1218    execution.
1219   </para>
1220
1221   <para>
1222    A number of probes or trace points are already inserted into the source
1223    code. These probes are intended to be used by database developers and
1224    administrators. By default the probes are not compiled into
1225    <productname>PostgreSQL</productname>; the user needs to explicitly tell
1226    the configure script to make the probes available.
1227   </para>
1228
1229   <para>
1230    Currently, only the
1231    <ulink url="http://opensolaris.org/os/community/dtrace/">DTrace</ulink>
1232    utility is supported, which is available
1233    on OpenSolaris, Solaris 10, and Mac OS X Leopard. It is expected that
1234    DTrace will be available in the future on FreeBSD and possibly other
1235    operating systems.  The
1236    <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> project
1237    for Linux also provides a DTrace equivalent.  Supporting other dynamic
1238    tracing utilities is theoretically possible by changing the definitions for
1239    the macros in <filename>src/include/utils/probes.h</>.
1240   </para>
1241
1242   <sect2 id="compiling-for-trace">
1243    <title>Compiling for Dynamic Tracing</title>
1244
1245   <para>
1246    By default, probes are not available, so you will need to
1247    explicitly tell the configure script to make the probes available
1248    in <productname>PostgreSQL</productname>. To include DTrace support
1249    specify <option>--enable-dtrace</> to configure.  See <xref
1250    linkend="install-procedure"> for further information.
1251   </para>
1252   </sect2>
1253
1254   <sect2 id="trace-points">
1255    <title>Built-in Probes</title>
1256
1257   <para>
1258    A number of standard probes are provided in the source code,
1259    as shown in <xref linkend="dtrace-probe-point-table">.
1260    More can certainly be added to enhance <productname>PostgreSQL</>'s
1261    observability.
1262   </para>
1263
1264  <table id="dtrace-probe-point-table">
1265   <title>Built-in DTrace Probes</title>
1266   <tgroup cols="3">
1267    <thead>
1268     <row>
1269      <entry>Name</entry>
1270      <entry>Parameters</entry>
1271      <entry>Description</entry>
1272     </row>
1273    </thead>
1274
1275    <tbody>
1276
1277     <row>
1278      <entry>transaction-start</entry>
1279      <entry>(LocalTransactionId)</entry>
1280      <entry>Probe that fires at the start of a new transaction.
1281       arg0 is the transaction ID.</entry>
1282     </row>
1283     <row>
1284      <entry>transaction-commit</entry>
1285      <entry>(LocalTransactionId)</entry>
1286      <entry>Probe that fires when a transaction completes successfully.
1287       arg0 is the transaction ID.</entry>
1288     </row>
1289     <row>
1290      <entry>transaction-abort</entry>
1291      <entry>(LocalTransactionId)</entry>
1292      <entry>Probe that fires when a transaction completes unsuccessfully.
1293       arg0 is the transaction ID.</entry>
1294     </row>
1295     <row>
1296      <entry>query-start</entry>
1297      <entry>(const char *)</entry>
1298      <entry>Probe that fires when the processing of a query is started.
1299       arg0 is the query string.</entry>
1300     </row>
1301     <row>
1302      <entry>query-done</entry>
1303      <entry>(const char *)</entry>
1304      <entry>Probe that fires when the processing of a query is complete.
1305       arg0 is the query string.</entry>
1306     </row>
1307     <row>
1308      <entry>query-parse-start</entry>
1309      <entry>(const char *)</entry>
1310      <entry>Probe that fires when the parsing of a query is started.
1311       arg0 is the query string.</entry>
1312     </row>
1313     <row>
1314      <entry>query-parse-done</entry>
1315      <entry>(const char *)</entry>
1316      <entry>Probe that fires when the parsing of a query is complete.
1317       arg0 is the query string.</entry>
1318     </row>
1319     <row>
1320      <entry>query-rewrite-start</entry>
1321      <entry>(const char *)</entry>
1322      <entry>Probe that fires when the rewriting of a query is started.
1323       arg0 is the query string.</entry>
1324     </row>
1325     <row>
1326      <entry>query-rewrite-done</entry>
1327      <entry>(const char *)</entry>
1328      <entry>Probe that fires when the rewriting of a query is complete.
1329       arg0 is the query string.</entry>
1330     </row>
1331     <row>
1332      <entry>query-plan-start</entry>
1333      <entry>()</entry>
1334      <entry>Probe that fires when the planning of a query is started.</entry>
1335     </row>
1336     <row>
1337      <entry>query-plan-done</entry>
1338      <entry>()</entry>
1339      <entry>Probe that fires when the planning of a query is complete.</entry>
1340     </row>
1341     <row>
1342      <entry>query-execute-start</entry>
1343      <entry>()</entry>
1344      <entry>Probe that fires when the execution of a query is started.</entry>
1345     </row>
1346     <row>
1347      <entry>query-execute-done</entry>
1348      <entry>()</entry>
1349      <entry>Probe that fires when the execution of a query is complete.</entry>
1350     </row>
1351     <row>
1352      <entry>statement-status</entry>
1353      <entry>(const char *)</entry>
1354      <entry>Probe that fires anytime the server process updates its
1355       <structname>pg_stat_activity</>.<structfield>current_query</> status.
1356       arg0 is the new status string.</entry>
1357     </row>
1358     <row>
1359      <entry>checkpoint-start</entry>
1360      <entry>(int)</entry>
1361      <entry>Probe that fires when a checkpoint is started.
1362       arg0 holds the bitwise flags used to distinguish different checkpoint
1363       types, such as shutdown, immediate or force.</entry>
1364     </row>
1365     <row>
1366      <entry>checkpoint-done</entry>
1367      <entry>(int, int, int, int, int)</entry>
1368      <entry>Probe that fires when a checkpoint is complete.
1369       (The probes listed next fire in sequence during checkpoint processing.)
1370       arg0 is the number of buffers written. arg1 is the total number of
1371       buffers. arg2, arg3 and arg4 contain the number of xlog file(s) added,
1372       removed and recycled respectively.</entry>
1373     </row>
1374     <row>
1375      <entry>clog-checkpoint-start</entry>
1376      <entry>(bool)</entry>
1377      <entry>Probe that fires when the CLOG portion of a checkpoint is started.
1378       arg0 is true for normal checkpoint, false for shutdown
1379       checkpoint.</entry>
1380     </row>
1381     <row>
1382      <entry>clog-checkpoint-done</entry>
1383      <entry>(bool)</entry>
1384      <entry>Probe that fires when the CLOG portion of a checkpoint is
1385       complete. arg0 has the same meaning as for clog-checkpoint-start.</entry>
1386     </row>
1387     <row>
1388      <entry>subtrans-checkpoint-start</entry>
1389      <entry>(bool)</entry>
1390      <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
1391       started.
1392       arg0 is true for normal checkpoint, false for shutdown
1393       checkpoint.</entry>
1394     </row>
1395     <row>
1396      <entry>subtrans-checkpoint-done</entry>
1397      <entry>(bool)</entry>
1398      <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
1399       complete. arg0 has the same meaning as for
1400       subtrans-checkpoint-start.</entry>
1401     </row>
1402     <row>
1403      <entry>multixact-checkpoint-start</entry>
1404      <entry>(bool)</entry>
1405      <entry>Probe that fires when the MultiXact portion of a checkpoint is
1406       started.
1407       arg0 is true for normal checkpoint, false for shutdown
1408       checkpoint.</entry>
1409     </row>
1410     <row>
1411      <entry>multixact-checkpoint-done</entry>
1412      <entry>(bool)</entry>
1413      <entry>Probe that fires when the MultiXact portion of a checkpoint is
1414       complete. arg0 has the same meaning as for
1415       multixact-checkpoint-start.</entry>
1416     </row>
1417     <row>
1418      <entry>buffer-checkpoint-start</entry>
1419      <entry>(int)</entry>
1420      <entry>Probe that fires when the buffer-writing portion of a checkpoint
1421       is started.
1422       arg0 holds the bitwise flags used to distinguish different checkpoint
1423       types, such as shutdown, immediate or force.</entry>
1424     </row>
1425     <row>
1426      <entry>buffer-sync-start</entry>
1427      <entry>(int, int)</entry>
1428      <entry>Probe that fires when we begin to write dirty buffers during
1429       checkpoint (after identifying which buffers must be written).
1430       arg0 is the total number of buffers.
1431       arg1 is the number that are currently dirty and need to be written.</entry>
1432     </row>
1433     <row>
1434      <entry>buffer-sync-written</entry>
1435      <entry>(int)</entry>
1436      <entry>Probe that fires after each buffer is written during checkpoint.
1437       arg0 is the ID number of the buffer.</entry>
1438     </row>
1439     <row>
1440      <entry>buffer-sync-done</entry>
1441      <entry>(int, int, int)</entry>
1442      <entry>Probe that fires when all dirty buffers have been written.
1443       arg0 is the total number of buffers.
1444       arg1 is the number of buffers actually written by the checkpoint process.
1445       arg2 is the number that were expected to be written (arg1 of
1446       buffer-sync-start); any difference reflects other processes flushing
1447       buffers during the checkpoint.</entry>
1448     </row>
1449     <row>
1450      <entry>buffer-checkpoint-sync-start</entry>
1451      <entry>()</entry>
1452      <entry>Probe that fires after dirty buffers have been written to the
1453       kernel, and before starting to issue fsync requests.</entry>
1454     </row>
1455     <row>
1456      <entry>buffer-checkpoint-done</entry>
1457      <entry>()</entry>
1458      <entry>Probe that fires when syncing of buffers to disk is
1459       complete.</entry>
1460     </row>
1461     <row>
1462      <entry>twophase-checkpoint-start</entry>
1463      <entry>()</entry>
1464      <entry>Probe that fires when the two-phase portion of a checkpoint is
1465       started.</entry>
1466     </row>
1467     <row>
1468      <entry>twophase-checkpoint-done</entry>
1469      <entry>()</entry>
1470      <entry>Probe that fires when the two-phase portion of a checkpoint is
1471       complete.</entry>
1472     </row>
1473     <row>
1474      <entry>buffer-read-start</entry>
1475      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</entry>
1476      <entry>Probe that fires when a buffer read is started.
1477       arg0 and arg1 contain the fork and block numbers of the page (but
1478       arg1 will be -1 if this is a relation extension request).
1479       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1480       identifying the relation.
1481       arg5 is the ID of the backend which created the temporary relation for a
1482       local buffer, or InvalidBackendId (-1) for a shared buffer.
1483       arg6 is true for a relation extension request, false for normal
1484       read.</entry>
1485     </row>
1486     <row>
1487      <entry>buffer-read-done</entry>
1488      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool, bool)</entry>
1489      <entry>Probe that fires when a buffer read is complete.
1490       arg0 and arg1 contain the fork and block numbers of the page (if this
1491       is a relation extension request, arg1 now contains the block number
1492       of the newly added block).
1493       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1494       identifying the relation.
1495       arg5 is the ID of the backend which created the temporary relation for a
1496       local buffer, or InvalidBackendId (-1) for a shared buffer.
1497       arg6 is true for a relation extension request, false for normal
1498       read.
1499       arg7 is true if the buffer was found in the pool, false if not.</entry>
1500     </row>
1501     <row>
1502      <entry>buffer-flush-start</entry>
1503      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1504      <entry>Probe that fires before issuing any write request for a shared
1505       buffer.
1506       arg0 and arg1 contain the fork and block numbers of the page.
1507       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1508       identifying the relation.</entry>
1509     </row>
1510     <row>
1511      <entry>buffer-flush-done</entry>
1512      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1513      <entry>Probe that fires when a write request is complete.  (Note
1514       that this just reflects the time to pass the data to the kernel;
1515       it's typically not actually been written to disk yet.)
1516       The arguments are the same as for buffer-flush-start.</entry>
1517     </row>
1518     <row>
1519      <entry>buffer-write-dirty-start</entry>
1520      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1521      <entry>Probe that fires when a server process begins to write a dirty
1522       buffer.  (If this happens often, it implies that
1523       <xref linkend="guc-shared-buffers"> is too
1524       small or the bgwriter control parameters need adjustment.)
1525       arg0 and arg1 contain the fork and block numbers of the page.
1526       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1527       identifying the relation.</entry>
1528     </row>
1529     <row>
1530      <entry>buffer-write-dirty-done</entry>
1531      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1532      <entry>Probe that fires when a dirty-buffer write is complete.
1533       The arguments are the same as for buffer-write-dirty-start.</entry>
1534     </row>
1535     <row>
1536      <entry>wal-buffer-write-dirty-start</entry>
1537      <entry>()</entry>
1538      <entry>Probe that fires when when a server process begins to write a
1539       dirty WAL buffer because no more WAL buffer space is available.
1540       (If this happens often, it implies that
1541       <xref linkend="guc-wal-buffers"> is too small.)</entry>
1542     </row>
1543     <row>
1544      <entry>wal-buffer-write-dirty-done</entry>
1545      <entry>()</entry>
1546      <entry>Probe that fires when a dirty WAL buffer write is complete.</entry>
1547     </row>
1548     <row>
1549      <entry>xlog-insert</entry>
1550      <entry>(unsigned char, unsigned char)</entry>
1551      <entry>Probe that fires when a WAL record is inserted.
1552       arg0 is the resource manager (rmid) for the record.
1553       arg1 contains the info flags.</entry>
1554     </row>
1555     <row>
1556      <entry>xlog-switch</entry>
1557      <entry>()</entry>
1558      <entry>Probe that fires when a WAL segment switch is requested.</entry>
1559     </row>
1560     <row>
1561      <entry>smgr-md-read-start</entry>
1562      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
1563      <entry>Probe that fires when beginning to read a block from a relation.
1564       arg0 and arg1 contain the fork and block numbers of the page.
1565       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1566       identifying the relation.
1567       arg5 is the ID of the backend which created the temporary relation for a
1568       local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
1569     </row>
1570     <row>
1571      <entry>smgr-md-read-done</entry>
1572      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
1573      <entry>Probe that fires when a block read is complete.
1574       arg0 and arg1 contain the fork and block numbers of the page.
1575       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1576       identifying the relation.
1577       arg5 is the ID of the backend which created the temporary relation for a
1578       local buffer, or InvalidBackendId (-1) for a shared buffer.
1579       arg6 is the number of bytes actually read, while arg7 is the number
1580       requested (if these are different it indicates trouble).</entry>
1581     </row>
1582     <row>
1583      <entry>smgr-md-write-start</entry>
1584      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
1585      <entry>Probe that fires when beginning to write a block to a relation.
1586       arg0 and arg1 contain the fork and block numbers of the page.
1587       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1588       identifying the relation.
1589       arg5 is the ID of the backend which created the temporary relation for a
1590       local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
1591     </row>
1592     <row>
1593      <entry>smgr-md-write-done</entry>
1594      <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
1595      <entry>Probe that fires when a block write is complete.
1596       arg0 and arg1 contain the fork and block numbers of the page.
1597       arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1598       identifying the relation.
1599       arg5 is the ID of the backend which created the temporary relation for a
1600       local buffer, or InvalidBackendId (-1) for a shared buffer.
1601       arg6 is the number of bytes actually written, while arg7 is the number
1602       requested (if these are different it indicates trouble).</entry>
1603     </row>
1604     <row>
1605      <entry>sort-start</entry>
1606      <entry>(int, bool, int, int, bool)</entry>
1607      <entry>Probe that fires when a sort operation is started.
1608       arg0 indicates heap, index or datum sort.
1609       arg1 is true for unique-value enforcement.
1610       arg2 is the number of key columns.
1611       arg3 is the number of kilobytes of work memory allowed.
1612       arg4 is true if random access to the sort result is required.</entry>
1613     </row>
1614     <row>
1615      <entry>sort-done</entry>
1616      <entry>(bool, long)</entry>
1617      <entry>Probe that fires when a sort is complete.
1618       arg0 is true for external sort, false for internal sort.
1619       arg1 is the number of disk blocks used for an external sort,
1620       or kilobytes of memory used for an internal sort.</entry>
1621     </row>
1622     <row>
1623      <entry>lwlock-acquire</entry>
1624      <entry>(LWLockId, LWLockMode)</entry>
1625      <entry>Probe that fires when an LWLock has been acquired.
1626       arg0 is the LWLock's ID.
1627       arg1 is the requested lock mode, either exclusive or shared.</entry>
1628     </row>
1629     <row>
1630      <entry>lwlock-release</entry>
1631      <entry>(LWLockId)</entry>
1632      <entry>Probe that fires when an LWLock has been released (but note
1633       that any released waiters have not yet been awakened).
1634       arg0 is the LWLock's ID.</entry>
1635     </row>
1636     <row>
1637      <entry>lwlock-wait-start</entry>
1638      <entry>(LWLockId, LWLockMode)</entry>
1639      <entry>Probe that fires when an LWLock was not immediately available and
1640       a server process has begun to wait for the lock to become available.
1641       arg0 is the LWLock's ID.
1642       arg1 is the requested lock mode, either exclusive or shared.</entry>
1643     </row>
1644     <row>
1645      <entry>lwlock-wait-done</entry>
1646      <entry>(LWLockId, LWLockMode)</entry>
1647      <entry>Probe that fires when a server process has been released from its
1648       wait for an LWLock (it does not actually have the lock yet).
1649       arg0 is the LWLock's ID.
1650       arg1 is the requested lock mode, either exclusive or shared.</entry>
1651     </row>
1652     <row>
1653      <entry>lwlock-condacquire</entry>
1654      <entry>(LWLockId, LWLockMode)</entry>
1655      <entry>Probe that fires when an LWLock was successfully acquired when the
1656       caller specified no waiting.
1657       arg0 is the LWLock's ID.
1658       arg1 is the requested lock mode, either exclusive or shared.</entry>
1659     </row>
1660     <row>
1661      <entry>lwlock-condacquire-fail</entry>
1662      <entry>(LWLockId, LWLockMode)</entry>
1663      <entry>Probe that fires when an LWLock was not successfully acquired when
1664       the caller specified no waiting.
1665       arg0 is the LWLock's ID.
1666       arg1 is the requested lock mode, either exclusive or shared.</entry>
1667     </row>
1668     <row>
1669      <entry>lock-wait-start</entry>
1670      <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
1671      <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
1672       has begun to wait because the lock is not available.
1673       arg0 through arg3 are the tag fields identifying the object being
1674       locked.  arg4 indicates the type of object being locked.
1675       arg5 indicates the lock type being requested.</entry>
1676     </row>
1677     <row>
1678      <entry>lock-wait-done</entry>
1679      <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
1680      <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
1681       has finished waiting (i.e., has acquired the lock).
1682       The arguments are the same as for lock-wait-start.</entry>
1683     </row>
1684     <row>
1685      <entry>deadlock-found</entry>
1686      <entry>()</entry>
1687      <entry>Probe that fires when a deadlock is found by the deadlock
1688       detector.</entry>
1689     </row>
1690
1691    </tbody>
1692    </tgroup>
1693   </table>
1694
1695  <table id="typedefs-table">
1696   <title>Defined Types Used in Probe Parameters</title>
1697   <tgroup cols="2">
1698    <thead>
1699     <row>
1700      <entry>Type</entry>
1701      <entry>Definition</entry>
1702     </row>
1703    </thead>
1704
1705    <tbody>
1706
1707     <row>
1708      <entry>LocalTransactionId</entry>
1709      <entry>unsigned int</entry>
1710     </row>
1711     <row>
1712      <entry>LWLockId</entry>
1713      <entry>int</entry>
1714     </row>
1715     <row>
1716      <entry>LWLockMode</entry>
1717      <entry>int</entry>
1718     </row>
1719     <row>
1720      <entry>LOCKMODE</entry>
1721      <entry>int</entry>
1722     </row>
1723     <row>
1724      <entry>BlockNumber</entry>
1725      <entry>unsigned int</entry>
1726     </row>
1727     <row>
1728      <entry>Oid</entry>
1729      <entry>unsigned int</entry>
1730     </row>
1731     <row>
1732      <entry>ForkNumber</entry>
1733      <entry>int</entry>
1734     </row>
1735     <row>
1736      <entry>bool</entry>
1737      <entry>char</entry>
1738     </row>
1739
1740    </tbody>
1741    </tgroup>
1742   </table>
1743
1744
1745   </sect2>
1746
1747   <sect2 id="using-trace-points">
1748    <title>Using Probes</title>
1749
1750   <para>
1751    The example below shows a DTrace script for analyzing transaction
1752    counts in the system, as an alternative to snapshotting
1753    <structname>pg_stat_database</> before and after a performance test:
1754 <programlisting>
1755 #!/usr/sbin/dtrace -qs
1756
1757 postgresql$1:::transaction-start
1758 {
1759       @start["Start"] = count();
1760       self->ts  = timestamp;
1761 }
1762
1763 postgresql$1:::transaction-abort
1764 {
1765       @abort["Abort"] = count();
1766 }
1767
1768 postgresql$1:::transaction-commit
1769 /self->ts/
1770 {
1771       @commit["Commit"] = count();
1772       @time["Total time (ns)"] = sum(timestamp - self->ts);
1773       self->ts=0;
1774 }
1775 </programlisting>
1776    When executed, the example D script gives output such as:
1777 <screen>
1778 # ./txn_count.d `pgrep -n postgres` or ./txn_count.d &lt;PID&gt;
1779 ^C
1780
1781 Start                                          71
1782 Commit                                         70
1783 Total time (ns)                        2312105013
1784 </screen>
1785   </para>
1786
1787   <note>
1788    <para>
1789     SystemTap uses a different notation for trace scripts than DTrace does,
1790     even though the underlying trace points are compatible.  One point worth
1791     noting is that at this writing, SystemTap scripts must reference probe
1792     names using double underscores in place of hyphens.  This is expected to
1793     be fixed in future SystemTap releases.
1794    </para>
1795   </note>
1796
1797   <para>
1798    You should remember that DTrace scripts need to be carefully written and
1799    debugged, otherwise the trace information collected might
1800    be meaningless. In most cases where problems are found it is the
1801    instrumentation that is at fault, not the underlying system. When
1802    discussing information found using dynamic tracing, be sure to enclose
1803    the script used to allow that too to be checked and discussed.
1804   </para>
1805
1806   <para>
1807    More example scripts can be found in the PgFoundry
1808    <ulink url="http://pgfoundry.org/projects/dtrace/">dtrace project</ulink>.
1809   </para>
1810   </sect2>
1811
1812   <sect2 id="defining-trace-points">
1813    <title>Defining New Probes</title>
1814
1815   <para>
1816    New probes can be defined within the code wherever the developer
1817    desires, though this will require a recompilation. Below are the steps
1818    for inserting new probes:
1819   </para>
1820
1821   <procedure>
1822    <step>
1823     <para>
1824      Decide on probe names and data to be made available through the probes
1825     </para>
1826    </step>
1827
1828    <step>
1829     <para>
1830      Add the probe definitions to <filename>src/backend/utils/probes.d</>
1831     </para>
1832    </step>
1833
1834    <step>
1835     <para>
1836      Include <filename>pg_trace.h</> if it is not already present in the
1837      module(s) containing the probe points, and insert
1838      <literal>TRACE_POSTGRESQL</> probe macros at the desired locations
1839      in the source code
1840     </para>
1841    </step>
1842
1843    <step>
1844     <para>
1845      Recompile and verify that the new probes are available
1846     </para>
1847    </step>
1848   </procedure>
1849
1850   <formalpara>
1851    <title>Example:</title>
1852    <para>
1853     Here is an example of how you would add a probe to trace all new
1854     transactions by transaction ID.
1855    </para>
1856   </formalpara>
1857
1858   <procedure>
1859    <step>
1860     <para>
1861      Decide that the probe will be named <literal>transaction-start</> and
1862      requires a parameter of type LocalTransactionId
1863     </para>
1864    </step>
1865
1866    <step>
1867     <para>
1868      Add the probe definition to <filename>src/backend/utils/probes.d</>:
1869 <programlisting>
1870 probe transaction__start(LocalTransactionId);
1871 </programlisting>
1872      Note the use of the double underline in the probe name. In a DTrace
1873      script using the probe, the double underline needs to be replaced with a
1874      hyphen, so <literal>transaction-start</> is the name to document for
1875      users.
1876     </para>
1877    </step>
1878
1879    <step>
1880     <para>
1881      At compile time, <literal>transaction__start</> is converted to a macro
1882      called <literal>TRACE_POSTGRESQL_TRANSACTION_START</> (notice the
1883      underscores are single here), which is available by including
1884      <filename>pg_trace.h</>.  Add the macro call to the appropriate location
1885      in the source code.  In this case, it looks like the following:
1886
1887 <programlisting>
1888 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1889 </programlisting>
1890     </para>
1891    </step>
1892
1893    <step>
1894     <para>
1895      After recompiling and running the new binary, check that your newly added
1896      probe is available by executing the following DTrace command.  You
1897      should see similar output:
1898 <screen>
1899 # dtrace -ln transaction-start
1900    ID    PROVIDER          MODULE           FUNCTION NAME
1901 18705 postgresql49878     postgres     StartTransactionCommand transaction-start
1902 18755 postgresql49877     postgres     StartTransactionCommand transaction-start
1903 18805 postgresql49876     postgres     StartTransactionCommand transaction-start
1904 18855 postgresql49875     postgres     StartTransactionCommand transaction-start
1905 18986 postgresql49873     postgres     StartTransactionCommand transaction-start
1906 </screen>
1907     </para>
1908    </step>
1909   </procedure>
1910
1911   <para>
1912    There are a few things to be careful about when adding trace macros
1913    to the C code:
1914
1915    <itemizedlist>
1916     <listitem>
1917      <para>
1918       You should take care that the data types specified for a probe's
1919       parameters match the data types of the variables used in the macro.
1920       Otherwise, you will get compilation errors.
1921      </para>
1922     </listitem>
1923
1924
1925     <listitem>
1926      <para>
1927       On most platforms, if <productname>PostgreSQL</productname> is
1928       built with <option>--enable-dtrace</>, the arguments to a trace
1929       macro will be evaluated whenever control passes through the
1930       macro, <emphasis>even if no tracing is being done</>.  This is
1931       usually not worth worrying about if you are just reporting the
1932       values of a few local variables.  But beware of putting expensive
1933       function calls into the arguments.  If you need to do that,
1934       consider protecting the macro with a check to see if the trace
1935       is actually enabled:
1936
1937 <programlisting>
1938 if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
1939     TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
1940 </programlisting>
1941
1942       Each trace macro has a corresponding <literal>ENABLED</> macro.
1943      </para>
1944     </listitem>
1945    </itemizedlist>
1946
1947   </para>
1948
1949   </sect2>
1950
1951  </sect1>
1952
1953 </chapter>