postgresql-9.5

Updating integer column from jsonb member fails with: column is of type integer but expression is of type jsonb

泪湿孤枕 提交于 2020-01-13 22:52:38
问题 In a PostgreSQL 9.5 table I have an integer column social . When I try to update it in a stored procedure given the following JSON data (an array with 2 objects, each having a "social" key) in the in_users variable of type jsonb : '[{"sid":"12345284239407942","auth":"ddddc1808197a1161bc22dc307accccc",**"social":3**,"given":"Alexander1","family":"Farber","photo":"https:\/\/graph.facebook.com\/1015428423940942\/picture?type=large","place":"Bochum, Germany","female":0,"stamp":1450102770}, {"sid"

Updating integer column from jsonb member fails with: column is of type integer but expression is of type jsonb

最后都变了- 提交于 2020-01-13 22:52:21
问题 In a PostgreSQL 9.5 table I have an integer column social . When I try to update it in a stored procedure given the following JSON data (an array with 2 objects, each having a "social" key) in the in_users variable of type jsonb : '[{"sid":"12345284239407942","auth":"ddddc1808197a1161bc22dc307accccc",**"social":3**,"given":"Alexander1","family":"Farber","photo":"https:\/\/graph.facebook.com\/1015428423940942\/picture?type=large","place":"Bochum, Germany","female":0,"stamp":1450102770}, {"sid"

How to match not null + not empty?

霸气de小男生 提交于 2020-01-12 07:12:11
问题 I have to do some queries on a messy database. Some columns are filled with either null or empty string. I can do query like this: select * from a where b is not null and b <> ''; But is there a shortcut for this case? (match every "not empty" values) Something like: select * from a where b is filled; 回答1: Just: where b <> '' will do what you want as null <> '' is null and the row will not be returned 回答2: select * from a where COALESCE(b, '') <> ''; 来源: https://stackoverflow.com/questions

How to push a JSON object to a nested array in a JSONB column

假如想象 提交于 2020-01-01 10:28:09
问题 I need to somehow push a JSON object to a nested array of potentionally existing JSON objects - see "pages" in the below JSON snippet. { "session_id": "someuuid", "visitor_ui": 1, "pages": [ { "datetime": "2016-08-13T19:45:40.259Z", "duration,": 0, "device_id": 1, "url": { "path": "/" } }, { "datetime": "2016-08-14T19:45:40.259Z", "duration,": 0, "device_id": 1, "url": { "path": "/test" } }, // how can i push a new value (page) here?? ] "visit_page_count": 2 } I'm aware of the jsonb_set

How to push a JSON object to a nested array in a JSONB column

妖精的绣舞 提交于 2020-01-01 10:28:07
问题 I need to somehow push a JSON object to a nested array of potentionally existing JSON objects - see "pages" in the below JSON snippet. { "session_id": "someuuid", "visitor_ui": 1, "pages": [ { "datetime": "2016-08-13T19:45:40.259Z", "duration,": 0, "device_id": 1, "url": { "path": "/" } }, { "datetime": "2016-08-14T19:45:40.259Z", "duration,": 0, "device_id": 1, "url": { "path": "/test" } }, // how can i push a new value (page) here?? ] "visit_page_count": 2 } I'm aware of the jsonb_set

Return rows from INSERT with ON CONFLICT without needing to update

半世苍凉 提交于 2019-12-28 13:47:10
问题 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

restrict the number of connections in postgres using an ip address

旧城冷巷雨未停 提交于 2019-12-25 18:21:33
问题 Is it possible to restrict the number of connections in postgres, using the ip address of machine, since i'm not able to find which ip address is tagged to which user / role. 回答1: implicitly you can do it with pgbouncer - at least 1.7 version supports hba file, so you can bond db+user+ip and set limit for db+user in ini file. This way limiting connections to IP or network. explicitly you can try using HAProxy's or just IPTABLES (I think prefered way) lastly you can write some monkey job that

WHERE … IN condition and multiple columns in subquery

喜你入骨 提交于 2019-12-25 07:59:23
问题 Is it please possible to rewrite the SQL query SELECT DISTINCT ON (uid) uid, female, given, photo, place FROM words_social WHERE uid IN (SELECT player1 FROM games) OR uid IN (SELECT player2 FROM games) ORDER BY uid, stamp DESC where first column player1 is fetched in a subquery and then column player2 is fetched from the same table? I've searched around and it seems that a JOIN should be used here instead of the 2 subqueries, but can not figure out exactly how. Just to give more context -

JSONB column: sort only content of arrays stored in column with mixed JSONB content

我的梦境 提交于 2019-12-24 14:35:55
问题 I have a table with JSONB column storing JSONB arrays/strings ( value_r column in the below example). What would be the simplest (and efficent) way to sort only content of JSONB arrays within JSONB column (storing also strings)? I've been looking for the simplest method (as query, or procedure is needed?) because I have to apply this in more complicated SQL code. Here is the test code: CREATE TABLE test_table ( id integer, ordinality bigint, key_r text, value_r jsonb ); INSERT INTO test_table

How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?

房东的猫 提交于 2019-12-23 17:25:58
问题 In postgresql 9.5 how can the INSERT ... ON CONFLICT (id) DO UPDATE... syntax be used with a sequence ID? In a table tbltest that has the following columns: tbltest_ID tbltest_Name tbltest_Description Where tbltest_ID has a sequence in the db that does auto increment. The following works fine for updates, eg; updating the record with an ID of 4: INSERT INTO tbltest ( tbltest_ID, tbltest_Name, tbltest_Description) VALUES (4, 'test name','test description') ON CONFLICT (tbltest_ID) DO UPDATE