gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 line
31KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>27.5. Dynamic Tracing</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="progress-reporting.html" title="27.4. Progress Reporting" /><link rel="next" href="diskusage.html" title="Chapter 28. Monitoring Disk Usage" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">27.5. Dynamic Tracing</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="progress-reporting.html" title="27.4. Progress Reporting">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="monitoring.html" title="Chapter 27. Monitoring Database Activity">Up</a></td><th width="60%" align="center">Chapter 27. Monitoring Database Activity</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="diskusage.html" title="Chapter 28. Monitoring Disk Usage">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="DYNAMIC-TRACE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">27.5. Dynamic Tracing</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="dynamic-trace.html#COMPILING-FOR-TRACE">27.5.1. Compiling for Dynamic Tracing</a></span></dt><dt><span class="sect2"><a href="dynamic-trace.html#TRACE-POINTS">27.5.2. Built-in Probes</a></span></dt><dt><span class="sect2"><a href="dynamic-trace.html#USING-TRACE-POINTS">27.5.3. Using Probes</a></span></dt><dt><span class="sect2"><a href="dynamic-trace.html#DEFINING-TRACE-POINTS">27.5.4. Defining New Probes</a></span></dt></dl></div><a id="id-1.6.14.10.2" class="indexterm"></a><p>
  3. <span class="productname">PostgreSQL</span> provides facilities to support
  4. dynamic tracing of the database server. This allows an external
  5. utility to be called at specific points in the code and thereby trace
  6. execution.
  7. </p><p>
  8. A number of probes or trace points are already inserted into the source
  9. code. These probes are intended to be used by database developers and
  10. administrators. By default the probes are not compiled into
  11. <span class="productname">PostgreSQL</span>; the user needs to explicitly tell
  12. the configure script to make the probes available.
  13. </p><p>
  14. Currently, the
  15. <a class="ulink" href="https://en.wikipedia.org/wiki/DTrace" target="_top">DTrace</a>
  16. utility is supported, which, at the time of this writing, is available
  17. on Solaris, macOS, FreeBSD, NetBSD, and Oracle Linux. The
  18. <a class="ulink" href="https://sourceware.org/systemtap/" target="_top">SystemTap</a> project
  19. for Linux provides a DTrace equivalent and can also be used. Supporting other dynamic
  20. tracing utilities is theoretically possible by changing the definitions for
  21. the macros in <code class="filename">src/include/utils/probes.h</code>.
  22. </p><div class="sect2" id="COMPILING-FOR-TRACE"><div class="titlepage"><div><div><h3 class="title">27.5.1. Compiling for Dynamic Tracing</h3></div></div></div><p>
  23. By default, probes are not available, so you will need to
  24. explicitly tell the configure script to make the probes available
  25. in <span class="productname">PostgreSQL</span>. To include DTrace support
  26. specify <code class="option">--enable-dtrace</code> to configure. See <a class="xref" href="install-procedure.html" title="16.4. Installation Procedure">Section 16.4</a> for further information.
  27. </p></div><div class="sect2" id="TRACE-POINTS"><div class="titlepage"><div><div><h3 class="title">27.5.2. Built-in Probes</h3></div></div></div><p>
  28. A number of standard probes are provided in the source code,
  29. as shown in <a class="xref" href="dynamic-trace.html#DTRACE-PROBE-POINT-TABLE" title="Table 27.28. Built-in DTrace Probes">Table 27.28</a>;
  30. <a class="xref" href="dynamic-trace.html#TYPEDEFS-TABLE" title="Table 27.29. Defined Types Used in Probe Parameters">Table 27.29</a>
  31. shows the types used in the probes. More probes can certainly be
  32. added to enhance <span class="productname">PostgreSQL</span>'s observability.
  33. </p><div class="table" id="DTRACE-PROBE-POINT-TABLE"><p class="title"><strong>Table 27.28. Built-in DTrace Probes</strong></p><div class="table-contents"><table class="table" summary="Built-in DTrace Probes" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Parameters</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal">transaction-start</code></td><td><code class="literal">(LocalTransactionId)</code></td><td>Probe that fires at the start of a new transaction.
  34. arg0 is the transaction ID.</td></tr><tr><td><code class="literal">transaction-commit</code></td><td><code class="literal">(LocalTransactionId)</code></td><td>Probe that fires when a transaction completes successfully.
  35. arg0 is the transaction ID.</td></tr><tr><td><code class="literal">transaction-abort</code></td><td><code class="literal">(LocalTransactionId)</code></td><td>Probe that fires when a transaction completes unsuccessfully.
  36. arg0 is the transaction ID.</td></tr><tr><td><code class="literal">query-start</code></td><td><code class="literal">(const char *)</code></td><td>Probe that fires when the processing of a query is started.
  37. arg0 is the query string.</td></tr><tr><td><code class="literal">query-done</code></td><td><code class="literal">(const char *)</code></td><td>Probe that fires when the processing of a query is complete.
  38. arg0 is the query string.</td></tr><tr><td><code class="literal">query-parse-start</code></td><td><code class="literal">(const char *)</code></td><td>Probe that fires when the parsing of a query is started.
  39. arg0 is the query string.</td></tr><tr><td><code class="literal">query-parse-done</code></td><td><code class="literal">(const char *)</code></td><td>Probe that fires when the parsing of a query is complete.
  40. arg0 is the query string.</td></tr><tr><td><code class="literal">query-rewrite-start</code></td><td><code class="literal">(const char *)</code></td><td>Probe that fires when the rewriting of a query is started.
  41. arg0 is the query string.</td></tr><tr><td><code class="literal">query-rewrite-done</code></td><td><code class="literal">(const char *)</code></td><td>Probe that fires when the rewriting of a query is complete.
  42. arg0 is the query string.</td></tr><tr><td><code class="literal">query-plan-start</code></td><td><code class="literal">()</code></td><td>Probe that fires when the planning of a query is started.</td></tr><tr><td><code class="literal">query-plan-done</code></td><td><code class="literal">()</code></td><td>Probe that fires when the planning of a query is complete.</td></tr><tr><td><code class="literal">query-execute-start</code></td><td><code class="literal">()</code></td><td>Probe that fires when the execution of a query is started.</td></tr><tr><td><code class="literal">query-execute-done</code></td><td><code class="literal">()</code></td><td>Probe that fires when the execution of a query is complete.</td></tr><tr><td><code class="literal">statement-status</code></td><td><code class="literal">(const char *)</code></td><td>Probe that fires anytime the server process updates its
  43. <code class="structname">pg_stat_activity</code>.<code class="structfield">status</code>.
  44. arg0 is the new status string.</td></tr><tr><td><code class="literal">checkpoint-start</code></td><td><code class="literal">(int)</code></td><td>Probe that fires when a checkpoint is started.
  45. arg0 holds the bitwise flags used to distinguish different checkpoint
  46. types, such as shutdown, immediate or force.</td></tr><tr><td><code class="literal">checkpoint-done</code></td><td><code class="literal">(int, int, int, int, int)</code></td><td>Probe that fires when a checkpoint is complete.
  47. (The probes listed next fire in sequence during checkpoint processing.)
  48. arg0 is the number of buffers written. arg1 is the total number of
  49. buffers. arg2, arg3 and arg4 contain the number of WAL files added,
  50. removed and recycled respectively.</td></tr><tr><td><code class="literal">clog-checkpoint-start</code></td><td><code class="literal">(bool)</code></td><td>Probe that fires when the CLOG portion of a checkpoint is started.
  51. arg0 is true for normal checkpoint, false for shutdown
  52. checkpoint.</td></tr><tr><td><code class="literal">clog-checkpoint-done</code></td><td><code class="literal">(bool)</code></td><td>Probe that fires when the CLOG portion of a checkpoint is
  53. complete. arg0 has the same meaning as for <code class="literal">clog-checkpoint-start</code>.</td></tr><tr><td><code class="literal">subtrans-checkpoint-start</code></td><td><code class="literal">(bool)</code></td><td>Probe that fires when the SUBTRANS portion of a checkpoint is
  54. started.
  55. arg0 is true for normal checkpoint, false for shutdown
  56. checkpoint.</td></tr><tr><td><code class="literal">subtrans-checkpoint-done</code></td><td><code class="literal">(bool)</code></td><td>Probe that fires when the SUBTRANS portion of a checkpoint is
  57. complete. arg0 has the same meaning as for
  58. <code class="literal">subtrans-checkpoint-start</code>.</td></tr><tr><td><code class="literal">multixact-checkpoint-start</code></td><td><code class="literal">(bool)</code></td><td>Probe that fires when the MultiXact portion of a checkpoint is
  59. started.
  60. arg0 is true for normal checkpoint, false for shutdown
  61. checkpoint.</td></tr><tr><td><code class="literal">multixact-checkpoint-done</code></td><td><code class="literal">(bool)</code></td><td>Probe that fires when the MultiXact portion of a checkpoint is
  62. complete. arg0 has the same meaning as for
  63. <code class="literal">multixact-checkpoint-start</code>.</td></tr><tr><td><code class="literal">buffer-checkpoint-start</code></td><td><code class="literal">(int)</code></td><td>Probe that fires when the buffer-writing portion of a checkpoint
  64. is started.
  65. arg0 holds the bitwise flags used to distinguish different checkpoint
  66. types, such as shutdown, immediate or force.</td></tr><tr><td><code class="literal">buffer-sync-start</code></td><td><code class="literal">(int, int)</code></td><td>Probe that fires when we begin to write dirty buffers during
  67. checkpoint (after identifying which buffers must be written).
  68. arg0 is the total number of buffers.
  69. arg1 is the number that are currently dirty and need to be written.</td></tr><tr><td><code class="literal">buffer-sync-written</code></td><td><code class="literal">(int)</code></td><td>Probe that fires after each buffer is written during checkpoint.
  70. arg0 is the ID number of the buffer.</td></tr><tr><td><code class="literal">buffer-sync-done</code></td><td><code class="literal">(int, int, int)</code></td><td>Probe that fires when all dirty buffers have been written.
  71. arg0 is the total number of buffers.
  72. arg1 is the number of buffers actually written by the checkpoint process.
  73. arg2 is the number that were expected to be written (arg1 of
  74. <code class="literal">buffer-sync-start</code>); any difference reflects other processes flushing
  75. buffers during the checkpoint.</td></tr><tr><td><code class="literal">buffer-checkpoint-sync-start</code></td><td><code class="literal">()</code></td><td>Probe that fires after dirty buffers have been written to the
  76. kernel, and before starting to issue fsync requests.</td></tr><tr><td><code class="literal">buffer-checkpoint-done</code></td><td><code class="literal">()</code></td><td>Probe that fires when syncing of buffers to disk is
  77. complete.</td></tr><tr><td><code class="literal">twophase-checkpoint-start</code></td><td><code class="literal">()</code></td><td>Probe that fires when the two-phase portion of a checkpoint is
  78. started.</td></tr><tr><td><code class="literal">twophase-checkpoint-done</code></td><td><code class="literal">()</code></td><td>Probe that fires when the two-phase portion of a checkpoint is
  79. complete.</td></tr><tr><td><code class="literal">buffer-read-start</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool)</code></td><td>Probe that fires when a buffer read is started.
  80. arg0 and arg1 contain the fork and block numbers of the page (but
  81. arg1 will be -1 if this is a relation extension request).
  82. arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
  83. identifying the relation.
  84. arg5 is the ID of the backend which created the temporary relation for a
  85. local buffer, or <code class="symbol">InvalidBackendId</code> (-1) for a shared buffer.
  86. arg6 is true for a relation extension request, false for normal
  87. read.</td></tr><tr><td><code class="literal">buffer-read-done</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid, int, bool, bool)</code></td><td>Probe that fires when a buffer read is complete.
  88. arg0 and arg1 contain the fork and block numbers of the page (if this
  89. is a relation extension request, arg1 now contains the block number
  90. of the newly added block).
  91. arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
  92. identifying the relation.
  93. arg5 is the ID of the backend which created the temporary relation for a
  94. local buffer, or <code class="symbol">InvalidBackendId</code> (-1) for a shared buffer.
  95. arg6 is true for a relation extension request, false for normal
  96. read.
  97. arg7 is true if the buffer was found in the pool, false if not.</td></tr><tr><td><code class="literal">buffer-flush-start</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid)</code></td><td>Probe that fires before issuing any write request for a shared
  98. buffer.
  99. arg0 and arg1 contain the fork and block numbers of the page.
  100. arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
  101. identifying the relation.</td></tr><tr><td><code class="literal">buffer-flush-done</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid)</code></td><td>Probe that fires when a write request is complete. (Note
  102. that this just reflects the time to pass the data to the kernel;
  103. it's typically not actually been written to disk yet.)
  104. The arguments are the same as for <code class="literal">buffer-flush-start</code>.</td></tr><tr><td><code class="literal">buffer-write-dirty-start</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid)</code></td><td>Probe that fires when a server process begins to write a dirty
  105. buffer. (If this happens often, it implies that
  106. <a class="xref" href="runtime-config-resource.html#GUC-SHARED-BUFFERS">shared_buffers</a> is too
  107. small or the background writer control parameters need adjustment.)
  108. arg0 and arg1 contain the fork and block numbers of the page.
  109. arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
  110. identifying the relation.</td></tr><tr><td><code class="literal">buffer-write-dirty-done</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid)</code></td><td>Probe that fires when a dirty-buffer write is complete.
  111. The arguments are the same as for <code class="literal">buffer-write-dirty-start</code>.</td></tr><tr><td><code class="literal">wal-buffer-write-dirty-start</code></td><td><code class="literal">()</code></td><td>Probe that fires when a server process begins to write a
  112. dirty WAL buffer because no more WAL buffer space is available.
  113. (If this happens often, it implies that
  114. <a class="xref" href="runtime-config-wal.html#GUC-WAL-BUFFERS">wal_buffers</a> is too small.)</td></tr><tr><td><code class="literal">wal-buffer-write-dirty-done</code></td><td><code class="literal">()</code></td><td>Probe that fires when a dirty WAL buffer write is complete.</td></tr><tr><td><code class="literal">wal-insert</code></td><td><code class="literal">(unsigned char, unsigned char)</code></td><td>Probe that fires when a WAL record is inserted.
  115. arg0 is the resource manager (rmid) for the record.
  116. arg1 contains the info flags.</td></tr><tr><td><code class="literal">wal-switch</code></td><td><code class="literal">()</code></td><td>Probe that fires when a WAL segment switch is requested.</td></tr><tr><td><code class="literal">smgr-md-read-start</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</code></td><td>Probe that fires when beginning to read a block from a relation.
  117. arg0 and arg1 contain the fork and block numbers of the page.
  118. arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
  119. identifying the relation.
  120. arg5 is the ID of the backend which created the temporary relation for a
  121. local buffer, or <code class="symbol">InvalidBackendId</code> (-1) for a shared buffer.</td></tr><tr><td><code class="literal">smgr-md-read-done</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</code></td><td>Probe that fires when a block read is complete.
  122. arg0 and arg1 contain the fork and block numbers of the page.
  123. arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
  124. identifying the relation.
  125. arg5 is the ID of the backend which created the temporary relation for a
  126. local buffer, or <code class="symbol">InvalidBackendId</code> (-1) for a shared buffer.
  127. arg6 is the number of bytes actually read, while arg7 is the number
  128. requested (if these are different it indicates trouble).</td></tr><tr><td><code class="literal">smgr-md-write-start</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid, int)</code></td><td>Probe that fires when beginning to write a block to a relation.
  129. arg0 and arg1 contain the fork and block numbers of the page.
  130. arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
  131. identifying the relation.
  132. arg5 is the ID of the backend which created the temporary relation for a
  133. local buffer, or <code class="symbol">InvalidBackendId</code> (-1) for a shared buffer.</td></tr><tr><td><code class="literal">smgr-md-write-done</code></td><td><code class="literal">(ForkNumber, BlockNumber, Oid, Oid, Oid, int, int, int)</code></td><td>Probe that fires when a block write is complete.
  134. arg0 and arg1 contain the fork and block numbers of the page.
  135. arg2, arg3, and arg4 contain the tablespace, database, and relation OIDs
  136. identifying the relation.
  137. arg5 is the ID of the backend which created the temporary relation for a
  138. local buffer, or <code class="symbol">InvalidBackendId</code> (-1) for a shared buffer.
  139. arg6 is the number of bytes actually written, while arg7 is the number
  140. requested (if these are different it indicates trouble).</td></tr><tr><td><code class="literal">sort-start</code></td><td><code class="literal">(int, bool, int, int, bool, int)</code></td><td>Probe that fires when a sort operation is started.
  141. arg0 indicates heap, index or datum sort.
  142. arg1 is true for unique-value enforcement.
  143. arg2 is the number of key columns.
  144. arg3 is the number of kilobytes of work memory allowed.
  145. arg4 is true if random access to the sort result is required.
  146. arg5 indicates serial when <code class="literal">0</code>, parallel worker when
  147. <code class="literal">1</code>, or parallel leader when <code class="literal">2</code>.</td></tr><tr><td><code class="literal">sort-done</code></td><td><code class="literal">(bool, long)</code></td><td>Probe that fires when a sort is complete.
  148. arg0 is true for external sort, false for internal sort.
  149. arg1 is the number of disk blocks used for an external sort,
  150. or kilobytes of memory used for an internal sort.</td></tr><tr><td><code class="literal">lwlock-acquire</code></td><td><code class="literal">(char *, LWLockMode)</code></td><td>Probe that fires when an LWLock has been acquired.
  151. arg0 is the LWLock's tranche.
  152. arg1 is the requested lock mode, either exclusive or shared.</td></tr><tr><td><code class="literal">lwlock-release</code></td><td><code class="literal">(char *)</code></td><td>Probe that fires when an LWLock has been released (but note
  153. that any released waiters have not yet been awakened).
  154. arg0 is the LWLock's tranche.</td></tr><tr><td><code class="literal">lwlock-wait-start</code></td><td><code class="literal">(char *, LWLockMode)</code></td><td>Probe that fires when an LWLock was not immediately available and
  155. a server process has begun to wait for the lock to become available.
  156. arg0 is the LWLock's tranche.
  157. arg1 is the requested lock mode, either exclusive or shared.</td></tr><tr><td><code class="literal">lwlock-wait-done</code></td><td><code class="literal">(char *, LWLockMode)</code></td><td>Probe that fires when a server process has been released from its
  158. wait for an LWLock (it does not actually have the lock yet).
  159. arg0 is the LWLock's tranche.
  160. arg1 is the requested lock mode, either exclusive or shared.</td></tr><tr><td><code class="literal">lwlock-condacquire</code></td><td><code class="literal">(char *, LWLockMode)</code></td><td>Probe that fires when an LWLock was successfully acquired when the
  161. caller specified no waiting.
  162. arg0 is the LWLock's tranche.
  163. arg1 is the requested lock mode, either exclusive or shared.</td></tr><tr><td><code class="literal">lwlock-condacquire-fail</code></td><td><code class="literal">(char *, LWLockMode)</code></td><td>Probe that fires when an LWLock was not successfully acquired when
  164. the caller specified no waiting.
  165. arg0 is the LWLock's tranche.
  166. arg1 is the requested lock mode, either exclusive or shared.</td></tr><tr><td><code class="literal">lock-wait-start</code></td><td><code class="literal">(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</code></td><td>Probe that fires when a request for a heavyweight lock (lmgr lock)
  167. has begun to wait because the lock is not available.
  168. arg0 through arg3 are the tag fields identifying the object being
  169. locked. arg4 indicates the type of object being locked.
  170. arg5 indicates the lock type being requested.</td></tr><tr><td><code class="literal">lock-wait-done</code></td><td><code class="literal">(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, LOCKMODE)</code></td><td>Probe that fires when a request for a heavyweight lock (lmgr lock)
  171. has finished waiting (i.e., has acquired the lock).
  172. The arguments are the same as for <code class="literal">lock-wait-start</code>.</td></tr><tr><td><code class="literal">deadlock-found</code></td><td><code class="literal">()</code></td><td>Probe that fires when a deadlock is found by the deadlock
  173. detector.</td></tr></tbody></table></div></div><br class="table-break" /><div class="table" id="TYPEDEFS-TABLE"><p class="title"><strong>Table 27.29. Defined Types Used in Probe Parameters</strong></p><div class="table-contents"><table class="table" summary="Defined Types Used in Probe Parameters" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Type</th><th>Definition</th></tr></thead><tbody><tr><td><code class="type">LocalTransactionId</code></td><td><code class="type">unsigned int</code></td></tr><tr><td><code class="type">LWLockMode</code></td><td><code class="type">int</code></td></tr><tr><td><code class="type">LOCKMODE</code></td><td><code class="type">int</code></td></tr><tr><td><code class="type">BlockNumber</code></td><td><code class="type">unsigned int</code></td></tr><tr><td><code class="type">Oid</code></td><td><code class="type">unsigned int</code></td></tr><tr><td><code class="type">ForkNumber</code></td><td><code class="type">int</code></td></tr><tr><td><code class="type">bool</code></td><td><code class="type">unsigned char</code></td></tr></tbody></table></div></div><br class="table-break" /></div><div class="sect2" id="USING-TRACE-POINTS"><div class="titlepage"><div><div><h3 class="title">27.5.3. Using Probes</h3></div></div></div><p>
  174. The example below shows a DTrace script for analyzing transaction
  175. counts in the system, as an alternative to snapshotting
  176. <code class="structname">pg_stat_database</code> before and after a performance test:
  177. </p><pre class="programlisting">
  178. #!/usr/sbin/dtrace -qs
  179. postgresql$1:::transaction-start
  180. {
  181. @start["Start"] = count();
  182. self-&gt;ts = timestamp;
  183. }
  184. postgresql$1:::transaction-abort
  185. {
  186. @abort["Abort"] = count();
  187. }
  188. postgresql$1:::transaction-commit
  189. /self-&gt;ts/
  190. {
  191. @commit["Commit"] = count();
  192. @time["Total time (ns)"] = sum(timestamp - self-&gt;ts);
  193. self-&gt;ts=0;
  194. }
  195. </pre><p>
  196. When executed, the example D script gives output such as:
  197. </p><pre class="screen">
  198. # ./txn_count.d `pgrep -n postgres` or ./txn_count.d &lt;PID&gt;
  199. ^C
  200. Start 71
  201. Commit 70
  202. Total time (ns) 2312105013
  203. </pre><p>
  204. </p><div class="note"><h3 class="title">Note</h3><p>
  205. SystemTap uses a different notation for trace scripts than DTrace does,
  206. even though the underlying trace points are compatible. One point worth
  207. noting is that at this writing, SystemTap scripts must reference probe
  208. names using double underscores in place of hyphens. This is expected to
  209. be fixed in future SystemTap releases.
  210. </p></div><p>
  211. You should remember that DTrace scripts need to be carefully written and
  212. debugged, otherwise the trace information collected might
  213. be meaningless. In most cases where problems are found it is the
  214. instrumentation that is at fault, not the underlying system. When
  215. discussing information found using dynamic tracing, be sure to enclose
  216. the script used to allow that too to be checked and discussed.
  217. </p></div><div class="sect2" id="DEFINING-TRACE-POINTS"><div class="titlepage"><div><div><h3 class="title">27.5.4. Defining New Probes</h3></div></div></div><p>
  218. New probes can be defined within the code wherever the developer
  219. desires, though this will require a recompilation. Below are the steps
  220. for inserting new probes:
  221. </p><div class="procedure"><ol class="procedure" type="1"><li class="step"><p>
  222. Decide on probe names and data to be made available through the probes
  223. </p></li><li class="step"><p>
  224. Add the probe definitions to <code class="filename">src/backend/utils/probes.d</code>
  225. </p></li><li class="step"><p>
  226. Include <code class="filename">pg_trace.h</code> if it is not already present in the
  227. module(s) containing the probe points, and insert
  228. <code class="literal">TRACE_POSTGRESQL</code> probe macros at the desired locations
  229. in the source code
  230. </p></li><li class="step"><p>
  231. Recompile and verify that the new probes are available
  232. </p></li></ol></div><p><strong>Example: </strong>
  233. Here is an example of how you would add a probe to trace all new
  234. transactions by transaction ID.
  235. </p><div class="procedure"><ol class="procedure" type="1"><li class="step"><p>
  236. Decide that the probe will be named <code class="literal">transaction-start</code> and
  237. requires a parameter of type <code class="type">LocalTransactionId</code>
  238. </p></li><li class="step"><p>
  239. Add the probe definition to <code class="filename">src/backend/utils/probes.d</code>:
  240. </p><pre class="programlisting">
  241. probe transaction__start(LocalTransactionId);
  242. </pre><p>
  243. Note the use of the double underline in the probe name. In a DTrace
  244. script using the probe, the double underline needs to be replaced with a
  245. hyphen, so <code class="literal">transaction-start</code> is the name to document for
  246. users.
  247. </p></li><li class="step"><p>
  248. At compile time, <code class="literal">transaction__start</code> is converted to a macro
  249. called <code class="literal">TRACE_POSTGRESQL_TRANSACTION_START</code> (notice the
  250. underscores are single here), which is available by including
  251. <code class="filename">pg_trace.h</code>. Add the macro call to the appropriate location
  252. in the source code. In this case, it looks like the following:
  253. </p><pre class="programlisting">
  254. TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
  255. </pre><p>
  256. </p></li><li class="step"><p>
  257. After recompiling and running the new binary, check that your newly added
  258. probe is available by executing the following DTrace command. You
  259. should see similar output:
  260. </p><pre class="screen">
  261. # dtrace -ln transaction-start
  262. ID PROVIDER MODULE FUNCTION NAME
  263. 18705 postgresql49878 postgres StartTransactionCommand transaction-start
  264. 18755 postgresql49877 postgres StartTransactionCommand transaction-start
  265. 18805 postgresql49876 postgres StartTransactionCommand transaction-start
  266. 18855 postgresql49875 postgres StartTransactionCommand transaction-start
  267. 18986 postgresql49873 postgres StartTransactionCommand transaction-start
  268. </pre><p>
  269. </p></li></ol></div><p>
  270. There are a few things to be careful about when adding trace macros
  271. to the C code:
  272. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  273. You should take care that the data types specified for a probe's
  274. parameters match the data types of the variables used in the macro.
  275. Otherwise, you will get compilation errors.
  276. </p></li><li class="listitem"><p>
  277. On most platforms, if <span class="productname">PostgreSQL</span> is
  278. built with <code class="option">--enable-dtrace</code>, the arguments to a trace
  279. macro will be evaluated whenever control passes through the
  280. macro, <span class="emphasis"><em>even if no tracing is being done</em></span>. This is
  281. usually not worth worrying about if you are just reporting the
  282. values of a few local variables. But beware of putting expensive
  283. function calls into the arguments. If you need to do that,
  284. consider protecting the macro with a check to see if the trace
  285. is actually enabled:
  286. </p><pre class="programlisting">
  287. if (TRACE_POSTGRESQL_TRANSACTION_START_ENABLED())
  288. TRACE_POSTGRESQL_TRANSACTION_START(some_function(...));
  289. </pre><p>
  290. Each trace macro has a corresponding <code class="literal">ENABLED</code> macro.
  291. </p></li></ul></div><p>
  292. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="progress-reporting.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="monitoring.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="diskusage.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">27.4. Progress Reporting </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 28. Monitoring Disk Usage</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1