postgresql-9.5

How to Retrieve id of inserted row when using upsert with WITH cluase in Posgres 9.5?

删除回忆录丶 提交于 2019-11-30 19:53:14
问题 I'm trying to do upset query in Postgres 9.5 using "WITH" with s as ( select id from products where product_key = 'test123' ), i as ( insert into products (product_key, count_parts) select 'test123', 33 where not exists (select 1 from s) returning id ) update products set product_key='test123', count_parts=33 where id = (select id from s) returning id Apparently I'm retrieving the id only on the updates and get nothing on insertions even though I know insertions succeeded. I need to modify

How to correctly do upsert in postgres 9.5

会有一股神秘感。 提交于 2019-11-30 07:45:21
问题 correct syntax of upsert with postgresql 9.5, below query shows column reference "gallery_id" is ambiguous error , why? var dbQuery = `INSERT INTO category_gallery ( category_id, gallery_id, create_date, create_by_user_id ) VALUES ($1, $2, $3, $4) ON CONFLICT (category_id) DO UPDATE SET category_id = $1, last_modified_date = $3, last_modified_by_user_id = $4 WHERE gallery_id = $2`; I tried change WHERE gallery_id = $2; to WHERE category_gallery.gallery_id = $2; then shows error there is no

PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values

旧时模样 提交于 2019-11-29 20:54:52
When you are upserting a row (PostgreSQL >= 9.5), and you want the possible INSERT to be exactly the same as the possible UPDATE, you can write it like this: INSERT INTO tablename (id, username, password, level, email) VALUES (1, 'John', 'qwerty', 5, 'john@mail.com') ON CONFLICT (id) DO UPDATE SET id=EXCLUDED.id, username=EXCLUDED.username, password=EXCLUDED.password, level=EXCLUDED.level,email=EXCLUDED.email Is there a shorter way? To just say: use all the EXCLUDE values. In SQLite I used to do : INSERT OR REPLACE INTO tablename (id, user, password, level, email) VALUES (1, 'John', 'qwerty',

Postgresql - unable to drop database because of some auto connections to DB

橙三吉。 提交于 2019-11-29 18:45:33
Whenever I try to drop database I get: ERROR: database "pilot" is being accessed by other users DETAIL: There is 1 other session using the database. When I use: SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'TARGET_DB'; I terminated the connection from that DB, but if I try to drop database after that somehow someone automatically connects to that database and gives this error. What could be doing that? No one uses this database, except me. You can prevent future connections: REVOKE CONNECT ON DATABASE thedb FROM public; (and possibly

GIN index on smallint[] column not used or error “operator is not unique”

ぃ、小莉子 提交于 2019-11-29 12:49:58
create table test( id serial primary key, tagged smallint[] ); There is gin index on tagged column, with _int2_ops operator class: CREATE INDEX ix ON test USING GIN(col _int2_ops); When I run this query: select * from test where tagged @> ARRAY[11] order by id limit 100; EXPLAIN ANALYZE shows: Limit (cost=0.43..19524.39 rows=100 width=36) (actual time=25024.124..25027.263 rows=100 loops=1) -> Index Scan using test_pkey on test (cost=0.43..508404.37 rows=2604 width=36) (actual time=25024.121..25027.251 rows=100 loops=1) Filter: ((tagged)::integer[] @> '{11}'::integer[]) Rows Removed by Filter:

How to correctly do upsert in postgres 9.5

谁都会走 提交于 2019-11-29 05:27:23
correct syntax of upsert with postgresql 9.5, below query shows column reference "gallery_id" is ambiguous error , why? var dbQuery = `INSERT INTO category_gallery ( category_id, gallery_id, create_date, create_by_user_id ) VALUES ($1, $2, $3, $4) ON CONFLICT (category_id) DO UPDATE SET category_id = $1, last_modified_date = $3, last_modified_by_user_id = $4 WHERE gallery_id = $2`; I tried change WHERE gallery_id = $2; to WHERE category_gallery.gallery_id = $2; then shows error there is no unique or exclusion constraint matching the ON CONFLICT specification , but I don't want to set gallery

How to install Postgis to a Keg installation of Postgres@9.6 using Homebrew?

早过忘川 提交于 2019-11-29 03:06:24
问题 I have installed Postgresql@9.6 and Postgis via Homebrew. However, installing Postgis via Homebrew installs the latest version of Postgresql at 10 as dependency and pinning Postgresql at 9.6.5 blocks the install of Postgis via Homebrew. Performing 'CREATE EXTENSION postgis;' returns: ERROR: could not open extension control file "/usr/local/Cellar/postgresql@9.6/9.6.5/share/postgresql@9.6/extension/postgis.control": No such file or directory I've also tried uninstalling the Postgresql (at 10)

Return rows from INSERT with ON CONFLICT without needing to update

心不动则不痛 提交于 2019-11-28 09:06:21
I have a situation where I very frequently need to get a row from a table with a unique constraint, and if none exists then create it and return. For example my table might be: CREATE TABLE names( id SERIAL PRIMARY KEY, name TEXT, CONSTRAINT names_name_key UNIQUE (name) ); And it contains: id | name 1 | bob 2 | alice Then I'd like to: INSERT INTO names(name) VALUES ('bob') ON CONFLICT DO NOTHING RETURNING id; Or perhaps: INSERT INTO names(name) VALUES ('bob') ON CONFLICT (name) DO NOTHING RETURNING id and have it return bob's id 1 . However, RETURNING only returns either inserted or updated

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

若如初见. 提交于 2019-11-28 09:03:47
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 status FROM update_cte) UNION (SELECT status FROM insert_cte) This query will return either "updated" or

GIN index on smallint[] column not used or error “operator is not unique”

故事扮演 提交于 2019-11-28 06:37:03
问题 create table test( id serial primary key, tagged smallint[] ); There is gin index on tagged column, with _int2_ops operator class: CREATE INDEX ix ON test USING GIN(col _int2_ops); When I run this query: select * from test where tagged @> ARRAY[11] order by id limit 100; EXPLAIN ANALYZE shows: Limit (cost=0.43..19524.39 rows=100 width=36) (actual time=25024.124..25027.263 rows=100 loops=1) -> Index Scan using test_pkey on test (cost=0.43..508404.37 rows=2604 width=36) (actual time=25024.121.