1 <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.75 2010/01/28 14:25:41 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"
31 endterm="sql-explain-title"> command.
32 <xref linkend="using-explain"> discusses <command>EXPLAIN</>
33 and other methods for understanding the behavior of an individual
37 <sect1 id="monitoring-ps">
38 <title>Standard Unix Tools</Title>
40 <indexterm zone="monitoring-ps">
42 <secondary>to monitor activity</secondary>
46 On most platforms, <productname>PostgreSQL</productname> modifies its
47 command title as reported by <command>ps</>, so that individual server
48 processes can readily be identified. A sample display is
51 $ ps auxww | grep ^postgres
52 postgres 960 0.0 1.1 6104 1480 pts/1 SN 13:17 0:00 postgres -i
53 postgres 963 0.0 1.1 7084 1472 pts/1 SN 13:17 0:00 postgres: writer process
54 postgres 965 0.0 1.1 6152 1512 pts/1 SN 13:17 0:00 postgres: stats collector process
55 postgres 998 0.0 2.3 6532 2992 pts/1 SN 13:18 0:00 postgres: tgl runbug 127.0.0.1 idle
56 postgres 1003 0.0 2.4 6532 3128 pts/1 SN 13:19 0:00 postgres: tgl regression [local] SELECT waiting
57 postgres 1016 0.1 2.4 6532 3080 pts/1 SN 13:19 0:00 postgres: tgl regression [local] idle in transaction
60 (The appropriate invocation of <command>ps</> varies across different
61 platforms, as do the details of what is shown. This example is from a
62 recent Linux system.) The first process listed here is the
63 master server process. The command arguments
64 shown for it are the same ones given when it was launched. The next two
65 processes are background worker processes automatically launched by the
66 master process. (The <quote>stats collector</> process will not be present
68 the system not to start the statistics collector.) Each of the remaining
69 processes is a server process handling one client connection. Each such
70 process sets its command line display in the form
73 postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <replaceable>activity</>
76 The user, database, and connection source host items remain the same for
77 the life of the client connection, but the activity indicator changes.
78 The activity can be <literal>idle</> (i.e., waiting for a client command),
79 <literal>idle in transaction</> (waiting for client inside a <command>BEGIN</> block),
80 or a command type name such as <literal>SELECT</>. Also,
81 <literal>waiting</> is attached if the server process is presently waiting
82 on a lock held by another server process. In the above example we can infer
83 that process 1003 is waiting for process 1016 to complete its transaction and
84 thereby release some lock or other.
88 If you have turned off <xref linkend="guc-update-process-title"> then the
89 activity indicator is not updated; the process title is set only once
90 when a new process is launched. On some platforms this saves a useful
91 amount of per-command overhead, on others it's insignificant.
96 <productname>Solaris</productname> requires special handling. You must
97 use <command>/usr/ucb/ps</command>, rather than
98 <command>/bin/ps</command>. You also must use two <option>w</option>
99 flags, not just one. In addition, your original invocation of the
100 <command>postgres</command> command must have a shorter
101 <command>ps</command> status display than that provided by each
102 server process. If you fail to do all three things, the <command>ps</>
103 output for each server process will be the original <command>postgres</>
109 <sect1 id="monitoring-stats">
110 <title>The Statistics Collector</Title>
112 <indexterm zone="monitoring-stats">
113 <primary>statistics</primary>
117 <productname>PostgreSQL</productname>'s <firstterm>statistics collector</>
118 is a subsystem that supports collection and reporting of information about
119 server activity. Presently, the collector can count accesses to tables
120 and indexes in both disk-block and individual-row terms. It also tracks
121 total numbers of rows in each table, and the last vacuum and analyze times
122 for each table. It can also count calls to user-defined functions and
123 the total time spent in each one.
127 <productname>PostgreSQL</productname> also supports determining the exact
128 command currently being executed by other server processes. This is an
129 independent facility that does not depend on the collector process.
132 <sect2 id="monitoring-stats-setup">
133 <title>Statistics Collection Configuration</Title>
136 Since collection of statistics adds some overhead to query execution,
137 the system can be configured to collect or not collect information.
138 This is controlled by configuration parameters that are normally set in
139 <filename>postgresql.conf</>. (See <xref linkend="runtime-config"> for
140 details about setting configuration parameters.)
144 The parameter <xref linkend="guc-track-counts"> controls whether
145 statistics are collected about table and index accesses.
149 The parameter <xref linkend="guc-track-functions"> enables tracking of
150 usage of user-defined functions.
154 The parameter <xref linkend="guc-track-activities"> enables monitoring
155 of the current command being executed by any server process.
159 Normally these parameters are set in <filename>postgresql.conf</> so
160 that they apply to all server processes, but it is possible to turn
161 them on or off in individual sessions using the <xref
162 linkend="sql-set" endterm="sql-set-title"> command. (To prevent
163 ordinary users from hiding their activity from the administrator,
164 only superusers are allowed to change these parameters with
169 The statistics collector communicates with the backends needing
170 information (including autovacuum) through temporary files.
171 These files are stored in the <filename>pg_stat_tmp</filename> subdirectory.
172 When the postmaster shuts down, a permanent copy of the statistics
173 data is stored in the <filename>global</filename> subdirectory. For increased
174 performance, the parameter <xref linkend="guc-stats-temp-directory"> can
175 be pointed at a RAM based filesystem, decreasing physical I/O requirements.
180 <sect2 id="monitoring-stats-views">
181 <title>Viewing Collected Statistics</Title>
184 Several predefined views, listed in <xref
185 linkend="monitoring-stats-views-table">, are available to show the results
186 of statistics collection. Alternatively, one can
187 build custom views using the underlying statistics functions.
191 When using the statistics to monitor current activity, it is important
192 to realize that the information does not update instantaneously.
193 Each individual server process transmits new statistical counts to
194 the collector just before going idle; so a query or transaction still in
195 progress does not affect the displayed totals. Also, the collector itself
196 emits a new report at most once per <varname>PGSTAT_STAT_INTERVAL</varname>
197 milliseconds (500 unless altered while building the server). So the
198 displayed information lags behind actual activity. However, current-query
199 information collected by <varname>track_activities</varname> is
204 Another important point is that when a server process is asked to display
205 any of these statistics, it first fetches the most recent report emitted by
206 the collector process and then continues to use this snapshot for all
207 statistical views and functions until the end of its current transaction.
208 So the statistics will appear not to change as long as you continue the
209 current transaction. Similarly, information about the current queries of
210 all processes is collected when any such information is first requested
211 within a transaction, and the same information will be displayed throughout
213 This is a feature, not a bug, because it allows you to perform several
214 queries on the statistics and correlate the results without worrying that
215 the numbers are changing underneath you. But if you want to see new
216 results with each query, be sure to do the queries outside any transaction
217 block. Alternatively, you can invoke
218 <function>pg_stat_clear_snapshot</function>(), which will discard the
219 current transaction's statistics snapshot (if any). The next use of
220 statistical information will cause a new snapshot to be fetched.
223 <table id="monitoring-stats-views-table">
224 <title>Standard Statistics Views</title>
229 <entry>View Name</entry>
230 <entry>Description</entry>
236 <entry><structname>pg_stat_activity</></entry>
237 <entry>One row per server process, showing database OID, database
238 name, process <acronym>ID</>, user OID, user name, application name,
239 current query, query's waiting status, time at which the current
240 transaction and current query began execution, time at which the
241 process was started, and client's address and port number.
242 The columns that report data on the current query are available unless
243 the parameter <varname>track_activities</varname> has been turned off.
244 Furthermore, these columns are only visible if the user examining
245 the view is a superuser or the same as the user owning the process
251 <entry><structname>pg_stat_bgwriter</></entry>
252 <entry>One row only, showing cluster-wide statistics from the
253 background writer: number of scheduled checkpoints, requested
254 checkpoints, buffers written by checkpoints and cleaning scans,
255 and the number of times the background writer stopped a cleaning scan
256 because it had written too many buffers. Also includes
257 statistics about the shared buffer pool, including buffers written
258 by backends (that is, not by the background writer) and total buffers
264 <entry><structname>pg_stat_database</></entry>
265 <entry>One row per database, showing database OID, database name,
266 number of active server processes connected to that database,
267 number of transactions committed and rolled back in that database,
268 total disk blocks read, total buffer hits (i.e., block
269 read requests avoided by finding the block already in buffer cache),
270 number of rows returned, fetched, inserted, updated and deleted.
275 <entry><structname>pg_stat_all_tables</></entry>
276 <entry>For each table in the current database (including TOAST tables),
277 the table OID, schema and table name, number of sequential
278 scans initiated, number of live rows fetched by sequential
279 scans, number of index scans initiated (over all indexes
280 belonging to the table), number of live rows fetched by index
281 scans, numbers of row insertions, updates, and deletions,
282 number of row updates that were HOT (i.e., no separate index update),
283 numbers of live and dead rows,
284 the last time the table was vacuumed manually,
285 the last time it was vacuumed by the autovacuum daemon,
286 the last time it was analyzed manually,
287 and the last time it was analyzed by the autovacuum daemon.
292 <entry><structname>pg_stat_sys_tables</></entry>
293 <entry>Same as <structname>pg_stat_all_tables</>, except that only
294 system tables are shown.</entry>
298 <entry><structname>pg_stat_user_tables</></entry>
299 <entry>Same as <structname>pg_stat_all_tables</>, except that only user
300 tables are shown.</entry>
304 <entry><structname>pg_stat_all_indexes</></entry>
305 <entry>For each index in the current database,
306 the table and index OID, schema, table and index name,
307 number of index scans initiated on that index, number of
308 index entries returned by index scans, and number of live table rows
309 fetched by simple index scans using that index.
314 <entry><structname>pg_stat_sys_indexes</></entry>
315 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
316 indexes on system tables are shown.</entry>
320 <entry><structname>pg_stat_user_indexes</></entry>
321 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
322 indexes on user tables are shown.</entry>
326 <entry><structname>pg_statio_all_tables</></entry>
327 <entry>For each table in the current database (including TOAST tables),
328 the table OID, schema and table name, number of disk
329 blocks read from that table, number of buffer hits, numbers of
330 disk blocks read and buffer hits in all indexes of that table,
331 numbers of disk blocks read and buffer hits from that table's
332 auxiliary TOAST table (if any), and numbers of disk blocks read
333 and buffer hits for the TOAST table's index.
338 <entry><structname>pg_statio_sys_tables</></entry>
339 <entry>Same as <structname>pg_statio_all_tables</>, except that only
340 system tables are shown.</entry>
344 <entry><structname>pg_statio_user_tables</></entry>
345 <entry>Same as <structname>pg_statio_all_tables</>, except that only
346 user tables are shown.</entry>
350 <entry><structname>pg_statio_all_indexes</></entry>
351 <entry>For each index in the current database,
352 the table and index OID, schema, table and index name,
353 numbers of disk blocks read and buffer hits in that index.
358 <entry><structname>pg_statio_sys_indexes</></entry>
359 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
360 indexes on system tables are shown.</entry>
364 <entry><structname>pg_statio_user_indexes</></entry>
365 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
366 indexes on user tables are shown.</entry>
370 <entry><structname>pg_statio_all_sequences</></entry>
371 <entry>For each sequence object in the current database,
372 the sequence OID, schema and sequence name,
373 numbers of disk blocks read and buffer hits in that sequence.
378 <entry><structname>pg_statio_sys_sequences</></entry>
379 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
380 system sequences are shown. (Presently, no system sequences are defined,
381 so this view is always empty.)</entry>
385 <entry><structname>pg_statio_user_sequences</></entry>
386 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
387 user sequences are shown.</entry>
391 <entry><structname>pg_stat_user_functions</></entry>
392 <entry>For all tracked functions, function OID, schema, name, number
393 of calls, total time, and self time. Self time is the
394 amount of time spent in the function itself, total time includes the
395 time spent in functions it called. Time values are in milliseconds.
404 The per-index statistics are particularly useful to determine which
405 indexes are being used and how effective they are.
409 Beginning in <productname>PostgreSQL</productname> 8.1, indexes can be
410 used either directly or via <quote>bitmap scans</>. In a bitmap scan
411 the output of several indexes can be combined via AND or OR rules;
412 so it is difficult to associate individual heap row fetches
413 with specific indexes when a bitmap scan is used. Therefore, a bitmap
415 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
416 count(s) for the index(es) it uses, and it increments the
417 <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
418 count for the table, but it does not affect
419 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
424 Before <productname>PostgreSQL</productname> 8.1, the
425 <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
426 were essentially always equal. Now they can be different even without
427 considering bitmap scans, because <structfield>idx_tup_read</> counts
428 index entries retrieved from the index while <structfield>idx_tup_fetch</>
429 counts live rows fetched from the table; the latter will be less if any
430 dead or not-yet-committed rows are fetched using the index.
435 The <structname>pg_statio_</> views are primarily useful to
436 determine the effectiveness of the buffer cache. When the number
437 of actual disk reads is much smaller than the number of buffer
438 hits, then the cache is satisfying most read requests without
439 invoking a kernel call. However, these statistics do not give the
440 entire story: due to the way in which <productname>PostgreSQL</>
441 handles disk I/O, data that is not in the
442 <productname>PostgreSQL</> buffer cache might still reside in the
443 kernel's I/O cache, and might therefore still be fetched without
444 requiring a physical read. Users interested in obtaining more
445 detailed information on <productname>PostgreSQL</> I/O behavior are
446 advised to use the <productname>PostgreSQL</> statistics collector
447 in combination with operating system utilities that allow insight
448 into the kernel's handling of I/O.
452 Other ways of looking at the statistics can be set up by writing
453 queries that use the same underlying statistics access functions as
454 these standard views do. These functions are listed in <xref
455 linkend="monitoring-stats-funcs-table">. The per-database access
456 functions take a database OID as argument to identify which
457 database to report on. The per-table and per-index functions take
458 a table or index OID. The functions for function-call statistics
459 take a function OID. (Note that only tables, indexes, and functions
460 in the current database can be seen with these functions.) The
461 per-server-process access functions take a server process
462 number, which ranges from one to the number of currently active
466 <table id="monitoring-stats-funcs-table">
467 <title>Statistics Access Functions</title>
472 <entry>Function</entry>
473 <entry>Return Type</entry>
474 <entry>Description</entry>
480 <entry><literal><function>pg_stat_get_db_numbackends</function>(<type>oid</type>)</literal></entry>
481 <entry><type>integer</type></entry>
483 Number of active server processes for database
488 <entry><literal><function>pg_stat_get_db_xact_commit</function>(<type>oid</type>)</literal></entry>
489 <entry><type>bigint</type></entry>
491 Transactions committed in database
496 <entry><literal><function>pg_stat_get_db_xact_rollback</function>(<type>oid</type>)</literal></entry>
497 <entry><type>bigint</type></entry>
499 Transactions rolled back in database
504 <entry><literal><function>pg_stat_get_db_blocks_fetched</function>(<type>oid</type>)</literal></entry>
505 <entry><type>bigint</type></entry>
507 Number of disk block fetch requests for database
512 <entry><literal><function>pg_stat_get_db_blocks_hit</function>(<type>oid</type>)</literal></entry>
513 <entry><type>bigint</type></entry>
515 Number of disk block fetch requests found in cache for database
520 <entry><literal><function>pg_stat_get_db_tuples_returned</function>(<type>oid</type>)</literal></entry>
521 <entry><type>bigint</type></entry>
523 Number of tuples returned for database
528 <entry><literal><function>pg_stat_get_db_tuples_fetched</function>(<type>oid</type>)</literal></entry>
529 <entry><type>bigint</type></entry>
531 Number of tuples fetched for database
536 <entry><literal><function>pg_stat_get_db_tuples_inserted</function>(<type>oid</type>)</literal></entry>
537 <entry><type>bigint</type></entry>
539 Number of tuples inserted in database
544 <entry><literal><function>pg_stat_get_db_tuples_updated</function>(<type>oid</type>)</literal></entry>
545 <entry><type>bigint</type></entry>
547 Number of tuples updated in database
552 <entry><literal><function>pg_stat_get_db_tuples_deleted</function>(<type>oid</type>)</literal></entry>
553 <entry><type>bigint</type></entry>
555 Number of tuples deleted in database
560 <entry><literal><function>pg_stat_get_numscans</function>(<type>oid</type>)</literal></entry>
561 <entry><type>bigint</type></entry>
563 Number of sequential scans done when argument is a table,
564 or number of index scans done when argument is an index
569 <entry><literal><function>pg_stat_get_tuples_returned</function>(<type>oid</type>)</literal></entry>
570 <entry><type>bigint</type></entry>
572 Number of rows read by sequential scans when argument is a table,
573 or number of index entries returned when argument is an index
578 <entry><literal><function>pg_stat_get_tuples_fetched</function>(<type>oid</type>)</literal></entry>
579 <entry><type>bigint</type></entry>
581 Number of table rows fetched by bitmap scans when argument is a table,
582 or table rows fetched by simple index scans using the index
583 when argument is an index
588 <entry><literal><function>pg_stat_get_tuples_inserted</function>(<type>oid</type>)</literal></entry>
589 <entry><type>bigint</type></entry>
591 Number of rows inserted into table
596 <entry><literal><function>pg_stat_get_tuples_updated</function>(<type>oid</type>)</literal></entry>
597 <entry><type>bigint</type></entry>
599 Number of rows updated in table (includes HOT updates)
604 <entry><literal><function>pg_stat_get_tuples_deleted</function>(<type>oid</type>)</literal></entry>
605 <entry><type>bigint</type></entry>
607 Number of rows deleted from table
612 <entry><literal><function>pg_stat_get_tuples_hot_updated</function>(<type>oid</type>)</literal></entry>
613 <entry><type>bigint</type></entry>
615 Number of rows HOT-updated in table
620 <entry><literal><function>pg_stat_get_live_tuples</function>(<type>oid</type>)</literal></entry>
621 <entry><type>bigint</type></entry>
623 Number of live rows in table
628 <entry><literal><function>pg_stat_get_dead_tuples</function>(<type>oid</type>)</literal></entry>
629 <entry><type>bigint</type></entry>
631 Number of dead rows in table
636 <entry><literal><function>pg_stat_get_blocks_fetched</function>(<type>oid</type>)</literal></entry>
637 <entry><type>bigint</type></entry>
639 Number of disk block fetch requests for table or index
644 <entry><literal><function>pg_stat_get_blocks_hit</function>(<type>oid</type>)</literal></entry>
645 <entry><type>bigint</type></entry>
647 Number of disk block requests found in cache for table or index
652 <entry><literal><function>pg_stat_get_last_vacuum_time</function>(<type>oid</type>)</literal></entry>
653 <entry><type>timestamptz</type></entry>
655 Time of the last vacuum initiated by the user on this table
660 <entry><literal><function>pg_stat_get_last_autovacuum_time</function>(<type>oid</type>)</literal></entry>
661 <entry><type>timestamptz</type></entry>
663 Time of the last vacuum initiated by the autovacuum daemon on this table
668 <entry><literal><function>pg_stat_get_last_analyze_time</function>(<type>oid</type>)</literal></entry>
669 <entry><type>timestamptz</type></entry>
671 Time of the last analyze initiated by the user on this table
676 <entry><literal><function>pg_stat_get_last_autoanalyze_time</function>(<type>oid</type>)</literal></entry>
677 <entry><type>timestamptz</type></entry>
679 Time of the last analyze initiated by the autovacuum daemon on this
685 <!-- See also the entry for this in func.sgml -->
686 <entry><literal><function>pg_backend_pid</function>()</literal></entry>
687 <entry><type>integer</type></entry>
689 Process ID of the server process attached to the current session
694 <entry><literal><function>pg_stat_get_activity</function>(<type>integer</type>)</literal></entry>
695 <entry><type>setof record</type></entry>
697 Returns a record of information about the backend with the specified pid, or
698 one record for each active backend in the system if <symbol>NULL</symbol> is
699 specified. The fields returned are the same as in the
700 <structname>pg_stat_activity</structname> view
705 <entry><literal><function>pg_stat_get_function_calls</function>(<type>oid</type>)</literal></entry>
706 <entry><type>bigint</type></entry>
708 Number of times the function has been called.
713 <entry><literal><function>pg_stat_get_function_time</function>(<type>oid</type>)</literal></entry>
714 <entry><type>bigint</type></entry>
716 Total wall clock time spent in the function, in microseconds. Includes
717 the time spent in functions called by this one.
722 <entry><literal><function>pg_stat_get_function_self_time</function>(<type>oid</type>)</literal></entry>
723 <entry><type>bigint</type></entry>
725 Time spent in only this function. Time spent in called functions
731 <entry><literal><function>pg_stat_get_backend_idset</function>()</literal></entry>
732 <entry><type>setof integer</type></entry>
734 Set of currently active server process numbers (from 1 to the
735 number of active server processes). See usage example in the text
740 <entry><literal><function>pg_stat_get_backend_pid</function>(<type>integer</type>)</literal></entry>
741 <entry><type>integer</type></entry>
743 Process ID of the given server process
748 <entry><literal><function>pg_stat_get_backend_dbid</function>(<type>integer</type>)</literal></entry>
749 <entry><type>oid</type></entry>
751 Database ID of the given server process
756 <entry><literal><function>pg_stat_get_backend_userid</function>(<type>integer</type>)</literal></entry>
757 <entry><type>oid</type></entry>
759 User ID of the given server process
764 <entry><literal><function>pg_stat_get_backend_activity</function>(<type>integer</type>)</literal></entry>
765 <entry><type>text</type></entry>
767 Active command of the given server process, but only if the
768 current user is a superuser or the same user as that of
769 the session being queried (and
770 <varname>track_activities</varname> is on)
775 <entry><literal><function>pg_stat_get_backend_waiting</function>(<type>integer</type>)</literal></entry>
776 <entry><type>boolean</type></entry>
778 True if the given server process is waiting for a lock,
779 but only if the current user is a superuser or the same user as that of
780 the session being queried (and
781 <varname>track_activities</varname> is on)
786 <entry><literal><function>pg_stat_get_backend_activity_start</function>(<type>integer</type>)</literal></entry>
787 <entry><type>timestamp with time zone</type></entry>
789 The time at which the given server process' currently
790 executing query was started, but only if the
791 current user is a superuser or the same user as that of
792 the session being queried (and
793 <varname>track_activities</varname> is on)
798 <entry><literal><function>pg_stat_get_backend_xact_start</function>(<type>integer</type>)</literal></entry>
799 <entry><type>timestamp with time zone</type></entry>
801 The time at which the given server process' currently
802 executing transaction was started, but only if the
803 current user is a superuser or the same user as that of
804 the session being queried (and
805 <varname>track_activities</varname> is on)
810 <entry><literal><function>pg_stat_get_backend_start</function>(<type>integer</type>)</literal></entry>
811 <entry><type>timestamp with time zone</type></entry>
813 The time at which the given server process was started, or
814 null if the current user is not a superuser nor the same user
815 as that of the session being queried
820 <entry><literal><function>pg_stat_get_backend_client_addr</function>(<type>integer</type>)</literal></entry>
821 <entry><type>inet</type></entry>
823 The IP address of the client connected to the given
824 server process. Null if the connection is over a Unix domain
825 socket. Also null if the current user is not a superuser nor
826 the same user as that of the session being queried
831 <entry><literal><function>pg_stat_get_backend_client_port</function>(<type>integer</type>)</literal></entry>
832 <entry><type>integer</type></entry>
834 The IP port number of the client connected to the given
835 server process. -1 if the connection is over a Unix domain
836 socket. Null if the current user is not a superuser nor the
837 same user as that of the session being queried
842 <entry><literal><function>pg_stat_get_bgwriter_timed_checkpoints</function>()</literal></entry>
843 <entry><type>bigint</type></entry>
845 The number of times the background writer has started timed checkpoints
846 (because the <varname>checkpoint_timeout</varname> time has expired)
851 <entry><literal><function>pg_stat_get_bgwriter_requested_checkpoints</function>()</literal></entry>
852 <entry><type>bigint</type></entry>
854 The number of times the background writer has started checkpoints based
855 on requests from backends because the <varname>checkpoint_segments</varname>
856 has been exceeded or because the <command>CHECKPOINT</command>
857 command has been issued
862 <entry><literal><function>pg_stat_get_bgwriter_buf_written_checkpoints</function>()</literal></entry>
863 <entry><type>bigint</type></entry>
865 The number of buffers written by the background writer during checkpoints
870 <entry><literal><function>pg_stat_get_bgwriter_buf_written_clean</function>()</literal></entry>
871 <entry><type>bigint</type></entry>
873 The number of buffers written by the background writer for routine cleaning of
879 <entry><literal><function>pg_stat_get_bgwriter_maxwritten_clean</function>()</literal></entry>
880 <entry><type>bigint</type></entry>
882 The number of times the background writer has stopped its cleaning scan because
883 it has written more buffers than specified in the
884 <varname>bgwriter_lru_maxpages</varname> parameter
889 <entry><literal><function>pg_stat_get_buf_written_backend</function>()</literal></entry>
890 <entry><type>bigint</type></entry>
892 The number of buffers written by backends because they needed
893 to allocate a new buffer
898 <entry><literal><function>pg_stat_get_buf_alloc</function>()</literal></entry>
899 <entry><type>bigint</type></entry>
901 The total number of buffer allocations
906 <entry><literal><function>pg_stat_clear_snapshot</function>()</literal></entry>
907 <entry><type>void</type></entry>
909 Discard the current statistics snapshot
914 <entry><literal><function>pg_stat_reset</function>()</literal></entry>
915 <entry><type>void</type></entry>
917 Reset all statistics counters for the current database to zero
918 (requires superuser privileges)
923 <entry><literal><function>pg_stat_reset_shared</function>(text)</literal></entry>
924 <entry><type>void</type></entry>
926 Reset some of the shared statistics counters for the database cluster to
927 zero (requires superuser privileges). Calling
928 <literal>pg_stat_reset_shared('bgwriter')</> will zero all the values shown by
929 <structname>pg_stat_bgwriter</>.
934 <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal></entry>
935 <entry><type>void</type></entry>
937 Reset statistics for a single table or index in the current database to
938 zero (requires superuser privileges)
943 <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal></entry>
944 <entry><type>void</type></entry>
946 Reset statistics for a single function in the current database to
947 zero (requires superuser privileges)
956 <function>pg_stat_get_blocks_fetched</function> minus
957 <function>pg_stat_get_blocks_hit</function> gives the number of kernel
958 <function>read()</> calls issued for the table, index, or
959 database; the number of actual physical reads is usually
960 lower due to kernel-level buffering. The <literal>*_blks_read</>
961 statistics columns use this subtraction, i.e., fetched minus hit.
966 All functions to access information about backends are indexed by backend id
967 number, except <function>pg_stat_get_activity</function> which is indexed by PID.
968 The function <function>pg_stat_get_backend_idset</function> provides
969 a convenient way to generate one row for each active server process. For
970 example, to show the <acronym>PID</>s and current queries of all server processes:
973 SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
974 pg_stat_get_backend_activity(s.backendid) AS current_query
975 FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
982 <sect1 id="monitoring-locks">
983 <title>Viewing Locks</title>
985 <indexterm zone="monitoring-locks">
986 <primary>lock</primary>
987 <secondary>monitoring</secondary>
991 Another useful tool for monitoring database activity is the
992 <structname>pg_locks</structname> system table. It allows the
993 database administrator to view information about the outstanding
994 locks in the lock manager. For example, this capability can be used
1000 View all the locks currently outstanding, all the locks on
1001 relations in a particular database, all the locks on a
1002 particular relation, or all the locks held by a particular
1003 <productname>PostgreSQL</productname> session.
1009 Determine the relation in the current database with the most
1010 ungranted locks (which might be a source of contention among
1017 Determine the effect of lock contention on overall database
1018 performance, as well as the extent to which contention varies
1019 with overall database traffic.
1024 Details of the <structname>pg_locks</structname> view appear in
1025 <xref linkend="view-pg-locks">.
1026 For more information on locking and managing concurrency with
1027 <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
1031 <sect1 id="dynamic-trace">
1032 <title>Dynamic Tracing</title>
1034 <indexterm zone="dynamic-trace">
1035 <primary>DTrace</primary>
1039 <productname>PostgreSQL</productname> provides facilities to support
1040 dynamic tracing of the database server. This allows an external
1041 utility to be called at specific points in the code and thereby trace
1046 A number of probes or trace points are already inserted into the source
1047 code. These probes are intended to be used by database developers and
1048 administrators. By default the probes are not compiled into
1049 <productname>PostgreSQL</productname>; the user needs to explicitly tell
1050 the configure script to make the probes available.
1055 <ulink url="http://opensolaris.org/os/community/dtrace/">DTrace</ulink>
1056 utility is supported, which is available
1057 on OpenSolaris, Solaris 10, and Mac OS X Leopard. It is expected that
1058 DTrace will be available in the future on FreeBSD and possibly other
1059 operating systems. The
1060 <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> project
1061 for Linux also provides a DTrace equivalent. Supporting other dynamic
1062 tracing utilities is theoretically possible by changing the definitions for
1063 the macros in <filename>src/include/utils/probes.h</>.
1066 <sect2 id="compiling-for-trace">
1067 <title>Compiling for Dynamic Tracing</title>
1070 By default, probes are not available, so you will need to
1071 explicitly tell the configure script to make the probes available
1072 in <productname>PostgreSQL</productname>. To include DTrace support
1073 specify <option>--enable-dtrace</> to configure. See <xref
1074 linkend="install-procedure"> for further information.
1078 <sect2 id="trace-points">
1079 <title>Built-in Probes</title>
1082 A number of standard probes are provided in the source code,
1083 as shown in <xref linkend="dtrace-probe-point-table">.
1084 More can certainly be added to enhance <productname>PostgreSQL</>'s
1088 <table id="dtrace-probe-point-table">
1089 <title>Built-in DTrace Probes</title>
1094 <entry>Parameters</entry>
1095 <entry>Description</entry>
1102 <entry>transaction-start</entry>
1103 <entry>(LocalTransactionId)</entry>
1104 <entry>Probe that fires at the start of a new transaction.
1105 arg0 is the transaction id.</entry>
1108 <entry>transaction-commit</entry>
1109 <entry>(LocalTransactionId)</entry>
1110 <entry>Probe that fires when a transaction completes successfully.
1111 arg0 is the transaction id.</entry>
1114 <entry>transaction-abort</entry>
1115 <entry>(LocalTransactionId)</entry>
1116 <entry>Probe that fires when a transaction completes unsuccessfully.
1117 arg0 is the transaction id.</entry>
1120 <entry>query-start</entry>
1121 <entry>(const char *)</entry>
1122 <entry>Probe that fires when the processing of a query is started.
1123 arg0 is the query string.</entry>
1126 <entry>query-done</entry>
1127 <entry>(const char *)</entry>
1128 <entry>Probe that fires when the processing of a query is complete.
1129 arg0 is the query string.</entry>
1132 <entry>query-parse-start</entry>
1133 <entry>(const char *)</entry>
1134 <entry>Probe that fires when the parsing of a query is started.
1135 arg0 is the query string.</entry>
1138 <entry>query-parse-done</entry>
1139 <entry>(const char *)</entry>
1140 <entry>Probe that fires when the parsing of a query is complete.
1141 arg0 is the query string.</entry>
1144 <entry>query-rewrite-start</entry>
1145 <entry>(const char *)</entry>
1146 <entry>Probe that fires when the rewriting of a query is started.
1147 arg0 is the query string.</entry>
1150 <entry>query-rewrite-done</entry>
1151 <entry>(const char *)</entry>
1152 <entry>Probe that fires when the rewriting of a query is complete.
1153 arg0 is the query string.</entry>
1156 <entry>query-plan-start</entry>
1158 <entry>Probe that fires when the planning of a query is started.</entry>
1161 <entry>query-plan-done</entry>
1163 <entry>Probe that fires when the planning of a query is complete.</entry>
1166 <entry>query-execute-start</entry>
1168 <entry>Probe that fires when the execution of a query is started.</entry>
1171 <entry>query-execute-done</entry>
1173 <entry>Probe that fires when the execution of a query is complete.</entry>
1176 <entry>statement-status</entry>
1177 <entry>(const char *)</entry>
1178 <entry>Probe that fires anytime the server process updates its
1179 <structname>pg_stat_activity</>.<structfield>current_query</> status.
1180 arg0 is the new status string.</entry>
1183 <entry>checkpoint-start</entry>
1184 <entry>(int)</entry>
1185 <entry>Probe that fires when a checkpoint is started.
1186 arg0 holds the bitwise flags used to distinguish different checkpoint
1187 types, such as shutdown, immediate or force.</entry>
1190 <entry>checkpoint-done</entry>
1191 <entry>(int, int, int, int, int)</entry>
1192 <entry>Probe that fires when a checkpoint is complete.
1193 (The probes listed next fire in sequence during checkpoint processing.)
1194 arg0 is the number of buffers written. arg1 is the total number of
1195 buffers. arg2, arg3 and arg4 contain the number of xlog file(s) added,
1196 removed and recycled respectively.</entry>
1199 <entry>clog-checkpoint-start</entry>
1200 <entry>(bool)</entry>
1201 <entry>Probe that fires when the CLOG portion of a checkpoint is started.
1202 arg0 is true for normal checkpoint, false for shutdown
1206 <entry>clog-checkpoint-done</entry>
1207 <entry>(bool)</entry>
1208 <entry>Probe that fires when the CLOG portion of a checkpoint is
1209 complete. arg0 has the same meaning as for clog-checkpoint-start.</entry>
1212 <entry>subtrans-checkpoint-start</entry>
1213 <entry>(bool)</entry>
1214 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
1216 arg0 is true for normal checkpoint, false for shutdown
1220 <entry>subtrans-checkpoint-done</entry>
1221 <entry>(bool)</entry>
1222 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
1223 complete. arg0 has the same meaning as for
1224 subtrans-checkpoint-start.</entry>
1227 <entry>multixact-checkpoint-start</entry>
1228 <entry>(bool)</entry>
1229 <entry>Probe that fires when the MultiXact portion of a checkpoint is
1231 arg0 is true for normal checkpoint, false for shutdown
1235 <entry>multixact-checkpoint-done</entry>
1236 <entry>(bool)</entry>
1237 <entry>Probe that fires when the MultiXact portion of a checkpoint is
1238 complete. arg0 has the same meaning as for
1239 multixact-checkpoint-start.</entry>
1242 <entry>buffer-checkpoint-start</entry>
1243 <entry>(int)</entry>
1244 <entry>Probe that fires when the buffer-writing portion of a checkpoint
1246 arg0 holds the bitwise flags used to distinguish different checkpoint
1247 types, such as shutdown, immediate or force.</entry>
1250 <entry>buffer-sync-start</entry>
1251 <entry>(int, int)</entry>
1252 <entry>Probe that fires when we begin to write dirty buffers during
1253 checkpoint (after identifying which buffers must be written).
1254 arg0 is the total number of buffers.
1255 arg1 is the number that are currently dirty and need to be written.</entry>
1258 <entry>buffer-sync-written</entry>
1259 <entry>(int)</entry>
1260 <entry>Probe that fires after each buffer is written during checkpoint.
1261 arg0 is the ID number of the buffer.</entry>
1264 <entry>buffer-sync-done</entry>
1265 <entry>(int, int, int)</entry>
1266 <entry>Probe that fires when all dirty buffers have been written.
1267 arg0 is the total number of buffers.
1268 arg1 is the number of buffers actually written by the checkpoint process.
1269 arg2 is the number that were expected to be written (arg1 of
1270 buffer-sync-start); any difference reflects other processes flushing
1271 buffers during the checkpoint.</entry>
1274 <entry>buffer-checkpoint-sync-start</entry>
1276 <entry>Probe that fires after dirty buffers have been written to the
1277 kernel, and before starting to issue fsync requests.</entry>
1280 <entry>buffer-checkpoint-done</entry>
1282 <entry>Probe that fires when syncing of buffers to disk is
1286 <entry>twophase-checkpoint-start</entry>
1288 <entry>Probe that fires when the two-phase portion of a checkpoint is
1292 <entry>twophase-checkpoint-done</entry>
1294 <entry>Probe that fires when the two-phase portion of a checkpoint is
1298 <entry>buffer-read-start</entry>
1299 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, bool, bool)</entry>
1300 <entry>Probe that fires when a buffer read is started.
1301 arg0 and arg1 contain the fork and block numbers of the page (but
1302 arg1 will be -1 if this is a relation extension request).
1303 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1304 identifying the relation.
1305 arg5 is true for a local buffer, false for a shared buffer.
1306 arg6 is true for a relation extension request, false for normal
1310 <entry>buffer-read-done</entry>
1311 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, bool, bool, bool)</entry>
1312 <entry>Probe that fires when a buffer read is complete.
1313 arg0 and arg1 contain the fork and block numbers of the page (if this
1314 is a relation extension request, arg1 now contains the block number
1315 of the newly added block).
1316 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1317 identifying the relation.
1318 arg5 is true for a local buffer, false for a shared buffer.
1319 arg6 is true for a relation extension request, false for normal
1321 arg7 is true if the buffer was found in the pool, false if not.</entry>
1324 <entry>buffer-flush-start</entry>
1325 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1326 <entry>Probe that fires before issuing any write request for a shared
1328 arg0 and arg1 contain the fork and block numbers of the page.
1329 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1330 identifying the relation.</entry>
1333 <entry>buffer-flush-done</entry>
1334 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1335 <entry>Probe that fires when a write request is complete. (Note
1336 that this just reflects the time to pass the data to the kernel;
1337 it's typically not actually been written to disk yet.)
1338 The arguments are the same as for buffer-flush-start.</entry>
1341 <entry>buffer-write-dirty-start</entry>
1342 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1343 <entry>Probe that fires when a server process begins to write a dirty
1344 buffer. (If this happens often, it implies that
1345 <xref linkend="guc-shared-buffers"> is too
1346 small or the bgwriter control parameters need adjustment.)
1347 arg0 and arg1 contain the fork and block numbers of the page.
1348 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1349 identifying the relation.</entry>
1352 <entry>buffer-write-dirty-done</entry>
1353 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1354 <entry>Probe that fires when a dirty-buffer write is complete.
1355 The arguments are the same as for buffer-write-dirty-start.</entry>
1358 <entry>wal-buffer-write-dirty-start</entry>
1360 <entry>Probe that fires when when a server process begins to write a
1361 dirty WAL buffer because no more WAL buffer space is available.
1362 (If this happens often, it implies that
1363 <xref linkend="guc-wal-buffers"> is too small.)</entry>
1366 <entry>wal-buffer-write-dirty-done</entry>
1368 <entry>Probe that fires when a dirty WAL buffer write is complete.</entry>
1371 <entry>xlog-insert</entry>
1372 <entry>(unsigned char, unsigned char)</entry>
1373 <entry>Probe that fires when a WAL record is inserted.
1374 arg0 is the resource manager (rmid) for the record.
1375 arg1 contains the info flags.</entry>
1378 <entry>xlog-switch</entry>
1380 <entry>Probe that fires when a WAL segment switch is requested.</entry>
1383 <entry>smgr-md-read-start</entry>
1384 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1385 <entry>Probe that fires when beginning to read a block from a relation.
1386 arg0 and arg1 contain the fork and block numbers of the page.
1387 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1388 identifying the relation.</entry>
1391 <entry>smgr-md-read-done</entry>
1392 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int)</entry>
1393 <entry>Probe that fires when a block read is complete.
1394 arg0 and arg1 contain the fork and block numbers of the page.
1395 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1396 identifying the relation.
1397 arg5 is the number of bytes actually read, while arg6 is the number
1398 requested (if these are different it indicates trouble).</entry>
1401 <entry>smgr-md-write-start</entry>
1402 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
1403 <entry>Probe that fires when beginning to write a block to a relation.
1404 arg0 and arg1 contain the fork and block numbers of the page.
1405 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1406 identifying the relation.</entry>
1409 <entry>smgr-md-write-done</entry>
1410 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int)</entry>
1411 <entry>Probe that fires when a block write is complete.
1412 arg0 and arg1 contain the fork and block numbers of the page.
1413 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
1414 identifying the relation.
1415 arg5 is the number of bytes actually written, while arg6 is the number
1416 requested (if these are different it indicates trouble).</entry>
1419 <entry>sort-start</entry>
1420 <entry>(int, bool, int, int, bool)</entry>
1421 <entry>Probe that fires when a sort operation is started.
1422 arg0 indicates heap, index or datum sort.
1423 arg1 is true for unique-value enforcement.
1424 arg2 is the number of key columns.
1425 arg3 is the number of kilobytes of work memory allowed.
1426 arg4 is true if random access to the sort result is required.</entry>
1429 <entry>sort-done</entry>
1430 <entry>(bool, long)</entry>
1431 <entry>Probe that fires when a sort is complete.
1432 arg0 is true for external sort, false for internal sort.
1433 arg1 is the number of disk blocks used for an external sort,
1434 or kilobytes of memory used for an internal sort.</entry>
1437 <entry>lwlock-acquire</entry>
1438 <entry>(LWLockId, LWLockMode)</entry>
1439 <entry>Probe that fires when an LWLock has been acquired.
1440 arg0 is the LWLock's ID.
1441 arg1 is the requested lock mode, either exclusive or shared.</entry>
1444 <entry>lwlock-release</entry>
1445 <entry>(LWLockId)</entry>
1446 <entry>Probe that fires when an LWLock has been released (but note
1447 that any released waiters have not yet been awakened).
1448 arg0 is the LWLock's ID.</entry>
1451 <entry>lwlock-wait-start</entry>
1452 <entry>(LWLockId, LWLockMode)</entry>
1453 <entry>Probe that fires when an LWLock was not immediately available and
1454 a server process has begun to wait for the lock to become available.
1455 arg0 is the LWLock's ID.
1456 arg1 is the requested lock mode, either exclusive or shared.</entry>
1459 <entry>lwlock-wait-done</entry>
1460 <entry>(LWLockId, LWLockMode)</entry>
1461 <entry>Probe that fires when a server process has been released from its
1462 wait for an LWLock (it does not actually have the lock yet).
1463 arg0 is the LWLock's ID.
1464 arg1 is the requested lock mode, either exclusive or shared.</entry>
1467 <entry>lwlock-condacquire</entry>
1468 <entry>(LWLockId, LWLockMode)</entry>
1469 <entry>Probe that fires when an LWLock was successfully acquired when the
1470 caller specified no waiting.
1471 arg0 is the LWLock's ID.
1472 arg1 is the requested lock mode, either exclusive or shared.</entry>
1475 <entry>lwlock-condacquire-fail</entry>
1476 <entry>(LWLockId, LWLockMode)</entry>
1477 <entry>Probe that fires when an LWLock was not successfully acquired when
1478 the caller specified no waiting.
1479 arg0 is the LWLock's ID.
1480 arg1 is the requested lock mode, either exclusive or shared.</entry>
1483 <entry>lock-wait-start</entry>
1484 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
1485 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
1486 has begun to wait because the lock is not available.
1487 arg0 through arg3 are the tag fields identifying the object being
1488 locked. arg4 indicates the type of object being locked.
1489 arg5 indicates the lock type being requested.</entry>
1492 <entry>lock-wait-done</entry>
1493 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
1494 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
1495 has finished waiting (i.e., has acquired the lock).
1496 The arguments are the same as for lock-wait-start.</entry>
1499 <entry>deadlock-found</entry>
1501 <entry>Probe that fires when a deadlock is found by the deadlock
1509 <table id="typedefs-table">
1510 <title>Defined Types Used in Probe Parameters</title>
1515 <entry>Definition</entry>
1522 <entry>LocalTransactionId</entry>
1523 <entry>unsigned int</entry>
1526 <entry>LWLockId</entry>
1530 <entry>LWLockMode</entry>
1534 <entry>LOCKMODE</entry>
1538 <entry>BlockNumber</entry>
1539 <entry>unsigned int</entry>
1543 <entry>unsigned int</entry>
1546 <entry>ForkNumber</entry>
1561 <sect2 id="using-trace-points">
1562 <title>Using Probes</title>
1565 The example below shows a DTrace script for analyzing transaction
1566 counts in the system, as an alternative to snapshotting
1567 <structname>pg_stat_database</> before and after a performance test:
1569 #!/usr/sbin/dtrace -qs
1571 postgresql$1:::transaction-start
1573 @start["Start"] = count();
1574 self->ts = timestamp;
1577 postgresql$1:::transaction-abort
1579 @abort["Abort"] = count();
1582 postgresql$1:::transaction-commit
1585 @commit["Commit"] = count();
1586 @time["Total time (ns)"] = sum(timestamp - self->ts);
1590 When executed, the example D script gives output such as:
1592 # ./txn_count.d `pgrep -n postgres` or ./txn_count.d <PID>
1597 Total time (ns) 2312105013
1603 SystemTap uses a different notation for trace scripts than DTrace does,
1604 even though the underlying trace points are compatible. One point worth
1605 noting is that at this writing, SystemTap scripts must reference probe
1606 names using double underlines in place of hyphens. This is expected to
1607 be fixed in future SystemTap releases.
1612 You should remember that DTrace scripts need to be carefully written and
1613 debugged, otherwise the trace information collected might
1614 be meaningless. In most cases where problems are found it is the
1615 instrumentation that is at fault, not the underlying system. When
1616 discussing information found using dynamic tracing, be sure to enclose
1617 the script used to allow that too to be checked and discussed.
1621 More example scripts can be found in the PgFoundry
1622 <ulink url="http://pgfoundry.org/projects/dtrace/">dtrace project</ulink>.
1626 <sect2 id="defining-trace-points">
1627 <title>Defining New Probes</title>
1630 New probes can be defined within the code wherever the developer
1631 desires, though this will require a recompilation. Below are the steps
1632 for inserting new probes:
1638 Decide on probe names and data to be made available through the probes
1644 Add the probe definitions to <filename>src/backend/utils/probes.d</>
1650 Include <filename>pg_trace.h</> if it is not already present in the
1651 module(s) containing the probe points, and insert
1652 <literal>TRACE_POSTGRESQL</> probe macros at the desired locations
1659 Recompile and verify that the new probes are available
1665 <title>Example:</title>
1667 Here is an example of how you would add a probe to trace all new
1668 transactions by transaction ID.
1675 Decide that the probe will be named <literal>transaction-start</> and
1676 requires a parameter of type LocalTransactionId
1682 Add the probe definition to <filename>src/backend/utils/probes.d</>:
1684 probe transaction__start(LocalTransactionId);
1686 Note the use of the double underline in the probe name. In a DTrace
1687 script using the probe, the double underline needs to be replaced with a
1688 hyphen, so <literal>transaction-start</> is the name to document for
1695 At compile time, <literal>transaction__start</> is converted to a macro
1696 called <literal>TRACE_POSTGRESQL_TRANSACTION_START</> (notice the
1697 underscores are single here), which is available by including
1698 <filename>pg_trace.h</>. Add the macro call to the appropriate location
1699 in the source code. In this case, it looks like the following:
1702 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1709 After recompiling and running the new binary, check that your newly added
1710 probe is available by executing the following DTrace command. You
1711 should see similar output:
1713 # dtrace -ln transaction-start
1714 ID PROVIDER MODULE FUNCTION NAME
1715 18705 postgresql49878 postgres StartTransactionCommand transaction-start
1716 18755 postgresql49877 postgres StartTransactionCommand transaction-start
1717 18805 postgresql49876 postgres StartTransactionCommand transaction-start
1718 18855 postgresql49875 postgres StartTransactionCommand transaction-start
1719 18986 postgresql49873 postgres StartTransactionCommand transaction-start
1726 There are a few things to be careful about when adding trace macros
1732 You should take care that the data types specified for a probe's
1733 parameters match the data types of the variables used in the macro.
1734 Otherwise, you will get compilation errors.
1741 On most platforms, if <productname>PostgreSQL</productname> is
1742 built with <option>--enable-dtrace</>, the arguments to a trace
1743 macro will be evaluated whenever control passes through the
1744 macro, <emphasis>even if no tracing is being done</>. This is
1745 usually not worth worrying about if you are just reporting the
1746 values of a few local variables. But beware of putting expensive
1747 function calls into the arguments. If you need to do that,
1748 consider protecting the macro with a check to see if the trace
1749 is actually enabled:
1752 if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
1753 TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
1756 Each trace macro has a corresponding <literal>ENABLED</> macro.