postgresql-9.3

PostgreSQL Nested JSON Querying

浪子不回头ぞ 提交于 2019-11-28 22:25:18
On PostgreSQL 9.3.4, I have a JSON type column called "person" and the data stored in it is in the format {dogs: [{breed: <>, name: <>}, {breed: <>, name: <>}]} . I want to retrieve the breed of dog at index 0. Here are the two queries I ran: Doesn't work db=> select person->'dogs'->>0->'breed' from people where id = 77; ERROR: operator does not exist: text -> unknown LINE 1: select person->'dogs'->>0->'bree... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. Works select (person->'dogs'->>0)::json->'breed' from es_config_app

Check if a Postgres JSON array contains a string

不羁岁月 提交于 2019-11-28 16:02:47
I have a table to store information about my rabbits. It looks like this: create table rabbits (rabbit_id bigserial primary key, info json not null); insert into rabbits (info) values ('{"name":"Henry", "food":["lettuce","carrots"]}'), ('{"name":"Herald","food":["carrots","zucchini"]}'), ('{"name":"Helen", "food":["lettuce","cheese"]}'); How should I find the rabbits who like carrots? I came up with this: select info->>'name' from rabbits where exists ( select 1 from json_array_elements(info->'food') as food where food::text = '"carrots"' ); I don't like that query. It's a mess. As a full-time

postgresql list and order tables by size

谁说胖子不能爱 提交于 2019-11-28 16:02:45
Is there an easy way to list all tables from PostgreSQL database and order them by size? Pseudo-code SELECT * FROM tables ORDER by tables.size I am using PostgreSQL 9.3.2 . select table_name, pg_relation_size(quote_ident(table_name)) from information_schema.tables where table_schema = 'public' order by 2 This shows you the size of all tables in the schema public if you have multiple schemas, you might want to use: select table_schema, table_name, pg_relation_size('"'||table_schema||'"."'||table_name||'"') from information_schema.tables order by 3 SQLFiddle example: http://sqlfiddle.com/#!15

Hidden Features of PostgreSQL [closed]

梦想的初衷 提交于 2019-11-28 13:09:12
问题 I'm surprised this hasn't been posted yet. Any interesting tricks that you know about in Postgres? Obscure config options and scaling/perf tricks are particularly welcome. I'm sure we can beat the 9 comments on the corresponding MySQL thread :) 回答1: Since postgres is a lot more sane than MySQL, there are not that many "tricks" to report on ;-) The manual has some nice performance tips. A few other performance related things to keep in mind: Make sure autovacuum is turned on Make sure you've

How to count days except Sundays between two dates in Postgres?

空扰寡人 提交于 2019-11-28 12:56:35
To find the number of days between two dates we can use something like this: SELECT date_part('day',age('2017-01-31','2017-01-01')) as total_days; In the above query we got 30 as output instead of 31. Why is that? And I also want to find the number of days except Sundays. Expected output for the interval ('2017-01-01', '2017-01-31') : Total Days = 31 Total Days except Sundays = 26 You need to define "between two dates" more closely. Lower and upper bound included or excluded? A common definition would be to include the lower and exclude the upper bound of an interval. Plus, define the result

Find most common elements in array with a group by

情到浓时终转凉″ 提交于 2019-11-28 12:47:06
I have a table of rows with the following structure name TEXT, favorite_colors TEXT[], group_name INTEGER where each row has a list of everyone's favorite colors and the group that person belongs to. How can I GROUP BY group_name and return a list of the most common colors in each group? Could you do a combination of int[] && int[] to set for overlap, int[] & int[] to get the intersection and then something else to count and rank? Quick and dirty: SELECT group_name, color, count(*) AS ct FROM ( SELECT group_name, unnest(favorite_colors) AS color FROM tbl ) sub GROUP BY 1,2 ORDER BY 1,3 DESC;

sqlalchemy dynamic schema on entity at runtime

泪湿孤枕 提交于 2019-11-28 07:49:54
I'm using SQL Alchemy and have some schema's that are account specific. The name of the schema is derived using the account ID, so I don't have the name of the schema until I hit my application service or repository layer. I'm wondering if it's possible to run a query against an entity that has it's schema dynamically set at runtime? I know I need to set the __table_args__['schema'] and have tried doing that using the type() built-in, but I always get the following error: could not assemble any primary key columns for mapped table I'm ready to give up and just write straight sql, but I really

Equivalent to exclusion constraint composed of integer and range

风格不统一 提交于 2019-11-28 06:04:00
问题 I need to have something equivalent to this exclusion constraint drop table if exists t; create table t ( i int, tsr tstzrange, exclude using gist (i with =, tsr with &&) ); ERROR: data type integer has no default operator class for access method "gist" HINT: You must specify an operator class for the index or define a default operator class for the data type. I guess the problem is obvious from the error message. How to do it? 回答1: You need to install the additional module btree_gist to make

How to insert a updatable record with JSON column in PostgreSQL using JOOQ?

给你一囗甜甜゛ 提交于 2019-11-28 03:15:39
问题 I followed the answer in Is it possible to write a data type Converter to handle postgres JSON columns? to implement the nodeObject converter. Then I tried to use an updatable record to insert a record, I got "org.jooq.exception.SQLDialectNotSupportedException: Type class org.postgresql.util.PGobject is not supported in dialect POSTGRES" exception." How can I solve this? Following is my code: TableRecord r = create.newRecord(TABLE); ObjectNode node = JsonNodeFactory.instance.objectNode(); r

Lock for SELECT so another process doesn't get old data

做~自己de王妃 提交于 2019-11-28 01:09:43
问题 I have a table that could have two threads reading data from it. If the data is in a certain state (let's say state 1) then the process will do something (not relevant to this question) and then update the state to 2. It seems to me that there could be a case where thread 1 and thread 2 both perform a select within microseconds of one another and both see that the row is in state 1, and then both do the same thing and 2 updates occur after locks have been released. Question is: Is there a way