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