postgresql-9.4

Create nested json from sql query postgres 9.4

不想你离开。 提交于 2019-12-07 03:13:03
问题 I need to get as a result from query fully structured JSON. I can see in postgres that there are some built in functions that may be useful. As an example I created a structure as follows: -- Table: person -- DROP TABLE person; CREATE TABLE person ( id integer NOT NULL, name character varying(30), CONSTRAINT person_pk PRIMARY KEY (id) ) WITH ( OIDS=FALSE ); ALTER TABLE person OWNER TO postgres; -- Table: car -- DROP TABLE car; CREATE TABLE car ( id integer NOT NULL, type character varying(30)

Indexing jsonb for numeric comparison of fields

拟墨画扇 提交于 2019-12-07 01:52:35
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, "suturing": 25, "apostolic": 22, "unloosing": 37, "flagellated": 85} 2 | {"egoist": 75, "poshest": 0,

Roughly how fast is JSON -> JSONB column conversion in Postgres 9.4

混江龙づ霸主 提交于 2019-12-07 00:24:46
问题 I'm looking to migrate from Postgres 9.3 to 9.4, and have a lot of data in JSON columns. While it's fine, I wanted to have a look at migrating to the more efficient column storage (which JSONB seems to be — a really exciting piece of tech!). To actually migrate, I want to know migration characteristics for something like ALTER TABLE table_with_json ALTER COLUMN my_json SET DATA TYPE jsonb USING my_json::jsonb; (from this helpful question). Ideally, it would be good to know how long it takes

Difference between set, \set and \pset in psql

人盡茶涼 提交于 2019-12-06 19:39:03
问题 I get a little confused some times when working with psql between when to use a set vs. \set vs. \pset . I think that: set is for session variables on my connection to the db. For example SET ROLE dba ; \set is for local variables for this psql session. For example \set time 'select current_timestamp' \pset is for psql settings for this psql session. For example '\pset border 2' But, I've never found what I thought was a good explanation of each. Are my assumptions above correct? I'm using

Join tables using a value inside a JSONB column

心不动则不痛 提交于 2019-12-06 12:04:05
问题 There are two tables: Authorized Contacts ( auth_contacts ): ( userid varchar contacts jsonb ) contacts contains an array of contacts with attributes {contact_id, type} discussion : ( contact_id varchar discussion_id varchar discussion_details jsonb ) The table auth_contacts has at least 100k records making it non JSONB type is not appropriate according as it would double or triple the amount of records. Sample data for auth_contacts : userid | contacts '11111' | '{"contact": [{"type": "type

How do you create a Postgresql JSONB array in array index?

社会主义新天地 提交于 2019-12-06 07:32:32
I have structure like this: user_id, a. a is of type jsonb and has the following structure: { b: [ {ids: [1,2,3,4]}, {ids: [2,3,4]}, {ids: [1,2,4]}, ... ] } How would I make an index that enabled me to find all users (user_id) that has a certain id in the ids list? Is a GIN index what you want? It seems that you first need to organized the IDs into a form that is more tractable. I'm more familiar with Python than I am with the PostgreSQL ways of manipulating JSON, so I used PL/Python for this purpose. DROP TABLE IF EXISTS ids; CREATE TABLE ids (user_id integer, a jsonb); INSERT INTO ids VALUES

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

半腔热情 提交于 2019-12-06 06:22:45
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. 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 quick look at the source code shows that CREATE TRIGGER takes a ShareRowExclusiveLock , so in this case XL's

Duplicate (repeat) rows in sql query result

*爱你&永不变心* 提交于 2019-12-06 06:03:07
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. 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, lateral generate_series(1, t.value); 来源: https://stackoverflow.com/questions/35293084/duplicate-repeat-rows-in

Why can't I query directly on jsonb_array_elements?

孤街醉人 提交于 2019-12-06 01:49:46
问题 I have data stored as jsonb in a column called "data": {'people': [{"name": "Bob", "Occupation": "janitor"}, {"name": "Susan", "Occupation", "CEO"}]} I can query this via: SELECT mydata.pk FROM mydata, jsonb_array_elements(mydata.data->'people') AS a WHERE (a->>'name') = 'bob' Why can't I substitute "a" for the jsonb_array_elements(...)?: SELECT mydata.pk FROM mydata WHERE (jsonb_array_elements(mydata.data->'people')->>'name') = 'bob' Instead, I get the following: ERROR: argument of WHERE

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

﹥>﹥吖頭↗ 提交于 2019-12-05 19:50:50
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 = any(ret) THEN ret = array_append(ret, v); END IF; END LOOP; RETURN ret; END; $$ LANGUAGE plpgsql;