Documentation Home
MySQL Internals Manual

23.18.18 open


Opens a table.


virtual int open (




const char *

name ;


mode ;


test_if_locked ;


This is the open method.

Used for opening tables. The name will be the name of the file. A table is opened when it needs to be opened. For instance when a request comes in for a select on the table (tables are not open and closed for each request, they are cached).

Called from by handler::ha_open(). The server opens all tables by calling ha_open() which then calls the handler specific open().

A handler object is opened as part of its initialization and before being used for normal queries (not before meta-data changes always.) If the object was opened it will also be closed before being deleted.

This is the open method. open is called to open a database table.

The first parameter is the name of the table to be opened. The second parameter determines what file to open or what operation to take. The values are defined in handler.h and are copied here for your convenience:

#define HA_OPEN_KEYFILE      1
#define HA_OPEN_RNDFILE      2
#define HA_GET_INDEX         4
#define HA_GET_INFO          8 /* do a ha_info() after open */
#define HA_READ_ONLY        16 /* File opened as readonly */
#define HA_TRY_READ_ONLY    32 /* Try readonly if can't open with read and write */
#define HA_WAIT_IF_LOCKED   64 /* Wait if locked on open */
#define HA_ABORT_IF_LOCKED 128 /* skip if locked on open.*/
#define HA_BLOCK_LOCK      256 /* unlock when reading some records */
#define HA_OPEN_TEMPORARY  512

The final option dictates whether the handler should check for a lock on the table before opening it.

Typically your storage engine will need to implement some form of shared access control to prevent file corruption is a multi-threaded environment. For an example of how to implement file locking, see the get_share() and free_share() methods of sql/examples/


  • name

  • mode

  • test_if_locked

Return Values

There are no return values.


This example is from the CSV storage engine:

int ha_tina::open(const char *name, int mode, uint test_if_locked)

        if (!(share= get_share(name, table)))