INSERT

v3.2.0

Add rows to a table, with the primary key issued by the engine or supplied by you.

In this article

Adds one or more rows to a table. Each row either supplies its primary key or leaves it out and has the engine issue one.

Syntax

INSERT INTO <table> [(<column> [, ...])] VALUES (<value> [, ...]) [, (<value> [, ...])] ...
    [ON CONFLICT [(<primary key>)] DO NOTHING] [RETURNING <item> [, ...]]

INSERT INTO <table> [(<column> [, ...])] <select statement>
    [ON CONFLICT [(<primary key>)] DO NOTHING] [RETURNING <item> [, ...]]

Without a column list, values are positional. Column names match ignoring case.

Examples

Let the engine issue the key

On items (id, sku, name, category, price, in_stock), five values per row leave the key out:

INSERT INTO items VALUES ('KB-0006', 'Compact keyboard', 'peripherals', 59.00, 1);
INSERTED oid=4006 id=6 page=135 slot=5

Supply the key

Six values per row: the first is the key.

INSERT INTO items VALUES (500, 'SP-0500', 'Desk speakers', 'audio', 129.00, 1);
INSERTED oid=4006 id=500 page=135 slot=6

Several rows, mixed

One statement can mix supplied and issued keys. An issued key is always above every key supplied before it.

INSERT INTO items VALUES (7000, 'WC-7000', 'Webcam', 'peripherals', 69.90, 1), ('MP-7001', 'Mouse pad', 'accessories', 12.00, 1);   -- the mouse pad gets an id above 7000
INSERTED oid=4006 rows=2 first_id=7000 last_id=7001

Name the columns

With a column list, each row has one value per listed column, in the list’s order. The key is issued when the list leaves it out or its value is NULL:

INSERT INTO items (sku, name, category, price, in_stock) VALUES ('CL-0010', 'Cable', 'accessories', 9.00, 1);
INSERT INTO items (name, sku, price, category, in_stock, id) VALUES ('Lamp', 'LP-0900', 19.00, 'accessories', 1, 900);
INSERTED oid=4006 id=7002 page=135 slot=9
INSERTED oid=4006 id=900 page=135 slot=7

A column the list leaves out gets NULL, so it must be declared NULL. There is no DEFAULT:

kdbs> INSERT INTO items (sku, name) VALUES ('X', 'Y')
ERR column 'category' is NOT NULL and the column list leaves it out (byte 18); there is no DEFAULT

Copy rows with a SELECT

INSERT INTO purchases (account_id, item_id, qty, bought_on)
  SELECT account_id, item_id, qty, bought_on FROM purchases WHERE id = 1;
INSERTED oid=4013 id=6 page=138 slot=5

The SELECT runs whole, at a snapshot taken when the statement starts writing, before the first row is placed.

Skip rows whose key is taken

ON CONFLICT DO NOTHING drops a row whose supplied key is already bound, by a live row, a deleted row, or an earlier row of the same statement:

kdbs> INSERT INTO purchases VALUES (3, 2, 3, 1, '2026-10-03', 'reordered') ON CONFLICT DO NOTHING
INSERTED oid=4013 rows=0

Return the inserted rows

kdbs> INSERT INTO accounts (owner, balance) VALUES ('erin', 10 * 5) RETURNING id, owner, balance
id,owner,balance
4,erin,50
INSERTED oid=4002 id=4 page=132 slot=3

Values per row

On a table with n columns, a positional row has either n values (the first is the key) or n − 1 values (the key is issued). Any other count is refused, naming both:

ERR expected 6 value(s) including tuple key column 'id', or 5 to have it issued; got 7 (byte 26)

A full-width row whose key is NULL has its key issued too.

After ALTER TABLE ... ADD COLUMN or DROP COLUMN, only full-width positional rows are accepted, because a short row cannot be told from one written for the old columns:

ERR expected 3 value(s), one per column, with tuple key column 'id' NULL to have it issued; got 1 (byte 26). The relation's columns were changed by ALTER TABLE, so a row that leaves the key out is refused: it cannot be told from a row written for the old columns

A column list is never shifted by a column change. Prefer it in code that outlives a schema change.

Values are literals, NULL for columns declared NULL, or expressions in the grammar of UPDATE … SET that read no column, such as 10 * 5. An expression is computed once, before any row is placed.

Supplied keys

A supplied key must be an integer from 1 to 2⁴⁸ − 1. It is refused, with its byte position and nothing written, for exactly two reasons:

ReasonError
The key is taken by a live row, or by a deleted row that has not been purgedAlreadyExists: duplicate tuple key ...
The key is outside the rangeOutOfRange: tuple key ... is outside the Keystone id space ...

A key whose insert was rolled back is free again. Keys can be supplied in any order; each row is placed where its key sorts.

A row can also be refused by a unique index, a foreign key, an assertion or a Transition.

How it works

Each row, in order, goes through the full single-row path: foreign-key check, Transition and assertion checks, key, placement, index and Cabin maintenance, and the write-ahead log. A multi-row insert saves parsing and round trips, not checks.

A multi-row insert is atomic. If any row is refused, the error gives the row number, such as (row 3), and no row is inserted.

Replies

RowsReply
OneINSERTED oid=<table> id=<key> page=<page> slot=<slot>
Several, or noneINSERTED oid=<table> rows=<n> first_id=<key> last_id=<key> (rows=0 alone when nothing was inserted)
With RETURNINGThe listed items for each inserted row, then one of the replies above

first_id and last_id are the first and last rows in statement order, not the lowest and highest keys.

Limitations

  • At most max_insert_rows rows per VALUES list (default 1,024). More is refused, not truncated.
  • ON CONFLICT covers the primary key only. A row whose unique-index key is taken is refused, even with ON CONFLICT DO NOTHING, and ON CONFLICT (<column>) naming any other column is refused NOT_IMPLEMENTED.
  • No DEFAULT, and no ON CONFLICT ... DO UPDATE.
  • No escape for a quote inside a string literal.
  • A value of the wrong type is refused without a byte position, for example column 'price': 'notanumber' is not a valid decimal.

See also