Questions tagged [upsert]
UPSERT: SQL-UPDATE a row, or if not found INSERT a new row.
120 questions
1 vote
2 answers
99 views
Tuning UPSERT in PG14 w/ 130M records table
I'm tuning the server in order to speed up several SQL queries generated by a product and that can't be modified or tuned themselves. I'm stuck on a query that creates a CTO an then an INSERT / SELECT ...
5 votes
2 answers
842 views
Which approach to concurrency safe upserts is best for table-valued input if MERGE is forbidden?
This classic concurrency safety article is clearly designed for only upserting one row at a time. In my situation, I have a table-valued input and I want to upsert each row in a concurrency safe way. ...
-1 votes
1 answer
322 views
Performance of INSERT ON CONFLICT UPDATE query when update part has a large number of CASE WHEN conditions
I need to upsert data to a PostgreSQL database from a high traffic application which needs to be optimized for write performance. The different rows to upsert in a batch will have values for different ...
0 votes
1 answer
339 views
How to upsert in Sql using Merge statement with where clause
CREATE TABLE SourceProducts( ProductID INT, ProductName VARCHAR(50), Price DECIMAL(9,2) ) GO INSERT INTO SourceProducts(ProductID,ProductName, Price) VALUES(1,'...
3 votes
1 answer
4k views
How to have Postgres return id when ON CONFLICT DO NOTHING
Suppose we have the following table: CREATE TABLE names( id SERIAL NOT NULL, CONSTRAINT names__pk PRIMARY KEY(id), name TEXT NOT NULL, CONSTRAINT names__name__unq UNIQUE(name) ); INSERT INTO names(...
1 vote
1 answer
1k views
postrges on conflict update with all columns as unique constraint
I have a table with columns 'id', 'a', 'b, 'c', 'd'. Also, I have a unique constraint on ('a', 'b', 'c', 'd') so no entries can be added where all those fields are the same. I want to upsert entries e....
1 vote
1 answer
164 views
What are the idiomatic approaches to cross-server upserts?
This is a very common problem, faced by virtually every DBA who has to responsibilities to both application and BI teams. Consider the following: You have two T-SQL servers, Production and Reporting. ...
0 votes
1 answer
230 views
In PostgreSQL, can an index predicate be used to improve performance of `ON CONFLICT`?
Consider the following upsert, run in a trigger: INSERT INTO current_scores (user_id, score, updated_at) VALUES (NEW.user_id, NEW.score, NEW.inserted_at) ON CONFLICT (user_id) DO UPDATE SET ...
0 votes
1 answer
990 views
PostgreSQL: does the order of rows in a bulk insert control the order of `FOR EACH ROW` triggers?
I have a trigger like BEFORE INSERT ON table_a FOR EACH ROW EXECUTE FUNCTION upsert_into_table_b(). If I execute a bulk insert like INSERT INTO table_a ([columns]) VALUES ([row 1 values]), ([row 2 ...
0 votes
1 answer
533 views
In PostgreSQL, what is the `index_predicate` for in `INSERT`?
The docs on INSERT say: If an index_predicate is specified, it must, as a further requirement for inference, satisfy arbiter indexes. Note that this means a non-partial unique index (a unique index ...
0 votes
0 answers
159 views
SQLite Upsert - syntax error
I'm trying to use SQLite's upsert syntax as described in their documentation However I get the following error, in both WebSql (SQLite v3.41.2) and SQLite on Android (SQLite v3.40.0) could not prepare ...
1 vote
0 answers
679 views
duplicate key value INSERT ON CONFLICT UPDATE
With this table definition: create table t1 ( id uuid not null primary key, "u1" integer not null references u, "p1" integer not null references p, "c1" ...
-1 votes
1 answer
1k views
Including deleted_at in unique index for upsert
I have a table with a deleted_at column, see comment. The deleted_at column behaves like a one-way boolean event flag, i.e. once a row is deleted it can't be restored. There is also an asynchronous ...
0 votes
1 answer
91 views
Is UPSERT feature complete?
Here, a lot of energy seems spent on Coalesce - I think it is to avoid the case where a null value in the update record could overwrite a non-null value. Say we have a table of businesses and some ...
1 vote
2 answers
1k views
Insert values and add values to foreign key
I have two tables: CREATE TABLE est ( est_id serial PRIMARY KEY , est_nom varchar(70) ); CREATE TABLE bm ( id serial PRIMARY KEY , est_nom int , nom varchar(70) , CONSTRAINT FK_bm_est FOREIGN KEY(...