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">
3 <html xmlns="http://www.w3.org/1999/xhtml">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Configuring the Logging Subsystem</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
10 <link rel="up" href="filemanagement.html" title="Chapter 5. Managing DB Files" />
11 <link rel="prev" href="logfileremoval.html" title="Removing Log Files" />
12 <link rel="next" href="wrapup.html" title="Chapter 6. Summary and Examples" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Configuring the Logging Subsystem</th>
24 <td width="20%" align="left"><a accesskey="p" href="logfileremoval.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 5. Managing DB Files</th>
26 <td width="20%" align="right"> <a accesskey="n" href="wrapup.html">Next</a></td>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title" style="clear: both"><a id="logconfig"></a>Configuring the Logging Subsystem</h2>
43 <a href="logconfig.html#logfilesize">Setting the Log File Size</a>
48 <a href="logconfig.html#logregionsize">Configuring the Logging Region Size</a>
53 <a href="logconfig.html#inmemorylogging">Configuring In-Memory Logging</a>
58 <a href="logconfig.html#logbuffer">Setting the In-Memory Log Buffer Size</a>
64 You can configure the following aspects of the logging
67 <div class="itemizedlist">
71 Size of the log files.
76 Size of the logging subsystem's region.
77 See <a class="xref" href="logconfig.html#logregionsize" title="Configuring the Logging Region Size">Configuring the Logging Region Size</a>.
82 Maintain logs entirely in-memory.
83 See <a class="xref" href="logconfig.html#inmemorylogging" title="Configuring In-Memory Logging">Configuring In-Memory Logging</a>
89 Size of the log buffer in memory.
90 See <a class="xref" href="logconfig.html#logbuffer" title="Setting the In-Memory Log Buffer Size">Setting the In-Memory Log Buffer Size</a>.
95 On-disk location of your log files. See
96 <a class="xref" href="enabletxn.html#splittingdata" title="Identifying Specific File Locations">Identifying Specific File Locations</a>.
101 <div class="sect2" lang="en" xml:lang="en">
102 <div class="titlepage">
105 <h3 class="title"><a id="logfilesize"></a>Setting the Log File Size</h3>
110 Whenever a pre-defined amount of data is written to a
111 log file (10 MB by default), DB stops using the
112 current log file and starts writing to a new file.
113 You can change the maximum amount of data contained in each
114 log file by using the
116 <code class="methodname">DB_ENV->set_lg_max()</code>
121 Note that this method can be used at any time
122 during an application's lifetime.
125 Setting the log file size to something larger than its
126 default value is largely a matter of
127 convenience and a reflection of the application's
128 preference in backup media and frequency. However, if
129 you set the log file size too low relative to your
130 application's traffic patterns, you can cause
134 From a performance perspective, setting the log file
135 size to a low value can cause your active transactions to
136 pause their writing activities more frequently than would
137 occur with larger log file sizes. Whenever a
138 transaction completes the log buffer is flushed to
139 disk. Normally other transactions can continue to
140 write to the log buffer while this flush is in
141 progress. However, when one log file is being closed
142 and another created, all transactions must cease
143 writing to the log buffer until the switch over is
147 Beyond performance concerns, using smaller log files
148 can cause you to use more physical files on disk.
149 As a result, your application could run out
150 of log sequence numbers, depending on how busy your
154 Every log file is identified with a 10 digit number.
155 Moreover, the maximum number of log files that your application
156 is allowed to create in its lifetime is 2,000,000,000.
159 For example, if your application performs 6,000 transactions per
160 second for 24 hours a day, and you are logging 500 bytes of
161 data per transaction into 10 MB log files, then you
162 will run out of log files in around 221 years:
164 <pre class="programlisting"> (10 * 2^20 * 2000000000) / (6000 * 500 * 365 * 60 *60 * 24) = 221 </pre>
166 However, if you were writing 2000 bytes of data per
167 transaction, and using 1 MB log files, then the same
168 formula shows you running out of log files in 5 years time.
171 All of these time frames are quite long, to be sure,
172 but if you do run out of log files after, say, 5 years
173 of continuous operations, then you must reset your log
174 sequence numbers. To do so:
176 <div class="orderedlist">
180 Backup your databases as if to prepare for
181 catastrophic failure. See
182 <a class="xref" href="backuprestore.html" title="Backup Procedures">Backup Procedures</a>
183 for more information.
188 Reset the log file's sequence number using the
189 <span class="command"><strong>db_load</strong></span> utility's
190 <code class="literal">-r</code> option.
195 Remove all of the log files from your
196 environment. Note that this is the only
197 situation in which all of the log files are
198 removed from an environment; in all other
199 cases, at least a single log file is retained.
204 Restart your application.
210 <div class="sect2" lang="en" xml:lang="en">
211 <div class="titlepage">
214 <h3 class="title"><a id="logregionsize"></a>Configuring the Logging Region Size</h3>
219 The logging subsystem's default region size is 60 KB. The
220 logging region is used to store filenames, and so you may
221 need to increase its size if a large number of files (that
222 is, if you have a very large number of databases) will
223 be opened and registered with DB's log manager.
226 You can set the size of your logging region by using the
228 <code class="methodname">DB_ENV->set_lg_regionmax()</code>
232 method. Note that this method can only be called before the
233 first environment handle for your application is opened.
236 <div class="sect2" lang="en" xml:lang="en">
237 <div class="titlepage">
240 <h3 class="title"><a id="inmemorylogging"></a>Configuring In-Memory Logging</h3>
245 It is possible to configure your logging subsystem such
246 that logs are maintained entirely in memory. When
247 you do this, you give up your transactional durability
248 guarantee. Without log files, you have no way to run
249 recovery so any system or software failures that you might
250 experience can corrupt your databases.
253 However, by giving up your durability guarantees, you can
254 greatly improve your application's throughput by avoiding
255 the disk I/O necessary to write logging information to
256 disk. In this case, you still retain your transactional
257 atomicity, consistency, and isolation guarantees.
260 To configure your logging subsystem to maintain your logs
263 <div class="itemizedlist">
267 Make sure your log buffer is capable of holding all
268 log information that can accumulate during the longest
269 running transaction. See <a class="xref" href="logconfig.html#logbuffer" title="Setting the In-Memory Log Buffer Size">Setting the In-Memory Log Buffer Size</a> for details.
274 Do not run normal recovery when you open your environment. In this configuration, there are no
275 log files available against which you can run recovery. As a result, if you specify recovery
276 when you open your environment, it is ignored.
283 <code class="literal">DB_LOG_IN_MEMORY</code> to the
284 <code class="methodname">DB_ENV->log_set_config()</code>
288 method. Note that you must specify this before
289 your application opens its first environment
298 <pre class="programlisting">#include <stdio.h>
299 #include <stdlib.h>
307 u_int32_t db_flags, env_flags;
309 const char *db_home_dir = "/tmp/myEnvironment";
313 /* Create the environment */
314 ret = db_env_create(&envp, 0);
316 fprintf(stderr, "Error creating environment handle: %s\n",
318 return (EXIT_FAILURE);
322 * Indicate that logging is to be performed only in memory.
323 * Doing this means that we give up our transactional durability
326 envp->log_set_config(envp, DB_LOG_IN_MEMORY, 1);
329 * Configure the size of our log memory buffer. This must be
330 * large enough to hold all the logging information likely
331 * to be created for our longest running transaction. The
332 * default size for the logging buffer is 1 MB when logging
333 * is performed in-memory. For this example, we arbitrarily
334 * set the logging buffer to 5 MB.
336 ret = envp->set_lg_bsize(envp, 5 * 1024 * 1024);
338 fprintf(stderr, "Error setting log buffer size: %s\n",
344 * Set the normal flags for a transactional subsystem. Note that
345 * we DO NOT specify DB_RECOVER. Also, remember that the logging
346 * subsystem is automatically enabled when we initialize the
347 * transactional subsystem, so we do not explicitly enable
350 env_flags = DB_CREATE | /* If the environment does not
351 * exist, create it. */
352 DB_INIT_LOCK | /* Initialize locking */
353 DB_INIT_LOG | /* Initialize logging */
354 DB_INIT_MPOOL | /* Initialize the cache */
355 DB_THREAD | /* Free-thread the env handle. */
356 DB_INIT_TXN; /* Initialize transactions */
358 /* Open the environment as normal */
359 ret = envp->open(envp, db_home_dir, env_flags, 0);
361 fprintf(stderr, "Error opening environment: %s\n",
368 * From here, you create transactions and perform database operations
369 * exactly as you would if you were logging to disk. This part is
370 * omitted for brevity.
376 /* Close the databases (omitted) */
380 /* Close the environment */
382 ret_c = envp->close(envp, 0);
384 fprintf(stderr, "environment close failed: %s\n",
390 return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
393 <div class="sect2" lang="en" xml:lang="en">
394 <div class="titlepage">
397 <h3 class="title"><a id="logbuffer"></a>Setting the In-Memory Log Buffer Size</h3>
402 When your application is configured for on-disk logging
403 (the default behavior for transactional applications), log
404 information is stored in-memory until the storage space
405 fills up, or a transaction commit forces the log
406 information to be flushed to disk.
409 It is possible to increase the amount of memory available
410 to your file log buffer. Doing so improves throughput for
411 long-running transactions, or for transactions that produce
412 a large amount of data.
415 When you have your logging subsystem configured to maintain
416 your log entirely in memory (see
417 <a class="xref" href="logconfig.html#inmemorylogging" title="Configuring In-Memory Logging">Configuring In-Memory Logging</a>), it is very important
418 to configure your log buffer size because the log buffer
419 must be capable of holding all log information that can
420 accumulate during the longest running transaction.
421 You must make sure that the in-memory log buffer size is
422 large enough that no transaction will ever span the entire
423 buffer. You must also avoid a state where the in-memory
424 buffer is full and no space can be freed because a
425 transaction that started the first log "file" is still
429 When your logging subsystem is configured for on-disk
430 logging, the default log buffer space is 32 KB. When
431 in-memory logging is configured, the default log buffer
435 You can increase your log buffer space using the
437 <code class="methodname">DB_ENV->set_lg_bsize()</code>
441 method. Note that this method can only be called before the
442 first environment handle for your application is opened.
446 <div class="navfooter">
448 <table width="100%" summary="Navigation footer">
450 <td width="40%" align="left"><a accesskey="p" href="logfileremoval.html">Prev</a> </td>
451 <td width="20%" align="center">
452 <a accesskey="u" href="filemanagement.html">Up</a>
454 <td width="40%" align="right"> <a accesskey="n" href="wrapup.html">Next</a></td>
457 <td width="40%" align="left" valign="top">Removing Log Files </td>
458 <td width="20%" align="center">
459 <a accesskey="h" href="index.html">Home</a>
461 <td width="40%" align="right" valign="top"> Chapter 6. Summary and Examples</td>