1 <!-- doc/src/sgml/monitoring.sgml -->
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 15551 0.0 0.1 57536 7132 pts/0 S 18:02 0:00 postgres -i
52 postgres 15554 0.0 0.0 57536 1184 ? Ss 18:02 0:00 postgres: writer process
53 postgres 15555 0.0 0.0 57536 916 ? Ss 18:02 0:00 postgres: checkpointer process
54 postgres 15556 0.0 0.0 57536 916 ? Ss 18:02 0:00 postgres: wal writer process
55 postgres 15557 0.0 0.0 58504 2244 ? Ss 18:02 0:00 postgres: autovacuum launcher process
56 postgres 15558 0.0 0.0 17512 1068 ? Ss 18:02 0:00 postgres: stats collector process
57 postgres 15582 0.0 0.0 58772 3080 ? Ss 18:04 0:00 postgres: joe runbug 127.0.0.1 idle
58 postgres 15606 0.0 0.0 58772 3052 ? Ss 18:07 0:00 postgres: tgl regression [local] SELECT waiting
59 postgres 15610 0.0 0.0 58772 3056 ? Ss 18:07 0:00 postgres: tgl regression [local] idle in transaction
62 (The appropriate invocation of <command>ps</> varies across different
63 platforms, as do the details of what is shown. This example is from a
64 recent Linux system.) The first process listed here is the
65 master server process. The command arguments
66 shown for it are the same ones used when it was launched. The next five
67 processes are background worker processes automatically launched by the
68 master process. (The <quote>stats collector</> process will not be present
69 if you have set the system not to start the statistics collector; likewise
70 the <quote>autovacuum launcher</> process can be disabled.)
72 processes is a server process handling one client connection. Each such
73 process sets its command line display in the form
76 postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <replaceable>activity</>
79 The user, database, and (client) host items remain the same for
80 the life of the client connection, but the activity indicator changes.
81 The activity can be <literal>idle</> (i.e., waiting for a client command),
82 <literal>idle in transaction</> (waiting for client inside a <command>BEGIN</> block),
83 or a command type name such as <literal>SELECT</>. Also,
84 <literal>waiting</> is appended if the server process is presently waiting
85 on a lock held by another session. In the above example we can infer
86 that process 15606 is waiting for process 15610 to complete its transaction
87 and thereby release some lock. (Process 15610 must be the blocker, because
88 there is no other active session. In more complicated cases it would be
89 necessary to look into the
90 <link linkend="view-pg-locks"><structname>pg_locks</structname></link>
91 system view to determine who is blocking whom.)
95 If you have turned off <xref linkend="guc-update-process-title"> then the
96 activity indicator is not updated; the process title is set only once
97 when a new process is launched. On some platforms this saves a measurable
98 amount of per-command overhead; on others it's insignificant.
103 <productname>Solaris</productname> requires special handling. You must
104 use <command>/usr/ucb/ps</command>, rather than
105 <command>/bin/ps</command>. You also must use two <option>w</option>
106 flags, not just one. In addition, your original invocation of the
107 <command>postgres</command> command must have a shorter
108 <command>ps</command> status display than that provided by each
109 server process. If you fail to do all three things, the <command>ps</>
110 output for each server process will be the original <command>postgres</>
116 <sect1 id="monitoring-stats">
117 <title>The Statistics Collector</title>
119 <indexterm zone="monitoring-stats">
120 <primary>statistics</primary>
124 <productname>PostgreSQL</productname>'s <firstterm>statistics collector</>
125 is a subsystem that supports collection and reporting of information about
126 server activity. Presently, the collector can count accesses to tables
127 and indexes in both disk-block and individual-row terms. It also tracks
128 the total number of rows in each table, and information about vacuum and
129 analyze actions for each table. It can also count calls to user-defined
130 functions and the total time spent in each one.
134 <productname>PostgreSQL</productname> also supports reporting of the exact
135 command currently being executed by other server processes. This
136 facility is independent of the collector process.
139 <sect2 id="monitoring-stats-setup">
140 <title>Statistics Collection Configuration</title>
143 Since collection of statistics adds some overhead to query execution,
144 the system can be configured to collect or not collect information.
145 This is controlled by configuration parameters that are normally set in
146 <filename>postgresql.conf</>. (See <xref linkend="runtime-config"> for
147 details about setting configuration parameters.)
151 The parameter <xref linkend="guc-track-activities"> enables monitoring
152 of the current command being executed by any server process.
156 The parameter <xref linkend="guc-track-counts"> controls whether
157 statistics are collected about table and index accesses.
161 The parameter <xref linkend="guc-track-functions"> enables tracking of
162 usage of user-defined functions.
166 The parameter <xref linkend="guc-track-io-timing"> enables monitoring
167 of block read and write times.
171 Normally these parameters are set in <filename>postgresql.conf</> so
172 that they apply to all server processes, but it is possible to turn
173 them on or off in individual sessions using the <xref
174 linkend="sql-set"> command. (To prevent
175 ordinary users from hiding their activity from the administrator,
176 only superusers are allowed to change these parameters with
181 The statistics collector transmits the collected information to other
182 <productname>PostgreSQL</productname> processes through temporary files.
183 These files are stored in the directory named by the
184 <xref linkend="guc-stats-temp-directory"> parameter,
185 <filename>pg_stat_tmp</filename> by default.
186 For better performance, <varname>stats_temp_directory</> can be
187 pointed at a RAM-based file system, decreasing physical I/O requirements.
188 When the server shuts down cleanly, a permanent copy of the statistics
189 data is stored in the <filename>pg_stat</filename> subdirectory, so that
190 statistics can be retained across server restarts. When recovery is
191 performed at server start (e.g. after immediate shutdown, server crash,
192 and point-in-time recovery), all statistics counters are reset.
197 <sect2 id="monitoring-stats-views">
198 <title>Viewing Collected Statistics</title>
201 Several predefined views, listed in <xref
202 linkend="monitoring-stats-views-table">, are available to show the results
203 of statistics collection. Alternatively, one can
204 build custom views using the underlying statistics functions, as discussed
205 in <xref linkend="monitoring-stats-functions">.
209 When using the statistics to monitor current activity, it is important
210 to realize that the information does not update instantaneously.
211 Each individual server process transmits new statistical counts to
212 the collector just before going idle; so a query or transaction still in
213 progress does not affect the displayed totals. Also, the collector itself
214 emits a new report at most once per <varname>PGSTAT_STAT_INTERVAL</varname>
215 milliseconds (500 ms unless altered while building the server). So the
216 displayed information lags behind actual activity. However, current-query
217 information collected by <varname>track_activities</varname> is
222 Another important point is that when a server process is asked to display
223 any of these statistics, it first fetches the most recent report emitted by
224 the collector process and then continues to use this snapshot for all
225 statistical views and functions until the end of its current transaction.
226 So the statistics will show static information as long as you continue the
227 current transaction. Similarly, information about the current queries of
228 all sessions is collected when any such information is first requested
229 within a transaction, and the same information will be displayed throughout
231 This is a feature, not a bug, because it allows you to perform several
232 queries on the statistics and correlate the results without worrying that
233 the numbers are changing underneath you. But if you want to see new
234 results with each query, be sure to do the queries outside any transaction
235 block. Alternatively, you can invoke
236 <function>pg_stat_clear_snapshot</function>(), which will discard the
237 current transaction's statistics snapshot (if any). The next use of
238 statistical information will cause a new snapshot to be fetched.
242 A transaction can also see its own statistics (as yet untransmitted to the
243 collector) in the views <structname>pg_stat_xact_all_tables</>,
244 <structname>pg_stat_xact_sys_tables</>,
245 <structname>pg_stat_xact_user_tables</>, and
246 <structname>pg_stat_xact_user_functions</>. These numbers do not act as
247 stated above; instead they update continuously throughout the transaction.
250 <table id="monitoring-stats-views-table">
251 <title>Standard Statistics Views</title>
256 <entry>View Name</entry>
257 <entry>Description</entry>
264 <structname>pg_stat_activity</structname>
265 <indexterm><primary>pg_stat_activity</primary></indexterm>
268 One row per server process, showing information related to
269 the current activity of that process, such as state and current query.
270 See <xref linkend="pg-stat-activity-view"> for details.
275 <entry><structname>pg_stat_archiver</><indexterm><primary>pg_stat_archiver</primary></indexterm></entry>
276 <entry>One row only, showing statistics about the
277 WAL archiver process's activity. See
278 <xref linkend="pg-stat-archiver-view"> for details.
283 <entry><structname>pg_stat_bgwriter</><indexterm><primary>pg_stat_bgwriter</primary></indexterm></entry>
284 <entry>One row only, showing statistics about the
285 background writer process's activity. See
286 <xref linkend="pg-stat-bgwriter-view"> for details.
291 <entry><structname>pg_stat_database</><indexterm><primary>pg_stat_database</primary></indexterm></entry>
292 <entry>One row per database, showing database-wide statistics. See
293 <xref linkend="pg-stat-database-view"> for details.
298 <entry><structname>pg_stat_all_tables</><indexterm><primary>pg_stat_all_tables</primary></indexterm></entry>
300 One row for each table in the current database, showing statistics
301 about accesses to that specific table.
302 See <xref linkend="pg-stat-all-tables-view"> for details.
307 <entry><structname>pg_stat_sys_tables</><indexterm><primary>pg_stat_sys_tables</primary></indexterm></entry>
308 <entry>Same as <structname>pg_stat_all_tables</>, except that only
309 system tables are shown.</entry>
313 <entry><structname>pg_stat_user_tables</><indexterm><primary>pg_stat_user_tables</primary></indexterm></entry>
314 <entry>Same as <structname>pg_stat_all_tables</>, except that only user
315 tables are shown.</entry>
319 <entry><structname>pg_stat_xact_all_tables</><indexterm><primary>pg_stat_xact_all_tables</primary></indexterm></entry>
320 <entry>Similar to <structname>pg_stat_all_tables</>, but counts actions
321 taken so far within the current transaction (which are <emphasis>not</>
322 yet included in <structname>pg_stat_all_tables</> and related views).
323 The columns for numbers of live and dead rows and vacuum and
324 analyze actions are not present in this view.</entry>
328 <entry><structname>pg_stat_xact_sys_tables</><indexterm><primary>pg_stat_xact_sys_tables</primary></indexterm></entry>
329 <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
330 system tables are shown.</entry>
334 <entry><structname>pg_stat_xact_user_tables</><indexterm><primary>pg_stat_xact_user_tables</primary></indexterm></entry>
335 <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
336 user tables are shown.</entry>
340 <entry><structname>pg_stat_all_indexes</><indexterm><primary>pg_stat_all_indexes</primary></indexterm></entry>
342 One row for each index in the current database, showing statistics
343 about accesses to that specific index.
344 See <xref linkend="pg-stat-all-indexes-view"> for details.
349 <entry><structname>pg_stat_sys_indexes</><indexterm><primary>pg_stat_sys_indexes</primary></indexterm></entry>
350 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
351 indexes on system tables are shown.</entry>
355 <entry><structname>pg_stat_user_indexes</><indexterm><primary>pg_stat_user_indexes</primary></indexterm></entry>
356 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
357 indexes on user tables are shown.</entry>
361 <entry><structname>pg_statio_all_tables</><indexterm><primary>pg_statio_all_tables</primary></indexterm></entry>
363 One row for each table in the current database, showing statistics
364 about I/O on that specific table.
365 See <xref linkend="pg-statio-all-tables-view"> for details.
370 <entry><structname>pg_statio_sys_tables</><indexterm><primary>pg_statio_sys_tables</primary></indexterm></entry>
371 <entry>Same as <structname>pg_statio_all_tables</>, except that only
372 system tables are shown.</entry>
376 <entry><structname>pg_statio_user_tables</><indexterm><primary>pg_statio_user_tables</primary></indexterm></entry>
377 <entry>Same as <structname>pg_statio_all_tables</>, except that only
378 user tables are shown.</entry>
382 <entry><structname>pg_statio_all_indexes</><indexterm><primary>pg_statio_all_indexes</primary></indexterm></entry>
384 One row for each index in the current database,
385 showing statistics about I/O on that specific index.
386 See <xref linkend="pg-statio-all-indexes-view"> for details.
391 <entry><structname>pg_statio_sys_indexes</><indexterm><primary>pg_statio_sys_indexes</primary></indexterm></entry>
392 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
393 indexes on system tables are shown.</entry>
397 <entry><structname>pg_statio_user_indexes</><indexterm><primary>pg_statio_user_indexes</primary></indexterm></entry>
398 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
399 indexes on user tables are shown.</entry>
403 <entry><structname>pg_statio_all_sequences</><indexterm><primary>pg_statio_all_sequences</primary></indexterm></entry>
405 One row for each sequence in the current database,
406 showing statistics about I/O on that specific sequence.
407 See <xref linkend="pg-statio-all-sequences-view"> for details.
412 <entry><structname>pg_statio_sys_sequences</><indexterm><primary>pg_statio_sys_sequences</primary></indexterm></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</><indexterm><primary>pg_statio_user_sequences</primary></indexterm></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</><indexterm><primary>pg_stat_user_functions</primary></indexterm></entry>
427 One row for each tracked function, showing statistics
428 about executions of that function. See
429 <xref linkend="pg-stat-user-functions-view"> for details.
434 <entry><structname>pg_stat_xact_user_functions</><indexterm><primary>pg_stat_xact_user_functions</primary></indexterm></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>
441 <entry><structname>pg_stat_replication</><indexterm><primary>pg_stat_replication</primary></indexterm></entry>
442 <entry>One row per WAL sender process, showing statistics about
443 replication to that sender's connected standby server.
444 See <xref linkend="pg-stat-replication-view"> for details.
449 <entry><structname>pg_stat_database_conflicts</><indexterm><primary>pg_stat_database_conflicts</primary></indexterm></entry>
451 One row per database, showing database-wide statistics about
452 query cancels due to conflict with recovery on standby servers.
453 See <xref linkend="pg-stat-database-conflicts-view"> for details.
462 The per-index statistics are particularly useful to determine which
463 indexes are being used and how effective they are.
467 The <structname>pg_statio_</> views are primarily useful to
468 determine the effectiveness of the buffer cache. When the number
469 of actual disk reads is much smaller than the number of buffer
470 hits, then the cache is satisfying most read requests without
471 invoking a kernel call. However, these statistics do not give the
472 entire story: due to the way in which <productname>PostgreSQL</>
473 handles disk I/O, data that is not in the
474 <productname>PostgreSQL</> buffer cache might still reside in the
475 kernel's I/O cache, and might therefore still be fetched without
476 requiring a physical read. Users interested in obtaining more
477 detailed information on <productname>PostgreSQL</> I/O behavior are
478 advised to use the <productname>PostgreSQL</> statistics collector
479 in combination with operating system utilities that allow insight
480 into the kernel's handling of I/O.
484 <table id="pg-stat-activity-view" xreflabel="pg_stat_activity">
485 <title><structname>pg_stat_activity</structname> View</title>
490 <entry>Column</entry>
492 <entry>Description</entry>
498 <entry><structfield>datid</></entry>
499 <entry><type>oid</></entry>
500 <entry>OID of the database this backend is connected to</entry>
503 <entry><structfield>datname</></entry>
504 <entry><type>name</></entry>
505 <entry>Name of the database this backend is connected to</entry>
508 <entry><structfield>pid</></entry>
509 <entry><type>integer</></entry>
510 <entry>Process ID of this backend</entry>
513 <entry><structfield>usesysid</></entry>
514 <entry><type>oid</></entry>
515 <entry>OID of the user logged into this backend</entry>
518 <entry><structfield>usename</></entry>
519 <entry><type>name</></entry>
520 <entry>Name of the user logged into this backend</entry>
523 <entry><structfield>application_name</></entry>
524 <entry><type>text</></entry>
525 <entry>Name of the application that is connected
526 to this backend</entry>
529 <entry><structfield>client_addr</></entry>
530 <entry><type>inet</></entry>
531 <entry>IP address of the client connected to this backend.
532 If this field is null, it indicates either that the client is
533 connected via a Unix socket on the server machine or that this is an
534 internal process such as autovacuum.
538 <entry><structfield>client_hostname</></entry>
539 <entry><type>text</></entry>
540 <entry>Host name of the connected client, as reported by a
541 reverse DNS lookup of <structfield>client_addr</>. This field will
542 only be non-null for IP connections, and only when <xref
543 linkend="guc-log-hostname"> is enabled.
547 <entry><structfield>client_port</></entry>
548 <entry><type>integer</></entry>
549 <entry>TCP port number that the client is using for communication
550 with this backend, or <literal>-1</> if a Unix socket is used
554 <entry><structfield>backend_start</></entry>
555 <entry><type>timestamp with time zone</></entry>
556 <entry>Time when this process was started, i.e., when the
557 client connected to the server
561 <entry><structfield>xact_start</></entry>
562 <entry><type>timestamp with time zone</></entry>
563 <entry>Time when this process' current transaction was started, or null
564 if no transaction is active. If the current
565 query is the first of its transaction, this column is equal to the
566 <structfield>query_start</> column.
570 <entry><structfield>query_start</></entry>
571 <entry><type>timestamp with time zone</></entry>
572 <entry>Time when the currently active query was started, or if
573 <structfield>state</> is not <literal>active</>, when the last query
578 <entry><structfield>state_change</></entry>
579 <entry><type>timestamp with time zone</></entry>
580 <entry>Time when the <structfield>state</> was last changed</entry>
583 <entry><structfield>waiting</></entry>
584 <entry><type>boolean</></entry>
585 <entry>True if this backend is currently waiting on a lock</entry>
588 <entry><structfield>state</></entry>
589 <entry><type>text</></entry>
590 <entry>Current overall state of this backend.
595 <literal>active</>: The backend is executing a query.
600 <literal>idle</>: The backend is waiting for a new client command.
605 <literal>idle in transaction</>: The backend is in a transaction,
606 but is not currently executing a query.
611 <literal>idle in transaction (aborted)</>: This state is similar to
612 <literal>idle in transaction</>, except one of the statements in
613 the transaction caused an error.
618 <literal>fastpath function call</>: The backend is executing a
624 <literal>disabled</>: This state is reported if <xref
625 linkend="guc-track-activities"> is disabled in this backend.
632 <entry><structfield>backend_xid</structfield></entry>
633 <entry><type>xid</type></entry>
634 <entry>Toplevel transaction identifier of this backend, if any.</entry>
637 <entry><structfield>backend_xmin</structfield></entry>
638 <entry><type>xid</type></entry>
639 <entry>The current backend's <literal>xmin</> horizon.</entry>
642 <entry><structfield>query</></entry>
643 <entry><type>text</></entry>
644 <entry>Text of this backend's most recent query. If
645 <structfield>state</> is <literal>active</> this field shows the
646 currently executing query. In all other states, it shows the last query
655 The <structname>pg_stat_activity</structname> view will have one row
656 per server process, showing information related to
657 the current activity of that process.
662 The <structfield>waiting</> and <structfield>state</> columns are
663 independent. If a backend is in the <literal>active</> state,
664 it may or may not be <literal>waiting</>. If the state is
665 <literal>active</> and <structfield>waiting</> is true, it means
666 that a query is being executed, but is being blocked by a lock
667 somewhere in the system.
671 <table id="pg-stat-archiver-view" xreflabel="pg_stat_archiver">
672 <title><structname>pg_stat_archiver</structname> View</title>
677 <entry>Column</entry>
679 <entry>Description</entry>
685 <entry><structfield>archived_count</></entry>
686 <entry><type>bigint</type></entry>
687 <entry>Number of WAL files that have been successfully archived</entry>
690 <entry><structfield>last_archived_wal</></entry>
691 <entry><type>text</type></entry>
692 <entry>Name of the last WAL file successfully archived</entry>
695 <entry><structfield>last_archived_time</></entry>
696 <entry><type>timestamp with time zone</type></entry>
697 <entry>Time of the last successful archive operation</entry>
700 <entry><structfield>failed_count</></entry>
701 <entry><type>bigint</type></entry>
702 <entry>Number of failed attempts for archiving WAL files</entry>
705 <entry><structfield>last_failed_wal</></entry>
706 <entry><type>text</type></entry>
707 <entry>Name of the WAL file of the last failed archival operation</entry>
710 <entry><structfield>last_failed_time</></entry>
711 <entry><type>timestamp with time zone</type></entry>
712 <entry>Time of the last failed archival operation</entry>
715 <entry><structfield>stats_reset</></entry>
716 <entry><type>timestamp with time zone</type></entry>
717 <entry>Time at which these statistics were last reset</entry>
724 The <structname>pg_stat_archiver</structname> view will always have a
725 single row, containing data about the archiver process of the cluster.
728 <table id="pg-stat-bgwriter-view" xreflabel="pg_stat_bgwriter">
729 <title><structname>pg_stat_bgwriter</structname> View</title>
734 <entry>Column</entry>
736 <entry>Description</entry>
742 <entry><structfield>checkpoints_timed</></entry>
743 <entry><type>bigint</type></entry>
744 <entry>Number of scheduled checkpoints that have been performed</entry>
747 <entry><structfield>checkpoints_req</></entry>
748 <entry><type>bigint</type></entry>
749 <entry>Number of requested checkpoints that have been performed</entry>
752 <entry><structfield>checkpoint_write_time</></entry>
753 <entry><type>double precision</type></entry>
755 Total amount of time that has been spent in the portion of
756 checkpoint processing where files are written to disk, in milliseconds
760 <entry><structfield>checkpoint_sync_time</></entry>
761 <entry><type>double precision</type></entry>
763 Total amount of time that has been spent in the portion of
764 checkpoint processing where files are synchronized to disk, in
769 <entry><structfield>buffers_checkpoint</></entry>
770 <entry><type>bigint</type></entry>
771 <entry>Number of buffers written during checkpoints</entry>
774 <entry><structfield>buffers_clean</></entry>
775 <entry><type>bigint</type></entry>
776 <entry>Number of buffers written by the background writer</entry>
779 <entry><structfield>maxwritten_clean</></entry>
780 <entry><type>bigint</type></entry>
781 <entry>Number of times the background writer stopped a cleaning
782 scan because it had written too many buffers</entry>
785 <entry><structfield>buffers_backend</></entry>
786 <entry><type>bigint</type></entry>
787 <entry>Number of buffers written directly by a backend</entry>
790 <entry><structfield>buffers_backend_fsync</></entry>
791 <entry><type>bigint</type></entry>
792 <entry>Number of times a backend had to execute its own
793 <function>fsync</> call (normally the background writer handles those
794 even when the backend does its own write)</entry>
797 <entry><structfield>buffers_alloc</></entry>
798 <entry><type>bigint</type></entry>
799 <entry>Number of buffers allocated</entry>
802 <entry><structfield>stats_reset</></entry>
803 <entry><type>timestamp with time zone</type></entry>
804 <entry>Time at which these statistics were last reset</entry>
811 The <structname>pg_stat_bgwriter</structname> view will always have a
812 single row, containing global data for the cluster.
815 <table id="pg-stat-database-view" xreflabel="pg_stat_database">
816 <title><structname>pg_stat_database</structname> View</title>
820 <entry>Column</entry>
822 <entry>Description</entry>
828 <entry><structfield>datid</></entry>
829 <entry><type>oid</></entry>
830 <entry>OID of a database</entry>
833 <entry><structfield>datname</></entry>
834 <entry><type>name</></entry>
835 <entry>Name of this database</entry>
838 <entry><structfield>numbackends</></entry>
839 <entry><type>integer</></entry>
840 <entry>Number of backends currently connected to this database.
841 This is the only column in this view that returns a value reflecting
842 current state; all other columns return the accumulated values since
843 the last reset.</entry>
846 <entry><structfield>xact_commit</></entry>
847 <entry><type>bigint</></entry>
848 <entry>Number of transactions in this database that have been
852 <entry><structfield>xact_rollback</></entry>
853 <entry><type>bigint</></entry>
854 <entry>Number of transactions in this database that have been
858 <entry><structfield>blks_read</></entry>
859 <entry><type>bigint</></entry>
860 <entry>Number of disk blocks read in this database</entry>
863 <entry><structfield>blks_hit</></entry>
864 <entry><type>bigint</></entry>
865 <entry>Number of times disk blocks were found already in the buffer
866 cache, so that a read was not necessary (this only includes hits in the
867 PostgreSQL buffer cache, not the operating system's file system cache)
871 <entry><structfield>tup_returned</></entry>
872 <entry><type>bigint</></entry>
873 <entry>Number of rows returned by queries in this database</entry>
876 <entry><structfield>tup_fetched</></entry>
877 <entry><type>bigint</></entry>
878 <entry>Number of rows fetched by queries in this database</entry>
881 <entry><structfield>tup_inserted</></entry>
882 <entry><type>bigint</></entry>
883 <entry>Number of rows inserted by queries in this database</entry>
886 <entry><structfield>tup_updated</></entry>
887 <entry><type>bigint</></entry>
888 <entry>Number of rows updated by queries in this database</entry>
891 <entry><structfield>tup_deleted</></entry>
892 <entry><type>bigint</></entry>
893 <entry>Number of rows deleted by queries in this database</entry>
896 <entry><structfield>conflicts</></entry>
897 <entry><type>bigint</></entry>
898 <entry>Number of queries canceled due to conflicts with recovery
899 in this database. (Conflicts occur only on standby servers; see
900 <xref linkend="pg-stat-database-conflicts-view"> for details.)
904 <entry><structfield>temp_files</></entry>
905 <entry><type>bigint</></entry>
906 <entry>Number of temporary files created by queries in this database.
907 All temporary files are counted, regardless of why the temporary file
908 was created (e.g., sorting or hashing), and regardless of the
909 <xref linkend="guc-log-temp-files"> setting.
913 <entry><structfield>temp_bytes</></entry>
914 <entry><type>bigint</></entry>
915 <entry>Total amount of data written to temporary files by queries in
916 this database. All temporary files are counted, regardless of why
917 the temporary file was created, and
918 regardless of the <xref linkend="guc-log-temp-files"> setting.
922 <entry><structfield>deadlocks</></entry>
923 <entry><type>bigint</></entry>
924 <entry>Number of deadlocks detected in this database</entry>
927 <entry><structfield>blk_read_time</></entry>
928 <entry><type>double precision</></entry>
929 <entry>Time spent reading data file blocks by backends in this database,
930 in milliseconds</entry>
933 <entry><structfield>blk_write_time</></entry>
934 <entry><type>double precision</></entry>
935 <entry>Time spent writing data file blocks by backends in this database,
936 in milliseconds</entry>
939 <entry><structfield>stats_reset</></entry>
940 <entry><type>timestamp with time zone</></entry>
941 <entry>Time at which these statistics were last reset</entry>
948 The <structname>pg_stat_database</structname> view will contain one row
949 for each database in the cluster, showing database-wide statistics.
952 <table id="pg-stat-all-tables-view" xreflabel="pg_stat_all_tables">
953 <title><structname>pg_stat_all_tables</structname> View</title>
957 <entry>Column</entry>
959 <entry>Description</entry>
965 <entry><structfield>relid</></entry>
966 <entry><type>oid</></entry>
967 <entry>OID of a table</entry>
970 <entry><structfield>schemaname</></entry>
971 <entry><type>name</></entry>
972 <entry>Name of the schema that this table is in</entry>
975 <entry><structfield>relname</></entry>
976 <entry><type>name</></entry>
977 <entry>Name of this table</entry>
980 <entry><structfield>seq_scan</></entry>
981 <entry><type>bigint</></entry>
982 <entry>Number of sequential scans initiated on this table</entry>
985 <entry><structfield>seq_tup_read</></entry>
986 <entry><type>bigint</></entry>
987 <entry>Number of live rows fetched by sequential scans</entry>
990 <entry><structfield>idx_scan</></entry>
991 <entry><type>bigint</></entry>
992 <entry>Number of index scans initiated on this table</entry>
995 <entry><structfield>idx_tup_fetch</></entry>
996 <entry><type>bigint</></entry>
997 <entry>Number of live rows fetched by index scans</entry>
1000 <entry><structfield>n_tup_ins</></entry>
1001 <entry><type>bigint</></entry>
1002 <entry>Number of rows inserted</entry>
1005 <entry><structfield>n_tup_upd</></entry>
1006 <entry><type>bigint</></entry>
1007 <entry>Number of rows updated</entry>
1010 <entry><structfield>n_tup_del</></entry>
1011 <entry><type>bigint</></entry>
1012 <entry>Number of rows deleted</entry>
1015 <entry><structfield>n_tup_hot_upd</></entry>
1016 <entry><type>bigint</></entry>
1017 <entry>Number of rows HOT updated (i.e., with no separate index
1018 update required)</entry>
1021 <entry><structfield>n_live_tup</></entry>
1022 <entry><type>bigint</></entry>
1023 <entry>Estimated number of live rows</entry>
1026 <entry><structfield>n_dead_tup</></entry>
1027 <entry><type>bigint</></entry>
1028 <entry>Estimated number of dead rows</entry>
1031 <entry><structfield>n_mod_since_analyze</></entry>
1032 <entry><type>bigint</></entry>
1033 <entry>Estimated number of rows modified since this table was last analyzed</entry>
1036 <entry><structfield>last_vacuum</></entry>
1037 <entry><type>timestamp with time zone</></entry>
1038 <entry>Last time at which this table was manually vacuumed
1039 (not counting <command>VACUUM FULL</>)</entry>
1042 <entry><structfield>last_autovacuum</></entry>
1043 <entry><type>timestamp with time zone</></entry>
1044 <entry>Last time at which this table was vacuumed by the autovacuum
1048 <entry><structfield>last_analyze</></entry>
1049 <entry><type>timestamp with time zone</></entry>
1050 <entry>Last time at which this table was manually analyzed</entry>
1053 <entry><structfield>last_autoanalyze</></entry>
1054 <entry><type>timestamp with time zone</></entry>
1055 <entry>Last time at which this table was analyzed by the autovacuum
1059 <entry><structfield>vacuum_count</></entry>
1060 <entry><type>bigint</></entry>
1061 <entry>Number of times this table has been manually vacuumed
1062 (not counting <command>VACUUM FULL</>)</entry>
1065 <entry><structfield>autovacuum_count</></entry>
1066 <entry><type>bigint</></entry>
1067 <entry>Number of times this table has been vacuumed by the autovacuum
1071 <entry><structfield>analyze_count</></entry>
1072 <entry><type>bigint</></entry>
1073 <entry>Number of times this table has been manually analyzed</entry>
1076 <entry><structfield>autoanalyze_count</></entry>
1077 <entry><type>bigint</></entry>
1078 <entry>Number of times this table has been analyzed by the autovacuum
1086 The <structname>pg_stat_all_tables</structname> view will contain
1087 one row for each table in the current database (including TOAST
1088 tables), showing statistics about accesses to that specific table. The
1089 <structname>pg_stat_user_tables</structname> and
1090 <structname>pg_stat_sys_tables</structname> views
1091 contain the same information,
1092 but filtered to only show user and system tables respectively.
1095 <table id="pg-stat-all-indexes-view" xreflabel="pg_stat_all_indexes">
1096 <title><structname>pg_stat_all_indexes</structname> View</title>
1100 <entry>Column</entry>
1102 <entry>Description</entry>
1108 <entry><structfield>relid</></entry>
1109 <entry><type>oid</></entry>
1110 <entry>OID of the table for this index</entry>
1113 <entry><structfield>indexrelid</></entry>
1114 <entry><type>oid</></entry>
1115 <entry>OID of this index</entry>
1118 <entry><structfield>schemaname</></entry>
1119 <entry><type>name</></entry>
1120 <entry>Name of the schema this index is in</entry>
1123 <entry><structfield>relname</></entry>
1124 <entry><type>name</></entry>
1125 <entry>Name of the table for this index</entry>
1128 <entry><structfield>indexrelname</></entry>
1129 <entry><type>name</></entry>
1130 <entry>Name of this index</entry>
1133 <entry><structfield>idx_scan</></entry>
1134 <entry><type>bigint</></entry>
1135 <entry>Number of index scans initiated on this index</entry>
1138 <entry><structfield>idx_tup_read</></entry>
1139 <entry><type>bigint</></entry>
1140 <entry>Number of index entries returned by scans on this index</entry>
1143 <entry><structfield>idx_tup_fetch</></entry>
1144 <entry><type>bigint</></entry>
1145 <entry>Number of live table rows fetched by simple index scans using this
1153 The <structname>pg_stat_all_indexes</structname> view will contain
1154 one row for each index in the current database,
1155 showing statistics about accesses to that specific index. The
1156 <structname>pg_stat_user_indexes</structname> and
1157 <structname>pg_stat_sys_indexes</structname> views
1158 contain the same information,
1159 but filtered to only show user and system indexes respectively.
1163 Indexes can be used via either simple index scans or <quote>bitmap</>
1164 index scans. In a bitmap scan
1165 the output of several indexes can be combined via AND or OR rules,
1166 so it is difficult to associate individual heap row fetches
1167 with specific indexes when a bitmap scan is used. Therefore, a bitmap
1169 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
1170 count(s) for the index(es) it uses, and it increments the
1171 <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
1172 count for the table, but it does not affect
1173 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
1178 The <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
1179 can be different even without any use of bitmap scans,
1180 because <structfield>idx_tup_read</> counts
1181 index entries retrieved from the index while <structfield>idx_tup_fetch</>
1182 counts live rows fetched from the table. The latter will be less if any
1183 dead or not-yet-committed rows are fetched using the index, or if any
1184 heap fetches are avoided by means of an index-only scan.
1188 <table id="pg-statio-all-tables-view" xreflabel="pg_statio_all_tables">
1189 <title><structname>pg_statio_all_tables</structname> View</title>
1193 <entry>Column</entry>
1195 <entry>Description</entry>
1201 <entry><structfield>relid</></entry>
1202 <entry><type>oid</></entry>
1203 <entry>OID of a table</entry>
1206 <entry><structfield>schemaname</></entry>
1207 <entry><type>name</></entry>
1208 <entry>Name of the schema that this table is in</entry>
1211 <entry><structfield>relname</></entry>
1212 <entry><type>name</></entry>
1213 <entry>Name of this table</entry>
1216 <entry><structfield>heap_blks_read</></entry>
1217 <entry><type>bigint</></entry>
1218 <entry>Number of disk blocks read from this table</entry>
1221 <entry><structfield>heap_blks_hit</></entry>
1222 <entry><type>bigint</></entry>
1223 <entry>Number of buffer hits in this table</entry>
1226 <entry><structfield>idx_blks_read</></entry>
1227 <entry><type>bigint</></entry>
1228 <entry>Number of disk blocks read from all indexes on this table</entry>
1231 <entry><structfield>idx_blks_hit</></entry>
1232 <entry><type>bigint</></entry>
1233 <entry>Number of buffer hits in all indexes on this table</entry>
1236 <entry><structfield>toast_blks_read</></entry>
1237 <entry><type>bigint</></entry>
1238 <entry>Number of disk blocks read from this table's TOAST table (if any)</entry>
1241 <entry><structfield>toast_blks_hit</></entry>
1242 <entry><type>bigint</></entry>
1243 <entry>Number of buffer hits in this table's TOAST table (if any)</entry>
1246 <entry><structfield>tidx_blks_read</></entry>
1247 <entry><type>bigint</></entry>
1248 <entry>Number of disk blocks read from this table's TOAST table indexes (if any)</entry>
1251 <entry><structfield>tidx_blks_hit</></entry>
1252 <entry><type>bigint</></entry>
1253 <entry>Number of buffer hits in this table's TOAST table indexes (if any)</entry>
1260 The <structname>pg_statio_all_tables</structname> view will contain
1261 one row for each table in the current database (including TOAST
1262 tables), showing statistics about I/O on that specific table. The
1263 <structname>pg_statio_user_tables</structname> and
1264 <structname>pg_statio_sys_tables</structname> views
1265 contain the same information,
1266 but filtered to only show user and system tables respectively.
1269 <table id="pg-statio-all-indexes-view" xreflabel="pg_statio_all_indexes">
1270 <title><structname>pg_statio_all_indexes</structname> View</title>
1274 <entry>Column</entry>
1276 <entry>Description</entry>
1282 <entry><structfield>relid</></entry>
1283 <entry><type>oid</></entry>
1284 <entry>OID of the table for this index</entry>
1287 <entry><structfield>indexrelid</></entry>
1288 <entry><type>oid</></entry>
1289 <entry>OID of this index</entry>
1292 <entry><structfield>schemaname</></entry>
1293 <entry><type>name</></entry>
1294 <entry>Name of the schema this index is in</entry>
1297 <entry><structfield>relname</></entry>
1298 <entry><type>name</></entry>
1299 <entry>Name of the table for this index</entry>
1302 <entry><structfield>indexrelname</></entry>
1303 <entry><type>name</></entry>
1304 <entry>Name of this index</entry>
1307 <entry><structfield>idx_blks_read</></entry>
1308 <entry><type>bigint</></entry>
1309 <entry>Number of disk blocks read from this index</entry>
1312 <entry><structfield>idx_blks_hit</></entry>
1313 <entry><type>bigint</></entry>
1314 <entry>Number of buffer hits in this index</entry>
1321 The <structname>pg_statio_all_indexes</structname> view will contain
1322 one row for each index in the current database,
1323 showing statistics about I/O on that specific index. The
1324 <structname>pg_statio_user_indexes</structname> and
1325 <structname>pg_statio_sys_indexes</structname> views
1326 contain the same information,
1327 but filtered to only show user and system indexes respectively.
1330 <table id="pg-statio-all-sequences-view" xreflabel="pg_statio_all_sequences">
1331 <title><structname>pg_statio_all_sequences</structname> View</title>
1335 <entry>Column</entry>
1337 <entry>Description</entry>
1343 <entry><structfield>relid</></entry>
1344 <entry><type>oid</></entry>
1345 <entry>OID of a sequence</entry>
1348 <entry><structfield>schemaname</></entry>
1349 <entry><type>name</></entry>
1350 <entry>Name of the schema this sequence is in</entry>
1353 <entry><structfield>relname</></entry>
1354 <entry><type>name</></entry>
1355 <entry>Name of this sequence</entry>
1358 <entry><structfield>blks_read</></entry>
1359 <entry><type>bigint</></entry>
1360 <entry>Number of disk blocks read from this sequence</entry>
1363 <entry><structfield>blks_hit</></entry>
1364 <entry><type>bigint</></entry>
1365 <entry>Number of buffer hits in this sequence</entry>
1372 The <structname>pg_statio_all_sequences</structname> view will contain
1373 one row for each sequence in the current database,
1374 showing statistics about I/O on that specific sequence.
1377 <table id="pg-stat-user-functions-view" xreflabel="pg_stat_user_functions">
1378 <title><structname>pg_stat_user_functions</structname> View</title>
1382 <entry>Column</entry>
1384 <entry>Description</entry>
1390 <entry><structfield>funcid</></entry>
1391 <entry><type>oid</></entry>
1392 <entry>OID of a function</entry>
1395 <entry><structfield>schemaname</></entry>
1396 <entry><type>name</></entry>
1397 <entry>Name of the schema this function is in</entry>
1400 <entry><structfield>funcname</></entry>
1401 <entry><type>name</></entry>
1402 <entry>Name of this function</entry>
1405 <entry><structfield>calls</></entry>
1406 <entry><type>bigint</></entry>
1407 <entry>Number of times this function has been called</entry>
1410 <entry><structfield>total_time</></entry>
1411 <entry><type>double precision</></entry>
1412 <entry>Total time spent in this function and all other functions
1413 called by it, in milliseconds</entry>
1416 <entry><structfield>self_time</></entry>
1417 <entry><type>double precision</></entry>
1418 <entry>Total time spent in this function itself, not including
1419 other functions called by it, in milliseconds</entry>
1426 The <structname>pg_stat_user_functions</structname> view will contain
1427 one row for each tracked function, showing statistics about executions of
1428 that function. The <xref linkend="guc-track-functions"> parameter
1429 controls exactly which functions are tracked.
1432 <table id="pg-stat-replication-view" xreflabel="pg_stat_replication">
1433 <title><structname>pg_stat_replication</structname> View</title>
1437 <entry>Column</entry>
1439 <entry>Description</entry>
1445 <entry><structfield>pid</></entry>
1446 <entry><type>integer</></entry>
1447 <entry>Process ID of a WAL sender process</entry>
1450 <entry><structfield>usesysid</></entry>
1451 <entry><type>oid</></entry>
1452 <entry>OID of the user logged into this WAL sender process</entry>
1455 <entry><structfield>usename</></entry>
1456 <entry><type>name</></entry>
1457 <entry>Name of the user logged into this WAL sender process</entry>
1460 <entry><structfield>application_name</></entry>
1461 <entry><type>text</></entry>
1462 <entry>Name of the application that is connected
1463 to this WAL sender</entry>
1466 <entry><structfield>client_addr</></entry>
1467 <entry><type>inet</></entry>
1468 <entry>IP address of the client connected to this WAL sender.
1469 If this field is null, it indicates that the client is
1470 connected via a Unix socket on the server machine.
1474 <entry><structfield>client_hostname</></entry>
1475 <entry><type>text</></entry>
1476 <entry>Host name of the connected client, as reported by a
1477 reverse DNS lookup of <structfield>client_addr</>. This field will
1478 only be non-null for IP connections, and only when <xref
1479 linkend="guc-log-hostname"> is enabled.
1483 <entry><structfield>client_port</></entry>
1484 <entry><type>integer</></entry>
1485 <entry>TCP port number that the client is using for communication
1486 with this WAL sender, or <literal>-1</> if a Unix socket is used
1490 <entry><structfield>backend_start</></entry>
1491 <entry><type>timestamp with time zone</></entry>
1492 <entry>Time when this process was started, i.e., when the
1493 client connected to this WAL sender
1497 <entry><structfield>backend_xmin</structfield></entry>
1498 <entry><type>xid</type></entry>
1499 <entry>This standby's <literal>xmin</> horizon reported
1500 by <xref linkend="guc-hot-standby-feedback">.</entry>
1503 <entry><structfield>state</></entry>
1504 <entry><type>text</></entry>
1505 <entry>Current WAL sender state</entry>
1508 <entry><structfield>sent_location</></entry>
1509 <entry><type>pg_lsn</></entry>
1510 <entry>Last transaction log position sent on this connection</entry>
1513 <entry><structfield>write_location</></entry>
1514 <entry><type>pg_lsn</></entry>
1515 <entry>Last transaction log position written to disk by this standby
1519 <entry><structfield>flush_location</></entry>
1520 <entry><type>pg_lsn</></entry>
1521 <entry>Last transaction log position flushed to disk by this standby
1525 <entry><structfield>replay_location</></entry>
1526 <entry><type>pg_lsn</></entry>
1527 <entry>Last transaction log position replayed into the database on this
1528 standby server</entry>
1531 <entry><structfield>sync_priority</></entry>
1532 <entry><type>integer</></entry>
1533 <entry>Priority of this standby server for being chosen as the
1534 synchronous standby</entry>
1537 <entry><structfield>sync_state</></entry>
1538 <entry><type>text</></entry>
1539 <entry>Synchronous state of this standby server</entry>
1546 The <structname>pg_stat_replication</structname> view will contain one row
1547 per WAL sender process, showing statistics about replication to that
1548 sender's connected standby server. Only directly connected standbys are
1549 listed; no information is available about downstream standby servers.
1552 <table id="pg-stat-database-conflicts-view" xreflabel="pg_stat_database_conflicts">
1553 <title><structname>pg_stat_database_conflicts</structname> View</title>
1557 <entry>Column</entry>
1559 <entry>Description</entry>
1565 <entry><structfield>datid</></entry>
1566 <entry><type>oid</></entry>
1567 <entry>OID of a database</entry>
1570 <entry><structfield>datname</></entry>
1571 <entry><type>name</></entry>
1572 <entry>Name of this database</entry>
1575 <entry><structfield>confl_tablespace</></entry>
1576 <entry><type>bigint</></entry>
1577 <entry>Number of queries in this database that have been canceled due to
1578 dropped tablespaces</entry>
1581 <entry><structfield>confl_lock</></entry>
1582 <entry><type>bigint</></entry>
1583 <entry>Number of queries in this database that have been canceled due to
1584 lock timeouts</entry>
1587 <entry><structfield>confl_snapshot</></entry>
1588 <entry><type>bigint</></entry>
1589 <entry>Number of queries in this database that have been canceled due to
1590 old snapshots</entry>
1593 <entry><structfield>confl_bufferpin</></entry>
1594 <entry><type>bigint</></entry>
1595 <entry>Number of queries in this database that have been canceled due to
1596 pinned buffers</entry>
1599 <entry><structfield>confl_deadlock</></entry>
1600 <entry><type>bigint</></entry>
1601 <entry>Number of queries in this database that have been canceled due to
1609 The <structname>pg_stat_database_conflicts</structname> view will contain
1610 one row per database, showing database-wide statistics about
1611 query cancels occurring due to conflicts with recovery on standby servers.
1612 This view will only contain information on standby servers, since
1613 conflicts do not occur on master servers.
1618 <sect2 id="monitoring-stats-functions">
1619 <title>Statistics Functions</title>
1622 Other ways of looking at the statistics can be set up by writing
1623 queries that use the same underlying statistics access functions used by
1624 the standard views shown above. For details such as the functions' names,
1625 consult the definitions of the standard views. (For example, in
1626 <application>psql</> you could issue <literal>\d+ pg_stat_activity</>.)
1627 The access functions for per-database statistics take a database OID as an
1628 argument to identify which database to report on.
1629 The per-table and per-index functions take a table or index OID.
1630 The functions for per-function statistics take a function OID.
1631 Note that only tables, indexes, and functions in the current database
1632 can be seen with these functions.
1636 Additional functions related to statistics collection are listed in <xref
1637 linkend="monitoring-stats-funcs-table">.
1640 <table id="monitoring-stats-funcs-table">
1641 <title>Additional Statistics Functions</title>
1646 <entry>Function</entry>
1647 <entry>Return Type</entry>
1648 <entry>Description</entry>
1655 <!-- See also the entry for this in func.sgml -->
1656 <entry><literal><function>pg_backend_pid()</function></literal></entry>
1657 <entry><type>integer</type></entry>
1659 Process ID of the server process handling the current session
1664 <entry><literal><function>pg_stat_get_activity</function>(<type>integer</type>)</literal><indexterm><primary>pg_stat_get_activity</primary></indexterm></entry>
1665 <entry><type>setof record</type></entry>
1667 Returns a record of information about the backend with the specified PID, or
1668 one record for each active backend in the system if <symbol>NULL</symbol> is
1669 specified. The fields returned are a subset of those in the
1670 <structname>pg_stat_activity</structname> view.
1675 <entry><literal><function>pg_stat_clear_snapshot()</function></literal><indexterm><primary>pg_stat_clear_snapshot</primary></indexterm></entry>
1676 <entry><type>void</type></entry>
1678 Discard the current statistics snapshot
1683 <entry><literal><function>pg_stat_reset()</function></literal><indexterm><primary>pg_stat_reset</primary></indexterm></entry>
1684 <entry><type>void</type></entry>
1686 Reset all statistics counters for the current database to zero
1687 (requires superuser privileges)
1692 <entry><literal><function>pg_stat_reset_shared</function>(text)</literal><indexterm><primary>pg_stat_reset_shared</primary></indexterm></entry>
1693 <entry><type>void</type></entry>
1695 Reset some cluster-wide statistics counters to zero, depending on the
1696 argument (requires superuser privileges).
1697 Calling <literal>pg_stat_reset_shared('bgwriter')</> will zero all the
1698 counters shown in the <structname>pg_stat_bgwriter</> view.
1699 Calling <literal>pg_stat_reset_shared('archiver')</> will zero all the
1700 counters shown in the <structname>pg_stat_archiver</> view.
1705 <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal><indexterm><primary>pg_stat_reset_single_table_counters</primary></indexterm></entry>
1706 <entry><type>void</type></entry>
1708 Reset statistics for a single table or index in the current database to
1709 zero (requires superuser privileges)
1714 <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal><indexterm><primary>pg_stat_reset_single_function_counters</primary></indexterm></entry>
1715 <entry><type>void</type></entry>
1717 Reset statistics for a single function in the current database to
1718 zero (requires superuser privileges)
1726 <function>pg_stat_get_activity</function>, the underlying function of
1727 the <structname>pg_stat_activity</> view, returns a set of records
1728 containing all the available information about each backend process.
1729 Sometimes it may be more convenient to obtain just a subset of this
1730 information. In such cases, an older set of per-backend statistics
1731 access functions can be used; these are shown in <xref
1732 linkend="monitoring-stats-backend-funcs-table">.
1733 These access functions use a backend ID number, which ranges from one
1734 to the number of currently active backends.
1735 The function <function>pg_stat_get_backend_idset</function> provides a
1736 convenient way to generate one row for each active backend for
1737 invoking these functions. For example, to show the <acronym>PID</>s and
1738 current queries of all backends:
1741 SELECT pg_stat_get_backend_pid(s.backendid) AS pid,
1742 pg_stat_get_backend_activity(s.backendid) AS query
1743 FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
1747 <table id="monitoring-stats-backend-funcs-table">
1748 <title>Per-Backend Statistics Functions</title>
1753 <entry>Function</entry>
1754 <entry>Return Type</entry>
1755 <entry>Description</entry>
1762 <entry><literal><function>pg_stat_get_backend_idset()</function></literal></entry>
1763 <entry><type>setof integer</type></entry>
1764 <entry>Set of currently active backend ID numbers (from 1 to the
1765 number of active backends)</entry>
1769 <entry><literal><function>pg_stat_get_backend_activity(integer)</function></literal></entry>
1770 <entry><type>text</type></entry>
1771 <entry>Text of this backend's most recent query</>
1775 <entry><literal><function>pg_stat_get_backend_activity_start(integer)</function></literal></entry>
1776 <entry><type>timestamp with time zone</type></entry>
1777 <entry>Time when the most recent query was started</entry>
1781 <entry><literal><function>pg_stat_get_backend_client_addr(integer)</function></literal></entry>
1782 <entry><type>inet</type></entry>
1783 <entry>IP address of the client connected to this backend</entry>
1787 <entry><literal><function>pg_stat_get_backend_client_port(integer)</function></literal></entry>
1788 <entry><type>integer</type></entry>
1789 <entry>TCP port number that the client is using for communication</entry>
1793 <entry><literal><function>pg_stat_get_backend_dbid(integer)</function></literal></entry>
1794 <entry><type>oid</type></entry>
1795 <entry>OID of the database this backend is connected to</entry>
1799 <entry><literal><function>pg_stat_get_backend_pid(integer)</function></literal></entry>
1800 <entry><type>integer</type></entry>
1801 <entry>Process ID of this backend</entry>
1805 <entry><literal><function>pg_stat_get_backend_start(integer)</function></literal></entry>
1806 <entry><type>timestamp with time zone</type></entry>
1807 <entry>Time when this process was started</entry>
1811 <entry><literal><function>pg_stat_get_backend_userid(integer)</function></literal></entry>
1812 <entry><type>oid</type></entry>
1813 <entry>OID of the user logged into this backend</entry>
1817 <entry><literal><function>pg_stat_get_backend_waiting(integer)</function></literal></entry>
1818 <entry><type>boolean</type></entry>
1819 <entry>True if this backend is currently waiting on a lock</entry>
1823 <entry><literal><function>pg_stat_get_backend_xact_start(integer)</function></literal></entry>
1824 <entry><type>timestamp with time zone</type></entry>
1825 <entry>Time when the current transaction was started</entry>
1835 <sect1 id="monitoring-locks">
1836 <title>Viewing Locks</title>
1838 <indexterm zone="monitoring-locks">
1839 <primary>lock</primary>
1840 <secondary>monitoring</secondary>
1844 Another useful tool for monitoring database activity is the
1845 <structname>pg_locks</structname> system table. It allows the
1846 database administrator to view information about the outstanding
1847 locks in the lock manager. For example, this capability can be used
1853 View all the locks currently outstanding, all the locks on
1854 relations in a particular database, all the locks on a
1855 particular relation, or all the locks held by a particular
1856 <productname>PostgreSQL</productname> session.
1862 Determine the relation in the current database with the most
1863 ungranted locks (which might be a source of contention among
1870 Determine the effect of lock contention on overall database
1871 performance, as well as the extent to which contention varies
1872 with overall database traffic.
1877 Details of the <structname>pg_locks</structname> view appear in
1878 <xref linkend="view-pg-locks">.
1879 For more information on locking and managing concurrency with
1880 <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
1884 <sect1 id="dynamic-trace">
1885 <title>Dynamic Tracing</title>
1887 <indexterm zone="dynamic-trace">
1888 <primary>DTrace</primary>
1892 <productname>PostgreSQL</productname> provides facilities to support
1893 dynamic tracing of the database server. This allows an external
1894 utility to be called at specific points in the code and thereby trace
1899 A number of probes or trace points are already inserted into the source
1900 code. These probes are intended to be used by database developers and
1901 administrators. By default the probes are not compiled into
1902 <productname>PostgreSQL</productname>; the user needs to explicitly tell
1903 the configure script to make the probes available.
1908 <ulink url="https://en.wikipedia.org/wiki/DTrace">DTrace</ulink>
1909 utility is supported, which, at the time of this writing, is available
1910 on Solaris, Mac OS X, FreeBSD, NetBSD, and Oracle Linux. The
1911 <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> project
1912 for Linux provides a DTrace equivalent and can also be used. Supporting other dynamic
1913 tracing utilities is theoretically possible by changing the definitions for
1914 the macros in <filename>src/include/utils/probes.h</>.
1917 <sect2 id="compiling-for-trace">
1918 <title>Compiling for Dynamic Tracing</title>
1921 By default, probes are not available, so you will need to
1922 explicitly tell the configure script to make the probes available
1923 in <productname>PostgreSQL</productname>. To include DTrace support
1924 specify <option>--enable-dtrace</> to configure. See <xref
1925 linkend="install-procedure"> for further information.
1929 <sect2 id="trace-points">
1930 <title>Built-in Probes</title>
1933 A number of standard probes are provided in the source code,
1934 as shown in <xref linkend="dtrace-probe-point-table">;
1935 <xref linkend="typedefs-table">
1936 shows the types used in the probes. More probes can certainly be
1937 added to enhance <productname>PostgreSQL</>'s observability.
1940 <table id="dtrace-probe-point-table">
1941 <title>Built-in DTrace Probes</title>
1946 <entry>Parameters</entry>
1947 <entry>Description</entry>
1954 <entry>transaction-start</entry>
1955 <entry>(LocalTransactionId)</entry>
1956 <entry>Probe that fires at the start of a new transaction.
1957 arg0 is the transaction ID.</entry>
1960 <entry>transaction-commit</entry>
1961 <entry>(LocalTransactionId)</entry>
1962 <entry>Probe that fires when a transaction completes successfully.
1963 arg0 is the transaction ID.</entry>
1966 <entry>transaction-abort</entry>
1967 <entry>(LocalTransactionId)</entry>
1968 <entry>Probe that fires when a transaction completes unsuccessfully.
1969 arg0 is the transaction ID.</entry>
1972 <entry>query-start</entry>
1973 <entry>(const char *)</entry>
1974 <entry>Probe that fires when the processing of a query is started.
1975 arg0 is the query string.</entry>
1978 <entry>query-done</entry>
1979 <entry>(const char *)</entry>
1980 <entry>Probe that fires when the processing of a query is complete.
1981 arg0 is the query string.</entry>
1984 <entry>query-parse-start</entry>
1985 <entry>(const char *)</entry>
1986 <entry>Probe that fires when the parsing of a query is started.
1987 arg0 is the query string.</entry>
1990 <entry>query-parse-done</entry>
1991 <entry>(const char *)</entry>
1992 <entry>Probe that fires when the parsing of a query is complete.
1993 arg0 is the query string.</entry>
1996 <entry>query-rewrite-start</entry>
1997 <entry>(const char *)</entry>
1998 <entry>Probe that fires when the rewriting of a query is started.
1999 arg0 is the query string.</entry>
2002 <entry>query-rewrite-done</entry>
2003 <entry>(const char *)</entry>
2004 <entry>Probe that fires when the rewriting of a query is complete.
2005 arg0 is the query string.</entry>
2008 <entry>query-plan-start</entry>
2010 <entry>Probe that fires when the planning of a query is started.</entry>
2013 <entry>query-plan-done</entry>
2015 <entry>Probe that fires when the planning of a query is complete.</entry>
2018 <entry>query-execute-start</entry>
2020 <entry>Probe that fires when the execution of a query is started.</entry>
2023 <entry>query-execute-done</entry>
2025 <entry>Probe that fires when the execution of a query is complete.</entry>
2028 <entry>statement-status</entry>
2029 <entry>(const char *)</entry>
2030 <entry>Probe that fires anytime the server process updates its
2031 <structname>pg_stat_activity</>.<structfield>status</>.
2032 arg0 is the new status string.</entry>
2035 <entry>checkpoint-start</entry>
2036 <entry>(int)</entry>
2037 <entry>Probe that fires when a checkpoint is started.
2038 arg0 holds the bitwise flags used to distinguish different checkpoint
2039 types, such as shutdown, immediate or force.</entry>
2042 <entry>checkpoint-done</entry>
2043 <entry>(int, int, int, int, int)</entry>
2044 <entry>Probe that fires when a checkpoint is complete.
2045 (The probes listed next fire in sequence during checkpoint processing.)
2046 arg0 is the number of buffers written. arg1 is the total number of
2047 buffers. arg2, arg3 and arg4 contain the number of xlog file(s) added,
2048 removed and recycled respectively.</entry>
2051 <entry>clog-checkpoint-start</entry>
2052 <entry>(bool)</entry>
2053 <entry>Probe that fires when the CLOG portion of a checkpoint is started.
2054 arg0 is true for normal checkpoint, false for shutdown
2058 <entry>clog-checkpoint-done</entry>
2059 <entry>(bool)</entry>
2060 <entry>Probe that fires when the CLOG portion of a checkpoint is
2061 complete. arg0 has the same meaning as for clog-checkpoint-start.</entry>
2064 <entry>subtrans-checkpoint-start</entry>
2065 <entry>(bool)</entry>
2066 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
2068 arg0 is true for normal checkpoint, false for shutdown
2072 <entry>subtrans-checkpoint-done</entry>
2073 <entry>(bool)</entry>
2074 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
2075 complete. arg0 has the same meaning as for
2076 subtrans-checkpoint-start.</entry>
2079 <entry>multixact-checkpoint-start</entry>
2080 <entry>(bool)</entry>
2081 <entry>Probe that fires when the MultiXact portion of a checkpoint is
2083 arg0 is true for normal checkpoint, false for shutdown
2087 <entry>multixact-checkpoint-done</entry>
2088 <entry>(bool)</entry>
2089 <entry>Probe that fires when the MultiXact portion of a checkpoint is
2090 complete. arg0 has the same meaning as for
2091 multixact-checkpoint-start.</entry>
2094 <entry>buffer-checkpoint-start</entry>
2095 <entry>(int)</entry>
2096 <entry>Probe that fires when the buffer-writing portion of a checkpoint
2098 arg0 holds the bitwise flags used to distinguish different checkpoint
2099 types, such as shutdown, immediate or force.</entry>
2102 <entry>buffer-sync-start</entry>
2103 <entry>(int, int)</entry>
2104 <entry>Probe that fires when we begin to write dirty buffers during
2105 checkpoint (after identifying which buffers must be written).
2106 arg0 is the total number of buffers.
2107 arg1 is the number that are currently dirty and need to be written.</entry>
2110 <entry>buffer-sync-written</entry>
2111 <entry>(int)</entry>
2112 <entry>Probe that fires after each buffer is written during checkpoint.
2113 arg0 is the ID number of the buffer.</entry>
2116 <entry>buffer-sync-done</entry>
2117 <entry>(int, int, int)</entry>
2118 <entry>Probe that fires when all dirty buffers have been written.
2119 arg0 is the total number of buffers.
2120 arg1 is the number of buffers actually written by the checkpoint process.
2121 arg2 is the number that were expected to be written (arg1 of
2122 buffer-sync-start); any difference reflects other processes flushing
2123 buffers during the checkpoint.</entry>
2126 <entry>buffer-checkpoint-sync-start</entry>
2128 <entry>Probe that fires after dirty buffers have been written to the
2129 kernel, and before starting to issue fsync requests.</entry>
2132 <entry>buffer-checkpoint-done</entry>
2134 <entry>Probe that fires when syncing of buffers to disk is
2138 <entry>twophase-checkpoint-start</entry>
2140 <entry>Probe that fires when the two-phase portion of a checkpoint is
2144 <entry>twophase-checkpoint-done</entry>
2146 <entry>Probe that fires when the two-phase portion of a checkpoint is
2150 <entry>buffer-read-start</entry>
2151 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</entry>
2152 <entry>Probe that fires when a buffer read is started.
2153 arg0 and arg1 contain the fork and block numbers of the page (but
2154 arg1 will be -1 if this is a relation extension request).
2155 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2156 identifying the relation.
2157 arg5 is the ID of the backend which created the temporary relation for a
2158 local buffer, or InvalidBackendId (-1) for a shared buffer.
2159 arg6 is true for a relation extension request, false for normal
2163 <entry>buffer-read-done</entry>
2164 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool, bool)</entry>
2165 <entry>Probe that fires when a buffer read is complete.
2166 arg0 and arg1 contain the fork and block numbers of the page (if this
2167 is a relation extension request, arg1 now contains the block number
2168 of the newly added block).
2169 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2170 identifying the relation.
2171 arg5 is the ID of the backend which created the temporary relation for a
2172 local buffer, or InvalidBackendId (-1) for a shared buffer.
2173 arg6 is true for a relation extension request, false for normal
2175 arg7 is true if the buffer was found in the pool, false if not.</entry>
2178 <entry>buffer-flush-start</entry>
2179 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2180 <entry>Probe that fires before issuing any write request for a shared
2182 arg0 and arg1 contain the fork and block numbers of the page.
2183 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2184 identifying the relation.</entry>
2187 <entry>buffer-flush-done</entry>
2188 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2189 <entry>Probe that fires when a write request is complete. (Note
2190 that this just reflects the time to pass the data to the kernel;
2191 it's typically not actually been written to disk yet.)
2192 The arguments are the same as for buffer-flush-start.</entry>
2195 <entry>buffer-write-dirty-start</entry>
2196 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2197 <entry>Probe that fires when a server process begins to write a dirty
2198 buffer. (If this happens often, it implies that
2199 <xref linkend="guc-shared-buffers"> is too
2200 small or the bgwriter control parameters need adjustment.)
2201 arg0 and arg1 contain the fork and block numbers of the page.
2202 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2203 identifying the relation.</entry>
2206 <entry>buffer-write-dirty-done</entry>
2207 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2208 <entry>Probe that fires when a dirty-buffer write is complete.
2209 The arguments are the same as for buffer-write-dirty-start.</entry>
2212 <entry>wal-buffer-write-dirty-start</entry>
2214 <entry>Probe that fires when a server process begins to write a
2215 dirty WAL buffer because no more WAL buffer space is available.
2216 (If this happens often, it implies that
2217 <xref linkend="guc-wal-buffers"> is too small.)</entry>
2220 <entry>wal-buffer-write-dirty-done</entry>
2222 <entry>Probe that fires when a dirty WAL buffer write is complete.</entry>
2225 <entry>xlog-insert</entry>
2226 <entry>(unsigned char, unsigned char)</entry>
2227 <entry>Probe that fires when a WAL record is inserted.
2228 arg0 is the resource manager (rmid) for the record.
2229 arg1 contains the info flags.</entry>
2232 <entry>xlog-switch</entry>
2234 <entry>Probe that fires when a WAL segment switch is requested.</entry>
2237 <entry>smgr-md-read-start</entry>
2238 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
2239 <entry>Probe that fires when beginning to read a block from a relation.
2240 arg0 and arg1 contain the fork and block numbers of the page.
2241 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2242 identifying the relation.
2243 arg5 is the ID of the backend which created the temporary relation for a
2244 local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
2247 <entry>smgr-md-read-done</entry>
2248 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
2249 <entry>Probe that fires when a block read is complete.
2250 arg0 and arg1 contain the fork and block numbers of the page.
2251 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2252 identifying the relation.
2253 arg5 is the ID of the backend which created the temporary relation for a
2254 local buffer, or InvalidBackendId (-1) for a shared buffer.
2255 arg6 is the number of bytes actually read, while arg7 is the number
2256 requested (if these are different it indicates trouble).</entry>
2259 <entry>smgr-md-write-start</entry>
2260 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
2261 <entry>Probe that fires when beginning to write a block to a relation.
2262 arg0 and arg1 contain the fork and block numbers of the page.
2263 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2264 identifying the relation.
2265 arg5 is the ID of the backend which created the temporary relation for a
2266 local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
2269 <entry>smgr-md-write-done</entry>
2270 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
2271 <entry>Probe that fires when a block write is complete.
2272 arg0 and arg1 contain the fork and block numbers of the page.
2273 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2274 identifying the relation.
2275 arg5 is the ID of the backend which created the temporary relation for a
2276 local buffer, or InvalidBackendId (-1) for a shared buffer.
2277 arg6 is the number of bytes actually written, while arg7 is the number
2278 requested (if these are different it indicates trouble).</entry>
2281 <entry>sort-start</entry>
2282 <entry>(int, bool, int, int, bool)</entry>
2283 <entry>Probe that fires when a sort operation is started.
2284 arg0 indicates heap, index or datum sort.
2285 arg1 is true for unique-value enforcement.
2286 arg2 is the number of key columns.
2287 arg3 is the number of kilobytes of work memory allowed.
2288 arg4 is true if random access to the sort result is required.</entry>
2291 <entry>sort-done</entry>
2292 <entry>(bool, long)</entry>
2293 <entry>Probe that fires when a sort is complete.
2294 arg0 is true for external sort, false for internal sort.
2295 arg1 is the number of disk blocks used for an external sort,
2296 or kilobytes of memory used for an internal sort.</entry>
2299 <entry>lwlock-acquire</entry>
2300 <entry>(char *, int, LWLockMode)</entry>
2301 <entry>Probe that fires when an LWLock has been acquired.
2302 arg0 is the LWLock's tranche.
2303 arg1 is the LWLock's offset within its tranche.
2304 arg2 is the requested lock mode, either exclusive or shared.</entry>
2307 <entry>lwlock-release</entry>
2308 <entry>(char *, int)</entry>
2309 <entry>Probe that fires when an LWLock has been released (but note
2310 that any released waiters have not yet been awakened).
2311 arg0 is the LWLock's tranche.
2312 arg1 is the LWLock's offset within its tranche.</entry>
2315 <entry>lwlock-wait-start</entry>
2316 <entry>(char *, int, LWLockMode)</entry>
2317 <entry>Probe that fires when an LWLock was not immediately available and
2318 a server process has begun to wait for the lock to become available.
2319 arg0 is the LWLock's tranche.
2320 arg1 is the LWLock's offset within its tranche.
2321 arg2 is the requested lock mode, either exclusive or shared.</entry>
2324 <entry>lwlock-wait-done</entry>
2325 <entry>(char *, int, LWLockMode)</entry>
2326 <entry>Probe that fires when a server process has been released from its
2327 wait for an LWLock (it does not actually have the lock yet).
2328 arg0 is the LWLock's tranche.
2329 arg1 is the LWLock's offset within its tranche.
2330 arg2 is the requested lock mode, either exclusive or shared.</entry>
2333 <entry>lwlock-condacquire</entry>
2334 <entry>(char *, int, LWLockMode)</entry>
2335 <entry>Probe that fires when an LWLock was successfully acquired when the
2336 caller specified no waiting.
2337 arg0 is the LWLock's tranche.
2338 arg1 is the LWLock's offset within its tranche.
2339 arg2 is the requested lock mode, either exclusive or shared.</entry>
2342 <entry>lwlock-condacquire-fail</entry>
2343 <entry>(char *, int, LWLockMode)</entry>
2344 <entry>Probe that fires when an LWLock was not successfully acquired when
2345 the caller specified no waiting.
2346 arg0 is the LWLock's tranche.
2347 arg1 is the LWLock's offset within its tranche.
2348 arg2 is the requested lock mode, either exclusive or shared.</entry>
2351 <entry>lock-wait-start</entry>
2352 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
2353 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
2354 has begun to wait because the lock is not available.
2355 arg0 through arg3 are the tag fields identifying the object being
2356 locked. arg4 indicates the type of object being locked.
2357 arg5 indicates the lock type being requested.</entry>
2360 <entry>lock-wait-done</entry>
2361 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
2362 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
2363 has finished waiting (i.e., has acquired the lock).
2364 The arguments are the same as for lock-wait-start.</entry>
2367 <entry>deadlock-found</entry>
2369 <entry>Probe that fires when a deadlock is found by the deadlock
2377 <table id="typedefs-table">
2378 <title>Defined Types Used in Probe Parameters</title>
2383 <entry>Definition</entry>
2390 <entry>LocalTransactionId</entry>
2391 <entry>unsigned int</entry>
2394 <entry>LWLockMode</entry>
2398 <entry>LOCKMODE</entry>
2402 <entry>BlockNumber</entry>
2403 <entry>unsigned int</entry>
2407 <entry>unsigned int</entry>
2410 <entry>ForkNumber</entry>
2425 <sect2 id="using-trace-points">
2426 <title>Using Probes</title>
2429 The example below shows a DTrace script for analyzing transaction
2430 counts in the system, as an alternative to snapshotting
2431 <structname>pg_stat_database</> before and after a performance test:
2433 #!/usr/sbin/dtrace -qs
2435 postgresql$1:::transaction-start
2437 @start["Start"] = count();
2438 self->ts = timestamp;
2441 postgresql$1:::transaction-abort
2443 @abort["Abort"] = count();
2446 postgresql$1:::transaction-commit
2449 @commit["Commit"] = count();
2450 @time["Total time (ns)"] = sum(timestamp - self->ts);
2454 When executed, the example D script gives output such as:
2456 # ./txn_count.d `pgrep -n postgres` or ./txn_count.d <PID>
2461 Total time (ns) 2312105013
2467 SystemTap uses a different notation for trace scripts than DTrace does,
2468 even though the underlying trace points are compatible. One point worth
2469 noting is that at this writing, SystemTap scripts must reference probe
2470 names using double underscores in place of hyphens. This is expected to
2471 be fixed in future SystemTap releases.
2476 You should remember that DTrace scripts need to be carefully written and
2477 debugged, otherwise the trace information collected might
2478 be meaningless. In most cases where problems are found it is the
2479 instrumentation that is at fault, not the underlying system. When
2480 discussing information found using dynamic tracing, be sure to enclose
2481 the script used to allow that too to be checked and discussed.
2485 More example scripts can be found in the PgFoundry
2486 <ulink url="http://pgfoundry.org/projects/dtrace/">dtrace project</ulink>.
2490 <sect2 id="defining-trace-points">
2491 <title>Defining New Probes</title>
2494 New probes can be defined within the code wherever the developer
2495 desires, though this will require a recompilation. Below are the steps
2496 for inserting new probes:
2502 Decide on probe names and data to be made available through the probes
2508 Add the probe definitions to <filename>src/backend/utils/probes.d</>
2514 Include <filename>pg_trace.h</> if it is not already present in the
2515 module(s) containing the probe points, and insert
2516 <literal>TRACE_POSTGRESQL</> probe macros at the desired locations
2523 Recompile and verify that the new probes are available
2529 <title>Example:</title>
2531 Here is an example of how you would add a probe to trace all new
2532 transactions by transaction ID.
2539 Decide that the probe will be named <literal>transaction-start</> and
2540 requires a parameter of type LocalTransactionId
2546 Add the probe definition to <filename>src/backend/utils/probes.d</>:
2548 probe transaction__start(LocalTransactionId);
2550 Note the use of the double underline in the probe name. In a DTrace
2551 script using the probe, the double underline needs to be replaced with a
2552 hyphen, so <literal>transaction-start</> is the name to document for
2559 At compile time, <literal>transaction__start</> is converted to a macro
2560 called <literal>TRACE_POSTGRESQL_TRANSACTION_START</> (notice the
2561 underscores are single here), which is available by including
2562 <filename>pg_trace.h</>. Add the macro call to the appropriate location
2563 in the source code. In this case, it looks like the following:
2566 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
2573 After recompiling and running the new binary, check that your newly added
2574 probe is available by executing the following DTrace command. You
2575 should see similar output:
2577 # dtrace -ln transaction-start
2578 ID PROVIDER MODULE FUNCTION NAME
2579 18705 postgresql49878 postgres StartTransactionCommand transaction-start
2580 18755 postgresql49877 postgres StartTransactionCommand transaction-start
2581 18805 postgresql49876 postgres StartTransactionCommand transaction-start
2582 18855 postgresql49875 postgres StartTransactionCommand transaction-start
2583 18986 postgresql49873 postgres StartTransactionCommand transaction-start
2590 There are a few things to be careful about when adding trace macros
2596 You should take care that the data types specified for a probe's
2597 parameters match the data types of the variables used in the macro.
2598 Otherwise, you will get compilation errors.
2605 On most platforms, if <productname>PostgreSQL</productname> is
2606 built with <option>--enable-dtrace</>, the arguments to a trace
2607 macro will be evaluated whenever control passes through the
2608 macro, <emphasis>even if no tracing is being done</>. This is
2609 usually not worth worrying about if you are just reporting the
2610 values of a few local variables. But beware of putting expensive
2611 function calls into the arguments. If you need to do that,
2612 consider protecting the macro with a check to see if the trace
2613 is actually enabled:
2616 if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
2617 TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
2620 Each trace macro has a corresponding <literal>ENABLED</> macro.