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