1- <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.55 2007/11/28 15:42:31 petere Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.56 2008/03/25 22:50:27 neilc Exp $ -->
22
33<chapter id="monitoring">
44 <title>Monitoring Database Activity</title>
@@ -947,26 +947,26 @@ SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
947947 </para>
948948
949949 <para>
950- A number oftrace points, often called probes, are already inserted
951- into the source code. By default these probes aredisabled, and
952- the user needs to explicitly tell the configure script to make the
953- probes available in <productname>PostgreSQL</productname>.
950+ A number ofprobes or trace points are already inserted
951+ into the source code. By default these probes arenot compiled into the
952+ binary, and the user needs to explicitly tell the configure script to make
953+ the probes available in <productname>PostgreSQL</productname>.
954954 </para>
955955
956956 <para>
957- Currently, only the DTrace utility is supported, which isonly available
958- on Solaris Express and Solaris 10+ . It is expected that DTrace will
959- be available in the future on FreeBSD and Mac OS X .
957+ Currently, only the DTrace utility is supported, which is available
958+ on Solaris Express, Solaris 10, and Mac OS X Leopard . It is expected that
959+ DTrace will be available in the future on FreeBSD.
960960 Supporting other dynamic tracing utilities is theoretically possible by
961- changing the definitions for the<literal>PG_TRACE</> macros in
962- <filename>src/include/pg_trace .h</>.
961+ changing the definitions for the macros in
962+ <filename>src/include/utils/probes .h</>.
963963 </para>
964964
965965 <sect2 id="compiling-for-trace">
966966 <title>Compiling for Dynamic Tracing</title>
967967
968968 <para>
969- By default,trace points aredisabled , so you will need to
969+ By default,probes arenot available , so you will need to
970970 explicitly tell the configure script to make the probes available
971971 in <productname>PostgreSQL</productname>. To include DTrace support
972972 specify <option>--enable-dtrace</> to configure. See <xref
@@ -975,16 +975,16 @@ SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
975975 </sect2>
976976
977977 <sect2 id="trace-points">
978- <title>Built-inTrace Points </title>
978+ <title>Built-inProbes </title>
979979
980980 <para>
981- A few standardtrace points are provided in the source code
981+ A few standardprobes are provided in the source code
982982 (of course, more can be added as needed for a particular problem).
983983 These are shown in <xref linkend="trace-point-table">.
984984 </para>
985985
986986 <table id="trace-point-table">
987- <title>Built-inTrace Points </title>
987+ <title>Built-inProbes </title>
988988 <tgroup cols="3">
989989 <thead>
990990 <row>
@@ -996,67 +996,67 @@ SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
996996
997997 <tbody>
998998 <row>
999- <entry>transaction__start </entry>
999+ <entry>transaction-start </entry>
10001000 <entry>(int transactionId)</entry>
10011001 <entry>The start of a new transaction.</entry>
10021002 </row>
10031003 <row>
1004- <entry>transaction__commit </entry>
1004+ <entry>transaction-commit </entry>
10051005 <entry>(int transactionId)</entry>
10061006 <entry>The successful completion of a transaction.</entry>
10071007 </row>
10081008 <row>
1009- <entry>transaction__abort </entry>
1009+ <entry>transaction-abort </entry>
10101010 <entry>(int transactionId)</entry>
10111011 <entry>The unsuccessful completion of a transaction.</entry>
10121012 </row>
10131013
10141014 <row>
1015- <entry>lwlock__acquire </entry>
1015+ <entry>lwlock-acquire </entry>
10161016 <entry>(int lockid, int mode)</entry>
10171017 <entry>An LWLock has been acquired.</entry>
10181018 </row>
10191019 <row>
1020- <entry>lwlock__release </entry>
1020+ <entry>lwlock-release </entry>
10211021 <entry>(int lockid, int mode)</entry>
10221022 <entry>An LWLock has been released.</entry>
10231023 </row>
10241024 <row>
1025- <entry>lwlock__startwait </entry>
1025+ <entry>lwlock-startwait </entry>
10261026 <entry>(int lockid, int mode)</entry>
10271027 <entry>An LWLock was not immediately available and a backend
10281028 has begun to wait for the lock to become available.
10291029 </entry>
10301030 </row>
10311031 <row>
1032- <entry>lwlock__endwait </entry>
1032+ <entry>lwlock-endwait </entry>
10331033 <entry>(int lockid, int mode)</entry>
10341034 <entry>A backend has been released from its wait for an LWLock.
10351035 </entry>
10361036 </row>
10371037 <row>
1038- <entry>lwlock__condacquire </entry>
1038+ <entry>lwlock-condacquire </entry>
10391039 <entry>(int lockid, int mode)</entry>
10401040 <entry>An LWLock was successfully acquired when the caller specified no
10411041 waiting.
10421042 </entry>
10431043 </row>
10441044 <row>
1045- <entry>lwlock__condacquire__fail </entry>
1045+ <entry>lwlock-condacquire-fail </entry>
10461046 <entry>(int lockid, int mode)</entry>
10471047 <entry>An LWLock was not successfully acquired when the caller specified
10481048 no waiting.
10491049 </entry>
10501050 </row>
10511051 <row>
1052- <entry>lock__startwait </entry>
1052+ <entry>lock-startwait </entry>
10531053 <entry>(int locktag_field2, int lockmode)</entry>
10541054 <entry>A request for a heavyweight lock (lmgr lock) has begun to wait
10551055 because the lock is not available.
10561056 </entry>
10571057 </row>
10581058 <row>
1059- <entry>lock__endwait </entry>
1059+ <entry>lock-endwait </entry>
10601060 <entry>(int locktag_field2, int lockmode)</entry>
10611061 <entry>A request for a heavyweight lock (lmgr lock) has finished waiting
10621062 (i.e., has acquired the lock).
@@ -1068,7 +1068,7 @@ SELECT pg_stat_get_backend_pid(s.backendid) AS procpid,
10681068 </sect2>
10691069
10701070 <sect2 id="using-trace-points">
1071- <title>UsingTrace Points </title>
1071+ <title>UsingProbes </title>
10721072
10731073 <para>
10741074 The example below shows a DTrace script for analyzing transaction
@@ -1096,11 +1096,9 @@ postgresql$1:::transaction-commit
10961096 self->ts=0;
10971097}
10981098</programlisting>
1099- Note how the double underline in trace point names needs to
1100- be replaced by a hyphen when using D script.
11011099 When executed, the example D script gives output such as:
11021100<screen>
1103- # ./txn_count.d `pgrep -n postgres`
1101+ # ./txn_count.d `pgrep -n postgres` or ./txn_count.d <PID>
11041102^C
11051103
11061104Start 71
@@ -1109,7 +1107,7 @@ Total time (ns) 2312105013
11091107</screen>
11101108 </para>
11111109 <para>
1112- You should remember thattrace programs need to be carefully written and
1110+ You should remember thatDTrace scripts need to be carefully written and
11131111 debugged prior to their use, otherwise the trace information collected might
11141112 be meaningless. In most cases where problems are found it is the
11151113 instrumentation that is at fault, not the underlying system. When
@@ -1119,75 +1117,127 @@ Total time (ns) 2312105013
11191117 </sect2>
11201118
11211119 <sect2 id="defining-trace-points">
1122- <title>DefiningTrace Points </title>
1120+ <title>DefiningNew Probes </title>
11231121
11241122 <para>
1125- New trace points can be defined within the code wherever the developer
1126- desires, though this will require a recompilation.
1123+ New probes can be defined within the code wherever the developer
1124+ desires, though this will require a recompilation. Below are the steps
1125+ for inserting new probes:
11271126 </para>
11281127
1129- <para>
1130- A trace point can be inserted by using one of the
1131- trace macros. These are chosen according to how many variables will
1132- be made available for inspection at that trace point. Tracing the
1133- occurrence of an event can be achieved with a single line, using
1134- just the trace point name, e.g.:
1135- <programlisting>
1136- PG_TRACE (my__new__trace__point);
1137- </programlisting>
1138- More complex trace points can be provided with one or more variables
1139- for inspection by the dynamic tracing utility by using the
1140- <literal>PG_TRACE</><replaceable>n</> macro that corresponds to the number
1141- of parameters after the trace point name:
1128+ <procedure>
1129+ <step>
1130+ <para>
1131+ Decide on probe names and data to be made available through the probes
1132+ </para>
1133+ </step>
1134+
1135+ <step>
1136+ <para>
1137+ Add the probe definitons to <filename>src/backend/src/utils/probes.d</>
1138+ </para>
1139+ </step>
1140+
1141+ <step>
1142+ <para>
1143+ Insert a one-line probe macros at the desired locations in the source code
1144+ </para>
1145+ </step>
1146+
1147+ <step>
1148+ <para>
1149+ Recompile and verify that the new probes are available
1150+ </para>
1151+ </step>
1152+ </procedure>
1153+
1154+ <formalpara>
1155+ <title>Example:</title>
1156+ <para>
1157+ Here is an example of how you would add a probe to trace all new transactions by transaction ID.
1158+ </para>
1159+ </formalpara>
1160+
1161+ <procedure>
1162+ <step>
1163+ <para>
1164+ Name the probe transaction-start and give it a parameter of type integer (type of transaction ID)
1165+ </para>
1166+ </step>
1167+
1168+ <step>
1169+ <para>
1170+ Add <quote>probe transaction__start(int);</quote> to
1171+ <filename>src/backend/src/utils/probes.d</>, and it should look like the following:
11421172<programlisting>
1143- PG_TRACE3 (my__complex__event, varX, varY, varZ);
1173+ provider postgresql {
1174+ ...
1175+ probe transaction__start(int);
1176+ ...
1177+ };
11441178</programlisting>
1145- The definition of the transaction__start trace point is shown below:
1179+ Note the use of the double underline in the probe name. In the DTrace
1180+ script, the double underline needs to be replaced with a hyphen.
1181+ </para>
1182+
1183+ <para>
1184+ You should take care that the data types specified for the probe arguments
1185+ match the data types of the variables used in the macro. Otherwise, you
1186+ will get compilation errors.
1187+ </para>
1188+ </step>
1189+
1190+ <step>
1191+ <para>
1192+ At compile time, transaction__start is converted to a macro called
1193+ TRACE_POSTGRESQL_TRANSACTION_START, and it resides in
1194+ <filename>src/backend/src/utils/probes.h</>. Before recompiling, add
1195+ the single line macro to the appropriate location in the source code.
1196+ In this case, it looks like the following:
1197+ </para>
1198+
1199+ <para>
11461200<programlisting>
11471201static void
11481202StartTransaction(void)
11491203{
11501204 ...
11511205
11521206 /*
1153- * generate a new transaction id
1207+ * Advertise it in the proc array. We assume assignment of
1208+ * LocalTransactionID is atomic, and the backendId should be set already.
11541209 */
1155- s->transactionId = GetNewTransactionId(false);
1210+ Assert(MyProc->backendId == vxid.backendId);
1211+ MyProc->lxid = vxid.localTransactionId;
11561212
1157- XactLockTableInsert(s->transactionId);
1158-
1159- PG_TRACE1(transaction__start, s->transactionId);
1213+ TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
11601214
11611215 ...
11621216}
11631217</programlisting>
1164- Note how the transaction ID is made available to the dynamic tracing
1165- utility.
1166- </para>
1218+ Note how the transaction ID is made available to the dynamic tracing
1219+ utility.
1220+ </para>
1221+ </step>
11671222
1168- <para>
1169- The dynamic tracing utility might require you to further define these trace
1170- points. For example, DTrace requires you to add new probes to the file
1171- <filename>src/backend/utils/probes.d</> as shown here:
1172- <programlisting>
1173- provider postgresql {
1174- ...
1175- probe transaction__start(int);
1176- ...
1177- };
1178- </programlisting>
1179- </para>
1223+ <step>
1224+ <para>
1225+ After recompiling and running the new binary, check that your newly added
1226+ probe is available by executing the following DTrace command, and you
1227+ should see similar output.
1228+ <screen>
1229+ # dtrace -ln transaction-start
1230+ ID PROVIDER MODULE FUNCTION NAME
1231+ 18705 postgresql49878 postgres StartTransactionCommand transaction-start
1232+ 18755 postgresql49877 postgres StartTransactionCommand transaction-start
1233+ 18805 postgresql49876 postgres StartTransactionCommand transaction-start
1234+ 18855 postgresql49875 postgres StartTransactionCommand transaction-start
1235+ 18986 postgresql49873 postgres StartTransactionCommand transaction-start
1236+ </screen>
1237+ </para>
1238+ </step>
1239+ </procedure>
11801240
1181- <para>
1182- You should take care that the data types specified for the probe arguments
1183- match the data types of the variables used in the <literal>PG_TRACE</>
1184- macro. This is not checked at compile time. You can check that your newly
1185- added trace point is available by recompiling, then running the new binary,
1186- and as root, executing a DTrace command such as:
1187- <programlisting>
1188- dtrace -l -n transaction-start
1189- </programlisting>
1190- </para>
11911241 </sect2>
11921242
11931243 </sect1>