postgresql-9.5

Use multiple conflict_target in ON CONFLICT clause

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:08:11
I have two columns in table col1 , col2 , they both are unique indexed (col1 is unique and so is col2). I need at insert into this table, use ON CONFLICT syntax and update other columns, but I can't use both column in conflict_target clause. It works: INSERT INTO table ... ON CONFLICT ( col1 ) DO UPDATE SET -- update needed columns here But how to do this for several columns, something like this: ... ON CONFLICT ( col1, col2 ) DO UPDATE SET .... e4c5 A sample table and data CREATE TABLE dupes(col1 int primary key, col2 int, col3 text, CONSTRAINT col2_unique UNIQUE (col2) ); INSERT INTO dupes

PostgreSQL Upsert differentiate inserted and updated rows using system columns XMIN, XMAX and others

北战南征 提交于 2019-11-27 14:03:36
Disclaimer: theoretical question. Several questions here was asked about how to differentiate inserted and updated rows in the PostgreSQL upsert statement. Here is a simple example: nd@postgres=# create table t(i int primary key, x int); nd@postgres=# insert into t values(1,1); nd@postgres=# insert into t values(1,11),(2,22) on conflict(i) do update set x = excluded.i*11 returning *, xmin, xmax; ╔═══╤════╤══════╤══════╗ ║ i │ x │ xmin │ xmax ║ ╠═══╪════╪══════╪══════╣ ║ 1 │ 11 │ 7696 │ 7696 ║ ║ 2 │ 22 │ 7696 │ 0 ║ ╚═══╧════╧══════╧══════╝ So, xmax > 0 (or xmax = xmin ) - row was updated; xmax

PostgreSQL rename attribute in jsonb field

风格不统一 提交于 2019-11-27 02:40:06
问题 In postgresql 9.5, is there a way to rename an attribute in a jsonb field? For example: { "nme" : "test" } should be renamed to { "name" : "test"} 回答1: In UPDATE use delete (-) and concatenate (||) operators, e.g.: create table example(id int primary key, js jsonb); insert into example values (1, '{"nme": "test"}'), (2, '{"nme": "second test"}'); update example set js = js - 'nme' || jsonb_build_object('name', js->'nme') where js ? 'nme' returning *; id | js ----+------------------------- 1 |

Use multiple conflict_target in ON CONFLICT clause

霸气de小男生 提交于 2019-11-27 01:00:47
问题 I have two columns in table col1 , col2 , they both are unique indexed (col1 is unique and so is col2). I need at insert into this table, use ON CONFLICT syntax and update other columns, but I can't use both column in conflict_target clause. It works: INSERT INTO table ... ON CONFLICT ( col1 ) DO UPDATE SET -- update needed columns here But how to do this for several columns, something like this: ... ON CONFLICT ( col1, col2 ) DO UPDATE SET .... 回答1: A sample table and data CREATE TABLE dupes

How to find out if an upsert was an update with PostgreSQL 9.5+ UPSERT?

萝らか妹 提交于 2019-11-26 20:55:42
问题 Writable CTEs were considered a solution to UPSERT prior to 9.5 as described in Insert, on duplicate update in PostgreSQL? It is possible to perform an UPSERT with the information whether it ended up as an UPDATE or an INSERT with the following Writable CTEs idiom: WITH update_cte AS ( UPDATE t SET v = $1 WHERE id = $2 RETURNING 'updated'::text status ), insert_cte AS ( INSERT INTO t(id, v) SELECT $2, $1 WHERE NOT EXISTS (SELECT 1 FROM update_cte) RETURNING 'inserted'::text status ) (SELECT

Optimize query with OFFSET on large table

白昼怎懂夜的黑 提交于 2019-11-26 17:48:46
I have table create table big_table ( id serial primary key, -- other columns here vote int ); This table is very big, approximately 70 million rows, I need to query: SELECT * FROM big_table ORDER BY vote [ASC|DESC], id [ASC|DESC] OFFSET x LIMIT n -- I need this for pagination As you may know, when x is a large number, queries like this are very slow. For performance optimization I added indexes: create index vote_order_asc on big_table (vote asc, id asc); and create index vote_order_desc on big_table (vote desc, id desc); EXPLAIN shows that the above SELECT query uses these indexes, but it's

Optimize query with OFFSET on large table

笑着哭i 提交于 2019-11-26 05:36:03
问题 I have table create table big_table ( id serial primary key, -- other columns here vote int ); This table is very big, approximately 70 million rows, I need to query: SELECT * FROM big_table ORDER BY vote [ASC|DESC], id [ASC|DESC] OFFSET x LIMIT n -- I need this for pagination As you may know, when x is a large number, queries like this are very slow. For performance optimization I added indexes: create index vote_order_asc on big_table (vote asc, id asc); and create index vote_order_desc on