postgresql-9.4

How to update a jsonb column's field in PostgreSQL?

ε祈祈猫儿з 提交于 2019-12-10 14:05:21
问题 So I wanted to try jsonb of PostgreSQL. In my table, I have a column called extras of jsonb type. Sample data in extras looks like {"param1": 10, "param2": 15} I would like to modify the JSON using sql statements only. I want to do something like this: Update param1 of extras field by adding 10 to its value if param2 of extras exceeds 12. How can I write a SQL statement like this? I know I can easily do this in the application layer but I would like to do this in the SQL layer itself as the

Postgres GROUP BY on jsonb inner field

不羁的心 提交于 2019-12-10 02:12:05
问题 I am using Postgresql 9.4 and have a table test , with id::int and content::jsonb , as follows: id | content ----+----------------- 1 | {"a": {"b": 1}} 2 | {"a": {"b": 1}} 3 | {"a": {"b": 2}} 4 | {"a": {"c": 1}} How do I GROUP BY on an inner field in the content column and return each group as an array? Specifically, the results I am looking for are: content --------------------------------- [{"a": {"b": 1}},{"a": {"b": 1}}] [{"a": {"b": 2}}] (2 rows) Trying: SELECT json_agg(content) as

Query and order by number of matches in JSON array

感情迁移 提交于 2019-12-10 01:23:34
问题 Using JSON arrays in a jsonb column in Postgres 9.4 and Rails, I can set up a scope that returns all rows containing any elements from an array passed to the scope method - like so: scope :tagged, ->(tags) { where(["data->'tags' ?| ARRAY[:tags]", { tags: tags }]) } I'd also like to order the results based on the number of matched elements in the array. I appreciate I might need to step outside the confines of ActiveRecord to do this, so a vanilla Postgres SQL answer is helpful too, but bonus

execute external program with trigger in postgres 9.4

流过昼夜 提交于 2019-12-09 07:48:28
I am searching for a way to execute a system command after update or insert with a trigger. I hope that postgres can do this. Is it possible? CREATE TRIGGER check_update AFTER UPDATE ON allowed_member_type FOR EACH ROW EXECUTE PROCEDURE check_account_update(); thanks in advance Disclamer: I work with Andreas Fritsch on the same project. We have solved this problem in the following way. There is an "Language"-extension PL/sh Procedural Language Handler for PostgreSQL coded by Peter Eisentraut which does exactly what we need. You define a shell-script like this: CREATE or REPLACE FUNCTION test

Inserting DEFAULT value into a column when a parameter is NULL

拥有回忆 提交于 2019-12-08 19:46:26
问题 I would like to write a stored procedure like this: CREATE OR REPLACE FUNCTION my_function(param_1 text, param_2 text DEFAULT NULL::text) RETURNS bigint AS $$ DECLARE ret bigint; BEGIN INSERT INTO my_table(val_1, val_2) VALUES (param_1, param_2); -- do some more stuff RETURN ret; END; $$ LANGUAGE plpgsql; However, I would like to use val_2 column's DEFAULT value instead of NULL - if NULL is provided as the param_2 value. Something like this: INSERT INTO my_table(val_1, val_2) VALUES (param_1,

Indexing jsonb for numeric comparison of fields

余生长醉 提交于 2019-12-08 08:56:04
问题 I've defined a simple table with create table resources (id serial primary key, fields jsonb); And it contains data with keys (drawn from a large set) and values between 1 and 100, like: id | fields --------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1 | {"tex": 23, "blair": 46, "cubic": 50, "raider": 57, "retard": 53, "hoariest": 78,

execute external program with trigger in postgres 9.4

巧了我就是萌 提交于 2019-12-08 08:01:52
问题 I am searching for a way to execute a system command after update or insert with a trigger. I hope that postgres can do this. Is it possible? CREATE TRIGGER check_update AFTER UPDATE ON allowed_member_type FOR EACH ROW EXECUTE PROCEDURE check_account_update(); thanks in advance 回答1: Disclamer: I work with Andreas Fritsch on the same project. We have solved this problem in the following way. There is an "Language"-extension PL/sh Procedural Language Handler for PostgreSQL coded by Peter

Duplicate (repeat) rows in sql query result

删除回忆录丶 提交于 2019-12-08 00:47:59
问题 Let's say I have a table with two rows id | value | ----+-------+ 1 | 2 | 2 | 3 | I want to write a query that will duplicate (repeat) each row based on the value. I want this result (5 rows total): id | value | ----+-------+ 1 | 2 | 1 | 2 | 2 | 3 | 2 | 3 | 2 | 3 | I'm using PostgreSQL 9.4. 回答1: You can use generate_series() : select t.id, t.value from (select t.id, t.value, generate_series(1, t.value) from t ) t; You can do the same thing with a lateral join: select t.id, t.value from t,

What lock, if any, does 'CREATE TRIGGER' use in PostgreSQL 9.4.2

最后都变了- 提交于 2019-12-07 23:59:35
问题 according to postgres-xl, CREATE TRIGGER uses the SHARE ROW EXCLUSIVE lock, but according to the official Postgres docs for SHARE ROW EXCLUSIVE : This lock mode is not automatically acquired by any PostgreSQL command. 回答1: You're comparing Postgres-XL with the main PostgreSQL documentation. Two different products, albeit with a shared history. Postgres-XL has lots of changes from stock PostgreSQL. CREATE TRIGGER should be listed in the Pg docs and isn't, though, and that's an oversight. A

How do I get the type of an array's elements?

谁说我不能喝 提交于 2019-12-07 07:14:45
问题 I'm writing a polymorphic PL/pgSQL function that iterates over an array. I am interested in using FOREACH , however I cannot figure out how to declare a temporary variable with the right type. My function is below, for more information see the comment on line 4. CREATE OR REPLACE FUNCTION uniq(ary anyarray) RETURNS anyarray AS $$ DECLARE ret ary%TYPE := '{}'; v ???; -- how do I get the element type of @ary@? BEGIN IF ary IS NULL THEN return NULL; END IF; FOREACH v IN ARRAY ary LOOP IF NOT v =