| 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"> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 |
<title>Chapter 5. Managing DB Files</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="index.html" title="Getting Started with Berkeley DB Transaction Processing" /> |
| 11 |
<link rel="prev" href="reversesplit.html" title="Reverse BTree Splits" /> |
| 12 |
<link rel="next" href="backuprestore.html" title="Backup Procedures" /> |
| 13 |
</head> |
| 14 |
<body> |
| 15 |
<div xmlns="" class="navheader"> |
| 16 |
<div class="libver"> |
| 17 |
<p>Library Version 11.2.5.3</p> |
| 18 |
</div> |
| 19 |
<table width="100%" summary="Navigation header"> |
| 20 |
<tr> |
| 21 |
<th colspan="3" align="center">Chapter 5. Managing DB Files</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 24 |
<td width="20%" align="left"><a accesskey="p" href="reversesplit.html">Prev</a> </td> |
| 25 |
<th width="60%" align="center"> </th> |
| 26 |
<td width="20%" align="right"> <a accesskey="n" href="backuprestore.html">Next</a></td> |
| 27 |
</tr> |
| 28 |
</table> |
| 29 |
<hr /> |
| 30 |
</div> |
| 31 |
<div class="chapter" lang="en" xml:lang="en"> |
| 32 |
<div class="titlepage"> |
| 33 |
<div> |
| 34 |
<div> |
| 35 |
<h2 class="title"><a id="filemanagement"></a>Chapter 5. Managing DB Files</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<div class="toc"> |
| 40 |
<p> |
| 41 |
<b>Table of Contents</b> |
| 42 |
</p> |
| 43 |
<dl> |
| 44 |
<dt> |
| 45 |
<span class="sect1"> |
| 46 |
<a href="filemanagement.html#checkpoints">Checkpoints</a> |
| 47 |
</span> |
| 48 |
</dt> |
| 49 |
<dt> |
| 50 |
<span class="sect1"> |
| 51 |
<a href="backuprestore.html">Backup Procedures</a> |
| 52 |
</span> |
| 53 |
</dt> |
| 54 |
<dd> |
| 55 |
<dl> |
| 56 |
<dt> |
| 57 |
<span class="sect2"> |
| 58 |
<a href="backuprestore.html#copyutilities">About Unix Copy Utilities</a> |
| 59 |
</span> |
| 60 |
</dt> |
| 61 |
<dt> |
| 62 |
<span class="sect2"> |
| 63 |
<a href="backuprestore.html#standardbackup">Offline Backups</a> |
| 64 |
</span> |
| 65 |
</dt> |
| 66 |
<dt> |
| 67 |
<span class="sect2"> |
| 68 |
<a href="backuprestore.html#hotbackup">Hot Backup</a> |
| 69 |
</span> |
| 70 |
</dt> |
| 71 |
<dt> |
| 72 |
<span class="sect2"> |
| 73 |
<a href="backuprestore.html#incrementalbackups">Incremental Backups</a> |
| 74 |
</span> |
| 75 |
</dt> |
| 76 |
</dl> |
| 77 |
</dd> |
| 78 |
<dt> |
| 79 |
<span class="sect1"> |
| 80 |
<a href="recovery.html">Recovery Procedures</a> |
| 81 |
</span> |
| 82 |
</dt> |
| 83 |
<dd> |
| 84 |
<dl> |
| 85 |
<dt> |
| 86 |
<span class="sect2"> |
| 87 |
<a href="recovery.html#normalrecovery">Normal Recovery</a> |
| 88 |
</span> |
| 89 |
</dt> |
| 90 |
<dt> |
| 91 |
<span class="sect2"> |
| 92 |
<a href="recovery.html#catastrophicrecovery">Catastrophic Recovery</a> |
| 93 |
</span> |
| 94 |
</dt> |
| 95 |
</dl> |
| 96 |
</dd> |
| 97 |
<dt> |
| 98 |
<span class="sect1"> |
| 99 |
<a href="architectrecovery.html">Designing Your Application for Recovery</a> |
| 100 |
</span> |
| 101 |
</dt> |
| 102 |
<dd> |
| 103 |
<dl> |
| 104 |
<dt> |
| 105 |
<span class="sect2"> |
| 106 |
<a href="architectrecovery.html#multithreadrecovery">Recovery for Multi-Threaded Applications</a> |
| 107 |
</span> |
| 108 |
</dt> |
| 109 |
<dt> |
| 110 |
<span class="sect2"> |
| 111 |
<a href="architectrecovery.html#multiprocessrecovery">Recovery in Multi-Process Applications</a> |
| 112 |
</span> |
| 113 |
</dt> |
| 114 |
</dl> |
| 115 |
</dd> |
| 116 |
<dt> |
| 117 |
<span class="sect1"> |
| 118 |
<a href="hotfailover.html">Using Hot Failovers</a> |
| 119 |
</span> |
| 120 |
</dt> |
| 121 |
<dt> |
| 122 |
<span class="sect1"> |
| 123 |
<a href="logfileremoval.html">Removing Log Files</a> |
| 124 |
</span> |
| 125 |
</dt> |
| 126 |
<dt> |
| 127 |
<span class="sect1"> |
| 128 |
<a href="logconfig.html">Configuring the Logging Subsystem</a> |
| 129 |
</span> |
| 130 |
</dt> |
| 131 |
<dd> |
| 132 |
<dl> |
| 133 |
<dt> |
| 134 |
<span class="sect2"> |
| 135 |
<a href="logconfig.html#logfilesize">Setting the Log File Size</a> |
| 136 |
</span> |
| 137 |
</dt> |
| 138 |
<dt> |
| 139 |
<span class="sect2"> |
| 140 |
<a href="logconfig.html#logregionsize">Configuring the Logging Region Size</a> |
| 141 |
</span> |
| 142 |
</dt> |
| 143 |
<dt> |
| 144 |
<span class="sect2"> |
| 145 |
<a href="logconfig.html#inmemorylogging">Configuring In-Memory Logging</a> |
| 146 |
</span> |
| 147 |
</dt> |
| 148 |
<dt> |
| 149 |
<span class="sect2"> |
| 150 |
<a href="logconfig.html#logbuffer">Setting the In-Memory Log Buffer Size</a> |
| 151 |
</span> |
| 152 |
</dt> |
| 153 |
</dl> |
| 154 |
</dd> |
| 155 |
</dl> |
| 156 |
</div> |
| 157 |
<p> |
| 158 |
DB is capable of storing several types of files on disk: |
| 159 |
</p> |
| 160 |
<div class="itemizedlist"> |
| 161 |
<ul type="disc"> |
| 162 |
<li> |
| 163 |
<p> |
| 164 |
Data files, which contain the actual data in your database. |
| 165 |
</p> |
| 166 |
</li> |
| 167 |
<li> |
| 168 |
<p> |
| 169 |
Log files, which contain information required to recover your |
| 170 |
database in the event of a system or application failure. |
| 171 |
</p> |
| 172 |
</li> |
| 173 |
<li> |
| 174 |
<p> |
| 175 |
Region files, which contain information necessary for the |
| 176 |
overall operation of your application. |
| 177 |
</p> |
| 178 |
</li> |
| 179 |
<li> |
| 180 |
<p> |
| 181 |
Temporary files, which are created only under certain special circumstances. These files never need to |
| 182 |
be backed up or otherwise managed and so they are not a consideration for the topics described in this |
| 183 |
chapter. See <a class="xref" href="enabletxn.html#security" title="Security Considerations">Security Considerations</a> |
| 184 |
for more information on temporary files. |
| 185 |
</p> |
| 186 |
</li> |
| 187 |
</ul> |
| 188 |
</div> |
| 189 |
<p> |
| 190 |
Of these, you must manage your data and log files by ensuring that they |
| 191 |
are backed up. You should also pay attention to the amount of disk space |
| 192 |
your log files are consuming, and periodically remove any unneeded |
| 193 |
files. Finally, you can optionally tune your logging subsystem to best |
| 194 |
suit your application's needs and requirements. |
| 195 |
These topics are discussed in this chapter. |
| 196 |
</p> |
| 197 |
<div class="sect1" lang="en" xml:lang="en"> |
| 198 |
<div class="titlepage"> |
| 199 |
<div> |
| 200 |
<div> |
| 201 |
<h2 class="title" style="clear: both"><a id="checkpoints"></a>Checkpoints</h2> |
| 202 |
</div> |
| 203 |
</div> |
| 204 |
</div> |
| 205 |
<p> |
| 206 |
Before we can discuss DB file management, we need to |
| 207 |
describe checkpoints. When databases are modified (that is, a |
| 208 |
transaction is committed), the modifications are recorded in |
| 209 |
DB's logs, but they are <span class="emphasis"><em>not</em></span> |
| 210 |
necessarily reflected in the actual database files on disk. |
| 211 |
</p> |
| 212 |
<p> |
| 213 |
This means that as time goes on, increasingly |
| 214 |
more data is contained in your log files that is not |
| 215 |
contained in your data files. As a result, you must keep more |
| 216 |
log files around than you might actually need. Also, any |
| 217 |
recovery run from your log files will take increasingly longer |
| 218 |
amounts of time, because there is more data in the log files |
| 219 |
that must be reflected back into the data files during the |
| 220 |
recovery process. |
| 221 |
</p> |
| 222 |
<p> |
| 223 |
You can reduce these problems by periodically |
| 224 |
running a checkpoint against your environment. The checkpoint: |
| 225 |
</p> |
| 226 |
<div class="itemizedlist"> |
| 227 |
<ul type="disc"> |
| 228 |
<li> |
| 229 |
<p> |
| 230 |
Flushes dirty pages from the in-memory cache. This means that data modifications found in your |
| 231 |
in-memory cache are written to the database files on disk. Note that a checkpoint also causes data |
| 232 |
dirtied by an uncommitted transaction to also be written to your database files on disk. In this latter |
| 233 |
case, DB's normal recovery is used to remove any such modifications that were subsequently |
| 234 |
abandoned by your application using a transaction abort. |
| 235 |
</p> |
| 236 |
<p> |
| 237 |
Normal recovery is describe in <a class="xref" href="recovery.html" title="Recovery Procedures">Recovery Procedures</a>. |
| 238 |
</p> |
| 239 |
</li> |
| 240 |
<li> |
| 241 |
<p> |
| 242 |
Writes a checkpoint record. |
| 243 |
</p> |
| 244 |
</li> |
| 245 |
<li> |
| 246 |
<p> |
| 247 |
Flushes the log. This causes all log data that has not yet been written to disk to be written. |
| 248 |
</p> |
| 249 |
</li> |
| 250 |
<li> |
| 251 |
<p> |
| 252 |
Writes a list of open databases. |
| 253 |
</p> |
| 254 |
</li> |
| 255 |
</ul> |
| 256 |
</div> |
| 257 |
<p> |
| 258 |
There are several ways to run a checkpoint. One way is to use |
| 259 |
the <span class="command"><strong>db_checkpoint</strong></span> command line utility. (Note, however, that this command line utility |
| 260 |
cannot be used if your environment was opened using |
| 261 |
<span> |
| 262 |
<code class="literal">DB_PRIVATE</code>.) |
| 263 |
</span> |
| 264 |
|
| 265 |
</p> |
| 266 |
<p> |
| 267 |
You can also run a thread that periodically checkpoints your |
| 268 |
environment for you by calling the |
| 269 |
<code class="methodname">DB_ENV->txn_checkpoint()</code> |
| 270 |
|
| 271 |
|
| 272 |
method. |
| 273 |
</p> |
| 274 |
<p> |
| 275 |
Note that you can prevent a checkpoint from occurring unless more |
| 276 |
than a specified amount of log data has been written since the |
| 277 |
last checkpoint. You can also prevent the checkpoint from |
| 278 |
running unless more than a specified amount of time has |
| 279 |
occurred since the last checkpoint. These conditions are |
| 280 |
particularly interesting if you have multiple threads |
| 281 |
<span>or processes</span> |
| 282 |
running checkpoints. |
| 283 |
</p> |
| 284 |
<p> |
| 285 |
For configuration information, see the |
| 286 |
<a class="ulink" href="http://download.oracle.com/docs/cd/E17076_02/html/api_reference/C/txncheckpoint.html" target="_top"> |
| 287 |
DB_ENV->txn_checkpoint() API reference page. |
| 288 |
</a> |
| 289 |
|
| 290 |
|
| 291 |
</p> |
| 292 |
<p> |
| 293 |
Note that running checkpoints can be quite expensive. DB must |
| 294 |
flush every dirty page to the backing database files. On the |
| 295 |
other hand, if you do not run checkpoints often enough, your |
| 296 |
recovery time can be unnecessarily long and you may be using more |
| 297 |
disk space than you really need. Also, you cannot remove log files |
| 298 |
until a checkpoint is run. Therefore, deciding how frequently |
| 299 |
to run a checkpoint is one of the most |
| 300 |
common tuning activity for DB applications. |
| 301 |
</p> |
| 302 |
<p> |
| 303 |
For example, to run a checkpoint from a separate thread of control: |
| 304 |
</p> |
| 305 |
<pre class="programlisting">#include <stdio.h> |
| 306 |
#include <pthread.h> |
| 307 |
#include <stdlib.h> |
| 308 |
#include "db.h" |
| 309 |
|
| 310 |
void *checkpoint_thread(void *); |
| 311 |
|
| 312 |
int |
| 313 |
main(void) |
| 314 |
{ |
| 315 |
int ret; |
| 316 |
u_int32_t env_flags; |
| 317 |
DB_ENV *envp; |
| 318 |
const char *db_home_dir = "/tmp/myEnvironment"; |
| 319 |
pthread_t ptid; |
| 320 |
|
| 321 |
envp = NULL; |
| 322 |
|
| 323 |
/* Open the environment */ |
| 324 |
ret = db_env_create(&envp, 0); |
| 325 |
if (ret != 0) { |
| 326 |
fprintf(stderr, "Error creating environment handle: %s\n", |
| 327 |
db_strerror(ret)); |
| 328 |
return (EXIT_FAILURE); |
| 329 |
} |
| 330 |
|
| 331 |
env_flags = DB_CREATE | /* If the environment does not |
| 332 |
* exist, create it. */ |
| 333 |
DB_INIT_LOCK | /* Initialize locking */ |
| 334 |
DB_INIT_LOG | /* Initialize logging */ |
| 335 |
DB_INIT_MPOOL | /* Initialize the cache */ |
| 336 |
DB_THREAD | /* Free-thread the env handle. */ |
| 337 |
DB_INIT_TXN; /* Initialize transactions */ |
| 338 |
|
| 339 |
/* Open the environment. */ |
| 340 |
ret = envp->open(envp, db_home_dir, env_flags, 0); |
| 341 |
if (ret != 0) { |
| 342 |
fprintf(stderr, "Error opening environment: %s\n", |
| 343 |
db_strerror(ret)); |
| 344 |
goto err; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
/* Start a checkpoint thread. */ |
| 349 |
if ((ret = pthread_create( |
| 350 |
&ptid, NULL, checkpoint_thread, (void *)envp)) != 0) { |
| 351 |
fprintf(stderr, |
| 352 |
"txnapp: failed spawning checkpoint thread: %s\n", |
| 353 |
strerror(ret)); |
| 354 |
goto err; |
| 355 |
} |
| 356 |
|
| 357 |
/* |
| 358 |
* All other threads and application shutdown code |
| 359 |
* omitted for brevity. |
| 360 |
*/ |
| 361 |
|
| 362 |
... |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
void * |
| 367 |
checkpoint_thread(void *arg) { |
| 368 |
DB_ENV *dbenv; |
| 369 |
int ret; |
| 370 |
|
| 371 |
dbenv = arg; |
| 372 |
|
| 373 |
/* Checkpoint once a minute. */ |
| 374 |
for (;; sleep(60)) |
| 375 |
if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0)) != 0) { |
| 376 |
dbenv->err(dbenv, ret, "checkpoint thread"); |
| 377 |
exit (1); |
| 378 |
} |
| 379 |
|
| 380 |
/* NOTREACHED */ |
| 381 |
} </pre> |
| 382 |
</div> |
| 383 |
</div> |
| 384 |
<div class="navfooter"> |
| 385 |
<hr /> |
| 386 |
<table width="100%" summary="Navigation footer"> |
| 387 |
<tr> |
| 388 |
<td width="40%" align="left"><a accesskey="p" href="reversesplit.html">Prev</a> </td> |
| 389 |
<td width="20%" align="center"> </td> |
| 390 |
<td width="40%" align="right"> <a accesskey="n" href="backuprestore.html">Next</a></td> |
| 391 |
</tr> |
| 392 |
<tr> |
| 393 |
<td width="40%" align="left" valign="top">Reverse BTree Splits </td> |
| 394 |
<td width="20%" align="center"> |
| 395 |
<a accesskey="h" href="index.html">Home</a> |
| 396 |
</td> |
| 397 |
<td width="40%" align="right" valign="top"> Backup Procedures</td> |
| 398 |
</tr> |
| 399 |
</table> |
| 400 |
</div> |
| 401 |
</body> |
| 402 |
</html> |