1 <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.82 2010/08/08 16:27:03 tgl Exp $ -->
3 <chapter id="monitoring">
4 <title>Monitoring Database Activity</title>
6 <indexterm zone="monitoring">
7 <primary>monitoring</primary>
8 <secondary>database activity</secondary>
11 <indexterm zone="monitoring">
12 <primary>database activity</primary>
13 <secondary>monitoring</secondary>
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.
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
36 <sect1 id="monitoring-ps">
37 <title>Standard Unix Tools</Title>
39 <indexterm zone="monitoring-ps">
41 <secondary>to monitor activity</secondary>
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
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
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
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
72 postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <replaceable>activity</>
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.
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.
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</>
108 <sect1 id="monitoring-stats">
109 <title>The Statistics Collector</Title>
111 <indexterm zone="monitoring-stats">
112 <primary>statistics</primary>
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.
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.
131 <sect2 id="monitoring-stats-setup">
132 <title>Statistics Collection Configuration</Title>
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.)
143 The parameter <xref linkend="guc-track-counts"> controls whether
144 statistics are collected about table and index accesses.
148 The parameter <xref linkend="guc-track-functions"> enables tracking of
149 usage of user-defined functions.
153 The parameter <xref linkend="guc-track-activities"> enables monitoring
154 of the current command being executed by any server process.
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
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.
179 <sect2 id="monitoring-stats-views">
180 <title>Viewing Collected Statistics</Title>
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.
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
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
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.
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.
232 <table id="monitoring-stats-views-table">
233 <title>Standard Statistics Views</title>
238 <entry>View Name</entry>
239 <entry>Description</entry>
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
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
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.
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.
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>
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>
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>
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>
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>
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.
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>
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>
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.
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>
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>
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.
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>
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>
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.
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>
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>
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.
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>
441 The per-index statistics are particularly useful to determine which
442 indexes are being used and how effective they are.
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
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</>.
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.
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.
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
503 <table id="monitoring-stats-funcs-table">
504 <title>Statistics Access Functions</title>
509 <entry>Function</entry>
510 <entry>Return Type</entry>
511 <entry>Description</entry>
517 <entry><literal><function>pg_stat_get_db_numbackends</function>(<type>oid</type>)</literal></entry>
518 <entry><type>integer</type></entry>
520 Number of active server processes for database
525 <entry><literal><function>pg_stat_get_db_xact_commit</function>(<type>oid</type>)</literal></entry>
526 <entry><type>bigint</type></entry>
528 Number of transactions committed in database
533 <entry><literal><function>pg_stat_get_db_xact_rollback</function>(<type>oid</type>)</literal></entry>
534 <entry><type>bigint</type></entry>
536 Number of transactions rolled back in database
541 <entry><literal><function>pg_stat_get_db_blocks_fetched</function>(<type>oid</type>)</literal></entry>
542 <entry><type>bigint</type></entry>
544 Number of disk block fetch requests for database
549 <entry><literal><function>pg_stat_get_db_blocks_hit</function>(<type>oid</type>)</literal></entry>
550 <entry><type>bigint</type></entry>
552 Number of disk block fetch requests found in cache for database
557 <entry><literal><function>pg_stat_get_db_tuples_returned</function>(<type>oid</type>)</literal></entry>
558 <entry><type>bigint</type></entry>
560 Number of tuples returned for database
565 <entry><literal><function>pg_stat_get_db_tuples_fetched</function>(<type>oid</type>)</literal></entry>
566 <entry><type>bigint</type></entry>
568 Number of tuples fetched for database
573 <entry><literal><function>pg_stat_get_db_tuples_inserted</function>(<type>oid</type>)</literal></entry>
574 <entry><type>bigint</type></entry>
576 Number of tuples inserted in database
581 <entry><literal><function>pg_stat_get_db_tuples_updated</function>(<type>oid</type>)</literal></entry>
582 <entry><type>bigint</type></entry>
584 Number of tuples updated in database
589 <entry><literal><function>pg_stat_get_db_tuples_deleted</function>(<type>oid</type>)</literal></entry>
590 <entry><type>bigint</type></entry>
592 Number of tuples deleted in database
597 <entry><literal><function>pg_stat_get_numscans</function>(<type>oid</type>)</literal></entry>
598 <entry><type>bigint</type></entry>
600 Number of sequential scans done when argument is a table,
601 or number of index scans done when argument is an index
606 <entry><literal><function>pg_stat_get_tuples_returned</function>(<type>oid</type>)</literal></entry>
607 <entry><type>bigint</type></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
615 <entry><literal><function>pg_stat_get_tuples_fetched</function>(<type>oid</type>)</literal></entry>
616 <entry><type>bigint</type></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
625 <entry><literal><function>pg_stat_get_tuples_inserted</function>(<type>oid</type>)</literal></entry>
626 <entry><type>bigint</type></entry>
628 Number of rows inserted into table
633 <entry><literal><function>pg_stat_get_tuples_updated</function>(<type>oid</type>)</literal></entry>
634 <entry><type>bigint</type></entry>
636 Number of rows updated in table (includes HOT updates)
641 <entry><literal><function>pg_stat_get_tuples_deleted</function>(<type>oid</type>)</literal></entry>
642 <entry><type>bigint</type></entry>
644 Number of rows deleted from table
649 <entry><literal><function>pg_stat_get_tuples_hot_updated</function>(<type>oid</type>)</literal></entry>
650 <entry><type>bigint</type></entry>
652 Number of rows HOT-updated in table
657 <entry><literal><function>pg_stat_get_live_tuples</function>(<type>oid</type>)</literal></entry>
658 <entry><type>bigint</type></entry>
660 Number of live rows in table
665 <entry><literal><function>pg_stat_get_dead_tuples</function>(<type>oid</type>)</literal></entry>
666 <entry><type>bigint</type></entry>
668 Number of dead rows in table
673 <entry><literal><function>pg_stat_get_blocks_fetched</function>(<type>oid</type>)</literal></entry>
674 <entry><type>bigint</type></entry>
676 Number of disk block fetch requests for table or index
681 <entry><literal><function>pg_stat_get_blocks_hit</function>(<type>oid</type>)</literal></entry>
682 <entry><type>bigint</type></entry>
684 Number of disk block requests found in cache for table or index
689 <entry><literal><function>pg_stat_get_last_vacuum_time</function>(<type>oid</type>)</literal></entry>
690 <entry><type>timestamptz</type></entry>
692 Time of the last vacuum initiated by the user on this table
697 <entry><literal><function>pg_stat_get_last_autovacuum_time</function>(<type>oid</type>)</literal></entry>
698 <entry><type>timestamptz</type></entry>
700 Time of the last vacuum initiated by the autovacuum daemon on this table
705 <entry><literal><function>pg_stat_get_last_analyze_time</function>(<type>oid</type>)</literal></entry>
706 <entry><type>timestamptz</type></entry>
708 Time of the last analyze initiated by the user on this table
713 <entry><literal><function>pg_stat_get_last_autoanalyze_time</function>(<type>oid</type>)</literal></entry>
714 <entry><type>timestamptz</type></entry>
716 Time of the last analyze initiated by the autovacuum daemon on this
722 <entry><literal><function>pg_stat_get_xact_numscans</function>(<type>oid</type>)</literal></entry>
723 <entry><type>bigint</type></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
731 <entry><literal><function>pg_stat_get_xact_tuples_returned</function>(<type>oid</type>)</literal></entry>
732 <entry><type>bigint</type></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
740 <entry><literal><function>pg_stat_get_xact_tuples_fetched</function>(<type>oid</type>)</literal></entry>
741 <entry><type>bigint</type></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
750 <entry><literal><function>pg_stat_get_xact_tuples_inserted</function>(<type>oid</type>)</literal></entry>
751 <entry><type>bigint</type></entry>
753 Number of rows inserted into table, in the current transaction
758 <entry><literal><function>pg_stat_get_xact_tuples_updated</function>(<type>oid</type>)</literal></entry>
759 <entry><type>bigint</type></entry>
761 Number of rows updated in table (includes HOT updates), in the current transaction
766 <entry><literal><function>pg_stat_get_xact_tuples_deleted</function>(<type>oid</type>)</literal></entry>
767 <entry><type>bigint</type></entry>
769 Number of rows deleted from table, in the current transaction
774 <entry><literal><function>pg_stat_get_xact_tuples_hot_updated</function>(<type>oid</type>)</literal></entry>
775 <entry><type>bigint</type></entry>
777 Number of rows HOT-updated in table, in the current transaction
782 <entry><literal><function>pg_stat_get_xact_blocks_fetched</function>(<type>oid</type>)</literal></entry>
783 <entry><type>bigint</type></entry>
785 Number of disk block fetch requests for table or index, in the current transaction
790 <entry><literal><function>pg_stat_get_xact_blocks_hit</function>(<type>oid</type>)</literal></entry>
791 <entry><type>bigint</type></entry>
793 Number of disk block requests found in cache for table or index, in the current transaction
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>
802 Process ID of the server process attached to the current session
807 <entry><literal><function>pg_stat_get_activity</function>(<type>integer</type>)</literal></entry>
808 <entry><type>setof record</type></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.
818 <entry><literal><function>pg_stat_get_function_calls</function>(<type>oid</type>)</literal></entry>
819 <entry><type>bigint</type></entry>
821 Number of times the function has been called
826 <entry><literal><function>pg_stat_get_function_time</function>(<type>oid</type>)</literal></entry>
827 <entry><type>bigint</type></entry>
829 Total wall clock time spent in the function, in microseconds. Includes
830 the time spent in functions called by this one.
835 <entry><literal><function>pg_stat_get_function_self_time</function>(<type>oid</type>)</literal></entry>
836 <entry><type>bigint</type></entry>
838 Time spent in only this function. Time spent in called functions
844 <entry><literal><function>pg_stat_get_xact_function_calls</function>(<type>oid</type>)</literal></entry>
845 <entry><type>bigint</type></entry>
847 Number of times the function has been called, in the current transaction.
852 <entry><literal><function>pg_stat_get_xact_function_time</function>(<type>oid</type>)</literal></entry>
853 <entry><type>bigint</type></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
862 <entry><literal><function>pg_stat_get_xact_function_self_time</function>(<type>oid</type>)</literal></entry>
863 <entry><type>bigint</type></entry>
865 Time spent in only this function, in the current transaction. Time
866 spent in called functions is excluded.
871 <entry><literal><function>pg_stat_get_backend_idset</function>()</literal></entry>
872 <entry><type>setof integer</type></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.
880 <entry><literal><function>pg_stat_get_backend_pid</function>(<type>integer</type>)</literal></entry>
881 <entry><type>integer</type></entry>
883 Process ID of the given server process
888 <entry><literal><function>pg_stat_get_backend_dbid</function>(<type>integer</type>)</literal></entry>
889 <entry><type>oid</type></entry>
891 Database ID of the given server process
896 <entry><literal><function>pg_stat_get_backend_userid</function>(<type>integer</type>)</literal></entry>
897 <entry><type>oid</type></entry>
899 User ID of the given server process
904 <entry><literal><function>pg_stat_get_backend_activity</function>(<type>integer</type>)</literal></entry>
905 <entry><type>text</type></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)
915 <entry><literal><function>pg_stat_get_backend_waiting</function>(<type>integer</type>)</literal></entry>
916 <entry><type>boolean</type></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)
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>
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)
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>
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)
950 <entry><literal><function>pg_stat_get_backend_start</function>(<type>integer</type>)</literal></entry>
951 <entry><type>timestamp with time zone</type></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
960 <entry><literal><function>pg_stat_get_backend_client_addr</function>(<type>integer</type>)</literal></entry>
961 <entry><type>inet</type></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
971 <entry><literal><function>pg_stat_get_backend_client_port</function>(<type>integer</type>)</literal></entry>
972 <entry><type>integer</type></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
982 <entry><literal><function>pg_stat_get_bgwriter_timed_checkpoints</function>()</literal></entry>
983 <entry><type>bigint</type></entry>
985 Number of times the background writer has started timed checkpoints
986 (because the <varname>checkpoint_timeout</varname> time has expired)
991 <entry><literal><function>pg_stat_get_bgwriter_requested_checkpoints</function>()</literal></entry>
992 <entry><type>bigint</type></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
1002 <entry><literal><function>pg_stat_get_bgwriter_buf_written_checkpoints</function>()</literal></entry>
1003 <entry><type>bigint</type></entry>
1005 Number of buffers written by the background writer during checkpoints
1010 <entry><literal><function>pg_stat_get_bgwriter_buf_written_clean</function>()</literal></entry>
1011 <entry><type>bigint</type></entry>
1013 Number of buffers written by the background writer for routine cleaning of
1019 <entry><literal><function>pg_stat_get_bgwriter_maxwritten_clean</function>()</literal></entry>
1020 <entry><type>bigint</type></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
1029 <entry><literal><function>pg_stat_get_buf_written_backend</function>()</literal></entry>
1030 <entry><type>bigint</type></entry>
1032 Number of buffers written by backends because they needed
1033 to allocate a new buffer
1038 <entry><literal><function>pg_stat_get_buf_alloc</function>()</literal></entry>
1039 <entry><type>bigint</type></entry>
1041 Total number of buffer allocations
1046 <entry><literal><function>pg_stat_clear_snapshot</function>()</literal></entry>
1047 <entry><type>void</type></entry>
1049 Discard the current statistics snapshot
1054 <entry><literal><function>pg_stat_reset</function>()</literal></entry>
1055 <entry><type>void</type></entry>
1057 Reset all statistics counters for the current database to zero
1058 (requires superuser privileges)
1063 <entry><literal><function>pg_stat_reset_shared</function>(text)</literal></entry>
1064 <entry><type>void</type></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</>.
1074 <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal></entry>
1075 <entry><type>void</type></entry>
1077 Reset statistics for a single table or index in the current database to
1078 zero (requires superuser privileges)
1083 <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal></entry>
1084 <entry><type>void</type></entry>
1086 Reset statistics for a single function in the current database to
1087 zero (requires superuser privileges)
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.
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:
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;
1122 <sect1 id="monitoring-locks">
1123 <title>Viewing Locks</title>
1125 <indexterm zone="monitoring-locks">
1126 <primary>lock</primary>
1127 <secondary>monitoring</secondary>
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
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.
1149 Determine the relation in the current database with the most
1150 ungranted locks (which might be a source of contention among
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.
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">.
1171 <sect1 id="dynamic-trace">
1172 <title>Dynamic Tracing</title>
1174 <indexterm zone="dynamic-trace">
1175 <primary>DTrace</primary>
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
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.
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</>.
1206 <sect2 id="compiling-for-trace">
1207 <title>Compiling for Dynamic Tracing</title>
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.
1218 <sect2 id="trace-points">
1219 <title>Built-in Probes</title>
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
1228 <table id="dtrace-probe-point-table">
1229 <title>Built-in DTrace Probes</title>
1234 <entry>Parameters</entry>
1235 <entry>Description</entry>
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>
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>
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>
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>
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>
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>
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>
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>
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>
1296 <entry>query-plan-start</entry>
1298 <entry>Probe that fires when the planning of a query is started.</entry>
1301 <entry>query-plan-done</entry>
1303 <entry>Probe that fires when the planning of a query is complete.</entry>
1306 <entry>query-execute-start</entry>
1308 <entry>Probe that fires when the execution of a query is started.</entry>
1311 <entry>query-execute-done</entry>
1313 <entry>Probe that fires when the execution of a query is complete.</entry>
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>
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>
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>
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
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>
1352 <entry>subtrans-checkpoint-start</entry>
1353 <entry>(bool)</entry>
1354 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
1356 arg0 is true for normal checkpoint, false for shutdown
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>
1367 <entry>multixact-checkpoint-start</entry>
1368 <entry>(bool)</entry>
1369 <entry>Probe that fires when the MultiXact portion of a checkpoint is
1371 arg0 is true for normal checkpoint, false for shutdown
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>
1382 <entry>buffer-checkpoint-start</entry>
1383 <entry>(int)</entry>
1384 <entry>Probe that fires when the buffer-writing portion of a checkpoint
1386 arg0 holds the bitwise flags used to distinguish different checkpoint
1387 types, such as shutdown, immediate or force.</entry>
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>
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>
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>
1414 <entry>buffer-checkpoint-sync-start</entry>
1416 <entry>Probe that fires after dirty buffers have been written to the
1417 kernel, and before starting to issue fsync requests.</entry>
1420 <entry>buffer-checkpoint-done</entry>
1422 <entry>Probe that fires when syncing of buffers to disk is
1426 <entry>twophase-checkpoint-start</entry>
1428 <entry>Probe that fires when the two-phase portion of a checkpoint is
1432 <entry>twophase-checkpoint-done</entry>
1434 <entry>Probe that fires when the two-phase portion of a checkpoint is
1438 <entry>buffer-read-start</entry>
1439 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, bool, 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 true for a local buffer, false for a shared buffer.
1446 arg6 is true for a relation extension request, false for normal
1450 <entry>buffer-read-done</entry>
1451 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, bool, bool, bool)</entry>
1452 <entry>Probe that fires when a buffer read is complete.
1453 arg0 and arg1 contain the fork and block numbers of the page (if this
1454 is a relation extension request, arg1 now contains the block number
1455 of the newly added block).
1456 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1457 identifying the relation.
1458 arg5 is true for a local buffer, false for a shared buffer.
1459 arg6 is true for a relation extension request, false for normal
1461 arg7 is true if the buffer was found in the pool, false if not.</entry>
1464 <entry>buffer-flush-start</entry>
1465 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1466 <entry>Probe that fires before issuing any write request for a shared
1468 arg0 and arg1 contain the fork and block numbers of the page.
1469 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1470 identifying the relation.</entry>
1473 <entry>buffer-flush-done</entry>
1474 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1475 <entry>Probe that fires when a write request is complete. (Note
1476 that this just reflects the time to pass the data to the kernel;
1477 it's typically not actually been written to disk yet.)
1478 The arguments are the same as for buffer-flush-start.</entry>
1481 <entry>buffer-write-dirty-start</entry>
1482 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1483 <entry>Probe that fires when a server process begins to write a dirty
1484 buffer. (If this happens often, it implies that
1485 <xref linkend="guc-shared-buffers"> is too
1486 small or the bgwriter control parameters need adjustment.)
1487 arg0 and arg1 contain the fork and block numbers of the page.
1488 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1489 identifying the relation.</entry>
1492 <entry>buffer-write-dirty-done</entry>
1493 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1494 <entry>Probe that fires when a dirty-buffer write is complete.
1495 The arguments are the same as for buffer-write-dirty-start.</entry>
1498 <entry>wal-buffer-write-dirty-start</entry>
1500 <entry>Probe that fires when when a server process begins to write a
1501 dirty WAL buffer because no more WAL buffer space is available.
1502 (If this happens often, it implies that
1503 <xref linkend="guc-wal-buffers"> is too small.)</entry>
1506 <entry>wal-buffer-write-dirty-done</entry>
1508 <entry>Probe that fires when a dirty WAL buffer write is complete.</entry>
1511 <entry>xlog-insert</entry>
1512 <entry>(unsigned char, unsigned char)</entry>
1513 <entry>Probe that fires when a WAL record is inserted.
1514 arg0 is the resource manager (rmid) for the record.
1515 arg1 contains the info flags.</entry>
1518 <entry>xlog-switch</entry>
1520 <entry>Probe that fires when a WAL segment switch is requested.</entry>
1523 <entry>smgr-md-read-start</entry>
1524 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1525 <entry>Probe that fires when beginning to read a block from a relation.
1526 arg0 and arg1 contain the fork and block numbers of the page.
1527 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1528 identifying the relation.</entry>
1531 <entry>smgr-md-read-done</entry>
1532 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int)</entry>
1533 <entry>Probe that fires when a block read is complete.
1534 arg0 and arg1 contain the fork and block numbers of the page.
1535 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1536 identifying the relation.
1537 arg5 is the number of bytes actually read, while arg6 is the number
1538 requested (if these are different it indicates trouble).</entry>
1541 <entry>smgr-md-write-start</entry>
1542 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1543 <entry>Probe that fires when beginning to write a block to a relation.
1544 arg0 and arg1 contain the fork and block numbers of the page.
1545 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1546 identifying the relation.</entry>
1549 <entry>smgr-md-write-done</entry>
1550 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int)</entry>
1551 <entry>Probe that fires when a block write is complete.
1552 arg0 and arg1 contain the fork and block numbers of the page.
1553 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1554 identifying the relation.
1555 arg5 is the number of bytes actually written, while arg6 is the number
1556 requested (if these are different it indicates trouble).</entry>
1559 <entry>sort-start</entry>
1560 <entry>(int, bool, int, int, bool)</entry>
1561 <entry>Probe that fires when a sort operation is started.
1562 arg0 indicates heap, index or datum sort.
1563 arg1 is true for unique-value enforcement.
1564 arg2 is the number of key columns.
1565 arg3 is the number of kilobytes of work memory allowed.
1566 arg4 is true if random access to the sort result is required.</entry>
1569 <entry>sort-done</entry>
1570 <entry>(bool, long)</entry>
1571 <entry>Probe that fires when a sort is complete.
1572 arg0 is true for external sort, false for internal sort.
1573 arg1 is the number of disk blocks used for an external sort,
1574 or kilobytes of memory used for an internal sort.</entry>
1577 <entry>lwlock-acquire</entry>
1578 <entry>(LWLockId, LWLockMode)</entry>
1579 <entry>Probe that fires when an LWLock has been acquired.
1580 arg0 is the LWLock's ID.
1581 arg1 is the requested lock mode, either exclusive or shared.</entry>
1584 <entry>lwlock-release</entry>
1585 <entry>(LWLockId)</entry>
1586 <entry>Probe that fires when an LWLock has been released (but note
1587 that any released waiters have not yet been awakened).
1588 arg0 is the LWLock's ID.</entry>
1591 <entry>lwlock-wait-start</entry>
1592 <entry>(LWLockId, LWLockMode)</entry>
1593 <entry>Probe that fires when an LWLock was not immediately available and
1594 a server process has begun to wait for the lock to become available.
1595 arg0 is the LWLock's ID.
1596 arg1 is the requested lock mode, either exclusive or shared.</entry>
1599 <entry>lwlock-wait-done</entry>
1600 <entry>(LWLockId, LWLockMode)</entry>
1601 <entry>Probe that fires when a server process has been released from its
1602 wait for an LWLock (it does not actually have the lock yet).
1603 arg0 is the LWLock's ID.
1604 arg1 is the requested lock mode, either exclusive or shared.</entry>
1607 <entry>lwlock-condacquire</entry>
1608 <entry>(LWLockId, LWLockMode)</entry>
1609 <entry>Probe that fires when an LWLock was successfully acquired when the
1610 caller specified no waiting.
1611 arg0 is the LWLock's ID.
1612 arg1 is the requested lock mode, either exclusive or shared.</entry>
1615 <entry>lwlock-condacquire-fail</entry>
1616 <entry>(LWLockId, LWLockMode)</entry>
1617 <entry>Probe that fires when an LWLock was not successfully acquired when
1618 the caller specified no waiting.
1619 arg0 is the LWLock's ID.
1620 arg1 is the requested lock mode, either exclusive or shared.</entry>
1623 <entry>lock-wait-start</entry>
1624 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
1625 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
1626 has begun to wait because the lock is not available.
1627 arg0 through arg3 are the tag fields identifying the object being
1628 locked. arg4 indicates the type of object being locked.
1629 arg5 indicates the lock type being requested.</entry>
1632 <entry>lock-wait-done</entry>
1633 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
1634 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
1635 has finished waiting (i.e., has acquired the lock).
1636 The arguments are the same as for lock-wait-start.</entry>
1639 <entry>deadlock-found</entry>
1641 <entry>Probe that fires when a deadlock is found by the deadlock
1649 <table id="typedefs-table">
1650 <title>Defined Types Used in Probe Parameters</title>
1655 <entry>Definition</entry>
1662 <entry>LocalTransactionId</entry>
1663 <entry>unsigned int</entry>
1666 <entry>LWLockId</entry>
1670 <entry>LWLockMode</entry>
1674 <entry>LOCKMODE</entry>
1678 <entry>BlockNumber</entry>
1679 <entry>unsigned int</entry>
1683 <entry>unsigned int</entry>
1686 <entry>ForkNumber</entry>
1701 <sect2 id="using-trace-points">
1702 <title>Using Probes</title>
1705 The example below shows a DTrace script for analyzing transaction
1706 counts in the system, as an alternative to snapshotting
1707 <structname>pg_stat_database</> before and after a performance test:
1709 #!/usr/sbin/dtrace -qs
1711 postgresql$1:::transaction-start
1713 @start["Start"] = count();
1714 self->ts = timestamp;
1717 postgresql$1:::transaction-abort
1719 @abort["Abort"] = count();
1722 postgresql$1:::transaction-commit
1725 @commit["Commit"] = count();
1726 @time["Total time (ns)"] = sum(timestamp - self->ts);
1730 When executed, the example D script gives output such as:
1732 # ./txn_count.d `pgrep -n postgres` or ./txn_count.d <PID>
1737 Total time (ns) 2312105013
1743 SystemTap uses a different notation for trace scripts than DTrace does,
1744 even though the underlying trace points are compatible. One point worth
1745 noting is that at this writing, SystemTap scripts must reference probe
1746 names using double underscores in place of hyphens. This is expected to
1747 be fixed in future SystemTap releases.
1752 You should remember that DTrace scripts need to be carefully written and
1753 debugged, otherwise the trace information collected might
1754 be meaningless. In most cases where problems are found it is the
1755 instrumentation that is at fault, not the underlying system. When
1756 discussing information found using dynamic tracing, be sure to enclose
1757 the script used to allow that too to be checked and discussed.
1761 More example scripts can be found in the PgFoundry
1762 <ulink url="http://pgfoundry.org/projects/dtrace/">dtrace project</ulink>.
1766 <sect2 id="defining-trace-points">
1767 <title>Defining New Probes</title>
1770 New probes can be defined within the code wherever the developer
1771 desires, though this will require a recompilation. Below are the steps
1772 for inserting new probes:
1778 Decide on probe names and data to be made available through the probes
1784 Add the probe definitions to <filename>src/backend/utils/probes.d</>
1790 Include <filename>pg_trace.h</> if it is not already present in the
1791 module(s) containing the probe points, and insert
1792 <literal>TRACE_POSTGRESQL</> probe macros at the desired locations
1799 Recompile and verify that the new probes are available
1805 <title>Example:</title>
1807 Here is an example of how you would add a probe to trace all new
1808 transactions by transaction ID.
1815 Decide that the probe will be named <literal>transaction-start</> and
1816 requires a parameter of type LocalTransactionId
1822 Add the probe definition to <filename>src/backend/utils/probes.d</>:
1824 probe transaction__start(LocalTransactionId);
1826 Note the use of the double underline in the probe name. In a DTrace
1827 script using the probe, the double underline needs to be replaced with a
1828 hyphen, so <literal>transaction-start</> is the name to document for
1835 At compile time, <literal>transaction__start</> is converted to a macro
1836 called <literal>TRACE_POSTGRESQL_TRANSACTION_START</> (notice the
1837 underscores are single here), which is available by including
1838 <filename>pg_trace.h</>. Add the macro call to the appropriate location
1839 in the source code. In this case, it looks like the following:
1842 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1849 After recompiling and running the new binary, check that your newly added
1850 probe is available by executing the following DTrace command. You
1851 should see similar output:
1853 # dtrace -ln transaction-start
1854 ID PROVIDER MODULE FUNCTION NAME
1855 18705 postgresql49878 postgres StartTransactionCommand transaction-start
1856 18755 postgresql49877 postgres StartTransactionCommand transaction-start
1857 18805 postgresql49876 postgres StartTransactionCommand transaction-start
1858 18855 postgresql49875 postgres StartTransactionCommand transaction-start
1859 18986 postgresql49873 postgres StartTransactionCommand transaction-start
1866 There are a few things to be careful about when adding trace macros
1872 You should take care that the data types specified for a probe's
1873 parameters match the data types of the variables used in the macro.
1874 Otherwise, you will get compilation errors.
1881 On most platforms, if <productname>PostgreSQL</productname> is
1882 built with <option>--enable-dtrace</>, the arguments to a trace
1883 macro will be evaluated whenever control passes through the
1884 macro, <emphasis>even if no tracing is being done</>. This is
1885 usually not worth worrying about if you are just reporting the
1886 values of a few local variables. But beware of putting expensive
1887 function calls into the arguments. If you need to do that,
1888 consider protecting the macro with a check to see if the trace
1889 is actually enabled:
1892 if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
1893 TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
1896 Each trace macro has a corresponding <literal>ENABLED</> macro.