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 960 0.0 1.1 6104 1480 pts/1 SN 13:17 0:00 postgres -i
52 postgres 963 0.0 1.1 7084 1472 pts/1 SN 13:17 0:00 postgres: writer process
53 postgres 965 0.0 1.1 6152 1512 pts/1 SN 13:17 0:00 postgres: stats collector process
54 postgres 998 0.0 2.3 6532 2992 pts/1 SN 13:18 0:00 postgres: tgl runbug 127.0.0.1 idle
55 postgres 1003 0.0 2.4 6532 3128 pts/1 SN 13:19 0:00 postgres: tgl regression [local] SELECT waiting
56 postgres 1016 0.1 2.4 6532 3080 pts/1 SN 13:19 0:00 postgres: tgl regression [local] idle in transaction
59 (The appropriate invocation of <command>ps</> varies across different
60 platforms, as do the details of what is shown. This example is from a
61 recent Linux system.) The first process listed here is the
62 master server process. The command arguments
63 shown for it are the same ones used when it was launched. The next two
64 processes are background worker processes automatically launched by the
65 master process. (The <quote>stats collector</> process will not be present
67 the system not to start the statistics collector.) Each of the remaining
68 processes is a server process handling one client connection. Each such
69 process sets its command line display in the form
72 postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <replaceable>activity</>
75 The user, database, and (client) host items remain the same for
76 the life of the client connection, but the activity indicator changes.
77 The activity can be <literal>idle</> (i.e., waiting for a client command),
78 <literal>idle in transaction</> (waiting for client inside a <command>BEGIN</> block),
79 or a command type name such as <literal>SELECT</>. Also,
80 <literal>waiting</> is appended if the server process is presently waiting
81 on a lock held by another session. In the above example we can infer
82 that process 1003 is waiting for process 1016 to complete its transaction and
83 thereby release some lock.
87 If you have turned off <xref linkend="guc-update-process-title"> then the
88 activity indicator is not updated; the process title is set only once
89 when a new process is launched. On some platforms this saves a measurable
90 amount of per-command overhead; on others it's insignificant.
95 <productname>Solaris</productname> requires special handling. You must
96 use <command>/usr/ucb/ps</command>, rather than
97 <command>/bin/ps</command>. You also must use two <option>w</option>
98 flags, not just one. In addition, your original invocation of the
99 <command>postgres</command> command must have a shorter
100 <command>ps</command> status display than that provided by each
101 server process. If you fail to do all three things, the <command>ps</>
102 output for each server process will be the original <command>postgres</>
108 <sect1 id="monitoring-stats">
109 <title>The Statistics Collector</title>
111 <indexterm zone="monitoring-stats">
112 <primary>statistics</primary>
116 <productname>PostgreSQL</productname>'s <firstterm>statistics collector</>
117 is a subsystem that supports collection and reporting of information about
118 server activity. Presently, the collector can count accesses to tables
119 and indexes in both disk-block and individual-row terms. It also tracks
120 the total number of rows in each table, and information about vacuum and
121 analyze actions for each table. It can also count calls to user-defined
122 functions and the total time spent in each one.
126 <productname>PostgreSQL</productname> also supports reporting of the exact
127 command currently being executed by other server processes. This
128 facility is independent of the collector process.
131 <sect2 id="monitoring-stats-setup">
132 <title>Statistics Collection Configuration</title>
135 Since collection of statistics adds some overhead to query execution,
136 the system can be configured to collect or not collect information.
137 This is controlled by configuration parameters that are normally set in
138 <filename>postgresql.conf</>. (See <xref linkend="runtime-config"> for
139 details about setting configuration parameters.)
143 The parameter <xref linkend="guc-track-counts"> controls whether
144 statistics are collected about table and index accesses.
148 The parameter <xref linkend="guc-track-iotiming"> enables monitoring
149 of block read and write times.
153 The parameter <xref linkend="guc-track-functions"> enables tracking of
154 usage of user-defined functions.
158 The parameter <xref linkend="guc-track-activities"> enables monitoring
159 of the current command being executed by any server process.
163 Normally these parameters are set in <filename>postgresql.conf</> so
164 that they apply to all server processes, but it is possible to turn
165 them on or off in individual sessions using the <xref
166 linkend="sql-set"> command. (To prevent
167 ordinary users from hiding their activity from the administrator,
168 only superusers are allowed to change these parameters with
173 The statistics collector transmits the collected
174 information to backends (including autovacuum) through temporary files.
175 These files are stored in the <filename>pg_stat_tmp</filename> subdirectory.
176 When the postmaster shuts down, a permanent copy of the statistics
177 data is stored in the <filename>global</filename> subdirectory. For increased
178 performance, the parameter <xref linkend="guc-stats-temp-directory"> can
179 be pointed at a RAM-based file system, decreasing physical I/O requirements.
184 <sect2 id="monitoring-stats-views">
185 <title>Viewing Collected Statistics</title>
188 Several predefined views, listed in <xref
189 linkend="monitoring-stats-views-table">, are available to show the results
190 of statistics collection. Alternatively, one can
191 build custom views using the underlying statistics functions.
195 When using the statistics to monitor current activity, it is important
196 to realize that the information does not update instantaneously.
197 Each individual server process transmits new statistical counts to
198 the collector just before going idle; so a query or transaction still in
199 progress does not affect the displayed totals. Also, the collector itself
200 emits a new report at most once per <varname>PGSTAT_STAT_INTERVAL</varname>
201 milliseconds (500 unless altered while building the server). So the
202 displayed information lags behind actual activity. However, current-query
203 information collected by <varname>track_activities</varname> is
208 Another important point is that when a server process is asked to display
209 any of these statistics, it first fetches the most recent report emitted by
210 the collector process and then continues to use this snapshot for all
211 statistical views and functions until the end of its current transaction.
212 So the statistics will show static information as long as you continue the
213 current transaction. Similarly, information about the current queries of
214 all sessions is collected when any such information is first requested
215 within a transaction, and the same information will be displayed throughout
217 This is a feature, not a bug, because it allows you to perform several
218 queries on the statistics and correlate the results without worrying that
219 the numbers are changing underneath you. But if you want to see new
220 results with each query, be sure to do the queries outside any transaction
221 block. Alternatively, you can invoke
222 <function>pg_stat_clear_snapshot</function>(), which will discard the
223 current transaction's statistics snapshot (if any). The next use of
224 statistical information will cause a new snapshot to be fetched.
228 A transaction can also see its own statistics (as yet untransmitted to the
229 collector) in the views <structname>pg_stat_xact_all_tables</>,
230 <structname>pg_stat_xact_sys_tables</>,
231 <structname>pg_stat_xact_user_tables</>, and
232 <structname>pg_stat_xact_user_functions</>, or via these views' underlying
233 functions (named the same as the standard statistics functions but with the
234 prefix <function>pg_stat_get_xact_</function>). These numbers do not act
235 as stated above; instead they update continuously throughout the transaction.
238 <table id="monitoring-stats-views-table">
239 <title>Standard Statistics Views</title>
244 <entry>View Name</entry>
245 <entry>Description</entry>
252 <structname>pg_stat_activity</structname>
253 <indexterm><primary>pg_stat_activity</primary></indexterm>
256 <para>One row per server process, showing information related to
257 each connection to the server. Unless the
258 <xref linkend="guc-track-activities"> parameter has been turned
259 off, it is possible to monitor state and query information of
260 all running processes.
263 See <xref linkend="pg-stat-activity-view"> for more details.
269 <entry><structname>pg_stat_bgwriter</><indexterm><primary>pg_stat_bgwriter</primary></indexterm></entry>
270 <entry>One row only, showing cluster-wide statistics. See
271 <xref linkend="pg-stat-bgwriter-view"> for more details.
276 <entry><structname>pg_stat_database</><indexterm><primary>pg_stat_database</primary></indexterm></entry>
277 <entry>One row per database, showing database-wide statistics. See
278 <xref linkend="pg-stat-database-view"> for more details.
283 <entry><structname>pg_stat_database_conflicts</><indexterm><primary>pg_stat_database_conflicts</primary></indexterm></entry>
285 One row per database showing database-wide statistics about
286 query cancels due to conflict with recovery on standby servers.
287 Will only contain information on standby servers, since
288 conflicts do not occur on master servers.
289 See <xref linkend="pg-stat-database-conflicts-view"> for more details.
294 <entry><structname>pg_stat_replication</><indexterm><primary>pg_stat_replication</primary></indexterm></entry>
295 <entry>One row per WAL sender process, showing statistics about the
296 replication to this slave. See <xref linkend="pg-stat-replication-view">
297 for more details. Only directly connected standbys are listed; no
298 information about downstream standby servers is recorded.
303 <entry><structname>pg_stat_all_tables</><indexterm><primary>pg_stat_all_tables</primary></indexterm></entry>
305 One row for each table in the current database (including TOAST
306 tables) with information about accesses to this specific table.
307 See <xref linkend="pg-stat-all-tables-view"> for more details.
312 <entry><structname>pg_stat_sys_tables</><indexterm><primary>pg_stat_sys_tables</primary></indexterm></entry>
313 <entry>Same as <structname>pg_stat_all_tables</>, except that only
314 system tables are shown.</entry>
318 <entry><structname>pg_stat_user_tables</><indexterm><primary>pg_stat_user_tables</primary></indexterm></entry>
319 <entry>Same as <structname>pg_stat_all_tables</>, except that only user
320 tables are shown.</entry>
324 <entry><structname>pg_stat_xact_all_tables</><indexterm><primary>pg_stat_xact_all_tables</primary></indexterm></entry>
325 <entry>Similar to <structname>pg_stat_all_tables</>, but counts actions
326 taken so far within the current transaction (which are <emphasis>not</>
327 yet included in <structname>pg_stat_all_tables</> and related views).
328 The columns for numbers of live and dead rows and vacuum and
329 analyze actions are not present in this view.</entry>
333 <entry><structname>pg_stat_xact_sys_tables</><indexterm><primary>pg_stat_xact_sys_tables</primary></indexterm></entry>
334 <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
335 system tables are shown.</entry>
339 <entry><structname>pg_stat_xact_user_tables</><indexterm><primary>pg_stat_xact_user_tables</primary></indexterm></entry>
340 <entry>Same as <structname>pg_stat_xact_all_tables</>, except that only
341 user tables are shown.</entry>
345 <entry><structname>pg_stat_all_indexes</><indexterm><primary>pg_stat_all_indexes</primary></indexterm></entry>
347 One row for each index in the current database with information
348 about accesses to this specific index.
349 See <xref linkend="pg-stat-all-indexes-view"> for more details.
354 <entry><structname>pg_stat_sys_indexes</><indexterm><primary>pg_stat_sys_indexes</primary></indexterm></entry>
355 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
356 indexes on system tables are shown.</entry>
360 <entry><structname>pg_stat_user_indexes</><indexterm><primary>pg_stat_user_indexes</primary></indexterm></entry>
361 <entry>Same as <structname>pg_stat_all_indexes</>, except that only
362 indexes on user tables are shown.</entry>
366 <entry><structname>pg_statio_all_tables</><indexterm><primary>pg_statio_all_tables</primary></indexterm></entry>
368 One row for each table in the current database (including TOAST
369 tables) with information about I/O on this specific table.
370 See <xref linkend="pg-statio-all-tables-view"> for more details.
375 <entry><structname>pg_statio_sys_tables</><indexterm><primary>pg_statio_sys_tables</primary></indexterm></entry>
376 <entry>Same as <structname>pg_statio_all_tables</>, except that only
377 system tables are shown.</entry>
381 <entry><structname>pg_statio_user_tables</><indexterm><primary>pg_statio_user_tables</primary></indexterm></entry>
382 <entry>Same as <structname>pg_statio_all_tables</>, except that only
383 user tables are shown.</entry>
387 <entry><structname>pg_statio_all_indexes</><indexterm><primary>pg_statio_all_indexes</primary></indexterm></entry>
389 One row for each index in the current database
390 with information about I/O on this specific index.
391 See <xref linkend="pg-statio-all-indexes-view"> for more details.
396 <entry><structname>pg_statio_sys_indexes</><indexterm><primary>pg_statio_sys_indexes</primary></indexterm></entry>
397 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
398 indexes on system tables are shown.</entry>
402 <entry><structname>pg_statio_user_indexes</><indexterm><primary>pg_statio_user_indexes</primary></indexterm></entry>
403 <entry>Same as <structname>pg_statio_all_indexes</>, except that only
404 indexes on user tables are shown.</entry>
408 <entry><structname>pg_statio_all_sequences</><indexterm><primary>pg_statio_all_sequences</primary></indexterm></entry>
410 One row for each sequence in the current database
411 with information about I/O on this specific sequence.
412 See <xref linkend="pg-statio-all-sequences-view"> for more details.
417 <entry><structname>pg_statio_sys_sequences</><indexterm><primary>pg_statio_sys_sequences</primary></indexterm></entry>
418 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
419 system sequences are shown. (Presently, no system sequences are defined,
420 so this view is always empty.)</entry>
424 <entry><structname>pg_statio_user_sequences</><indexterm><primary>pg_statio_user_sequences</primary></indexterm></entry>
425 <entry>Same as <structname>pg_statio_all_sequences</>, except that only
426 user sequences are shown.</entry>
430 <entry><structname>pg_stat_user_functions</><indexterm><primary>pg_stat_user_functions</primary></indexterm></entry>
432 One row for each tracked function (as specified by the
433 <xref linkend="guc-track-functions"> parameter). See
434 <xref linkend="pg-stat-user-functions-view"> for more details.
439 <entry><structname>pg_stat_xact_user_functions</><indexterm><primary>pg_stat_xact_user_functions</primary></indexterm></entry>
440 <entry>Similar to <structname>pg_stat_user_functions</>, but counts only
441 calls during the current transaction (which are <emphasis>not</>
442 yet included in <structname>pg_stat_user_functions</>).</entry>
450 The per-index statistics are particularly useful to determine which
451 indexes are being used and how effective they are.
456 used either directly or via <quote>bitmap scans</>. In a bitmap scan
457 the output of several indexes can be combined via AND or OR rules;
458 so it is difficult to associate individual heap row fetches
459 with specific indexes when a bitmap scan is used. Therefore, a bitmap
461 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_read</>
462 count(s) for the index(es) it uses, and it increments the
463 <structname>pg_stat_all_tables</>.<structfield>idx_tup_fetch</>
464 count for the table, but it does not affect
465 <structname>pg_stat_all_indexes</>.<structfield>idx_tup_fetch</>.
470 Before <productname>PostgreSQL</productname> 8.1, the
471 <structfield>idx_tup_read</> and <structfield>idx_tup_fetch</> counts
472 were essentially always equal. Now they can be different even without
473 considering bitmap scans, because <structfield>idx_tup_read</> counts
474 index entries retrieved from the index while <structfield>idx_tup_fetch</>
475 counts live rows fetched from the table; the latter will be less if any
476 dead or not-yet-committed rows are fetched using the index, or if any
477 heap fetches are avoided by means of an index-only scan.
482 The <structname>pg_statio_</> views are primarily useful to
483 determine the effectiveness of the buffer cache. When the number
484 of actual disk reads is much smaller than the number of buffer
485 hits, then the cache is satisfying most read requests without
486 invoking a kernel call. However, these statistics do not give the
487 entire story: due to the way in which <productname>PostgreSQL</>
488 handles disk I/O, data that is not in the
489 <productname>PostgreSQL</> buffer cache might still reside in the
490 kernel's I/O cache, and might therefore still be fetched without
491 requiring a physical read. Users interested in obtaining more
492 detailed information on <productname>PostgreSQL</> I/O behavior are
493 advised to use the <productname>PostgreSQL</> statistics collector
494 in combination with operating system utilities that allow insight
495 into the kernel's handling of I/O.
499 Other ways of looking at the statistics can be set up by writing
500 queries that use the same underlying statistics access functions as
501 these standard views do. These functions are listed in <xref
502 linkend="monitoring-stats-funcs-table">. The per-database access
503 functions take a database OID as an argument to identify which
504 database to report on. The per-table and per-index functions take
505 a table or index OID. The functions for function-call statistics
506 take a function OID. (Note that only tables, indexes, and functions
507 in the current database can be seen with these functions.) The
508 per-server-process access functions take a server process
509 number, which ranges from one to the number of currently active
514 <table id="pg-stat-activity-view" xreflabel="pg_stat_activity">
515 <title>pg_stat_activity view</title>
520 <entry>Column</entry>
522 <entry>Description</entry>
529 <entry><type>oid</></entry>
530 <entry>The OID of the database the backend is connected to.
531 This value can also be returned by directly calling
532 the <function>pg_stat_get_backend_dbid</function> function.</entry>
535 <entry>datname</entry>
536 <entry><type>name</></entry>
537 <entry>The name of the database the backend is connected to.</entry>
541 <entry><type>integer</></entry>
542 <entry>The process ID of the backend.
543 This value can also be returned by directly calling
544 the <function>pg_stat_get_backend_pid</function>.</entry>
547 <entry>usesysid</entry>
548 <entry><type>oid</></entry>
549 <entry>The id of the user logged into the backend.
550 This value can also be returned by directly calling
551 the <function>pg_stat_get_backend_userid</function>.</entry>
554 <entry>usename</entry>
555 <entry><type>name</></entry>
556 <entry>The name of the user logged into the backend.</entry>
559 <entry>application_name</entry>
560 <entry><type>text</></entry>
561 <entry>The name of the application that has initiated the connection
562 to the backend.</entry>
565 <entry>client_addr</entry>
566 <entry><type>inet</></entry>
567 <entry>The remote IP of the client connected to the backend.
568 If this field is not set, it indicates that the client is either connected
569 via a Unix socket on the server machine or is an internal process such
571 This value can also be returned by directly calling
572 the <function>pg_stat_get_backend_client_addr</function>.
576 <entry>client_hostname</entry>
577 <entry><type>text</></entry>
579 If available, the hostname of the client as reported by a
580 reverse lookup of <structfield>client_addr</>. This field will
581 only be set when <xref linkend="guc-log-hostname"> is enabled.
585 <entry>client_port</entry>
586 <entry><type>integer</></entry>
588 The remote TCP port that the client is using for communication
589 to the backend, or <symbol>NULL</> if a unix socket is used.
590 This value can also be returned by directly calling
591 the <function>pg_stat_get_backend_client_port</function>.
595 <entry>backend_start</entry>
596 <entry><type>timestamp with time zone</></entry>
598 The time when this process was started, i.e. when the
599 client connected to the server.
600 This value can also be returned by directly calling
601 the <function>pg_stat_get_backend_start</function>.
605 <entry>xact_start</entry>
606 <entry><type>timestamp with time zone</></entry>
608 The time when the current transaction was started. If the client is
609 using autocommit for transactions, this value is equal to the
611 This value can also be returned by directly calling
612 the <function>pg_stat_get_backend_xact_start</function>.
616 <entry>query_start</entry>
617 <entry><type>timestamp with time zone</></entry>
619 The time when the currently active query started, or if
620 <structfield>state</> is <literal>idle</>, when the last query
622 This value can also be returned by directly calling
623 the <function>pg_stat_get_backend_activity_start</function>.
627 <entry>state_change</entry>
628 <entry><type>timestamp with time zone</></entry>
629 <entry>The time when the <structfield>state</> was last changed.</entry>
632 <entry>waiting</entry>
633 <entry><type>boolean</></entry>
635 Boolean indicating if a backend is currently waiting on a lock.
636 This value can also be returned by directly calling
637 the <function>pg_stat_get_backend_waiting</function>.
642 <entry><type>text</></entry>
644 The <structfield>state</> of the currently running query.
651 The backend is executing a query.
659 There is no query executing in the backend.
664 <term>idle in transaction</term>
667 The backend is in a transaction, but is currently not currently
673 <term>idle in transaction (aborted)</term>
676 This state is similar to <literal>idle in transaction</>,
677 except one of the statements in the transaction caused an error.
682 <term>fastpath function call</term>
685 The backend is executing a fast-path function.
690 <term>disabled</term>
693 This state indicates that <xref linkend="guc-track-activities">
701 The <structfield>waiting</> and <structfield>state</> columns are
702 independent. If a query is in the <literal>active</> state,
703 it may or may not be <literal>waiting</>. If a query is
704 <literal>active</> and <structfield>waiting</> is true, it means
705 that the query is being executed, but is being blocked by a lock
706 somewhere in the system.
713 <entry><type>text</></entry>
715 The most recent query that the backend has executed. If
716 <structfield>state</> is <literal>active</> this means the currently
717 executing query. In all other states, it means the last query that was
726 The <structname>pg_stat_activity</structname> view will have one row
727 per server process, showing information related to each connection to
731 All functions used in the view are indexed by backend id number. The
732 function <function>pg_stat_get_backend_idset</function> provides a
733 convenient way to generate one row for each active server process. For
734 example, to show the <acronym>PID</>s and current queries of all server processes:
737 SELECT pg_stat_get_backend_pid(s.backendid) AS pid,
738 pg_stat_get_backend_activity(s.backendid) AS query
739 FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
743 <table id="pg-stat-bgwriter-view" xreflabel="pg_stat_bgwriter">
744 <title>pg_stat_bgwriter view</title>
749 <entry>Column</entry>
751 <entry>Description</entry>
757 <entry>checkpoints_timed</entry>
758 <entry><type>bigint</type></entry>
759 <entry>Number of scheduled checkpoints.
760 This value can also be returned by directly calling
761 the <function>pg_stat_get_bgwriter_timed_checkpoints</function> function.</entry>
764 <entry>checkpoints_requested</entry>
765 <entry><type>bigint</type></entry>
766 <entry>Number of requested checkpoints.
767 This value can also be returned by directly calling
768 the <function>pg_stat_get_bgwriter_requested_checkpoints</function> function.</entry>
771 <entry>checkpoint_write_time</entry>
772 <entry><type>bigint</type></entry>
774 Total amount of time that has been spent in the portion of
775 checkpoint processing where files are written to disk, in milliseconds.
776 This value can also be returned by directly calling the
777 <function>pg_stat_get_checkpoint_write_time</function>
782 <entry>checkpoint_sync_time</entry>
783 <entry><type>bigint</type></entry>
785 Total amount of time that has been spent in the portion of
786 checkpoint processing where files are synchronized to disk, in
787 milliseconds. This value can also be returned by directly calling
788 the <function>pg_stat_get_checkpoint_sync_time</function>
793 <entry>buffers_checkpoint</entry>
794 <entry><type>bigint</type></entry>
795 <entry>Number of buffers written during checkpoints.
796 This value can also be returned by directly calling
797 the <function>pg_stat_get_bgwriter_buf_written_checkpoints</function> function.</entry>
800 <entry>buffers_clean</entry>
801 <entry><type>bigint</type></entry>
802 <entry>Number of buffers written by the background writer.
803 This value can also be returned by directly calling
804 the <function>pg_stat_get_bgwriter_buf_written_clean</function> function.</entry>
807 <entry>maxwritten_clean</entry>
808 <entry><type>bigint</type></entry>
809 <entry>Number of times the background writer stopped a cleaning
810 scan because it had written too many buffers.
811 This value can also be returned by directly calling
812 the <function>pg_stat_get_bgwriter_maxwritten_clean</function> function.</entry>
815 <entry>buffers_backend</entry>
816 <entry><type>bigint</type></entry>
817 <entry>Number of buffers written directly by a backend.
818 This value can also be returned by directly calling
819 the <function>pg_stat_get_buf_written_backend</function> function.</entry>
822 <entry>buffers_backend_fsync</entry>
823 <entry><type>bigint</type></entry>
824 <entry>Number of times a backend had to execute its own fsync
825 call (normally the background writer handles those even when the
826 backend does its own write)</entry>
829 <entry>buffers_alloc</entry>
830 <entry><type>bigint</type></entry>
831 <entry>Number of buffers allocated.
832 This value can also be returned by directly calling
833 the <function>pg_stat_get_buf_alloc</function> function.</entry>
836 <entry>stats_reset</entry>
837 <entry><type>bigint</type></entry>
838 <entry>The last time these statistics were reset.
839 This value can also be returned by directly calling
840 the <function>pg_stat_get_bgwriter_stat_reset_time</function> function.</entry>
847 The <structname>pg_stat_bgwriter</structname> view will always have a
848 single row with global data for the cluster.
851 <table id="pg-stat-database-view" xreflabel="pg_stat_database">
852 <title>pg_stat_database view</title>
856 <entry>Column</entry>
858 <entry>Description</entry>
865 <entry><type>oid</></entry>
866 <entry>The OID of the database</entry>
869 <entry>datname</entry>
870 <entry><type>name</></entry>
871 <entry>The name of the database</entry>
874 <entry>numbackends</entry>
875 <entry><type>integer</></entry>
876 <entry>The number of backends currently connected to this database.
877 This is the only column in this view that returns a value for the
878 current state, all other columns return the accumulated values since
879 the last reset. This value can also be returned by directly calling
880 the <function>pg_stat_get_db_numbackends</function> function.</entry>
883 <entry>xact_commit</entry>
884 <entry><type>bigint</></entry>
885 <entry>The number of transactions in this database that have been
886 committed. This value can also be returned by directly calling
887 the <function>pg_stat_get_db_xact_commit</function> function.</entry>
890 <entry>xact_rollback</entry>
891 <entry><type>bigint</></entry>
892 <entry>The number of transactions in this database that have been
893 rolled back. This value can also be returned by directly calling
894 the <function>pg_stat_get_db_xact_rollback</function> function.</entry>
897 <entry>blks_read</entry>
898 <entry><type>bigint</></entry>
899 <entry>The number of disk blocks read in this database.
900 This value can also be returned by directly calling
901 the <function>pg_stat_get_db_blocks_fetched</function> and
902 <function>pg_stat_get_db_blocks_hit</function> functions and
903 subtracting the results.</entry>
906 <entry>blks_hits</entry>
907 <entry><type>bigint</></entry>
908 <entry>The number of disk blocks read from the buffer cache
909 (this only includes hits in the PostgreSQL buffer cache, and not
910 the operating system filesystem cache).
911 This value can also be returned by directly calling
912 the <function>pg_stat_get_db_blocks_hit</function> function.</entry>
915 <entry>tup_returned</entry>
916 <entry><type>bigint</></entry>
917 <entry>The number of rows returned by queries in this database.
918 This value can also be returned by directly calling
919 the <function>pg_stat_get_db_tuples_returned</function> function.</entry>
922 <entry>tup_fetched</entry>
923 <entry><type>bigint</></entry>
924 <entry>The number of rows fetched by queries in this database.
925 This value can also be returned by directly calling
926 the <function>pg_stat_get_db_tuples_fetched</function> function.</entry>
929 <entry>tup_inserted</entry>
930 <entry><type>bigint</></entry>
931 <entry>The number of rows inserted by queries in this database.
932 This value can also be returned by directly calling
933 the <function>pg_stat_get_db_tuples_inserted</function> function.</entry>
936 <entry>tup_updated</entry>
937 <entry><type>bigint</></entry>
938 <entry>The number of rows updated by queries in this database.
939 This value can also be returned by directly calling
940 the <function>pg_stat_get_db_tuples_updated</function> function.</entry>
943 <entry>tup_deleted</entry>
944 <entry><type>bigint</></entry>
945 <entry>The number of rows deleted by queries in this database.
946 This value can also be returned by directly calling
947 the <function>pg_stat_get_db_tuples_deleted</function> function.</entry>
950 <entry>conflicts</entry>
951 <entry><type>bigint</></entry>
953 The number of queries canceled due to conflict with recovery
954 (on standby servers) in this database. (See
955 <xref linkend="pg-stat-database-conflicts-view"> for more details).
956 This value can also be returned by directly calling
957 the <function>pg_stat_get_db_conflict_all</function> function.
961 <entry>temp_files</entry>
962 <entry><type>bigint</></entry>
964 The number of temporary files written by queries in the database.
965 All temporary files are counted, regardless of why the temporary file
966 was created (sorting or hash) or file size, and regardless of the
967 <xref linkend="guc-log-temp-files"> setting.
968 This value can also be returned by directly calling
969 the <function>pg_stat_get_db_temp_files</function> function.
973 <entry>temp_bytes</entry>
974 <entry><type>bigint</></entry>
976 The amount of data written to temporary files by queries in
977 the database. All temporary files are counted, regardless of why
978 the temporary file was created (sorting or hash) or file size, and
979 regardless of the <xref linkend="guc-log-temp-files"> setting.
980 This value can also be returned by directly calling
981 the <function>pg_stat_get_db_temp_bytes</function> function.
985 <entry>deadlocks</entry>
986 <entry><type>bigint</></entry>
987 <entry>Number of deadlocks detected in the database.
988 This value can also be returned by directly calling
989 the <function>pg_stat_get_db_deadlocks</function> function.</entry>
992 <entry>block_read_time</entry>
993 <entry><type>bigint</></entry>
994 <entry>Time spent by backends reading data file blocks, in milliseconds.
995 The same value can be returned in microseconds by directly calling
996 the <function>pg_stat_get_db_block_time_read</function> function.</entry>
999 <entry>block_write_time</entry>
1000 <entry><type>bigint</></entry>
1001 <entry>Time spent by backends writing data file blocks, in milliseconds.
1002 The same value can be returned in microseconds by directly calling
1003 the <function>pg_stat_get_db_block_time_write</function> function.</entry>
1006 <entry>stats_reset</entry>
1007 <entry><type>timestamptz</></entry>
1008 <entry>The last time the statistics were reset.
1009 This value can also be returned by directly calling
1010 the <function>pg_stat_get_reset_time</function> function.</entry>
1017 The <structname>pg_stat_database</structname> view will contain one row
1018 for each database in the cluster showing database-wide statistics.
1021 <table id="pg-stat-database-conflicts-view" xreflabel="pg_stat_database_conflicts">
1022 <title>pg_stat_database_conflicts view</title>
1026 <entry>Column</entry>
1028 <entry>Description</entry>
1034 <entry>datid</entry>
1035 <entry><type>oid</></entry>
1036 <entry>The OID of the database</entry>
1039 <entry>datname</entry>
1040 <entry><type>name</></entry>
1041 <entry>The name of the database</entry>
1044 <entry>confl_tablespace</entry>
1045 <entry><type>bigint</></entry>
1046 <entry>The number of queries that have been canceled due to
1047 dropped tablespaces. This value can also be returned by directly calling
1048 the <function>pg_stat_get_db_conflict_tablespace</function> function.</entry>
1051 <entry>confl_lock</entry>
1052 <entry><type>bigint</></entry>
1053 <entry>The number of queries that have been canceled due to
1054 lock timeouts. This value can also be returned by directly calling
1055 the <function>pg_stat_get_db_conflict_lock</function> function.</entry>
1058 <entry>confl_snapshot</entry>
1059 <entry><type>bigint</></entry>
1060 <entry>The number of queries that have been canceled due to
1061 old snapshots. This value can also be returned by directly calling
1062 the <function>pg_stat_get_db_conflict_snapshot</function> function.</entry>
1065 <entry>confl_bufferpin</entry>
1066 <entry><type>bigint</></entry>
1067 <entry>The number of queries that have been canceled due to
1068 pinned buffers. This value can also be returned by directly calling
1069 the <function>pg_stat_get_db_conflict_bufferpin</function> function.</entry>
1072 <entry>confl_deadlock</entry>
1073 <entry><type>bigint</></entry>
1074 <entry>The number of queries that have been canceled due to
1075 deadlocks. This value can also be returned by directly calling
1076 the <function>pg_stat_get_db_conflict_startup_deadlock</function>
1084 The <structname>pg_stat_database_conflicts</structname> view will contain
1085 one row per database showing database-wide statistics about
1086 query cancels due to conflict with recovery on standby servers.
1087 Will only contain information on standby servers, since
1088 conflicts do not occur on master servers.
1091 <table id="pg-stat-replication-view" xreflabel="pg_stat_replication">
1092 <title>pg_stat_replication view</title>
1096 <entry>Column</entry>
1098 <entry>Description</entry>
1105 <entry><type>integer</></entry>
1106 <entry>The process id of the WAL sender process</entry>
1109 <entry>usesysid</entry>
1110 <entry><type>oid</></entry>
1111 <entry>The OID of the user logged into this WAL sender process</entry>
1114 <entry>usename</entry>
1115 <entry><type>name</></entry>
1116 <entry>The name of the user logged into this WAL sender process</entry>
1119 <entry>application_name</entry>
1120 <entry><type>text</></entry>
1121 <entry>The name of the application that has initiated the connection
1122 to the WAL sender.</entry>
1125 <entry>client_addr</entry>
1126 <entry><type>inet</></entry>
1127 <entry>The remote IP of the client connected to the WAL sender.
1128 If this field is not set, it indicates that the client is
1129 connected via a Unix socket on the server machine.
1133 <entry>client_hostname</entry>
1134 <entry><type>text</></entry>
1136 If available, the hostname of the client as reported by a
1137 reverse lookup of <structfield>client_addr</>. This field will
1138 only be set when <xref linkend="guc-log-hostname"> is enabled.
1142 <entry>client_port</entry>
1143 <entry><type>integer</></entry>
1145 The remote TCP port that the client is using for communication
1146 to the, or <symbol>NULL</> if a unix socket is used.
1150 <entry>backend_start</entry>
1151 <entry><type>timestamp with time zone</></entry>
1153 The time when this process was started, i.e. when the
1154 client connected to the WAL sender.
1158 <entry>state</entry>
1159 <entry><type>text</></entry>
1160 <entry>Current WAL sender state</entry>
1163 <entry>sent_location</entry>
1164 <entry><type>text</></entry>
1165 <entry>Last transaction log position sent on this connection</entry>
1168 <entry>write_location</entry>
1169 <entry><type>text</></entry>
1170 <entry>Last transaction log position written to disk by the slave</entry>
1173 <entry>flush_location</entry>
1174 <entry><type>text</></entry>
1175 <entry>Last transaction log position flushed to disk by the slave</entry>
1178 <entry>replay_location</entry>
1179 <entry><type>text</></entry>
1180 <entry>Last transaction log position replayed into the database on the slave</entry>
1183 <entry>sync_priority</entry>
1184 <entry><type>int</></entry>
1186 The priority in the order which this slave will be picked as
1187 the synchronous standby.
1191 <entry>sync_state</entry>
1192 <entry><type>text</></entry>
1194 The synchronous state of this slave.
1202 The <structname>pg_stat_replication</structname> view will contain one row
1203 per WAL sender process, showing statistics about the replication to this
1204 slave. Only directly connected standbys are listed; no information about
1205 downstream standby servers is recorded.
1208 <table id="pg-stat-all-tables-view" xreflabel="pg_stat_all_tables">
1209 <title>pg_stat_all_tables view</title>
1213 <entry>Column</entry>
1215 <entry>Description</entry>
1221 <entry>relid</entry>
1222 <entry><type>oid</></entry>
1223 <entry>The OID of the table</entry>
1226 <entry>schemaname</entry>
1227 <entry><type>name</></entry>
1228 <entry>The name of the schema that the table is in</entry>
1231 <entry>relname</entry>
1232 <entry><type>name</></entry>
1233 <entry>The name of the table</entry>
1236 <entry>seq_scan</entry>
1237 <entry><type>bigint</></entry>
1238 <entry>The number of sequential scans initiated on this table.
1239 This value can also be returned by directly calling
1240 the <function>pg_stat_get_numscans</function> function.</entry>
1243 <entry>seq_tup_read</entry>
1244 <entry><type>bigint</></entry>
1245 <entry>The number of live rows fetch by sequential scans.
1246 This value can also be returned by directly calling
1247 the <function>pg_stat_get_tuples_returned</function> function.</entry>
1250 <entry>idx_scan</entry>
1251 <entry><type>bigint</></entry>
1252 <entry>The number of index scans initiated on this table</entry>
1255 <entry>idx_tup_fetch</entry>
1256 <entry><type>bigint</></entry>
1257 <entry>The number of live rows fetch by index scans.</entry>
1260 <entry>n_tup_ins</entry>
1261 <entry><type>bigint</></entry>
1262 <entry>The number of rows inserted.
1263 This value can also be returned by directly calling
1264 the <function>pg_stat_get_tuples_inserted</function> function.</entry>
1267 <entry>n_tup_upd</entry>
1268 <entry><type>bigint</></entry>
1269 <entry>The number of rows updated.
1270 This value can also be returned by directly calling
1271 the <function>pg_stat_get_tuples_updated</function> function.</entry>
1274 <entry>n_tup_del</entry>
1275 <entry><type>bigint</></entry>
1276 <entry>The number of rows deleted.
1277 This value can also be returned by directly calling
1278 the <function>pg_stat_get_tuples_deleted</function> function.</entry>
1281 <entry>n_tup_hot_upd</entry>
1282 <entry><type>bigint</></entry>
1283 <entry>The number of rows HOT (i.e., no separate index update) updated.
1284 This value can also be returned by directly calling
1285 the <function>pg_stat_get_tuples_hot_updated</function> function.</entry>
1288 <entry>n_live_tup</entry>
1289 <entry><type>bigint</></entry>
1290 <entry>The number of live rows.
1291 This value can also be returned by directly calling
1292 the <function>pg_stat_get_live_tuples</function> function.</entry>
1295 <entry>n_dead_tup</entry>
1296 <entry><type>bigint</></entry>
1297 <entry>The number of dead rows.
1298 This value can also be returned by directly calling
1299 the <function>pg_stat_get_dead_tuples</function> function.</entry>
1302 <entry>last_vacuum</entry>
1303 <entry><type>timestamp with time zone</></entry>
1304 <entry>The last time the table was manually non-<option>FULL</> vacuumed.
1305 This value can also be returned by directly calling
1306 the <function>pg_stat_get_last_vacuum_time</function> function.</entry>
1309 <entry>last_autovacuum</entry>
1310 <entry><type>timestamp with time zone</></entry>
1311 <entry>The last time the table was vacuumed by the autovacuum daemon.
1312 This value can also be returned by directly calling
1313 the <function>pg_stat_get_last_autovacuum_time</function> function.</entry>
1316 <entry>last_analyze</entry>
1317 <entry><type>timestamp with time zone</></entry>
1318 <entry>The last time the table was manually analyzed.
1319 This value can also be returned by directly calling
1320 the <function>pg_stat_get_last_analyze_time</function> function.</entry>
1323 <entry>last_autoanalyze</entry>
1324 <entry><type>timestamp with time zone</></entry>
1325 <entry>The last time the table was analyzed by the autovacuum daemon.
1326 This value can also be returned by directly calling
1327 the <function>pg_stat_get_last_autoanalyze_time</function> function.</entry>
1330 <entry>vacuum_count</entry>
1331 <entry><type>bigint</></entry>
1332 <entry>The number of times this table has been manually non-<option>FULL</> vacuumed.
1333 This value can also be returned by directly calling
1334 the <function>pg_stat_get_vacuum_count</function> function.</entry>
1337 <entry>autovacuum_count</entry>
1338 <entry><type>bigint</></entry>
1339 <entry>The number of times this table has been vacuumed by the autovacuum daemon.
1340 This value can also be returned by directly calling
1341 the <function>pg_stat_get_autovacuum_count</function> function.</entry>
1344 <entry>analyze_count</entry>
1345 <entry><type>bigint</></entry>
1346 <entry>The number of times this table has been manually analyzed.
1347 This value can also be returned by directly calling
1348 the <function>pg_stat_get_analyze_count</function> function.</entry>
1351 <entry>autoanalyze_count</entry>
1352 <entry><type>bigint</></entry>
1353 <entry>The number of times this table has been analyzed by the autovacuum daemon.
1354 This value can also be returned by directly calling
1355 the <function>pg_stat_get_autoanalyze_count</function> function.</entry>
1362 The <structname>pg_stat_all_tables</structname> view will contain
1363 one row for each table in the current database (including TOAST
1364 tables) with information about accesses to this specific table. The
1365 <structname>pg_stat_user_tables</structname> and
1366 <structname>pg_stat_sys_tables</structname> contain the same information,
1367 but filtered to only have rows for user and system tables.
1370 <table id="pg-stat-all-indexes-view" xreflabel="pg_stat_all_indexes">
1371 <title>pg_stat_all_indexes view</title>
1375 <entry>Column</entry>
1377 <entry>Description</entry>
1383 <entry>relid</entry>
1384 <entry><type>oid</></entry>
1385 <entry>The OID of the table for this index</entry>
1388 <entry>indexrelid</entry>
1389 <entry><type>oid</></entry>
1390 <entry>The OID of the index</entry>
1393 <entry>schemaname</entry>
1394 <entry><type>name</></entry>
1395 <entry>The name of the schema the index is in</entry>
1398 <entry>relname</entry>
1399 <entry><type>name</></entry>
1400 <entry>The name of the table for this index</entry>
1403 <entry>indexrelname</entry>
1404 <entry><type>name</></entry>
1405 <entry>The name of the index</entry>
1408 <entry>idx_scan</entry>
1409 <entry><type>bigint</></entry>
1410 <entry>Number of index scans initiated on this index.
1411 This value can also be returned by directly calling
1412 the <function>pg_stat_get_numscans</function> function.</entry>
1415 <entry>idx_tup_read</entry>
1416 <entry><type>bigint</></entry>
1417 <entry>Number of index entries returned by scans on this index.
1418 This value can also be returned by directly calling
1419 the <function>pg_stat_get_tuples_returned</function> function.</entry>
1422 <entry>idx_tup_fetch</entry>
1423 <entry><type>bigint</></entry>
1424 <entry>Number of live table rows fetched by simple index scans using this index.
1425 This value can also be returned by directly calling
1426 the <function>pg_stat_get_tuples_fetched</function> function.</entry>
1433 The <structname>pg_stat_all_indexes</structname> view will contain
1434 one row for each index in the current database
1435 with information about accesses to this specific index. The
1436 <structname>pg_stat_user_indexes</structname> and
1437 <structname>pg_stat_sys_indexes</structname> contain the same information,
1438 but filtered to only have rows for user and system indexes.
1441 <table id="pg-statio-all-tables-view" xreflabel="pg_statio_all_tables">
1442 <title>pg_statio_all_tables view</title>
1446 <entry>Column</entry>
1448 <entry>Description</entry>
1454 <entry>relid</entry>
1455 <entry><type>oid</></entry>
1456 <entry>The OID of the table</entry>
1459 <entry>schemaname</entry>
1460 <entry><type>name</></entry>
1461 <entry>The name of the schema that the table is in</entry>
1464 <entry>relname</entry>
1465 <entry><type>name</></entry>
1466 <entry>The name of the table</entry>
1469 <entry>heap_blks_read</entry>
1470 <entry><type>name</></entry>
1471 <entry>Number of disk blocks read from this table.
1472 This value can also be returned by directly calling
1473 the <function>pg_stat_get_blocks_fetched</function> and
1474 <function>pg_stat_get_blocks_hit</function> functions and
1475 subtracting the results.</entry>
1478 <entry>heap_blks_hit</entry>
1479 <entry><type>name</></entry>
1480 <entry>Number of buffer hits in this table.
1481 This value can also be returned by directly calling
1482 the <function>pg_stat_get_blocks_hit</function> function.</entry>
1485 <entry>idx_blks_read</entry>
1486 <entry><type>name</></entry>
1487 <entry>Number of disk blocks read from all indexes on this table</entry>
1490 <entry>idx_blks_hit</entry>
1491 <entry><type>name</></entry>
1492 <entry>Number of buffer hits in all indexes of this table.</entry>
1495 <entry>toast_blks_read</entry>
1496 <entry><type>name</></entry>
1497 <entry>Number of disk blocks read from this table's TOAST table (if any)</entry>
1500 <entry>toast_blks_hit</entry>
1501 <entry><type>name</></entry>
1502 <entry>Number of buffer hits in this table's TOAST table (if any)</entry>
1505 <entry>tidx_blks_read</entry>
1506 <entry><type>name</></entry>
1507 <entry>Number of disk blocks read from this table's TOAST table index (if any)</entry>
1510 <entry>tidx_blks_hit</entry>
1511 <entry><type>name</></entry>
1512 <entry>Number of buffer hits in this table's TOAST table index (if any)</entry>
1519 The <structname>pg_statio_all_tables</structname> view will contain
1520 one row for each table in the current database (including TOAST
1521 tables) with information about I/O on this specific table. The
1522 <structname>pg_statio_user_tables</structname> and
1523 <structname>pg_statio_sys_tables</structname> contain the same information,
1524 but filtered to only have rows for user and system tables.
1527 <table id="pg-statio-all-indexes-view" xreflabel="pg_statio_all_indexes">
1528 <title>pg_statio_all_indexes view</title>
1532 <entry>Column</entry>
1534 <entry>Description</entry>
1540 <entry>relid</entry>
1541 <entry><type>oid</></entry>
1542 <entry>The OID of the table for this index</entry>
1545 <entry>indexrelid</entry>
1546 <entry><type>oid</></entry>
1547 <entry>The OID of the index</entry>
1550 <entry>schemaname</entry>
1551 <entry><type>name</></entry>
1552 <entry>The name of the schema the index is in</entry>
1555 <entry>relname</entry>
1556 <entry><type>name</></entry>
1557 <entry>The name of the table for this index</entry>
1560 <entry>indexrelname</entry>
1561 <entry><type>name</></entry>
1562 <entry>The name of the index</entry>
1565 <entry>idx_blks_read</entry>
1566 <entry><type>name</></entry>
1567 <entry>Number of disk blocks read from the index.
1568 This value can also be returned by directly calling
1569 the <function>pg_stat_get_blocks_fetched</function> and
1570 <function>pg_stat_get_blocks_hit</function> functions and
1571 subtracting the results.</entry>
1574 <entry>idx_blks_hit</entry>
1575 <entry><type>name</></entry>
1576 <entry>Number of buffer hits in the index.
1577 This value can also be returned by directly calling
1578 the <function>pg_stat_get_blocks_hit</function> function.</entry>
1585 The <structname>pg_statio_all_indexes</structname> view will contain
1586 one row for each index in the current database
1587 with information about I/O on this specific index. The
1588 <structname>pg_statio_user_indexes</structname> and
1589 <structname>pg_statio_sys_indexes</structname> contain the same information,
1590 but filtered to only have rows for user and system indexes.
1593 <table id="pg-statio-all-sequences-view" xreflabel="pg_statio_all_sequences">
1594 <title>pg_statio_all_sequences view</title>
1598 <entry>Column</entry>
1600 <entry>Description</entry>
1606 <entry>relid</entry>
1607 <entry><type>oid</></entry>
1608 <entry>The OID of the sequence</entry>
1611 <entry>schemaname</entry>
1612 <entry><type>name</></entry>
1613 <entry>The name of the schema the sequence is in</entry>
1616 <entry>relname</entry>
1617 <entry><type>name</></entry>
1618 <entry>The name of the sequence</entry>
1621 <entry>blks_read</entry>
1622 <entry><type>name</></entry>
1623 <entry>Number of disk blocks read from the sequence</entry>
1626 <entry>blks_hit</entry>
1627 <entry><type>name</></entry>
1628 <entry>Number of buffer hits in the sequence</entry>
1635 The <structname>pg_statio_all_indexes</structname> view will contain
1636 one row for each sequence in the current database
1637 with information about I/O on this specific sequence.
1640 <table id="pg-stat-user-functions-view" xreflabel="pg_stat_user_functions">
1641 <title>pg_stat_user_functions view</title>
1645 <entry>Column</entry>
1647 <entry>Description</entry>
1653 <entry>funcid</entry>
1654 <entry><type>oid</></entry>
1655 <entry>The OID of the function</entry>
1658 <entry>schemaname</entry>
1659 <entry><type>name</></entry>
1660 <entry>The name of the schema the function is in</entry>
1663 <entry>funcname</entry>
1664 <entry><type>name</></entry>
1665 <entry>The name of the function</entry>
1668 <entry>calls</entry>
1669 <entry><type>bigint</></entry>
1670 <entry>Number of times the function has been called.
1671 This value can also be returned by directly calling
1672 the <function>pg_stat_get_function_calls</function> function.</entry>
1675 <entry>total_time</entry>
1676 <entry><type>bigint</></entry>
1677 <entry>Total time spent in this functions and all other functions
1678 called by it, in milliseconds.
1679 The same value can be returned in microseconds by directly calling
1680 the <function>pg_stat_get_function_time</function> function.</entry>
1683 <entry>self_time</entry>
1684 <entry><type>bigint</></entry>
1685 <entry>Total time spent in this functions itself but not including
1686 other functions called by it, in milliseconds.
1687 The same value can be returned in microseconds by directly calling
1688 the <function>pg_stat_get_function_self_time</function> function.</entry>
1695 The <structname>pg_stat_user_functions</structname> view will contain
1696 one row for each tracked function (as specified by the
1697 <xref linkend="guc-track-functions"> parameter).
1700 <sect3 id="monitoring-stats-functions">
1701 <title>Other Statistics Functions</title>
1704 <table id="monitoring-stats-funcs-table">
1705 <title>Other Statistics Functions</title>
1710 <entry>Function</entry>
1711 <entry>Return Type</entry>
1712 <entry>Description</entry>
1719 <!-- See also the entry for this in func.sgml -->
1720 <entry><literal><function>pg_backend_pid()</function></literal></entry>
1721 <entry><type>integer</type></entry>
1723 Process ID of the server process attached to the current session
1728 <entry><literal><function>pg_stat_get_activity</function>(<type>integer</type>)</literal></entry>
1729 <entry><type>setof record</type></entry>
1731 Returns a record of information about the backend with the specified PID, or
1732 one record for each active backend in the system if <symbol>NULL</symbol> is
1733 specified. The fields returned are a subset of those in the
1734 <structname>pg_stat_activity</structname> view.
1739 <entry><literal><function>pg_stat_get_backend_idset()</function></literal></entry>
1740 <entry><type>setof integer</type></entry>
1742 Set of currently active server process numbers (from 1 to the
1743 number of active server processes). See usage example in the text.
1749 <entry><literal><function>pg_stat_get_wal_senders()</function></literal></entry>
1750 <entry><type>setof record</type></entry>
1752 One record for each active wal sender. The fields returned are a subset
1753 of those in the <structname>pg_stat_replication</structname> view.
1758 <entry><literal><function>pg_stat_clear_snapshot()</function></literal></entry>
1759 <entry><type>void</type></entry>
1761 Discard the current statistics snapshot
1766 <entry><literal><function>pg_stat_reset()</function></literal></entry>
1767 <entry><type>void</type></entry>
1769 Reset all statistics counters for the current database to zero
1770 (requires superuser privileges)
1775 <entry><literal><function>pg_stat_reset_shared</function>(text)</literal></entry>
1776 <entry><type>void</type></entry>
1778 Reset some of the shared statistics counters for the database cluster to
1779 zero (requires superuser privileges). Calling
1780 <literal>pg_stat_reset_shared('bgwriter')</> will zero all the values shown by
1781 <structname>pg_stat_bgwriter</>.
1786 <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal></entry>
1787 <entry><type>void</type></entry>
1789 Reset statistics for a single table or index in the current database to
1790 zero (requires superuser privileges)
1795 <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal></entry>
1796 <entry><type>void</type></entry>
1798 Reset statistics for a single function in the current database to
1799 zero (requires superuser privileges)
1810 <sect1 id="monitoring-locks">
1811 <title>Viewing Locks</title>
1813 <indexterm zone="monitoring-locks">
1814 <primary>lock</primary>
1815 <secondary>monitoring</secondary>
1819 Another useful tool for monitoring database activity is the
1820 <structname>pg_locks</structname> system table. It allows the
1821 database administrator to view information about the outstanding
1822 locks in the lock manager. For example, this capability can be used
1828 View all the locks currently outstanding, all the locks on
1829 relations in a particular database, all the locks on a
1830 particular relation, or all the locks held by a particular
1831 <productname>PostgreSQL</productname> session.
1837 Determine the relation in the current database with the most
1838 ungranted locks (which might be a source of contention among
1845 Determine the effect of lock contention on overall database
1846 performance, as well as the extent to which contention varies
1847 with overall database traffic.
1852 Details of the <structname>pg_locks</structname> view appear in
1853 <xref linkend="view-pg-locks">.
1854 For more information on locking and managing concurrency with
1855 <productname>PostgreSQL</productname>, refer to <xref linkend="mvcc">.
1859 <sect1 id="dynamic-trace">
1860 <title>Dynamic Tracing</title>
1862 <indexterm zone="dynamic-trace">
1863 <primary>DTrace</primary>
1867 <productname>PostgreSQL</productname> provides facilities to support
1868 dynamic tracing of the database server. This allows an external
1869 utility to be called at specific points in the code and thereby trace
1874 A number of probes or trace points are already inserted into the source
1875 code. These probes are intended to be used by database developers and
1876 administrators. By default the probes are not compiled into
1877 <productname>PostgreSQL</productname>; the user needs to explicitly tell
1878 the configure script to make the probes available.
1883 <ulink url="http://opensolaris.org/os/community/dtrace/">DTrace</ulink>
1884 utility is supported, which is available
1885 on OpenSolaris, Solaris 10, and Mac OS X Leopard. It is expected that
1886 DTrace will be available in the future on FreeBSD and possibly other
1887 operating systems. The
1888 <ulink url="http://sourceware.org/systemtap/">SystemTap</ulink> project
1889 for Linux also provides a DTrace equivalent. Supporting other dynamic
1890 tracing utilities is theoretically possible by changing the definitions for
1891 the macros in <filename>src/include/utils/probes.h</>.
1894 <sect2 id="compiling-for-trace">
1895 <title>Compiling for Dynamic Tracing</title>
1898 By default, probes are not available, so you will need to
1899 explicitly tell the configure script to make the probes available
1900 in <productname>PostgreSQL</productname>. To include DTrace support
1901 specify <option>--enable-dtrace</> to configure. See <xref
1902 linkend="install-procedure"> for further information.
1906 <sect2 id="trace-points">
1907 <title>Built-in Probes</title>
1910 A number of standard probes are provided in the source code,
1911 as shown in <xref linkend="dtrace-probe-point-table">;
1912 <xref linkend="typedefs-table">
1913 shows the types used in the probes. More probes can certainly be
1914 added to enhance <productname>PostgreSQL</>'s observability.
1917 <table id="dtrace-probe-point-table">
1918 <title>Built-in DTrace Probes</title>
1923 <entry>Parameters</entry>
1924 <entry>Description</entry>
1931 <entry>transaction-start</entry>
1932 <entry>(LocalTransactionId)</entry>
1933 <entry>Probe that fires at the start of a new transaction.
1934 arg0 is the transaction ID.</entry>
1937 <entry>transaction-commit</entry>
1938 <entry>(LocalTransactionId)</entry>
1939 <entry>Probe that fires when a transaction completes successfully.
1940 arg0 is the transaction ID.</entry>
1943 <entry>transaction-abort</entry>
1944 <entry>(LocalTransactionId)</entry>
1945 <entry>Probe that fires when a transaction completes unsuccessfully.
1946 arg0 is the transaction ID.</entry>
1949 <entry>query-start</entry>
1950 <entry>(const char *)</entry>
1951 <entry>Probe that fires when the processing of a query is started.
1952 arg0 is the query string.</entry>
1955 <entry>query-done</entry>
1956 <entry>(const char *)</entry>
1957 <entry>Probe that fires when the processing of a query is complete.
1958 arg0 is the query string.</entry>
1961 <entry>query-parse-start</entry>
1962 <entry>(const char *)</entry>
1963 <entry>Probe that fires when the parsing of a query is started.
1964 arg0 is the query string.</entry>
1967 <entry>query-parse-done</entry>
1968 <entry>(const char *)</entry>
1969 <entry>Probe that fires when the parsing of a query is complete.
1970 arg0 is the query string.</entry>
1973 <entry>query-rewrite-start</entry>
1974 <entry>(const char *)</entry>
1975 <entry>Probe that fires when the rewriting of a query is started.
1976 arg0 is the query string.</entry>
1979 <entry>query-rewrite-done</entry>
1980 <entry>(const char *)</entry>
1981 <entry>Probe that fires when the rewriting of a query is complete.
1982 arg0 is the query string.</entry>
1985 <entry>query-plan-start</entry>
1987 <entry>Probe that fires when the planning of a query is started.</entry>
1990 <entry>query-plan-done</entry>
1992 <entry>Probe that fires when the planning of a query is complete.</entry>
1995 <entry>query-execute-start</entry>
1997 <entry>Probe that fires when the execution of a query is started.</entry>
2000 <entry>query-execute-done</entry>
2002 <entry>Probe that fires when the execution of a query is complete.</entry>
2005 <entry>statement-status</entry>
2006 <entry>(const char *)</entry>
2007 <entry>Probe that fires anytime the server process updates its
2008 <structname>pg_stat_activity</>.<structfield>status</>.
2009 arg0 is the new status string.</entry>
2012 <entry>checkpoint-start</entry>
2013 <entry>(int)</entry>
2014 <entry>Probe that fires when a checkpoint is started.
2015 arg0 holds the bitwise flags used to distinguish different checkpoint
2016 types, such as shutdown, immediate or force.</entry>
2019 <entry>checkpoint-done</entry>
2020 <entry>(int, int, int, int, int)</entry>
2021 <entry>Probe that fires when a checkpoint is complete.
2022 (The probes listed next fire in sequence during checkpoint processing.)
2023 arg0 is the number of buffers written. arg1 is the total number of
2024 buffers. arg2, arg3 and arg4 contain the number of xlog file(s) added,
2025 removed and recycled respectively.</entry>
2028 <entry>clog-checkpoint-start</entry>
2029 <entry>(bool)</entry>
2030 <entry>Probe that fires when the CLOG portion of a checkpoint is started.
2031 arg0 is true for normal checkpoint, false for shutdown
2035 <entry>clog-checkpoint-done</entry>
2036 <entry>(bool)</entry>
2037 <entry>Probe that fires when the CLOG portion of a checkpoint is
2038 complete. arg0 has the same meaning as for clog-checkpoint-start.</entry>
2041 <entry>subtrans-checkpoint-start</entry>
2042 <entry>(bool)</entry>
2043 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
2045 arg0 is true for normal checkpoint, false for shutdown
2049 <entry>subtrans-checkpoint-done</entry>
2050 <entry>(bool)</entry>
2051 <entry>Probe that fires when the SUBTRANS portion of a checkpoint is
2052 complete. arg0 has the same meaning as for
2053 subtrans-checkpoint-start.</entry>
2056 <entry>multixact-checkpoint-start</entry>
2057 <entry>(bool)</entry>
2058 <entry>Probe that fires when the MultiXact portion of a checkpoint is
2060 arg0 is true for normal checkpoint, false for shutdown
2064 <entry>multixact-checkpoint-done</entry>
2065 <entry>(bool)</entry>
2066 <entry>Probe that fires when the MultiXact portion of a checkpoint is
2067 complete. arg0 has the same meaning as for
2068 multixact-checkpoint-start.</entry>
2071 <entry>buffer-checkpoint-start</entry>
2072 <entry>(int)</entry>
2073 <entry>Probe that fires when the buffer-writing portion of a checkpoint
2075 arg0 holds the bitwise flags used to distinguish different checkpoint
2076 types, such as shutdown, immediate or force.</entry>
2079 <entry>buffer-sync-start</entry>
2080 <entry>(int, int)</entry>
2081 <entry>Probe that fires when we begin to write dirty buffers during
2082 checkpoint (after identifying which buffers must be written).
2083 arg0 is the total number of buffers.
2084 arg1 is the number that are currently dirty and need to be written.</entry>
2087 <entry>buffer-sync-written</entry>
2088 <entry>(int)</entry>
2089 <entry>Probe that fires after each buffer is written during checkpoint.
2090 arg0 is the ID number of the buffer.</entry>
2093 <entry>buffer-sync-done</entry>
2094 <entry>(int, int, int)</entry>
2095 <entry>Probe that fires when all dirty buffers have been written.
2096 arg0 is the total number of buffers.
2097 arg1 is the number of buffers actually written by the checkpoint process.
2098 arg2 is the number that were expected to be written (arg1 of
2099 buffer-sync-start); any difference reflects other processes flushing
2100 buffers during the checkpoint.</entry>
2103 <entry>buffer-checkpoint-sync-start</entry>
2105 <entry>Probe that fires after dirty buffers have been written to the
2106 kernel, and before starting to issue fsync requests.</entry>
2109 <entry>buffer-checkpoint-done</entry>
2111 <entry>Probe that fires when syncing of buffers to disk is
2115 <entry>twophase-checkpoint-start</entry>
2117 <entry>Probe that fires when the two-phase portion of a checkpoint is
2121 <entry>twophase-checkpoint-done</entry>
2123 <entry>Probe that fires when the two-phase portion of a checkpoint is
2127 <entry>buffer-read-start</entry>
2128 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</entry>
2129 <entry>Probe that fires when a buffer read is started.
2130 arg0 and arg1 contain the fork and block numbers of the page (but
2131 arg1 will be -1 if this is a relation extension request).
2132 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2133 identifying the relation.
2134 arg5 is the ID of the backend which created the temporary relation for a
2135 local buffer, or InvalidBackendId (-1) for a shared buffer.
2136 arg6 is true for a relation extension request, false for normal
2140 <entry>buffer-read-done</entry>
2141 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool, bool)</entry>
2142 <entry>Probe that fires when a buffer read is complete.
2143 arg0 and arg1 contain the fork and block numbers of the page (if this
2144 is a relation extension request, arg1 now contains the block number
2145 of the newly added block).
2146 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2147 identifying the relation.
2148 arg5 is the ID of the backend which created the temporary relation for a
2149 local buffer, or InvalidBackendId (-1) for a shared buffer.
2150 arg6 is true for a relation extension request, false for normal
2152 arg7 is true if the buffer was found in the pool, false if not.</entry>
2155 <entry>buffer-flush-start</entry>
2156 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2157 <entry>Probe that fires before issuing any write request for a shared
2159 arg0 and arg1 contain the fork and block numbers of the page.
2160 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2161 identifying the relation.</entry>
2164 <entry>buffer-flush-done</entry>
2165 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2166 <entry>Probe that fires when a write request is complete. (Note
2167 that this just reflects the time to pass the data to the kernel;
2168 it's typically not actually been written to disk yet.)
2169 The arguments are the same as for buffer-flush-start.</entry>
2172 <entry>buffer-write-dirty-start</entry>
2173 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2174 <entry>Probe that fires when a server process begins to write a dirty
2175 buffer. (If this happens often, it implies that
2176 <xref linkend="guc-shared-buffers"> is too
2177 small or the bgwriter control parameters need adjustment.)
2178 arg0 and arg1 contain the fork and block numbers of the page.
2179 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2180 identifying the relation.</entry>
2183 <entry>buffer-write-dirty-done</entry>
2184 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid)</entry>
2185 <entry>Probe that fires when a dirty-buffer write is complete.
2186 The arguments are the same as for buffer-write-dirty-start.</entry>
2189 <entry>wal-buffer-write-dirty-start</entry>
2191 <entry>Probe that fires when when a server process begins to write a
2192 dirty WAL buffer because no more WAL buffer space is available.
2193 (If this happens often, it implies that
2194 <xref linkend="guc-wal-buffers"> is too small.)</entry>
2197 <entry>wal-buffer-write-dirty-done</entry>
2199 <entry>Probe that fires when a dirty WAL buffer write is complete.</entry>
2202 <entry>xlog-insert</entry>
2203 <entry>(unsigned char, unsigned char)</entry>
2204 <entry>Probe that fires when a WAL record is inserted.
2205 arg0 is the resource manager (rmid) for the record.
2206 arg1 contains the info flags.</entry>
2209 <entry>xlog-switch</entry>
2211 <entry>Probe that fires when a WAL segment switch is requested.</entry>
2214 <entry>smgr-md-read-start</entry>
2215 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
2216 <entry>Probe that fires when beginning to read a block from a relation.
2217 arg0 and arg1 contain the fork and block numbers of the page.
2218 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2219 identifying the relation.
2220 arg5 is the ID of the backend which created the temporary relation for a
2221 local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
2224 <entry>smgr-md-read-done</entry>
2225 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
2226 <entry>Probe that fires when a block read is complete.
2227 arg0 and arg1 contain the fork and block numbers of the page.
2228 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2229 identifying the relation.
2230 arg5 is the ID of the backend which created the temporary relation for a
2231 local buffer, or InvalidBackendId (-1) for a shared buffer.
2232 arg6 is the number of bytes actually read, while arg7 is the number
2233 requested (if these are different it indicates trouble).</entry>
2236 <entry>smgr-md-write-start</entry>
2237 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</entry>
2238 <entry>Probe that fires when beginning to write a block to a relation.
2239 arg0 and arg1 contain the fork and block numbers of the page.
2240 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2241 identifying the relation.
2242 arg5 is the ID of the backend which created the temporary relation for a
2243 local buffer, or InvalidBackendId (-1) for a shared buffer.</entry>
2246 <entry>smgr-md-write-done</entry>
2247 <entry>(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</entry>
2248 <entry>Probe that fires when a block write is complete.
2249 arg0 and arg1 contain the fork and block numbers of the page.
2250 arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
2251 identifying the relation.
2252 arg5 is the ID of the backend which created the temporary relation for a
2253 local buffer, or InvalidBackendId (-1) for a shared buffer.
2254 arg6 is the number of bytes actually written, while arg7 is the number
2255 requested (if these are different it indicates trouble).</entry>
2258 <entry>sort-start</entry>
2259 <entry>(int, bool, int, int, bool)</entry>
2260 <entry>Probe that fires when a sort operation is started.
2261 arg0 indicates heap, index or datum sort.
2262 arg1 is true for unique-value enforcement.
2263 arg2 is the number of key columns.
2264 arg3 is the number of kilobytes of work memory allowed.
2265 arg4 is true if random access to the sort result is required.</entry>
2268 <entry>sort-done</entry>
2269 <entry>(bool, long)</entry>
2270 <entry>Probe that fires when a sort is complete.
2271 arg0 is true for external sort, false for internal sort.
2272 arg1 is the number of disk blocks used for an external sort,
2273 or kilobytes of memory used for an internal sort.</entry>
2276 <entry>lwlock-acquire</entry>
2277 <entry>(LWLockId, LWLockMode)</entry>
2278 <entry>Probe that fires when an LWLock has been acquired.
2279 arg0 is the LWLock's ID.
2280 arg1 is the requested lock mode, either exclusive or shared.</entry>
2283 <entry>lwlock-release</entry>
2284 <entry>(LWLockId)</entry>
2285 <entry>Probe that fires when an LWLock has been released (but note
2286 that any released waiters have not yet been awakened).
2287 arg0 is the LWLock's ID.</entry>
2290 <entry>lwlock-wait-start</entry>
2291 <entry>(LWLockId, LWLockMode)</entry>
2292 <entry>Probe that fires when an LWLock was not immediately available and
2293 a server process has begun to wait for the lock to become available.
2294 arg0 is the LWLock's ID.
2295 arg1 is the requested lock mode, either exclusive or shared.</entry>
2298 <entry>lwlock-wait-done</entry>
2299 <entry>(LWLockId, LWLockMode)</entry>
2300 <entry>Probe that fires when a server process has been released from its
2301 wait for an LWLock (it does not actually have the lock yet).
2302 arg0 is the LWLock's ID.
2303 arg1 is the requested lock mode, either exclusive or shared.</entry>
2306 <entry>lwlock-condacquire</entry>
2307 <entry>(LWLockId, LWLockMode)</entry>
2308 <entry>Probe that fires when an LWLock was successfully acquired when the
2309 caller specified no waiting.
2310 arg0 is the LWLock's ID.
2311 arg1 is the requested lock mode, either exclusive or shared.</entry>
2314 <entry>lwlock-condacquire-fail</entry>
2315 <entry>(LWLockId, LWLockMode)</entry>
2316 <entry>Probe that fires when an LWLock was not successfully acquired when
2317 the caller specified no waiting.
2318 arg0 is the LWLock's ID.
2319 arg1 is the requested lock mode, either exclusive or shared.</entry>
2322 <entry>lock-wait-start</entry>
2323 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
2324 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
2325 has begun to wait because the lock is not available.
2326 arg0 through arg3 are the tag fields identifying the object being
2327 locked. arg4 indicates the type of object being locked.
2328 arg5 indicates the lock type being requested.</entry>
2331 <entry>lock-wait-done</entry>
2332 <entry>(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</entry>
2333 <entry>Probe that fires when a request for a heavyweight lock (lmgr lock)
2334 has finished waiting (i.e., has acquired the lock).
2335 The arguments are the same as for lock-wait-start.</entry>
2338 <entry>deadlock-found</entry>
2340 <entry>Probe that fires when a deadlock is found by the deadlock
2348 <table id="typedefs-table">
2349 <title>Defined Types Used in Probe Parameters</title>
2354 <entry>Definition</entry>
2361 <entry>LocalTransactionId</entry>
2362 <entry>unsigned int</entry>
2365 <entry>LWLockId</entry>
2369 <entry>LWLockMode</entry>
2373 <entry>LOCKMODE</entry>
2377 <entry>BlockNumber</entry>
2378 <entry>unsigned int</entry>
2382 <entry>unsigned int</entry>
2385 <entry>ForkNumber</entry>
2400 <sect2 id="using-trace-points">
2401 <title>Using Probes</title>
2404 The example below shows a DTrace script for analyzing transaction
2405 counts in the system, as an alternative to snapshotting
2406 <structname>pg_stat_database</> before and after a performance test:
2408 #!/usr/sbin/dtrace -qs
2410 postgresql$1:::transaction-start
2412 @start["Start"] = count();
2413 self->ts = timestamp;
2416 postgresql$1:::transaction-abort
2418 @abort["Abort"] = count();
2421 postgresql$1:::transaction-commit
2424 @commit["Commit"] = count();
2425 @time["Total time (ns)"] = sum(timestamp - self->ts);
2429 When executed, the example D script gives output such as:
2431 # ./txn_count.d `pgrep -n postgres` or ./txn_count.d <PID>
2436 Total time (ns) 2312105013
2442 SystemTap uses a different notation for trace scripts than DTrace does,
2443 even though the underlying trace points are compatible. One point worth
2444 noting is that at this writing, SystemTap scripts must reference probe
2445 names using double underscores in place of hyphens. This is expected to
2446 be fixed in future SystemTap releases.
2451 You should remember that DTrace scripts need to be carefully written and
2452 debugged, otherwise the trace information collected might
2453 be meaningless. In most cases where problems are found it is the
2454 instrumentation that is at fault, not the underlying system. When
2455 discussing information found using dynamic tracing, be sure to enclose
2456 the script used to allow that too to be checked and discussed.
2460 More example scripts can be found in the PgFoundry
2461 <ulink url="http://pgfoundry.org/projects/dtrace/">dtrace project</ulink>.
2465 <sect2 id="defining-trace-points">
2466 <title>Defining New Probes</title>
2469 New probes can be defined within the code wherever the developer
2470 desires, though this will require a recompilation. Below are the steps
2471 for inserting new probes:
2477 Decide on probe names and data to be made available through the probes
2483 Add the probe definitions to <filename>src/backend/utils/probes.d</>
2489 Include <filename>pg_trace.h</> if it is not already present in the
2490 module(s) containing the probe points, and insert
2491 <literal>TRACE_POSTGRESQL</> probe macros at the desired locations
2498 Recompile and verify that the new probes are available
2504 <title>Example:</title>
2506 Here is an example of how you would add a probe to trace all new
2507 transactions by transaction ID.
2514 Decide that the probe will be named <literal>transaction-start</> and
2515 requires a parameter of type LocalTransactionId
2521 Add the probe definition to <filename>src/backend/utils/probes.d</>:
2523 probe transaction__start(LocalTransactionId);
2525 Note the use of the double underline in the probe name. In a DTrace
2526 script using the probe, the double underline needs to be replaced with a
2527 hyphen, so <literal>transaction-start</> is the name to document for
2534 At compile time, <literal>transaction__start</> is converted to a macro
2535 called <literal>TRACE_POSTGRESQL_TRANSACTION_START</> (notice the
2536 underscores are single here), which is available by including
2537 <filename>pg_trace.h</>. Add the macro call to the appropriate location
2538 in the source code. In this case, it looks like the following:
2541 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
2548 After recompiling and running the new binary, check that your newly added
2549 probe is available by executing the following DTrace command. You
2550 should see similar output:
2552 # dtrace -ln transaction-start
2553 ID PROVIDER MODULE FUNCTION NAME
2554 18705 postgresql49878 postgres StartTransactionCommand transaction-start
2555 18755 postgresql49877 postgres StartTransactionCommand transaction-start
2556 18805 postgresql49876 postgres StartTransactionCommand transaction-start
2557 18855 postgresql49875 postgres StartTransactionCommand transaction-start
2558 18986 postgresql49873 postgres StartTransactionCommand transaction-start
2565 There are a few things to be careful about when adding trace macros
2571 You should take care that the data types specified for a probe's
2572 parameters match the data types of the variables used in the macro.
2573 Otherwise, you will get compilation errors.
2580 On most platforms, if <productname>PostgreSQL</productname> is
2581 built with <option>--enable-dtrace</>, the arguments to a trace
2582 macro will be evaluated whenever control passes through the
2583 macro, <emphasis>even if no tracing is being done</>. This is
2584 usually not worth worrying about if you are just reporting the
2585 values of a few local variables. But beware of putting expensive
2586 function calls into the arguments. If you need to do that,
2587 consider protecting the macro with a check to see if the trace
2588 is actually enabled:
2591 if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
2592 TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
2595 Each trace macro has a corresponding <literal>ENABLED</> macro.