postgresql-9.5

Why postgres show two different format for same interval value?

烈酒焚心 提交于 2019-12-04 03:29:56
问题 I was helping with this question trying to change the format for the interval. from '01 day 22:10:37' to '46:10:37' I give a solution with string manipulation. But then I found postgres can show the same interval on two different format. SELECT '2016-01-27 08:51:02'::timestamp - '2016-01-25 10:40:25'::timestamp end_date, '46:10:37'::interval interval_date; Funny thing. There is a function doing the inverse process justify_hours('46:10:37'::interval) --> '1 day 22:10:37' So I wondering if

Why isn't row level security enabled for Postgres views?

拈花ヽ惹草 提交于 2019-12-03 22:44:41
I need strict control of the reading and writing of my Postgres data. Updatable views have always provided very good, strict, control of the reading of my data and allows me to add valuable computed columns. With Postgres 9.5 row level security has introduced a new and powerful way to control my data. But I can't use both technologies views, and row level security together. Why? Basically because it wasn't possible to retroactively change how views work. I'd like to be able to support SECURITY INVOKER (or equivalent) for views but as far as I know no such feature presently exists. You can

Change default schema for user

一曲冷凌霜 提交于 2019-12-03 16:58:47
问题 create schema bla; -- then create table table_name into this schema Then I want change default schema for user (user is postgres ) I do: ALTER ROLE postgres SET search_path TO bla; (Query returned successfully with no result). When I try SELECT * FROM table_name gives error relation "table_name" does not exist Though SELECT * FROM bla.table_name works fine. What is wrong in my attempt to change default schema for user? 回答1: I think you need to relogin for that. With ALTER USER ... SET you

Can two concurrent but identical DELETE statements cause a deadlock?

放肆的年华 提交于 2019-12-03 13:10:39
Assume some_table has two rows, with primary key 1 and 2 . The following sequence of statements can cause a deadlock: session 1: begin; session 2: begin; session 1: DELETE FROM my_table WHERE my_key = 1; session 2: DELETE FROM my_table WHERE my_key = 2; session 1: DELETE FROM my_table WHERE my_key = 2; session 2: DELETE FROM my_table WHERE my_key = 1; The deadlock would not have occurred if both sessions deleted in the same order. Now, coming to my question, what happens if the DELETE statement touches multiple rows? For example: session 1: begin; session 2: begin; session 1: DELETE FROM my

Postgres shuts down immediately when started with docker-compose

纵然是瞬间 提交于 2019-12-03 10:11:00
Postgres shuts down immediately when started with docker-compose. The yaml file used is below version: '2' services: postgres: image: postgres:9.5 container_name: local-postgres9.5 ports: - "5432:5432" The log when docker-compose up command is executed Creating local-postgres9.5 Attaching to local-postgres9.5 local-postgres9.5 | The files belonging to this database system will be owned by user "postgres". local-postgres9.5 | This user must also own the server process. local-postgres9.5 | local-postgres9.5 | The database cluster will be initialized with locale "en_US.utf8". local-postgres9.5 |

Appending (pushing) and removing from a JSON array in PostgreSQL 9.5+

。_饼干妹妹 提交于 2019-12-03 05:41:51
问题 For versions less than 9.5 see this question I have created a table in PostgreSQL using this: CREATE TEMP TABLE jsontesting AS SELECT id, jsondata::jsonb FROM ( VALUES (1, '["abra","value","mango", "apple", "sample"]'), (2, '["japan","china","india", "russia", "australia"]'), (3, '["must", "match"]'), (4, '["abra","value","true", "apple", "sample"]'), (5, '["abra","false","mango", "apple", "sample"]'), (6, '["string","value","mango", "apple", "sample"]'), (7, '["must", "watch"]') ) AS t(id

How do I create a nested function in PL/pgSQL?

我是研究僧i 提交于 2019-12-01 17:02:18
问题 I would like to create a function in PL/pgSQL with a couple of nested (or inner) functions within it. This way I can break the problem down into smaller pieces but not have my smaller pieces accessible outside of this function. Is it possible to do this in PL/pgSQL? If so, how? 回答1: Nested functions are not supported by PLpgSQL. The emulation has not any sense and it is nonproductive. 回答2: Try it: CREATE OR REPLACE FUNCTION outer() RETURNS void AS $outer$ DECLARE s text; BEGIN CREATE OR

Postgres 9.5 ON CONFLICT DO SELECT

淺唱寂寞╮ 提交于 2019-12-01 12:17:41
While doing UPSERT in Postgres 9.5, is it possible to return null on INSERT success and return something ON CONFLICT? I would like to something like this: insert into "user" (timestamp, user_id, member_id) values ($1, $2, $3) ON CONFLICT (user_id, member_id) DO select id from "user" where user_id = $2 returning user_id The only purpose of selecting user_id is to return something (anything other than null) ON CONFLICT, and without writing to disk. I know that this can be done with ON CONFLICT DO UPDATE but it would involve writing to disk. You could use CTE: WITH cte AS ( INSERT INTO "user"

How to upsert in Postgres on conflict on one of 2 columns?

限于喜欢 提交于 2019-12-01 03:48:09
Is it possible to do upsert in Postgres 9.5 when conflict happens on one of 2 columns in a table.? Basically I have 2 columns and if either column throws unique constraint violation, then I would like to perform update operation. Yes, and this behaviour is default. Any unique constraint violation constitutes a conflict and then the UPDATE is performed if ON CONFLICT DO UPDATE is specified. The INSERT statement can have only a single ON CONFLICT clause, but the conflict_target of that clause can specify multiple column names each of which must have an index, such as a UNIQUE constraint. You are

How to upsert in Postgres on conflict on one of 2 columns?

可紊 提交于 2019-12-01 00:18:42
问题 Is it possible to do upsert in Postgres 9.5 when conflict happens on one of 2 columns in a table.? Basically I have 2 columns and if either column throws unique constraint violation, then I would like to perform update operation. 回答1: Yes, and this behaviour is default. Any unique constraint violation constitutes a conflict and then the UPDATE is performed if ON CONFLICT DO UPDATE is specified. The INSERT statement can have only a single ON CONFLICT clause, but the conflict_target of that