upsert

Migrating an Oracle MERGE statement to a PostgreSQL UPSERT statement

社会主义新天地 提交于 2021-02-18 15:22:42
问题 Can you please help me converting the following Oracle MERGE statement into a valid UPSERT statement for use in a PostgreSQL 9.3 database? MERGE INTO my_table a USING (SELECT v_c1 key, v_c2 AS pkey, v_c3 AS wcount, v_c4 AS dcount FROM DUAL) b ON ( a.key = b.key AND a.pkey = b.pkey WHEN MATCHED THEN UPDATE SET wcount = b.wcount, dcount = b.dcount WHEN NOT MATCHED THEN INSERT (key, pkey, wcount, dcount) VALUES(b.key,b.pkey,b.wcount,b.dcount); 回答1: I don't think there's UPSERT statement in

How can I get the INSERTED and UPDATED rows for an UPSERT operation in postgres

烈酒焚心 提交于 2021-02-16 09:34:09
问题 I've got an UPSERT Operation like this: INSERT INTO people (SELECT * FROM people_update) ON CONFLICT (name,surname) DO UPDATE SET age = EXCLUDED.age , street = EXCLUDED.street , city = EXCLUDED.city , postal = EXCLUDED.postal WHERE (people.age,people.street,people.city,people.postal) IS DISTINCT FROM (EXCLUDED.age,EXCLUDED.street,EXCLUDED.city,EXCLUDED.postal) RETURNING case when xmax::text::int > 0 then 'updated' else 'inserted' end,name,surname,age,street,city,postal; (name,surname) is a

Is INSERT … SELECT an atomic transaction?

梦想与她 提交于 2021-02-10 18:15:40
问题 I use a query like this: INSERT INTO table SELECT * FROM table2 t2 JOIN ... ... WHERE table2.date < now() - '1 day'::INTERVAL FOR UPDATE OF t2 SKIP LOCKED ON CONFLICT (...) DO UPDATE SET ... RETURNING *; My question is about FOR UPDATE t2 SKIP LOCKED . Should I use it here? Or will Postgres lock these rows automatically with INSERT SELECT ON CONFLICT till the end of the transaction? My goal is to prevent other apps from (concurrently) capturing rows with the inner SELECT which are already

Why does my SQL Server UPSERT code sometimes not block?

纵然是瞬间 提交于 2021-02-08 20:46:07
问题 I have a table ImportSourceMetadata which I use to control an import batch process. It contains a PK column SourceId and a data column LastCheckpoint . The import batch process reads the LastCheckpoint for a given SourceId , performs some logic (on other tables), then updates the LastCheckpoint for that SourceId or inserts it if it doesn't exist yet . Multiple instances of the process run at the same time, usually with disjunct SourceIds , and I need high parallelity for those cases. However,

INSERT & UPDATE MySql table using PySpark DataFrames and JDBC

故事扮演 提交于 2021-02-08 09:36:06
问题 I'm trying to insert and update some data on MySql using PySpark SQL DataFrames and JDBC connection. I've succeeded to insert new data using the SaveMode.Append. Is there a way to update the existing data and insert new data in MySql Table from PySpark SQL? My code to insert is: myDataFrame.write.mode(SaveMode.Append).jdbc(JDBCurl,mySqlTable,connectionProperties) If I change to SaveMode.Overwrite it deletes the full table and creates a new one, I'm looking for something like the "ON DUPLICATE

PostgreSQL partial unique index and upsert

被刻印的时光 ゝ 提交于 2021-02-07 19:11:51
问题 I'm trying to do an upsert to a table that has partial unique indexes create table test ( p text not null, q text, r text, txt text, unique(p,q,r) ); create unique index test_p_idx on test(p) where q is null and r is null; create unique index test_pq_idx on test(p, q) where r IS NULL; create unique index test_pr_idx on test(p, r) where q is NULL; In plain terms, p is not null and only one of q or r can be null. Duplicate inserts throw constraint violations as expected insert into test(p,q,r

PostgreSQL partial unique index and upsert

六眼飞鱼酱① 提交于 2021-02-07 19:10:03
问题 I'm trying to do an upsert to a table that has partial unique indexes create table test ( p text not null, q text, r text, txt text, unique(p,q,r) ); create unique index test_p_idx on test(p) where q is null and r is null; create unique index test_pq_idx on test(p, q) where r IS NULL; create unique index test_pr_idx on test(p, r) where q is NULL; In plain terms, p is not null and only one of q or r can be null. Duplicate inserts throw constraint violations as expected insert into test(p,q,r

Mongodb: upsert multiple documents at the same time (with key like $in)

落花浮王杯 提交于 2021-01-29 18:38:25
问题 I stumbled upon a funny behavior in MongoDB: When I run: db.getCollection("words").update({ word: { $in: ["nico11"] } }, { $inc: { nbHits: 1 } }, { multi: 1, upsert: 1 }) it will create "nico11" if it doesn't exist, and increase nbHits by 1 (as expected). However, when I run: db.getCollection("words").update({ word: { $in: ["nico10", "nico11", "nico12"] } }, { $inc: { nbHits: 1 } }, { multi: 1, upsert: 1 }) it will correctly update the keys that are already in the DB, but not insert the

Update multiple columns on conflict postgres

℡╲_俬逩灬. 提交于 2021-01-29 06:40:50
问题 I have to write a query to update a record if it exists else insert it. I am doing this update/insert into a postgres DB. I have looked into the upsert examples and most of them use maximum of two fields to update. However, I want to update multiple columns. Example: query="""INSERT INTO table (col1,col2,col3,col4,col5,col6,col7,col8,col9,..col20) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT(col2) DO UPDATE SET (col1,col2,col3,col4,col5,col6,col7,col8,col9,.

How to use ON DUPLICATE KEY on hsqldb 2.3.4

风流意气都作罢 提交于 2021-01-29 04:34:47
问题 According to the update on hsqldb.org listed here: http://hsqldb.org/web/features200.html It now supports the mysql syntax ON DUPLICATE KEY in hsqldb 2.3.4, yet Im still getting sql errors when trying to run it. If im reading correctly I may need to set certain flags. But I cant find what to set to be able to use this synatx. 回答1: MySQL compatibility is documented in the Guide http://hsqldb.org/doc/2.0/guide/compatibility-chapt.html#coc_compatibility_mysql You need to execute SET DATABASE SQL