---
name: "db_open"
section: "3db"
platform: "linux"
package: "libdb2-dev"
version: "2.4.14-1"
release: "Debian 2.1 (Slink)"
platforms: ["linux"]
url: "https://new.carta.tech/db_open"
---

# db\_open(3db)

database access methods

## NAME

db\_open - database access methods

## SYNOPSIS

```
#include <db2/db.h>
int
db_open(const char *file, DBTYPE type, u_int32_t flags,
int mode, DB_ENV *dbenv, DB_INFO *dbinfo, DB **dbpp);
int
DB->close(DB *db, u_int32_t flags);
int
DB->cursor(DB *db, DB_TXN *txnid, DBC **cursorp);
int
DB->del(DB *db, DB_TXN *txnid, DBT *key, u_int32_t flags);
int
DB->fd(DB *db, int *fdp);
int
DB->get(DB *db, DB_TXN *txnid, DBT *key, DBT *data, u_int32_t flags);
int
DB->put(DB *db, DB_TXN *txnid, DBT *key, DBT *data, u_int32_t flags);
int
DB->sync(DB *db, u_int32_t flags);
int
DB->stat(DB *db, void *sp, void *(*db_malloc)(size_t), u_int32_t flags);
```

## DESCRIPTION

The DB library is a family of groups of functions that provides a modular programming interface to transactions and record-oriented file access. The library includes support for transactions, locking, logging and file page caching, as well as various indexed access methods. Many of the functional groups (e.g., the file page caching functions) are useful independent of the other DB functions, although some functional groups are explicitly based on other functional groups (e.g., transactions and logging). For a general description of the DB package, see *db\_intro*(3).

This manual page describes the overall structure of the DB library access methods.

The currently supported file formats are btree, hashed and recno. The btree format is a representation of a sorted, balanced tree structure. The hashed format is an extensible, dynamic hashing scheme. The recno format supports fixed or variable length records (optionally retrieved from a flat text file).

Storage and retrieval for the DB access methods are based on key/data pairs, or DBT structures as they are typedef'd in the \<db2/db.h\> include file. See *db\_dbt*(3) for specific information on the structure and capabilities of a DBT.

The *db\_open* function opens the database represented by *file* for both reading and writing. Files never intended to be shared or preserved on disk may be created by setting the file parameter to NULL.

The *db\_open* function copies a pointer to a DB structure (as typedef'd in the \<db2/db.h\> include file), into the memory location referenced by *dbpp*. This structure includes a set of functions to perform various database actions, as described below. The *db\_open* function returns the value of *errno* on failure and 0 on success.

Note, while most of the access methods use *file* as the name of an underlying file on disk, this is not guaranteed. Also, calling *db\_open* is a reasonably expensive operation. (This is based on a model where the DBMS keeps a set of files open for a long time rather than opening and closing them on each query.)

The *type* argument is of type DBTYPE (as defined in the \<db2/db.h\> include file) and must be set to one of DB\_BTREE, DB\_HASH, DB\_RECNO or DB\_UNKNOWN. If *type* is DB\_UNKNOWN, the database must already exist and *db\_open* will then determine if it is of type DB\_BTREE, DB\_HASH or DB\_RECNO.

The *flags* and *mode* arguments specify how files will be opened and/or created when they don't already exist. The flags value is specified by **or**'ing together one or more of the following values:

- DB\_CREATE

  Create any underlying files, as necessary. If the files do not already exist and the DB\_CREATE flag is not specified, the call will fail.

- DB\_NOMMAP

  Do not map this file (see *db\_mpool*(3) for further information).

- DB\_RDONLY

  Open the database for reading only. Any attempt to write the database using the access methods will fail regardless of the actual permissions of any underlying files.

-

  - DB\_THREAD

  Cause the DB handle returned by the *db\_open* function to be useable by multiple threads within a single address space, i.e., to be \`\`free-threaded''.

- DB\_TRUNCATE

  \`\`Truncate'' the database if it exists, i.e., behave as if the database were just created, discarding any previous contents.

All files created by the access methods are created with mode *mode* (as described in [*chmod*(2)](/chmod/2.md)) and modified by the process' umask value at the time of creation (see [*umask*(2)](/umask/2.md)). The group ownership of created files is based on the system and directory defaults, and is not further specified by DB.

## DB\_ENV

The access methods make calls to the other subsystems in the DB library based on the *dbenv* argument to *db\_open*, which is a pointer to a structure of type DB\_ENV (typedef'd in \<db2/db.h\>). Applications will normally use the same DB\_ENV structure (initialized by *db\_appinit*(3)), as an argument to all of the subsystems in the DB package.

References to the DB\_ENV structure are maintained by DB, so it may not be discarded until the last close function, corresponding to an open function for which it was an argument, has returned. In order to ensure compatibility with future releases of DB, all fields of the DB\_ENV structure that are not explicitly set should be initialized to 0 before the first time the structure is used. Do this by declaring the structure external or static, or by calling the C library routine [*bzero*(3)](/bzero/3.md) or [*memset*(3)](/memset/3.md).

The fields of the DB\_ENV structure used by *db\_open* are described below. If *dbenv* is NULL or any of its fields are set to 0, defaults appropriate for the system are used where possible.

The following fields in the DB\_ENV structure may be initialized before calling *db\_open*:

- DB\_LOG \*lg\_info;

  If modifications to the file being opened should be logged, the *lg\_info* field contains a return value from the function *log\_open*. If *lg\_info* is NULL, no logging is done by the DB access methods.

- DB\_LOCKTAB \*lk\_info;

  If locking is required for the file being opened (as is the case when multiple processes or threads are accessing the same file), the *lk\_info* field contains a return value from the function *lock\_open*. If *lk\_info* is NULL, no locking is done by the DB access methods.

  If both locking and transactions are being performed (i.e., both *lk\_info* and *tx\_info* are non-NULL), the transaction ID will be used as the locker ID. If only locking is being performed, *db\_open* will acquire a locker ID from *lock\_id*(3), and will use it for all locks required for this instance of *db\_open*.

- DB\_MPOOL \*mp\_info;

  If the cache for the file being opened should be maintained in a shared buffer pool, the *mp\_info* field contains a return value from the function *memp\_open*. If *mp\_info* is NULL, a memory pool may still be created by DB, but it will be private to the application and managed by DB.

- DB\_TXNMGR \*tx\_info;

  If the accesses to the file being opened should take place in the context of transactions (providing atomicity and error recovery), the *tx\_info* field contains a return value from the function *txn\_open* (see *db\_txn*(3)). If transactions are specified, the application is responsible for making suitable calls to *txn\_begin*, *txn\_abort*, and *txn\_commit*. If *tx\_info* is NULL, no transaction support is done by the DB access methods.

  When the access methods are used in conjunction with transactions, the application must abort the transaction (using *txn\_abort*) if any of the transaction protected access method calls (i.e., any calls other than open, close and sync) returns a system error (e.g., deadlock, which returns EAGAIN). As described by *db\_intro*(3), a system error is any value greater than 0.

## DB\_INFO

The access methods are configured using the DB\_INFO data structure argument to *db\_open*. The DB\_INFO structure is typedef'd in \<db2/db.h\> and has a large number of fields, most specific to a single access method, although a few are shared. The fields that are common to all access methods are listed here; those specific to an individual access method are described below. No reference to the DB\_INFO structure is maintained by DB, so it is possible to discard it as soon as the *db\_open* call returns.

In order to ensure compatibility with future releases of DB, all fields of the DB\_INFO structure should be initialized to 0 before the structure is used. Do this by declaring the structure external or static, or by calling the C library function [*bzero*(3)](/bzero/3.md) or [*memset*(3)](/memset/3.md).

If possible, defaults appropriate for the system are used for the DB\_INFO fields if *dbinfo* is NULL or any fields of the DB\_INFO structure are set to 0. The following DB\_INFO fields may be initialized before calling *db\_open*:

- size\_t db\_cachesize;

  A suggested maximum size of the memory pool cache, in bytes. If *db\_cachesize* is 0, an appropriate default is used. It is an error to specify both the *mp\_info* field and a non-zero *db\_cachesize*.

  **Note,** **the minimum number of pages in the cache should be no less than 10,** **and the access methods will fail if an insufficiently large cache is specified.** In addition, for applications that exhibit strong locality in their data access patterns, increasing the size of the cache can significantly improve application performance.

- int db\_lorder;

  The byte order for integers in the stored database metadata. The number should represent the order as an integer, for example, big endian order is the number 4,321, and little endian order is the number 1,234. If *db\_lorder* is 0, the host order of the machine where the DB library was compiled is used.

  The value of *db\_lorder* is ignored except when databases are being created. If a database already exists, the byte order it uses is determined when the file is read.

  **The access methods provide no guarantees about the byte ordering of the** **application data stored in the database,** **and applications are responsible for maintaining any necessary ordering.**

- size\_t db\_pagesize;

  The size of the pages used to hold items in the database, in bytes. The minimum page size is 512 bytes and the maximum page size is 64K bytes. If *db\_pagesize* is 0, a page size is selected based on the underlying filesystem I/O block size. The selected size has a lower limit of 512 bytes and an upper limit of 16K bytes.

- void \*(\*db\_malloc)(size\_t);

  The flag DB\_DBT\_MALLOC, when specified in the DBT structure, will cause the DB library to allocate memory which then becomes the responsibility of the calling application. See *db\_dbt*(3) for more information.

  On systems where there may be multiple library versions of malloc (notably Windows NT), specifying the DB\_DBT\_MALLOC flag will fail because the DB library will allocate memory from a different heap than the application will use to free it. To avoid this problem, the *db\_malloc* field should be set to point to the application's allocation routine. If *db\_malloc* is non-NULL, it will be used to allocate the memory returned when the DB\_DBT\_MALLOC flag is set. The *db\_malloc* function must match the calling conventions of the [*malloc*(3)](/malloc/3.md) library routine.

## BTREE

The btree data structure is a sorted, balanced tree structure storing associated key/data pairs. Searches, insertions, and deletions in the btree will all complete in O (lg base N) where base is the average number of keys per page. Often, inserting ordered data into btrees results in pages that are half-full. This implementation has been modified to make ordered (or inverse ordered) insertion the best case, resulting in nearly perfect page space utilization.

Space freed by deleting key/data pairs from the database is never reclaimed from the filesystem, although it is reused where possible. This means that the btree storage structure is grow-only. If sufficiently many keys are deleted from a tree that shrinking the underlying database file is desirable, this can be accomplished by creating a new tree from a scan of the existing one.

The following additional fields and flags may be initialized in the DB\_INFO structure before calling *db\_open*, when using the btree access method:

- int (\*bt\_compare)(const DBT \*, const DBT \*);

  The *bt\_compare* function is the key comparison function. It must return an integer less than, equal to, or greater than zero if the first key argument is considered to be respectively less than, equal to, or greater than the second key argument. The same comparison function must be used on a given tree every time it is opened.

  The *data* and *size* fields of the DBT are the only fields that may be used for the purposes of this comparison.

  If *bt\_compare* is NULL, the keys are compared lexically, with shorter keys collating before longer keys.

- u\_int32\_t bt\_minkey;

  The minimum number of keys that will be stored on any single page. This value is used to determine which keys will be stored on overflow pages, i.e. if a key or data item is larger than the pagesize divided by the *bt\_minkey* value, it will be stored on overflow pages instead of in the page itself. The *bt\_minkey* value specified must be at least 2; if *bt\_minkey* is 0, a value of 2 is used.

- size\_t (\*bt\_prefix)(const DBT \*, const DBT \*);

  The *bt\_prefix* function is the prefix comparison function. If specified, this function must return the number of bytes of the second key argument that are necessary to determine that it is greater than the first key argument. If the keys are equal, the key length should be returned.

  The *data* and *size* fields of the DBT are the only fields that may be used for the purposes of this comparison.

  This is used to compress the keys stored on the btree internal pages. The usefulness of this is data dependent, but in some data sets can produce significantly reduced tree sizes and search times. If *bt\_prefix* is NULL, and no comparison function is specified, a default lexical comparison function is used. If *bt\_prefix* is NULL and a comparison function is specified, no prefix comparison is done.

- u\_int32\_t flags;

  The following additional flags may be specified by **or**'ing together one or more of the following values:

  - DB\_DUP

    Permit duplicate keys in the tree, i.e. insertion when the key of the key/data pair being inserted already exists in the tree will be successful. The ordering of duplicates in the tree is determined by the order of insertion, unless the ordering is otherwise specified by use of a cursor (see *db\_cursor*(3) for more information.) It is an error to specify both DB\_DUP and DB\_RECNUM.

  - DB\_RECNUM

    Support retrieval from btrees using record numbers. For more information, see the DB\_SET\_RECNO flag to the *DB-\>get* function (below), and the cursor *c\_get* function (in *db\_cursor*(3)).

    Logical record numbers in btrees are mutable in the face of record insertion or deletion. See the DB\_RENUMBER flag in the RECNO section below for further discussion.

    Maintaining record counts within a btree introduces a serious point of contention, namely the page locations where the record counts are stored. In addition, the entire tree must be locked during both insertions and deletions, effectively single-threading the tree for those operations. Specifying DB\_RECNUM can result in serious performance degradation for some applications and data sets.

    It is an error to specify both DB\_DUP and DB\_RECNUM.

## HASH

The hash data structure is an extensible, dynamic hashing scheme. Backward compatible interfaces to the functions described in [*dbm*(3)](/dbm/3.md), *ndbm*(3) and [*hsearch*(3)](/hsearch/3.md) are provided, however these interfaces are not compatible with previous file formats.

The following additional fields and flags may be initialized in the DB\_INFO structure before calling *db\_open*, when using the hash access method:

- u\_int32\_t h\_ffactor;

  The desired density within the hash table. It is an approximation of the number of keys allowed to accumulate in any one bucket, determining when the hash table grows or shrinks. The default value is 0, indicating that the fill factor will be selected dynamically as pages are filled.

- u\_int32\_t (\*h\_hash)(const void \*, u\_int32\_t);

  The *h\_hash* field is a user defined hash function; if *h\_hash* is NULL, a default hash function is used. Since no hash function performs equally well on all possible data, the user may find that the built-in hash function performs poorly with a particular data set. User specified hash functions must take a pointer to a byte string and a length as arguments and return a u\_int32\_t value.

-

  If a hash function is specified, *hash\_open* will attempt to determine if the hash function specified is the same as the one with which the database was created, and will fail if it detects that it is not.

- u\_int32\_t h\_nelem;

  An estimate of the final size of the hash table. If not set or set too low, hash tables will expand gracefully as keys are entered, although a slight performance degradation may be noticed. The default value is 1.

- u\_int32\_t flags;

  The following additional flags may be specified by **or**'ing together one or more of the following values:

  - DB\_DUP

    Permit duplicate keys in the tree, i.e. insertion when the key of the key/data pair being inserted already exists in the tree will be successful. The ordering of duplicates in the tree is determined by the order of insertion, unless the ordering is otherwise specified by use of a cursor (see *db\_cursor*(3) for more information.)

## RECNO

The recno access method provides support for fixed and variable length records, optionally backed by a flat text (byte stream) file. Both fixed and variable length records are accessed by their logical record number.

It is valid to create a record whose record number is more than one greater than the last record currently in the database. For example, the creation of record number 8, when records 6 and 7 do not yet exist, is not an error. However, any attempt to retrieve such records (e.g., records 6 and 7) will return DB\_KEYEMPTY.

Deleting a record will not, by default, renumber records following the deleted record (see DB\_RENUMBER below for more information). Any attempt to retrieve deleted records will return DB\_KEYEMPTY.

The following additional fields and flags may be initialized in the DB\_INFO structure before calling *db\_open*, when using the recno access method:

- int re\_delim;

  For variable length records, if the *re\_source* file is specified and the DB\_DELIMITER flag is set, the delimiting byte used to mark the end of a record in the source file. If the *re\_source* file is specified and the DB\_DELIMITER flag is not set, \<newline\> characters (i.e. \`\`\\n'', 0x0a) are interpreted as end-of-record markers.

- u\_int32\_t re\_len;

  The length of a fixed-length record.

- int re\_pad;

  For fixed length records, if the DB\_PAD flag is set, the pad character for short records. If the DB\_PAD flag is not set, \<space\> characters (i.e., 0x20) are used for padding.

- char \*re\_source;

  The purpose of the *re\_source* field is to provide fast access and modification to databases that are normally stored as flat text files.

  If the *re\_source* field is non-NULL, it specifies an underlying flat text database file that is read to initialize a transient record number index. In the case of variable length records, the records are separated by the byte value *re\_delim*. For example, standard UNIX byte stream files can be interpreted as a sequence of variable length records separated by \<newline\> characters.

  In addition, when cached data would normally be written back to the underlying database file (e.g., the *close* or *sync* functions are called), the in-memory copy of the database will be written back to the *re\_source* file.

  By default, the backing source file is read lazily, i.e., records are not read from the file until they are requested by the application. **If multiple processes (not threads) are accessing a recno database** **concurrently and either inserting or deleting records,** **the backing source file must be read in its entirety before more than** **a single process accesses the database,** **and only that process should specify the backing source file as part** **of the db\_open call.** See the DB\_SNAPSHOT flag below for more information.

  **Reading and writing the backing source file specified by re\_source** **cannot be transactionally protected because it involves filesystem** **operations that are not part of the DB transaction methodology.** For this reason, if a temporary database is used to hold the records, i.e., a NULL was specified as the *file* argument to *db\_open*, it is possible to lose the contents of the *re\_source* file, e.g., if the system crashes at the right instant. If a file is used to hold the database, i.e., a file name was specified as the *file* argument to *db\_open*, normal database recovery on that file can be used to prevent information loss, although it is still possible that the contents of *re\_source* will be lost if the system crashes.

  The *re\_source* file must already exist (but may be zero-length) when *db\_open* is called.

  For all of the above reasons, the *re\_source* field is generally used to specify databases that are read-only for DB applications, and that are either generated on the fly by software tools, or modified using a different mechanism, e.g., a text editor.

- u\_int32\_t flags;

  The following additional flags may be specified by **or**'ing together one or more of the following values:

  - DB\_DELIMITER

    The *re\_delim* field is set.

  - DB\_FIXEDLEN

    The records are fixed-length, not byte delimited. The structure element *re\_len* specifies the length of the record, and the structure element *re\_pad* is used as the pad character.

    Any records added to the database that are less than *re\_len* bytes long are automatically padded. Any attempt to insert records into the database that are greater than *re\_len* bytes long will cause the call to fail immediately and return an error.

  - DB\_PAD

    The *re\_pad* field is set.

  - DB\_RENUMBER

    Specifying the DB\_RENUMBER flag causes the logical record numbers to be mutable, and change as records are added to and deleted from the database. For example, the deletion of record number 4 causes records numbered 5 and greater to be renumbered downward by 1. If a cursor was positioned to record number 4 before the deletion, it will reference the new record number 4, if any such record exists, after the deletion. If a cursor was positioned after record number 4 before the deletion, it will be shifted downward 1 logical record, continuing to reference the same record as it did before.

    Using the *c\_put* or *put* interfaces to create new records will cause the creation of multiple records if the record number is more than one greater than the largest record currently in the database. For example, creating record 28, when record 25 was previously the last record in the database, will create records 26 and 27 as well as 28. Attempts to retrieve records that were created in this manner will result in an error return of DB\_KEYEMPTY.

    If a created record is not at the end of the database, all records following the new record will be automatically renumbered upward by 1. For example, the creation of a new record numbered 8 causes records numbered 8 and greater to be renumbered upward by 1. If a cursor was positioned to record number 8 or greater before the insertion, it will be shifted upward 1 logical record, continuing to reference the same record as it did before.

    For these reasons, concurrent access to a recno database with the DB\_RENUMBER flag specified may be largely meaningless, although it is supported.

  - DB\_SNAPSHOT

    This flag specifies that any specified *re\_source* file be read in its entirety when *db\_open* is called. If this flag is not specified, the *re\_source* file may be read lazily.

## DB OPERATIONS

The DB structure returned by *db\_open* describes a database type, and includes a set of functions to perform various actions, as described below. Each of these functions takes a pointer to a DB structure, and may take one or more DBT \*'s and a flag value as well. The fields of the DB structure are as follows:

- DBTYPE type;

  The type of the underlying access method (and file format). Set to one of DB\_BTREE, DB\_HASH or DB\_RECNO. This field may be used to determine the type of the database after a return from *db\_open* with the *type* argument set to DB\_UNKNOWN.

- int (\*close)(DB \*db, u\_int32\_t flags);

  A pointer to a function to flush any cached information to disk, close any open cursors (see *db\_cursor*(3)), free any allocated resources, and close any underlying files. Since key/data pairs are cached in memory, failing to sync the file with the *close* or *sync* function may result in inconsistent or lost information.

-

  The *flags* parameter must be set to 0 or the following value:

  - DB\_NOSYNC

    Do not flush cached information to disk.

-

  The DB\_NOSYNC flag is a dangerous option. It should only be set if the application is doing logging (with transactions) so that the database is recoverable after a system or application crash, or if the database is always generated from scratch after any system or application crash.

-

  **It is important to understand that flushing cached information to disk** **only minimizes the window of opportunity for corrupted data.** While unlikely, it is possible for database corruption to happen if a system or application crash occurs while writing data to the database. To ensure that database corruption never occurs, applications must either: use transactions and logging with automatic recovery, use logging and application-specific recovery, or edit a copy of the database, and, once all applications using the database have successfully called *close*, replace the original database with the updated copy.

-

  When multiple threads are using the DB handle concurrently, only a single thread may call the DB handle close function.

-

  The *close* function returns the value of *errno* on failure and 0 on success.

- int (\*cursor)(DB \*db, DB\_TXN \*txnid, DBC \*\*cursorp);

  A pointer to a function to create a cursor and copy a pointer to it into the memory referenced by *cursorp*.

-

  A cursor is a structure used to provide sequential access through a database. This interface and its associated functions replaces the functionality provided by the *seq* function in previous releases of the DB library.

-

  -

  If the file is being accessed under transaction protection, the *txnid* parameter is a transaction ID returned from *txn\_begin*, otherwise, NULL. If transaction protection is enabled, cursors must be opened and closed within the context of a transaction, and the *txnid* parameter specifies the transaction context in which the cursor may be used. See *db\_cursor*(3) for more information.

-

  The *cursor* function returns the value of *errno* on failure and 0 on success.

- int (\*del)(DB \*db, DB\_TXN \*txnid, DBT \*key, u\_int32\_t flags);

  A pointer to a function to remove key/data pairs from the database. The key/data pair associated with the specified *key* is discarded from the database. In the presence of duplicate key values, all records associated with the designated key will be discarded.

-

  If the file is being accessed under transaction protection, the *txnid* parameter is a transaction ID returned from *txn\_begin*, otherwise, NULL.

-

  The *flags* parameter is currently unused, and must be set to 0.

-

  The *del* function returns the value of *errno* on failure, 0 on success, and DB\_NOTFOUND if the specified *key* did not exist in the file.

- int (\*fd)(DB \*db, int \*fdp);

  A pointer to a function that copies a file descriptor representative of the underlying database into the memory referenced by *fdp*. A file descriptor referencing the same file will be returned to all processes that call *db\_open* with the same *file* argument. This file descriptor may be safely used as an argument to the [*fcntl*(2)](/fcntl/2.md) and [*flock*(2)](/flock/2.md) locking functions. The file descriptor is not necessarily associated with any of the underlying files used by the access method.

-

  The *fd* function only supports a coarse-grained form of locking. Applications should use the lock manager where possible.

-

  The *fd* function returns the value of *errno* on failure and 0 on success.

- int (\*get)(DB \*db, DB\_TXN \*txnid,

  DBT \*key, DBT \*data, u\_int32\_t flags);\
   A pointer to a function that is an interface for keyed retrieval from the database. The address and length of the data associated with the specified *key* are returned in the structure referenced by *data*.

  In the presence of duplicate key values, *get* will return the first data item for the designated key. Duplicates are sorted by insert order except where this order has been overridden by cursor operations. **Retrieval of duplicates requires the use of cursor operations.** See *db\_cursor*(3) for details.

-

  If the file is being accessed under transaction protection, the *txnid* parameter is a transaction ID returned from *txn\_begin*, otherwise, NULL.

-

  The *flags* parameter must be set to 0 or the following value:

  - DB\_SET\_RECNO

    Retrieve the specified numbered key/data pair from a database. Upon return, both the *key* and *data* items will have been filled in, not just the data item as is done for all other uses of the *get* function.

    The *data* field of the specified *key* must be a pointer to a memory location from which a *db\_recno\_t* may be read, as described in *db\_dbt*(3). This memory location will be read to determine the record to be retrieved.

    For DB\_SET\_RECNO to be specified, the underlying database must be of type btree and it must have been created with the DB\_RECNUM flag (see *db\_open*(3)).

-

  If the database is a recno database and the requested key exists, but was never explicitly created by the application or was later deleted, the *get* function returns DB\_KEYEMPTY. Otherwise, if the requested key isn't in the database, the *get* function returns DB\_NOTFOUND. Otherwise, the *get* function returns the value of *errno* on failure and 0 on success.

- int (\*put)(DB \*db, DB\_TXN \*txnid,

  DBT \*key, DBT \*data, u\_int32\_t flags);\
   A pointer to a function to store key/data pairs in the database. If the database supports duplicates, the *put* function adds the new data value at the end of the duplicate set.

-

  If the file is being accessed under transaction protection, the *txnid* parameter is a transaction ID returned from *txn\_begin*, otherwise, NULL.

-

  The flags value is specified by **or**'ing together one or more of the following values:

  - DB\_APPEND

    Append the key/data pair to the end of the database. For DB\_APPEND to be specified, the underlying database must be of type recno. The record number allocated to the record is returned in the specified *key*.

  - DB\_NOOVERWRITE

    Enter the new key/data pair only if the key does not already appear in the database.

-

  The default behavior of the *put* function is to enter the new key/data pair, replacing any previously existing key if duplicates are disallowed, or to add a duplicate entry if duplicates are allowed. Even if the designated database allows duplicates, a call to *put* with the DB\_NOOVERWRITE flag set will fail if the key already exists in the database.

-

  The *put* function returns the value of *errno* on failure, 0 on success, and DB\_KEYEXIST if the DB\_NOOVERWRITE *flag* was set and the key already exists in the file.

- int (\*sync)(DB \*db, u\_int32\_t flags);

  A pointer to a function to flush any cached information to disk. If the database is in memory only, the *sync* function has no effect and will always succeed.

-

  The *flags* parameter is currently unused, and must be set to 0.

-

  See the *close* function description above for a discussion of DB and cached data.

-

  The *sync* function returns the value of *errno* on failure and 0 on success.

- int (\*stat)(DB \*db, void \*sp,

  void \*(\*db\_malloc)(size\_t), u\_int32\_t flags);\
   A pointer to a function to create a statistical structure and copy a pointer to it into user-specified memory locations. Specifically, if *sp* is non-NULL, a pointer to the statistics for the database are copied into the memory location it references.

  Statistical structures are created in allocated memory. If *db\_malloc* is non-NULL, it is called to allocate the memory, otherwise, the library function [*malloc*(3)](/malloc/3.md) is used. The function *db\_malloc* must match the calling conventions of the [*malloc*(3)](/malloc/3.md) library routine. Regardless, the caller is responsible for deallocating the returned memory. To deallocate the returned memory, free each returned memory pointer; pointers inside the memory do not need to be individually freed.

  **In the presence of multiple threads or processes accessing an active** **database,** **the returned information may be out-of-date.**

  **This function may access all of the pages in the database,** **and therefore may incur a severe performance penalty and have obvious** **negative effects on the underlying buffer pool.**

-

  The *flags* parameter must be set to 0 or the following value:

-

  - DB\_RECORDCOUNT

    Fill in the *bt\_nrecs* field of the statistics structure, but do not collect any other information. This flag makes it reasonable for applications to request a record count from a database without incurring a performance penalty. It is only available for recno databases, or btree databases where the underlying database was created with the DB\_RECNUM flag.

-

  The *stat* function returns the value of *errno* on failure and 0 on success.

-

  In the case of a btree or recno database, the statistics are stored in a structure of type DB\_BTREE\_STAT (typedef'd in \<db2/db.h\>). The following fields will be filled in:

  - u\_int32\_t bt\_magic;

    Magic number that identifies the file as a btree file.\

  - u\_int32\_t bt\_version;

    The version of the btree file type.\

  - u\_int32\_t bt\_flags;

    Permanent database flags, including DB\_DUP, DB\_FIXEDLEN, DB\_RECNUM and DB\_RENUMBER.\

  - u\_int32\_t bt\_minkey;

    The *bt\_minkey* value specified to *db\_open*(3), if any.\

  - u\_int32\_t bt\_re\_len;

    The *re\_len* value specified to *db\_open*(3), if any.\

  - u\_int32\_t bt\_re\_pad;

    The *re\_pad* value specified to *db\_open*(3), if any.\

  - u\_int32\_t bt\_pagesize;

    Underlying tree page size.\

  - u\_int32\_t bt\_levels;

    Number of levels in the tree.\

  - u\_int32\_t bt\_nrecs;

    Number of data items in the tree (since there may be multiple data items per key, this number may not be the same as the number of keys).\

  - u\_int32\_t bt\_int\_pg;

    Number of tree internal pages.\

  - u\_int32\_t bt\_leaf\_pg;

    Number of tree leaf pages.\

  - u\_int32\_t bt\_dup\_pg;

    Number of tree duplicate pages.\

  - u\_int32\_t bt\_over\_pg;

    Number of tree overflow pages.\

  - u\_int32\_t bt\_free;

    Number of pages on the free list.\

  - u\_int32\_t bt\_freed;

    Number of pages made available for reuse because they were emptied.\

  - u\_int32\_t bt\_int\_pgfree;

    Number of bytes free in tree internal pages.\

  - u\_int32\_t bt\_leaf\_pgfree;

    Number of bytes free in tree leaf pages.\

  - u\_int32\_t bt\_dup\_pgfree;

    Number of bytes free in tree duplicate pages.\

  - u\_int32\_t bt\_over\_pgfree;

    Number of bytes free in tree overflow pages.\

  - u\_int32\_t bt\_pfxsaved;

    Number of bytes saved by prefix compression.\

  - u\_int32\_t bt\_split;

    Total number of tree page splits (includes fast and root splits).\

  - u\_int32\_t bt\_rootsplit;

    Number of root page splits.\

  - u\_int32\_t bt\_fastsplit;

    Number of fast splits. When sorted keys are added to the database, the DB btree implementation will split left or right to increase the page-fill factor. This number is a measure of how often it was possible to make such a split.\

  - u\_int32\_t bt\_added;

    Number of keys added.\

  - u\_int32\_t bt\_deleted;

    Number of keys deleted.\

  - u\_int32\_t bt\_get;

    Number of keys retrieved. (Note, this value will not reflect any keys retrieved when the database was open for read-only access, as there is no permanent location to store the information in this case.)\

  - u\_int32\_t bt\_cache\_hit;

    Number of hits in tree fast-insert code. When sorted keys are added to the database, the DB btree implementation will check the last page where an insert occurred before doing a full lookup. This number is a measure of how often the lookup was successful.\

  - u\_int32\_t bt\_cache\_miss;

    Number of misses in tree fast-insert code. See the description of bt\_cache\_hit; this number is a measure of how often the lookup failed.

## ENVIRONMENT VARIABLES

The following environment variables affect the execution of *db\_open*:

- DB\_HOME

  If the *dbenv* argument to *db\_open* was initialized using *db\_appinit*, the environment variable DB\_HOME may be used as the path of the database home for the interpretation of the *dir* argument to *db\_open*, as described in *db\_appinit*(3). Specifically, *db\_open* is affected by the configuration string value of DB\_DATA\_DIR.

## EXAMPLES

Applications that create short-lived databases that are discarded or recreated when the system fails and are unconcerned with concurrent access and loss of data due to catastrophic failure, may wish to use the *db\_open* functionality without other parts of the DB library. Such applications will only be concerned with the DB access methods. The DB access methods will use the memory pool subsystem, but the application is unlikely to be aware of this. See the files *example/ex\_access.c* and *example/ex\_btrec.c* in the DB source distribution for C language code examples of how such applications might use the DB library.

## ERRORS

The *db\_open* function may fail and return *errno* for any of the errors specified for the following DB and library functions: DB-\>sync(3),  [calloc(3)](/calloc/3.md),  [close(2)](/close/2.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_get(3),  lock\_id(3),  lock\_put(3),  lock\_vec(3),  log\_put(3),  log\_register(3),  log\_unregister(3),  [malloc(3)](/malloc/3.md),  [memcpy(3)](/memcpy/3.md),  [memmove(3)](/memmove/3.md),  memp\_close(3),  memp\_fclose(3),  memp\_fget(3),  memp\_fopen(3),  memp\_fput(3),  memp\_fset(3),  memp\_fsync(3),  memp\_open(3),  memp\_register(3),  [memset(3)](/memset/3.md),  [mmap(2)](/mmap/2.md),  [munmap(2)](/munmap/2.md),  [open(2)](/open/2.md),  [read(2)](/read/2.md),  [realloc(3)](/realloc/3.md),  [sigfillset(3)](/sigfillset/3.md),  [sigprocmask(2)](/sigprocmask/2.md),  [stat(2)](/stat/2.md),  [strcpy(3)](/strcpy/3.md),  [strdup(3)](/strdup/3.md),  [strerror(3)](/strerror/3.md),  [strlen(3)](/strlen/3.md),  [time(3)](/time/3.md),  and [unlink(2)](/unlink/2.md).

In addition, the *db\_open* function may fail and return *errno* for the following conditions:

- \[EAGAIN\]

  A lock was unavailable.

- \[EINVAL\]

  An invalid flag value or parameter was specified (e.g., unknown database type, page size, hash function, recno pad byte, byte order) or a flag value or parameter that is incompatible with the current *file* specification.

  The DB\_THREAD flag was specified and spinlocks are not implemented for this architecture.

  There is a mismatch between the version number of *file* and the software.

  A *re\_source* file was specified with either the DB\_THREAD flag or a non-NULL *tx\_info* field in the DB\_ENV argument to db\_open.

- \[ENOENT\]

  A non-existent *re\_source* file was specified.

- \[EPERM\]

  Database corruption was detected. All subsequent database calls (other than *DB-\>close*) will return EPERM.

The *DB-\>close* function may fail and return *errno* for any of the errors specified for the following DB and library functions: DB-\>sync(3),  [calloc(3)](/calloc/3.md),  [close(2)](/close/2.md),  [fflush(3)](/fflush/3.md),  lock\_get(3),  lock\_put(3),  lock\_vec(3),  log\_put(3),  log\_unregister(3),  [malloc(3)](/malloc/3.md),  [memcpy(3)](/memcpy/3.md),  [memmove(3)](/memmove/3.md),  memp\_close(3),  memp\_fclose(3),  memp\_fget(3),  memp\_fput(3),  memp\_fset(3),  memp\_fsync(3),  [memset(3)](/memset/3.md),  [munmap(2)](/munmap/2.md),  [realloc(3)](/realloc/3.md),  and [strerror(3)](/strerror/3.md).

The *DB-\>cursor* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [calloc(3)](/calloc/3.md).

In addition, the *DB-\>cursor* function may fail and return *errno* for the following conditions:

- \[EINVAL\]

  An invalid flag value or parameter was specified.

- \[EPERM\]

  Database corruption was detected. All subsequent database calls (other than *DB-\>close*) will return EPERM.

The *DB-\>del* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [calloc(3)](/calloc/3.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_get(3),  lock\_id(3),  lock\_put(3),  lock\_vec(3),  log\_put(3),  [malloc(3)](/malloc/3.md),  [memcmp(3)](/memcmp/3.md),  [memcpy(3)](/memcpy/3.md),  [memmove(3)](/memmove/3.md),  memp\_fget(3),  memp\_fput(3),  memp\_fset(3),  [memset(3)](/memset/3.md),  [realloc(3)](/realloc/3.md),  and [strerror(3)](/strerror/3.md).

In addition, the *DB-\>del* function may fail and return *errno* for the following conditions:

- \[EAGAIN\]

  A lock was unavailable.

- \[EINVAL\]

  An invalid flag value or parameter was specified.

- \[EPERM\]

  Database corruption was detected. All subsequent database calls (other than *DB-\>close*) will return EPERM.

In addition, the *DB-\>fd* function may fail and return *errno* for the following conditions:

- \[ENOENT\]

  The *DB-\>fd* function was called for an in-memory database, or no underlying file has yet been created.

- \[EPERM\]

  Database corruption was detected. All subsequent database calls (other than *DB-\>close*) will return EPERM.

The *DB-\>get* function may fail and return *errno* for any of the errors specified for the following DB and library functions: DBcursor-\>c\_get(3),  [calloc(3)](/calloc/3.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_get(3),  lock\_id(3),  lock\_put(3),  lock\_vec(3),  log\_put(3),  [malloc(3)](/malloc/3.md),  [memcmp(3)](/memcmp/3.md),  [memcpy(3)](/memcpy/3.md),  [memmove(3)](/memmove/3.md),  memp\_fget(3),  memp\_fput(3),  memp\_fset(3),  [memset(3)](/memset/3.md),  [realloc(3)](/realloc/3.md),  and [strerror(3)](/strerror/3.md).

In addition, the *DB-\>get* function may fail and return *errno* for the following conditions:

- \[EAGAIN\]

  A lock was unavailable.

- \[EINVAL\]

  An invalid flag value or parameter was specified.

  The DB\_THREAD flag was specified to the *db\_open*(3) function and neither the DB\_DBT\_MALLOC or DB\_DBT\_USERMEM flags were set in the DBT.

  A record number of 0 was specified.

- \[EPERM\]

  Database corruption was detected. All subsequent database calls (other than *DB-\>close*) will return EPERM.

The *DB-\>put* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [calloc(3)](/calloc/3.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_get(3),  lock\_id(3),  lock\_put(3),  lock\_vec(3),  log\_put(3),  [malloc(3)](/malloc/3.md),  [memcmp(3)](/memcmp/3.md),  [memcpy(3)](/memcpy/3.md),  [memmove(3)](/memmove/3.md),  memp\_fget(3),  memp\_fput(3),  memp\_fset(3),  [memset(3)](/memset/3.md),  [realloc(3)](/realloc/3.md),  and [strerror(3)](/strerror/3.md).

In addition, the *DB-\>put* function may fail and return *errno* for the following conditions:

- \[EACCES\]

  An attempt was made to modify a read-only database.

- \[EAGAIN\]

  A lock was unavailable.

- \[EINVAL\]

  An invalid flag value or parameter was specified.

  A record number of 0 was specified.

  An attempt was made to add a record to a fixed-length database that was too large to fit.

  An attempt was made to do a partial put.

- \[EPERM\]

  Database corruption was detected. All subsequent database calls (other than *DB-\>close*) will return EPERM.

- \[ENOSPC\]

  A btree exceeded the maximum btree depth (255).

The *DB-\>stat* function may fail and return *errno* for any of the errors specified for the following DB and library functions: [calloc(3)](/calloc/3.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_get(3),  lock\_id(3),  lock\_put(3),  lock\_vec(3),  [malloc(3)](/malloc/3.md),  [memcpy(3)](/memcpy/3.md),  memp\_fget(3),  memp\_fput(3),  and [memset(3)](/memset/3.md).

The *DB-\>sync* function may fail and return *errno* for any of the errors specified for the following DB and library functions: DB-\>get(3),  DB-\>sync(3),  [calloc(3)](/calloc/3.md),  [close(2)](/close/2.md),  [fcntl(2)](/fcntl/2.md),  [fflush(3)](/fflush/3.md),  lock\_get(3),  lock\_id(3),  lock\_put(3),  lock\_vec(3),  log\_put(3),  [malloc(3)](/malloc/3.md),  [memcpy(3)](/memcpy/3.md),  [memmove(3)](/memmove/3.md),  memp\_fget(3),  memp\_fput(3),  memp\_fset(3),  memp\_fsync(3),  [memset(3)](/memset/3.md),  [munmap(2)](/munmap/2.md),  [open(2)](/open/2.md),  [realloc(3)](/realloc/3.md),  [strerror(3)](/strerror/3.md),  [unlink(2)](/unlink/2.md),  and [write(2)](/write/2.md).

In addition, the *DB-\>sync* function may fail and return *errno* for the following conditions:

- \[EINVAL\]

  An invalid flag value or parameter was specified.

- \[EPERM\]

  Database corruption was detected. All subsequent database calls (other than *DB-\>close*) will return EPERM.

## SEE ALSO

*The Ubiquitous B-tree*, Douglas Comer, ACM Comput. Surv. 11, 2 (June 1979), 121-138.

*Prefix B-trees*, Bayer and Unterauer, ACM Transactions on Database Systems, Vol. 2, 1 (March 1977), 11-26.

*The Art of Computer Programming Vol. 3: Sorting and Searching*, D.E. Knuth, 1968, pp 471-480.

*Dynamic Hash Tables*, Per-Ake Larson, Communications of the ACM, April 1988.

*A New Hash Package for UNIX*, Margo Seltzer, USENIX Proceedings, Winter 1991.

*Document Processing in a Relational Database System*, Michael Stonebraker, Heidi Stettner, Joseph Kalash, Antonin Guttman, Nadene Lynn, Memorandum No. UCB/ERL M82/32, May 1982.

[*db\_archive*(1)](/db_archive/1.md), [*db\_checkpoint*(1)](/db_checkpoint/1.md), [*db\_deadlock*(1)](/db_deadlock/1.md), [*db\_dump*(1)](/db_dump/1.md), [*db\_load*(1)](/db_load/1.md), [*db\_recover*(1)](/db_recover/1.md), [*db\_stat*(1)](/db_stat/1.md), *db\_intro*(3), *db\_appinit*(3), *db\_cursor*(3), *db\_dbm*(3), *db\_internal*(3), *db\_lock*(3), *db\_log*(3), *db\_mpool*(3), *db\_open*(3), *db\_thread*(3), *db\_txn*(3)
