1 <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.85 2010/08/21 10:59:17 mha 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 information about vacuum and
121 analyze actions for each table. It can also count calls to user-defined
122 functions and the total time spent in each one.
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 the last time it was analyzed by the autovacuum daemon,
297 number of times it has been vacuumed manually,
298 number of times it has been vacuumed by the autovacuum daemon,
299 number of times it has been analyzed manually,
300 and the number of times it has been analyzed by the autovacuum daemon.
305 <entry><structname>pg_stat_sys_tables</></entry>
306 <entry>Same as <structname>pg_stat_all_tables</>, except that only
307 system tables are shown.</entry>
311 <entry><structname>pg_stat_user_tables</></entry>
312 <entry>Same as <structname>pg_stat_all_tables</>, except that only user
313 tables are shown.</entry>
317 <entry><structname>pg_stat_xact_all_tables</></entry>
318 <entry>Similar to <structname>pg_stat_all_tables</>, but counts actions
319 taken so far within the current transaction (which are <emphasis>not</>
320 yet included in <structname>pg_stat_all_tables</> and related views).
321 The columns for numbers of live and dead rows and vacuum and
322 analyze actions are not present in this view.</entry>
326 <entry><structname>pg_stat_xact_sys_tables</></entry>
327 <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
328 system tables are shown.</entry>
332 <entry><structname>pg_stat_xact_user_tables</></entry>
333 <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
334 user tables are shown.</entry>
338 <entry><structname>pg_stat_all_indexes</></entry>
339 <entry>For each index in the current database,
340 the table and index OID, schema, table and index name,
341 number of index scans initiated on that index, number of
342 index entries returned by index scans, and number of live table rows
343 fetched by simple index scans using that index.
348 <entry><structname>pg_stat_sys_indexes</></entry>
349 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
350 indexes on system tables are shown.</entry>
354 <entry><structname>pg_stat_user_indexes</></entry>
355 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
356 indexes on user tables are shown.</entry>
360 <entry><structname>pg_statio_all_tables</></entry>
361 <entry>For each table in the current database (including TOAST tables),
362 the table OID, schema and table name, number of disk
363 blocks read from that table, number of buffer hits, numbers of
364 disk blocks read and buffer hits in all indexes of that table,
365 numbers of disk blocks read and buffer hits from that table's
366 auxiliary TOAST table (if any), and numbers of disk blocks read
367 and buffer hits for the TOAST table's index.
372 <entry><structname>pg_statio_sys_tables</></entry>
373 <entry>Same as <structname>pg_statio_all_tables</>, except that only
374 system tables are shown.</entry>
378 <entry><structname>pg_statio_user_tables</></entry>
379 <entry>Same as <structname>pg_statio_all_tables</>, except that only
380 user tables are shown.</entry>
384 <entry><structname>pg_statio_all_indexes</></entry>
385 <entry>For each index in the current database,
386 the table and index OID, schema, table and index name,
387 numbers of disk blocks read and buffer hits in that index.
392 <entry><structname>pg_statio_sys_indexes</></entry>
393 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
394 indexes on system tables are shown.</entry>
398 <entry><structname>pg_statio_user_indexes</></entry>
399 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
400 indexes on user tables are shown.</entry>
404 <entry><structname>pg_statio_all_sequences</></entry>
405 <entry>For each sequence object in the current database,
406 the sequence OID, schema and sequence name,
407 numbers of disk blocks read and buffer hits in that sequence.
412 <entry><structname>pg_statio_sys_sequences</></entry>
413 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
414 system sequences are shown. (Presently, no system sequences are defined,
415 so this view is always empty.)</entry>
419 <entry><structname>pg_statio_user_sequences</></entry>
420 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
421 user sequences are shown.</entry>
425 <entry><structname>pg_stat_user_functions</></entry>
426 <entry>For all tracked functions, function OID, schema, name, number
427 of calls, total time, and self time. Self time is the
428 amount of time spent in the function itself, total time includes the
429 time spent in functions it called. Time values are in milliseconds.
434 <entry><structname>pg_stat_xact_user_functions</></entry>
435 <entry>Similar to <structname>pg_stat_user_functions</>, but counts only
436 calls during the current transaction (which are <emphasis>not</>
437 yet included in <structname>pg_stat_user_functions</>).</entry>
445 The per-index statistics are particularly useful to determine which
446 indexes are being used and how effective they are.
451 used either directly or via <quote>bitmap scans</>. In a bitmap scan
452 the output of several indexes can be combined via AND or OR rules;
453 so it is difficult to associate individual heap row fetches
454 with specific indexes when a bitmap scan is used. Therefore, a bitmap
456 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
457 count(s) for the index(es) it uses, and it increments the
458 <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
459 count for the table, but it does not affect
460 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
465 Before <productname>PostgreSQL</productname> 8.1, the
466 <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
467 were essentially always equal. Now they can be different even without
468 considering bitmap scans, because <structfield>idx_tup_read</> counts
469 index entries retrieved from the index while <structfield>idx_tup_fetch</>
470 counts live rows fetched from the table; the latter will be less if any
471 dead or not-yet-committed rows are fetched using the index.
476 The <structname>pg_statio_</> views are primarily useful to
477 determine the effectiveness of the buffer cache. When the number
478 of actual disk reads is much smaller than the number of buffer
479 hits, then the cache is satisfying most read requests without
480 invoking a kernel call. However, these statistics do not give the
481 entire story: due to the way in which <productname>PostgreSQL</>
482 handles disk I/O, data that is not in the
483 <productname>PostgreSQL</> buffer cache might still reside in the
484 kernel's I/O cache, and might therefore still be fetched without
485 requiring a physical read. Users interested in obtaining more
486 detailed information on <productname>PostgreSQL</> I/O behavior are
487 advised to use the <productname>PostgreSQL</> statistics collector
488 in combination with operating system utilities that allow insight
489 into the kernel's handling of I/O.
493 Other ways of looking at the statistics can be set up by writing
494 queries that use the same underlying statistics access functions as
495 these standard views do. These functions are listed in <xref
496 linkend="monitoring-stats-funcs-table">. The per-database access
497 functions take a database OID as argument to identify which
498 database to report on. The per-table and per-index functions take
499 a table or index OID. The functions for function-call statistics
500 take a function OID. (Note that only tables, indexes, and functions
501 in the current database can be seen with these functions.) The
502 per-server-process access functions take a server process
503 number, which ranges from one to the number of currently active
507 <table id="monitoring-stats-funcs-table">
508 <title>Statistics Access Functions</title>
513 <entry>Function</entry>
514 <entry>Return Type</entry>
515 <entry>Description</entry>
521 <entry><literal><function>pg_stat_get_db_numbackends</function>(<type>oid</type>)</literal></entry>
522 <entry><type>integer</type></entry>
524 Number of active server processes for database
529 <entry><literal><function>pg_stat_get_db_xact_commit</function>(<type>oid</type>)</literal></entry>
530 <entry><type>bigint</type></entry>
532 Number of transactions committed in database
537 <entry><literal><function>pg_stat_get_db_xact_rollback</function>(<type>oid</type>)</literal></entry>
538 <entry><type>bigint</type></entry>
540 Number of transactions rolled back in database
545 <entry><literal><function>pg_stat_get_db_blocks_fetched</function>(<type>oid</type>)</literal></entry>
546 <entry><type>bigint</type></entry>
548 Number of disk block fetch requests for database
553 <entry><literal><function>pg_stat_get_db_blocks_hit</function>(<type>oid</type>)</literal></entry>
554 <entry><type>bigint</type></entry>
556 Number of disk block fetch requests found in cache for database
561 <entry><literal><function>pg_stat_get_db_tuples_returned</function>(<type>oid</type>)</literal></entry>
562 <entry><type>bigint</type></entry>
564 Number of tuples returned for database
569 <entry><literal><function>pg_stat_get_db_tuples_fetched</function>(<type>oid</type>)</literal></entry>
570 <entry><type>bigint</type></entry>
572 Number of tuples fetched for database
577 <entry><literal><function>pg_stat_get_db_tuples_inserted</function>(<type>oid</type>)</literal></entry>
578 <entry><type>bigint</type></entry>
580 Number of tuples inserted in database
585 <entry><literal><function>pg_stat_get_db_tuples_updated</function>(<type>oid</type>)</literal></entry>
586 <entry><type>bigint</type></entry>
588 Number of tuples updated in database
593 <entry><literal><function>pg_stat_get_db_tuples_deleted</function>(<type>oid</type>)</literal></entry>
594 <entry><type>bigint</type></entry>
596 Number of tuples deleted in database
601 <entry><literal><function>pg_stat_get_numscans</function>(<type>oid</type>)</literal></entry>
602 <entry><type>bigint</type></entry>
604 Number of sequential scans done when argument is a table,
605 or number of index scans done when argument is an index
610 <entry><literal><function>pg_stat_get_tuples_returned</function>(<type>oid</type>)</literal></entry>
611 <entry><type>bigint</type></entry>
613 Number of rows read by sequential scans when argument is a table,
614 or number of index entries returned when argument is an index
619 <entry><literal><function>pg_stat_get_tuples_fetched</function>(<type>oid</type>)</literal></entry>
620 <entry><type>bigint</type></entry>
622 Number of table rows fetched by bitmap scans when argument is a table,
623 or table rows fetched by simple index scans using the index
624 when argument is an index
629 <entry><literal><function>pg_stat_get_tuples_inserted</function>(<type>oid</type>)</literal></entry>
630 <entry><type>bigint</type></entry>
632 Number of rows inserted into table
637 <entry><literal><function>pg_stat_get_tuples_updated</function>(<type>oid</type>)</literal></entry>
638 <entry><type>bigint</type></entry>
640 Number of rows updated in table (includes HOT updates)
645 <entry><literal><function>pg_stat_get_tuples_deleted</function>(<type>oid</type>)</literal></entry>
646 <entry><type>bigint</type></entry>
648 Number of rows deleted from table
653 <entry><literal><function>pg_stat_get_tuples_hot_updated</function>(<type>oid</type>)</literal></entry>
654 <entry><type>bigint</type></entry>
656 Number of rows HOT-updated in table
661 <entry><literal><function>pg_stat_get_live_tuples</function>(<type>oid</type>)</literal></entry>
662 <entry><type>bigint</type></entry>
664 Number of live rows in table
669 <entry><literal><function>pg_stat_get_dead_tuples</function>(<type>oid</type>)</literal></entry>
670 <entry><type>bigint</type></entry>
672 Number of dead rows in table
677 <entry><literal><function>pg_stat_get_blocks_fetched</function>(<type>oid</type>)</literal></entry>
678 <entry><type>bigint</type></entry>
680 Number of disk block fetch requests for table or index
685 <entry><literal><function>pg_stat_get_blocks_hit</function>(<type>oid</type>)</literal></entry>
686 <entry><type>bigint</type></entry>
688 Number of disk block requests found in cache for table or index
693 <entry><literal><function>pg_stat_get_last_vacuum_time</function>(<type>oid</type>)</literal></entry>
694 <entry><type>timestamptz</type></entry>
696 Time of the last vacuum initiated by the user on this table
701 <entry><literal><function>pg_stat_get_last_autovacuum_time</function>(<type>oid</type>)</literal></entry>
702 <entry><type>timestamptz</type></entry>
704 Time of the last vacuum initiated by the autovacuum daemon on this table
709 <entry><literal><function>pg_stat_get_last_analyze_time</function>(<type>oid</type>)</literal></entry>
710 <entry><type>timestamptz</type></entry>
712 Time of the last analyze initiated by the user on this table
717 <entry><literal><function>pg_stat_get_last_autoanalyze_time</function>(<type>oid</type>)</literal></entry>
718 <entry><type>timestamptz</type></entry>
720 Time of the last analyze initiated by the autovacuum daemon on this
726 <entry><literal><function>pg_stat_get_vacuum_count</function>(<type>oid</type>)</literal></entry>
727 <entry><type>bigint</type></entry>
729 The number of times this table has been vacuumed manually
734 <entry><literal><function>pg_stat_get_autovacuum_count</function>(<type>oid</type>)</literal></entry>
735 <entry><type>bigint</type></entry>
737 The number of times this table has been vacuumed by the autovacuum daemon
742 <entry><literal><function>pg_stat_get_analyze_count</function>(<type>oid</type>)</literal></entry>
743 <entry><type>bigint</type></entry>
745 The number of times this table has been analyzed manually
750 <entry><literal><function>pg_stat_get_autoanalyze_count</function>(<type>oid</type>)</literal></entry>
751 <entry><type>bigint</type></entry>
753 The number of times this table has been analyzed by the autovacuum daemon
758 <entry><literal><function>pg_stat_get_xact_numscans</function>(<type>oid</type>)</literal></entry>
759 <entry><type>bigint</type></entry>
761 Number of sequential scans done when argument is a table,
762 or number of index scans done when argument is an index, in the current transaction
767 <entry><literal><function>pg_stat_get_xact_tuples_returned</function>(<type>oid</type>)</literal></entry>
768 <entry><type>bigint</type></entry>
770 Number of rows read by sequential scans when argument is a table,
771 or number of index entries returned when argument is an index, in the current transaction
776 <entry><literal><function>pg_stat_get_xact_tuples_fetched</function>(<type>oid</type>)</literal></entry>
777 <entry><type>bigint</type></entry>
779 Number of table rows fetched by bitmap scans when argument is a table,
780 or table rows fetched by simple index scans using the index
781 when argument is an index, in the current transaction
786 <entry><literal><function>pg_stat_get_xact_tuples_inserted</function>(<type>oid</type>)</literal></entry>
787 <entry><type>bigint</type></entry>
789 Number of rows inserted into table, in the current transaction
794 <entry><literal><function>pg_stat_get_xact_tuples_updated</function>(<type>oid</type>)</literal></entry>
795 <entry><type>bigint</type></entry>
797 Number of rows updated in table (includes HOT updates), in the current transaction
802 <entry><literal><function>pg_stat_get_xact_tuples_deleted</function>(<type>oid</type>)</literal></entry>
803 <entry><type>bigint</type></entry>
805 Number of rows deleted from table, in the current transaction
810 <entry><literal><function>pg_stat_get_xact_tuples_hot_updated</function>(<type>oid</type>)</literal></entry>
811 <entry><type>bigint</type></entry>
813 Number of rows HOT-updated in table, in the current transaction
818 <entry><literal><function>pg_stat_get_xact_blocks_fetched</function>(<type>oid</type>)</literal></entry>
819 <entry><type>bigint</type></entry>
821 Number of disk block fetch requests for table or index, in the current transaction
826 <entry><literal><function>pg_stat_get_xact_blocks_hit</function>(<type>oid</type>)</literal></entry>
827 <entry><type>bigint</type></entry>
829 Number of disk block requests found in cache for table or index, in the current transaction
834 <!-- See also the entry for this in func.sgml -->
835 <entry><literal><function>pg_backend_pid</function>()</literal></entry>
836 <entry><type>integer</type></entry>
838 Process ID of the server process attached to the current session
843 <entry><literal><function>pg_stat_get_activity</function>(<type>integer</type>)</literal></entry>
844 <entry><type>setof record</type></entry>
846 Returns a record of information about the backend with the specified PID, or
847 one record for each active backend in the system if <symbol>NULL</symbol> is
848 specified. The fields returned are a subset of those in the
849 <structname>pg_stat_activity</structname> view.
854 <entry><literal><function>pg_stat_get_function_calls</function>(<type>oid</type>)</literal></entry>
855 <entry><type>bigint</type></entry>
857 Number of times the function has been called
862 <entry><literal><function>pg_stat_get_function_time</function>(<type>oid</type>)</literal></entry>
863 <entry><type>bigint</type></entry>
865 Total wall clock time spent in the function, in microseconds. Includes
866 the time spent in functions called by this one.
871 <entry><literal><function>pg_stat_get_function_self_time</function>(<type>oid</type>)</literal></entry>
872 <entry><type>bigint</type></entry>
874 Time spent in only this function. Time spent in called functions
880 <entry><literal><function>pg_stat_get_xact_function_calls</function>(<type>oid</type>)</literal></entry>
881 <entry><type>bigint</type></entry>
883 Number of times the function has been called, in the current transaction.
888 <entry><literal><function>pg_stat_get_xact_function_time</function>(<type>oid</type>)</literal></entry>
889 <entry><type>bigint</type></entry>
891 Total wall clock time spent in the function, in microseconds, in the
892 current transaction. Includes the time spent in functions called by
898 <entry><literal><function>pg_stat_get_xact_function_self_time</function>(<type>oid</type>)</literal></entry>
899 <entry><type>bigint</type></entry>
901 Time spent in only this function, in the current transaction. Time
902 spent in called functions is excluded.
907 <entry><literal><function>pg_stat_get_backend_idset</function>()</literal></entry>
908 <entry><type>setof integer</type></entry>
910 Set of currently active server process numbers (from 1 to the
911 number of active server processes). See usage example in the text.
916 <entry><literal><function>pg_stat_get_backend_pid</function>(<type>integer</type>)</literal></entry>
917 <entry><type>integer</type></entry>
919 Process ID of the given server process
924 <entry><literal><function>pg_stat_get_backend_dbid</function>(<type>integer</type>)</literal></entry>
925 <entry><type>oid</type></entry>
927 Database ID of the given server process
932 <entry><literal><function>pg_stat_get_backend_userid</function>(<type>integer</type>)</literal></entry>
933 <entry><type>oid</type></entry>
935 User ID of the given server process
940 <entry><literal><function>pg_stat_get_backend_activity</function>(<type>integer</type>)</literal></entry>
941 <entry><type>text</type></entry>
943 Active command of the given server process, but only if the
944 current user is a superuser or the same user as that of
945 the session being queried (and
946 <varname>track_activities</varname> is on)
951 <entry><literal><function>pg_stat_get_backend_waiting</function>(<type>integer</type>)</literal></entry>
952 <entry><type>boolean</type></entry>
954 True if the given server process is waiting for a lock,
955 but only if the current user is a superuser or the same user as that of
956 the session being queried (and
957 <varname>track_activities</varname> is on)
962 <entry><literal><function>pg_stat_get_backend_activity_start</function>(<type>integer</type>)</literal></entry>
963 <entry><type>timestamp with time zone</type></entry>
965 The time at which the given server process' currently
966 executing query was started, but only if the
967 current user is a superuser or the same user as that of
968 the session being queried (and
969 <varname>track_activities</varname> is on)
974 <entry><literal><function>pg_stat_get_backend_xact_start</function>(<type>integer</type>)</literal></entry>
975 <entry><type>timestamp with time zone</type></entry>
977 The time at which the given server process' currently
978 executing transaction was started, but only if the
979 current user is a superuser or the same user as that of
980 the session being queried (and
981 <varname>track_activities</varname> is on)
986 <entry><literal><function>pg_stat_get_backend_start</function>(<type>integer</type>)</literal></entry>
987 <entry><type>timestamp with time zone</type></entry>
989 The time at which the given server process was started, or
990 null if the current user is not a superuser nor the same user
991 as that of the session being queried
996 <entry><literal><function>pg_stat_get_backend_client_addr</function>(<type>integer</type>)</literal></entry>
997 <entry><type>inet</type></entry>
999 The IP address of the client connected to the given
1000 server process; null if the connection is over a Unix domain
1001 socket, also null if the current user is not a superuser nor
1002 the same user as that of the session being queried
1007 <entry><literal><function>pg_stat_get_backend_client_port</function>(<type>integer</type>)</literal></entry>
1008 <entry><type>integer</type></entry>
1010 The TCP port number of the client connected to the given
1011 server process; -1 if the connection is over a Unix domain
1012 socket, null if the current user is not a superuser nor the
1013 same user as that of the session being queried
1018 <entry><literal><function>pg_stat_get_bgwriter_timed_checkpoints</function>()</literal></entry>
1019 <entry><type>bigint</type></entry>
1021 Number of times the background writer has started timed checkpoints
1022 (because the <varname>checkpoint_timeout</varname> time has expired)
1027 <entry><literal><function>pg_stat_get_bgwriter_requested_checkpoints</function>()</literal></entry>
1028 <entry><type>bigint</type></entry>
1030 Number of times the background writer has started checkpoints based
1031 on requests from backends because the <varname>checkpoint_segments</varname>
1032 has been exceeded or because the <command>CHECKPOINT</command>
1033 command has been issued
1038 <entry><literal><function>pg_stat_get_bgwriter_buf_written_checkpoints</function>()</literal></entry>
1039 <entry><type>bigint</type></entry>
1041 Number of buffers written by the background writer during checkpoints
1046 <entry><literal><function>pg_stat_get_bgwriter_buf_written_clean</function>()</literal></entry>
1047 <entry><type>bigint</type></entry>
1049 Number of buffers written by the background writer for routine cleaning of
1055 <entry><literal><function>pg_stat_get_bgwriter_maxwritten_clean</function>()</literal></entry>
1056 <entry><type>bigint</type></entry>
1058 Number of times the background writer has stopped its cleaning scan because
1059 it has written more buffers than specified in the
1060 <varname>bgwriter_lru_maxpages</varname> parameter
1065 <entry><literal><function>pg_stat_get_buf_written_backend</function>()</literal></entry>
1066 <entry><type>bigint</type></entry>
1068 Number of buffers written by backends because they needed
1069 to allocate a new buffer
1074 <entry><literal><function>pg_stat_get_buf_alloc</function>()</literal></entry>
1075 <entry><type>bigint</type></entry>
1077 Total number of buffer allocations
1082 <entry><literal><function>pg_stat_clear_snapshot</function>()</literal></entry>
1083 <entry><type>void</type></entry>
1085 Discard the current statistics snapshot
1090 <entry><literal><function>pg_stat_reset</function>()</literal></entry>
1091 <entry><type>void</type></entry>
1093 Reset all statistics counters for the current database to zero
1094 (requires superuser privileges)
1099 <entry><literal><function>pg_stat_reset_shared</function>(text)</literal></entry>
1100 <entry><type>void</type></entry>
1102 Reset some of the shared statistics counters for the database cluster to
1103 zero (requires superuser privileges). Calling
1104 <literal>pg_stat_reset_shared('bgwriter')</> will zero all the values shown by
1105 <structname>pg_stat_bgwriter</>.
1110 <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal></entry>
1111 <entry><type>void</type></entry>
1113 Reset statistics for a single table or index in the current database to
1114 zero (requires superuser privileges)
1119 <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal></entry>
1120 <entry><type>void</type></entry>
1122 Reset statistics for a single function in the current database to
1123 zero (requires superuser privileges)
1132 <function>pg_stat_get_blocks_fetched</function> minus
1133 <function>pg_stat_get_blocks_hit</function> gives the number of kernel
1134 <function>read()</> calls issued for the table, index, or
1135 database; the number of actual physical reads is usually
1136 lower due to kernel-level buffering. The <literal>*_blks_read</>
1137 statistics columns use this subtraction, i.e., fetched minus hit.
1142 All functions to access information about backends are indexed by backend id
1143 number, except <function>pg_stat_get_activity</function> which is indexed by PID.
1144 The function <function>pg_stat_get_backend_idset</function> provides
1145 a convenient way to generate one row for each active server process. For
1146 example, to show the <acronym>PID</>s and current queries of all server processes:
1149 SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
1150 pg_stat_get_backend_activity(s.backendid) AS current_query
1151 FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
1158 <sect1 id="monitoring-locks">
1159 <title>Viewing Locks</title>
1161 <indexterm zone="monitoring-locks">
1162 <primary>lock</primary>
1163 <secondary>monitoring</secondary>
1167 Another useful tool for monitoring database activity is the
1168 <structname>pg_locks</structname> system table. It allows the
1169 database administrator to view information about the outstanding
1170 locks in the lock manager. For example, this capability can be used
1176 View all the locks currently outstanding, all the locks on
1177 relations in a particular database, all the locks on a
1178 particular relation, or all the locks held by a particular
1179 <productname>PostgreSQL</productname> session.
1185 Determine the relation in the current database with the most
1186 ungranted locks (which might be a source of contention among
1193 Determine the effect of lock contention on overall database
1194 performance, as well as the extent to which contention varies
1195 with overall database traffic.
1200 Details of the <structname>pg_locks</structname> view appear in
1201 <xref linkend="view-pg-locks">.
1202 For more information on locking and managing concurrency with
1203 <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
1207 <sect1 id="dynamic-trace">
1208 <title>Dynamic Tracing</title>
1210 <indexterm zone="dynamic-trace">
1211 <primary>DTrace</primary>
1215 <productname>PostgreSQL</productname> provides facilities to support
1216 dynamic tracing of the database server. This allows an external
1217 utility to be called at specific points in the code and thereby trace
1222 A number of probes or trace points are already inserted into the source
1223 code. These probes are intended to be used by database developers and
1224 administrators. By default the probes are not compiled into
1225 <productname>PostgreSQL</productname>; the user needs to explicitly tell
1226 the configure script to make the probes available.
1231 <ulink url="http://opensolaris.org/os/community/dtrace/">DTrace</ulink>
1232 utility is supported, which is available
1233 on OpenSolaris, Solaris 10, and Mac OS X Leopard. It is expected that
1234 DTrace will be available in the future on FreeBSD and possibly other
1235 operating systems. The
1236 <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> project
1237 for Linux also provides a DTrace equivalent. Supporting other dynamic
1238 tracing utilities is theoretically possible by changing the definitions for
1239 the macros in <filename>src/include/utils/probes.h</>.
1242 <sect2 id="compiling-for-trace">
1243 <title>Compiling for Dynamic Tracing</title>
1246 By default, probes are not available, so you will need to
1247 explicitly tell the configure script to make the probes available
1248 in <productname>PostgreSQL</productname>. To include DTrace support
1249 specify <option>--enable-dtrace</> to configure. See <xref
1250 linkend="install-procedure"> for further information.
1254 <sect2 id="trace-points">
1255 <title>Built-in Probes</title>
1258 A number of standard probes are provided in the source code,
1259 as shown in <xref linkend="dtrace-probe-point-table">.
1260 More can certainly be added to enhance <productname>PostgreSQL</>'s
1264 <table id="dtrace-probe-point-table">
1265 <title>Built-in DTrace Probes</title>
1270 <entry>Parameters</entry>
1271 <entry>Description</entry>
1278 <entry>transaction-start</entry>
1279 <entry>(LocalTransactionId)</entry>
1280 <entry>Probe that fires at the start of a new transaction.
1281 arg0 is the transaction ID.</entry>
1284 <entry>transaction-commit</entry>
1285 <entry>(LocalTransactionId)</entry>
1286 <entry>Probe that fires when a transaction completes successfully.
1287 arg0 is the transaction ID.</entry>
1290 <entry>transaction-abort</entry>
1291 <entry>(LocalTransactionId)</entry>
1292 <entry>Probe that fires when a transaction completes unsuccessfully.
1293 arg0 is the transaction ID.</entry>
1296 <entry>query-start</entry>
1297 <entry>(const char *)</entry>
1298 <entry>Probe that fires when the processing of a query is started.
1299 arg0 is the query string.</entry>
1302 <entry>query-done</entry>
1303 <entry>(const char *)</entry>
1304 <entry>Probe that fires when the processing of a query is complete.
1305 arg0 is the query string.</entry>
1308 <entry>query-parse-start</entry>
1309 <entry>(const char *)</entry>
1310 <entry>Probe that fires when the parsing of a query is started.
1311 arg0 is the query string.</entry>
1314 <entry>query-parse-done</entry>
1315 <entry>(const char *)</entry>
1316 <entry>Probe that fires when the parsing of a query is complete.
1317 arg0 is the query string.</entry>
1320 <entry>query-rewrite-start</entry>
1321 <entry>(const char *)</entry>
1322 <entry>Probe that fires when the rewriting of a query is started.
1323 arg0 is the query string.</entry>
1326 <entry>query-rewrite-done</entry>
1327 <entry>(const char *)</entry>
1328 <entry>Probe that fires when the rewriting of a query is complete.
1329 arg0 is the query string.</entry>
1332 <entry>query-plan-start</entry>
1334 <entry>Probe that fires when the planning of a query is started.</entry>
1337 <entry>query-plan-done</entry>
1339 <entry>Probe that fires when the planning of a query is complete.</entry>
1342 <entry>query-execute-start</entry>
1344 <entry>Probe that fires when the execution of a query is started.</entry>
1347 <entry>query-execute-done</entry>
1349 <entry>Probe that fires when the execution of a query is complete.</entry>
1352 <entry>statement-status</entry>
1353 <entry>(const char *)</entry>
1354 <entry>Probe that fires anytime the server process updates its
1355 <structname>pg_stat_activity</>.<structfield>current_query</> status.
1356 arg0 is the new status string.</entry>
1359 <entry>checkpoint-start</entry>
1360 <entry>(int)</entry>
1361 <entry>Probe that fires when a checkpoint is started.
1362 arg0 holds the bitwise flags used to distinguish different checkpoint
1363 types, such as shutdown, immediate or force.</entry>
1366 <entry>checkpoint-done</entry>
1367 <entry>(int, int, int, int, int)</entry>
1368 <entry>Probe that fires when a checkpoint is complete.
1369 (The probes listed next fire in sequence during checkpoint processing.)
1370 arg0 is the number of buffers written. arg1 is the total number of
1371 buffers. arg2, arg3 and arg4 contain the number of xlog file(s) added,
1372 removed and recycled respectively.</entry>
1375 <entry>clog-checkpoint-start</entry>
1376 <entry>(bool)</entry>
1377 <entry>Probe that fires when the CLOG portion of a checkpoint is started.
1378 arg0 is true for normal checkpoint, false for shutdown
1382 <entry>clog-checkpoint-done</entry>
1383 <entry>(bool)</entry>
1384 <entry>Probe that fires when the CLOG portion of a checkpoint is
1385 complete. arg0 has the same meaning as for clog-checkpoint-start.</entry>
1388 <entry>subtrans-checkpoint-start</entry>
1389 <entry>(bool)</entry>
1390 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
1392 arg0 is true for normal checkpoint, false for shutdown
1396 <entry>subtrans-checkpoint-done</entry>
1397 <entry>(bool)</entry>
1398 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
1399 complete. arg0 has the same meaning as for
1400 subtrans-checkpoint-start.</entry>
1403 <entry>multixact-checkpoint-start</entry>
1404 <entry>(bool)</entry>
1405 <entry>Probe that fires when the MultiXact portion of a checkpoint is
1407 arg0 is true for normal checkpoint, false for shutdown
1411 <entry>multixact-checkpoint-done</entry>
1412 <entry>(bool)</entry>
1413 <entry>Probe that fires when the MultiXact portion of a checkpoint is
1414 complete. arg0 has the same meaning as for
1415 multixact-checkpoint-start.</entry>
1418 <entry>buffer-checkpoint-start</entry>
1419 <entry>(int)</entry>
1420 <entry>Probe that fires when the buffer-writing portion of a checkpoint
1422 arg0 holds the bitwise flags used to distinguish different checkpoint
1423 types, such as shutdown, immediate or force.</entry>
1426 <entry>buffer-sync-start</entry>
1427 <entry>(int, int)</entry>
1428 <entry>Probe that fires when we begin to write dirty buffers during
1429 checkpoint (after identifying which buffers must be written).
1430 arg0 is the total number of buffers.
1431 arg1 is the number that are currently dirty and need to be written.</entry>
1434 <entry>buffer-sync-written</entry>
1435 <entry>(int)</entry>
1436 <entry>Probe that fires after each buffer is written during checkpoint.
1437 arg0 is the ID number of the buffer.</entry>
1440 <entry>buffer-sync-done</entry>
1441 <entry>(int, int, int)</entry>
1442 <entry>Probe that fires when all dirty buffers have been written.
1443 arg0 is the total number of buffers.
1444 arg1 is the number of buffers actually written by the checkpoint process.
1445 arg2 is the number that were expected to be written (arg1 of
1446 buffer-sync-start); any difference reflects other processes flushing
1447 buffers during the checkpoint.</entry>
1450 <entry>buffer-checkpoint-sync-start</entry>
1452 <entry>Probe that fires after dirty buffers have been written to the
1453 kernel, and before starting to issue fsync requests.</entry>
1456 <entry>buffer-checkpoint-done</entry>
1458 <entry>Probe that fires when syncing of buffers to disk is
1462 <entry>twophase-checkpoint-start</entry>
1464 <entry>Probe that fires when the two-phase portion of a checkpoint is
1468 <entry>twophase-checkpoint-done</entry>
1470 <entry>Probe that fires when the two-phase portion of a checkpoint is
1474 <entry>buffer-read-start</entry>
1475 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</entry>
1476 <entry>Probe that fires when a buffer read is started.
1477 arg0 and arg1 contain the fork and block numbers of the page (but
1478 arg1 will be -1 if this is a relation extension request).
1479 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1480 identifying the relation.
1481 arg5 is the ID of the backend which created the temporary relation for a
1482 local buffer, or InvalidBackendId (-1) for a shared buffer.
1483 arg6 is true for a relation extension request, false for normal
1487 <entry>buffer-read-done</entry>
1488 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool, bool)</entry>
1489 <entry>Probe that fires when a buffer read is complete.
1490 arg0 and arg1 contain the fork and block numbers of the page (if this
1491 is a relation extension request, arg1 now contains the block number
1492 of the newly added block).
1493 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1494 identifying the relation.
1495 arg5 is the ID of the backend which created the temporary relation for a
1496 local buffer, or InvalidBackendId (-1) for a shared buffer.
1497 arg6 is true for a relation extension request, false for normal
1499 arg7 is true if the buffer was found in the pool, false if not.</entry>
1502 <entry>buffer-flush-start</entry>
1503 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1504 <entry>Probe that fires before issuing any write request for a shared
1506 arg0 and arg1 contain the fork and block numbers of the page.
1507 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1508 identifying the relation.</entry>
1511 <entry>buffer-flush-done</entry>
1512 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1513 <entry>Probe that fires when a write request is complete. (Note
1514 that this just reflects the time to pass the data to the kernel;
1515 it's typically not actually been written to disk yet.)
1516 The arguments are the same as for buffer-flush-start.</entry>
1519 <entry>buffer-write-dirty-start</entry>
1520 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1521 <entry>Probe that fires when a server process begins to write a dirty
1522 buffer. (If this happens often, it implies that
1523 <xref linkend="guc-shared-buffers"> is too
1524 small or the bgwriter control parameters need adjustment.)
1525 arg0 and arg1 contain the fork and block numbers of the page.
1526 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1527 identifying the relation.</entry>
1530 <entry>buffer-write-dirty-done</entry>
1531 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1532 <entry>Probe that fires when a dirty-buffer write is complete.
1533 The arguments are the same as for buffer-write-dirty-start.</entry>
1536 <entry>wal-buffer-write-dirty-start</entry>
1538 <entry>Probe that fires when when a server process begins to write a
1539 dirty WAL buffer because no more WAL buffer space is available.
1540 (If this happens often, it implies that
1541 <xref linkend="guc-wal-buffers"> is too small.)</entry>
1544 <entry>wal-buffer-write-dirty-done</entry>
1546 <entry>Probe that fires when a dirty WAL buffer write is complete.</entry>
1549 <entry>xlog-insert</entry>
1550 <entry>(unsigned char, unsigned char)</entry>
1551 <entry>Probe that fires when a WAL record is inserted.
1552 arg0 is the resource manager (rmid) for the record.
1553 arg1 contains the info flags.</entry>
1556 <entry>xlog-switch</entry>
1558 <entry>Probe that fires when a WAL segment switch is requested.</entry>
1561 <entry>smgr-md-read-start</entry>
1562 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
1563 <entry>Probe that fires when beginning to read a block from a relation.
1564 arg0 and arg1 contain the fork and block numbers of the page.
1565 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1566 identifying the relation.
1567 arg5 is the ID of the backend which created the temporary relation for a
1568 local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
1571 <entry>smgr-md-read-done</entry>
1572 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
1573 <entry>Probe that fires when a block read is complete.
1574 arg0 and arg1 contain the fork and block numbers of the page.
1575 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1576 identifying the relation.
1577 arg5 is the ID of the backend which created the temporary relation for a
1578 local buffer, or InvalidBackendId (-1) for a shared buffer.
1579 arg6 is the number of bytes actually read, while arg7 is the number
1580 requested (if these are different it indicates trouble).</entry>
1583 <entry>smgr-md-write-start</entry>
1584 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
1585 <entry>Probe that fires when beginning to write a block to a relation.
1586 arg0 and arg1 contain the fork and block numbers of the page.
1587 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1588 identifying the relation.
1589 arg5 is the ID of the backend which created the temporary relation for a
1590 local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
1593 <entry>smgr-md-write-done</entry>
1594 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
1595 <entry>Probe that fires when a block write is complete.
1596 arg0 and arg1 contain the fork and block numbers of the page.
1597 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1598 identifying the relation.
1599 arg5 is the ID of the backend which created the temporary relation for a
1600 local buffer, or InvalidBackendId (-1) for a shared buffer.
1601 arg6 is the number of bytes actually written, while arg7 is the number
1602 requested (if these are different it indicates trouble).</entry>
1605 <entry>sort-start</entry>
1606 <entry>(int, bool, int, int, bool)</entry>
1607 <entry>Probe that fires when a sort operation is started.
1608 arg0 indicates heap, index or datum sort.
1609 arg1 is true for unique-value enforcement.
1610 arg2 is the number of key columns.
1611 arg3 is the number of kilobytes of work memory allowed.
1612 arg4 is true if random access to the sort result is required.</entry>
1615 <entry>sort-done</entry>
1616 <entry>(bool, long)</entry>
1617 <entry>Probe that fires when a sort is complete.
1618 arg0 is true for external sort, false for internal sort.
1619 arg1 is the number of disk blocks used for an external sort,
1620 or kilobytes of memory used for an internal sort.</entry>
1623 <entry>lwlock-acquire</entry>
1624 <entry>(LWLockId, LWLockMode)</entry>
1625 <entry>Probe that fires when an LWLock has been acquired.
1626 arg0 is the LWLock's ID.
1627 arg1 is the requested lock mode, either exclusive or shared.</entry>
1630 <entry>lwlock-release</entry>
1631 <entry>(LWLockId)</entry>
1632 <entry>Probe that fires when an LWLock has been released (but note
1633 that any released waiters have not yet been awakened).
1634 arg0 is the LWLock's ID.</entry>
1637 <entry>lwlock-wait-start</entry>
1638 <entry>(LWLockId, LWLockMode)</entry>
1639 <entry>Probe that fires when an LWLock was not immediately available and
1640 a server process has begun to wait for the lock to become available.
1641 arg0 is the LWLock's ID.
1642 arg1 is the requested lock mode, either exclusive or shared.</entry>
1645 <entry>lwlock-wait-done</entry>
1646 <entry>(LWLockId, LWLockMode)</entry>
1647 <entry>Probe that fires when a server process has been released from its
1648 wait for an LWLock (it does not actually have the lock yet).
1649 arg0 is the LWLock's ID.
1650 arg1 is the requested lock mode, either exclusive or shared.</entry>
1653 <entry>lwlock-condacquire</entry>
1654 <entry>(LWLockId, LWLockMode)</entry>
1655 <entry>Probe that fires when an LWLock was successfully acquired when the
1656 caller specified no waiting.
1657 arg0 is the LWLock's ID.
1658 arg1 is the requested lock mode, either exclusive or shared.</entry>
1661 <entry>lwlock-condacquire-fail</entry>
1662 <entry>(LWLockId, LWLockMode)</entry>
1663 <entry>Probe that fires when an LWLock was not successfully acquired when
1664 the caller specified no waiting.
1665 arg0 is the LWLock's ID.
1666 arg1 is the requested lock mode, either exclusive or shared.</entry>
1669 <entry>lock-wait-start</entry>
1670 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
1671 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
1672 has begun to wait because the lock is not available.
1673 arg0 through arg3 are the tag fields identifying the object being
1674 locked. arg4 indicates the type of object being locked.
1675 arg5 indicates the lock type being requested.</entry>
1678 <entry>lock-wait-done</entry>
1679 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
1680 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
1681 has finished waiting (i.e., has acquired the lock).
1682 The arguments are the same as for lock-wait-start.</entry>
1685 <entry>deadlock-found</entry>
1687 <entry>Probe that fires when a deadlock is found by the deadlock
1695 <table id="typedefs-table">
1696 <title>Defined Types Used in Probe Parameters</title>
1701 <entry>Definition</entry>
1708 <entry>LocalTransactionId</entry>
1709 <entry>unsigned int</entry>
1712 <entry>LWLockId</entry>
1716 <entry>LWLockMode</entry>
1720 <entry>LOCKMODE</entry>
1724 <entry>BlockNumber</entry>
1725 <entry>unsigned int</entry>
1729 <entry>unsigned int</entry>
1732 <entry>ForkNumber</entry>
1747 <sect2 id="using-trace-points">
1748 <title>Using Probes</title>
1751 The example below shows a DTrace script for analyzing transaction
1752 counts in the system, as an alternative to snapshotting
1753 <structname>pg_stat_database</> before and after a performance test:
1755 #!/usr/sbin/dtrace -qs
1757 postgresql$1:::transaction-start
1759 @start["Start"] = count();
1760 self->ts = timestamp;
1763 postgresql$1:::transaction-abort
1765 @abort["Abort"] = count();
1768 postgresql$1:::transaction-commit
1771 @commit["Commit"] = count();
1772 @time["Total time (ns)"] = sum(timestamp - self->ts);
1776 When executed, the example D script gives output such as:
1778 # ./txn_count.d `pgrep -n postgres` or ./txn_count.d <PID>
1783 Total time (ns) 2312105013
1789 SystemTap uses a different notation for trace scripts than DTrace does,
1790 even though the underlying trace points are compatible. One point worth
1791 noting is that at this writing, SystemTap scripts must reference probe
1792 names using double underscores in place of hyphens. This is expected to
1793 be fixed in future SystemTap releases.
1798 You should remember that DTrace scripts need to be carefully written and
1799 debugged, otherwise the trace information collected might
1800 be meaningless. In most cases where problems are found it is the
1801 instrumentation that is at fault, not the underlying system. When
1802 discussing information found using dynamic tracing, be sure to enclose
1803 the script used to allow that too to be checked and discussed.
1807 More example scripts can be found in the PgFoundry
1808 <ulink url="http://pgfoundry.org/projects/dtrace/">dtrace project</ulink>.
1812 <sect2 id="defining-trace-points">
1813 <title>Defining New Probes</title>
1816 New probes can be defined within the code wherever the developer
1817 desires, though this will require a recompilation. Below are the steps
1818 for inserting new probes:
1824 Decide on probe names and data to be made available through the probes
1830 Add the probe definitions to <filename>src/backend/utils/probes.d</>
1836 Include <filename>pg_trace.h</> if it is not already present in the
1837 module(s) containing the probe points, and insert
1838 <literal>TRACE_POSTGRESQL</> probe macros at the desired locations
1845 Recompile and verify that the new probes are available
1851 <title>Example:</title>
1853 Here is an example of how you would add a probe to trace all new
1854 transactions by transaction ID.
1861 Decide that the probe will be named <literal>transaction-start</> and
1862 requires a parameter of type LocalTransactionId
1868 Add the probe definition to <filename>src/backend/utils/probes.d</>:
1870 probe transaction__start(LocalTransactionId);
1872 Note the use of the double underline in the probe name. In a DTrace
1873 script using the probe, the double underline needs to be replaced with a
1874 hyphen, so <literal>transaction-start</> is the name to document for
1881 At compile time, <literal>transaction__start</> is converted to a macro
1882 called <literal>TRACE_POSTGRESQL_TRANSACTION_START</> (notice the
1883 underscores are single here), which is available by including
1884 <filename>pg_trace.h</>. Add the macro call to the appropriate location
1885 in the source code. In this case, it looks like the following:
1888 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1895 After recompiling and running the new binary, check that your newly added
1896 probe is available by executing the following DTrace command. You
1897 should see similar output:
1899 # dtrace -ln transaction-start
1900 ID PROVIDER MODULE FUNCTION NAME
1901 18705 postgresql49878 postgres StartTransactionCommand transaction-start
1902 18755 postgresql49877 postgres StartTransactionCommand transaction-start
1903 18805 postgresql49876 postgres StartTransactionCommand transaction-start
1904 18855 postgresql49875 postgres StartTransactionCommand transaction-start
1905 18986 postgresql49873 postgres StartTransactionCommand transaction-start
1912 There are a few things to be careful about when adding trace macros
1918 You should take care that the data types specified for a probe's
1919 parameters match the data types of the variables used in the macro.
1920 Otherwise, you will get compilation errors.
1927 On most platforms, if <productname>PostgreSQL</productname> is
1928 built with <option>--enable-dtrace</>, the arguments to a trace
1929 macro will be evaluated whenever control passes through the
1930 macro, <emphasis>even if no tracing is being done</>. This is
1931 usually not worth worrying about if you are just reporting the
1932 values of a few local variables. But beware of putting expensive
1933 function calls into the arguments. If you need to do that,
1934 consider protecting the macro with a check to see if the trace
1935 is actually enabled:
1938 if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
1939 TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
1942 Each trace macro has a corresponding <literal>ENABLED</> macro.