Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PostgreSQL multi-row insertion all or nothing?

I would like to know if a PostgreSQL multi-row INSERT is safe (all data is inserted or no data is inserted in case of system/database failure). Example of query:

INSERT INTO "tests" ("name") VALUES ('1'), ('2')
like image 571
Lucas Batistussi Avatar asked Mar 30 '15 22:03

Lucas Batistussi


People also ask

How do I add multiple rows in PostgreSQL?

PostgreSQL INSERT Multiple Rows First, specify the name of the table that you want to insert data after the INSERT INTO keywords. Second, list the required columns or all columns of the table in parentheses that follow the table name. Third, supply a comma-separated list of rows after the VALUES keyword.

How many INSERTs can Postgres handle per second?

Single row INSERTs In the same region on AWS with lets say 1ms latency, this number can go up to ~500 INSERTs per second.

Does Postgres preserve insertion order?

The answer for this simple case is: "Yes".


1 Answers

Your own answer (now deleted) is mis-interpreting things. A single INSERT statement is always atomic. Your example INSERT with two rows is all or nothing - both rows are inserted or none.

Whether autocommit is enabled or not is up to your client.

In psql autocommit is enabled by default. All that means is that each statement runs as a transaction of its own, unless you wrap (any number of) statements into an explicit transaction wrapper. So this is "all or nothing":

INSERT INTO test (name) VALUES ('1'), ('2');

Just like this:

BEGIN;
INSERT INTO test (name) VALUES ('1');
INSERT INTO test (name) VALUES ('2');
COMMIT;

But not this:

INSERT INTO test (name) VALUES ('1');
INSERT INTO test (name) VALUES ('2');

In pgAdmin everything you send from the query window at once is wrapped into a transaction by default.

like image 98
Erwin Brandstetter Avatar answered Nov 05 '22 11:11

Erwin Brandstetter