INSERT
Creates new rows in a table.
Synopsis
[ WITH [ RECURSIVE ] <with_query> [, ...] ]
INSERT INTO <table_name> [ AS <alias> ] [( <column_name> [, ...] )]
[ OVERRIDING { SYSTEM | USER } VALUE ]
{DEFAULT VALUES | VALUES ( {<expression> | DEFAULT} [, ...] ) [, ...] | <query>}
[ ON CONFLICT [ <conflict_target> ] <conflict_action> ]
[RETURNING * | <output_expression> [ [AS] <output_name> ] [, ...]]
-- where <conflict_target> can be one of:
( { <index_column_name> | ( <index_expression> ) } [ COLLATE <collation> ] [ <opclass> ] [, ...] )
[ WHERE <index_predicate> ]
ON CONSTRAINT <constraint_name>
-- and <conflict_action> is one of:
DO NOTHING
DO UPDATE SET { <column_name> = { <expression> | DEFAULT } |
( <column_name> [, ...] ) = [ ROW ] ( { <expression> | DEFAULT } [, ...] ) |
( <column_name> [, ...] ) = ( sub-SELECT )
} [, ...]
[ WHERE <condition> ]
Description
INSERT
inserts new rows into a table. One can insert one or more rows specified by value expressions, or zero or more rows resulting from a query.
The target column names may be listed in any order. If no list of column names is given at all, the default is the columns of the table in their declared order; or the first N column names, if there are only N columns supplied by the VALUES
clause or query. The values supplied by the VALUES
clause or query are associated with the explicit or implicit column list left-to-right.
Each column not present in the explicit or implicit column list will be filled with a default value, either its declared default value or null if there is no default.
If the expression for any column is not of the correct data type, Apache Cloudberry attempts automatic type conversion.
INSERT
into tables that lack unique indexes will not be blocked by concurrent activity. Tables with unique indexes might block if concurrent sessions perform actions that lock or modify rows matching the unique index values being inserted; the details are covered in Index Uniqueness Checks in the PostgreSQL documentation. ON CONFLICT
can be used to specify an alternative action to raising a unique constraint or exclusion constraint violation error. (See ON CONFLICT Clause below.)
The optional RETURNING
clause causes INSERT
to compute and return value(s) based on each row actually inserted (or updated, if an ON CONFLICT DO UPDATE
clause was used). This is primarily useful for obtaining values that were supplied by defaults, such as a serial sequence number. However, any expression using the table's columns is allowed. The syntax of the RETURNING
list is identical to that of the output list of SELECT
. Only rows that were successfully inserted or updated will be returned. For example, if a row was locked but not updated because an ON CONFLICT DO UPDATE ... WHERE
clause condition was not satisfied, the row will not be returned.
You must have INSERT
privilege on a table in order to insert into it. If ON CONFLICT DO UPDATE
is present, UPDATE
privilege on the table is also required.
When a column list is specified, you only need INSERT
privilege on the listed columns. Similarly, when ON CONFLICT DO UPDATE
is specified, you only need UPDATE
privilege on the column(s) that are listed to be updated. However, ON CONFLICT DO UPDATE
also requires SELECT
privilege on any column whose values are read in the ON CONFLICT DO UPDATE
expressions or condition.
Use of the RETURNING
clause requires SELECT
privilege on all columns mentioned in RETURNING
. If you use the query clause to insert rows from a query, you must have SELECT
privilege on any table or column referenced in the query.