发布网友 发布时间:2022-04-09 02:46
共3个回答
懂视网 时间:2022-04-09 07:08
From this article, I tried to update or delete property of a JSONB
column:
CREATE TABLE xxx (id BIGSERIAL, data JSONB);
INSERT INTO xxx(data) VALUES( ‘{"a":1,"b":2}‘ );
SELECT * FROM data;
id | data
----+------------------
1 | {"a": 1, "b": 2}
create the update function:
CREATE FUNCTION jsonb_merge(JSONB, JSONB)
RETURNS JSONB AS $$
WITH json_union AS (
SELECT * FROM JSONB_EACH($1)
UNION ALL
SELECT * FROM JSONB_EACH($2)
) SELECT JSON_OBJECT_AGG(key, value)::JSONB FROM json_union;
$$ LANGUAGE SQL;
testing:
-- replace
UPDATE xxx SET data = jsonb_merge(data,‘{"b":3}‘) WHERE id = 1;
SELECT * FROM xxx;
id | data
----+------------------
1 | {"a": 1, "b": 3}
-- append
UPDATE xxx SET data = jsonb_merge(data,‘{"c":4}‘) WHERE id = 1;
SELECT * FROM xxx;
id | data
----+-------------------------
1 | {"a": 1, "b": 3, "c": 4}
The question is:
is there any drawback of using JSONB_EACH
(jsonb_merge) instead of JSONB_EACH_TEXT
(from the article) in this case?
how to modify the jsonb_merge
so if the second parameter property value is null
(something like {"b":null}
) the value would be erased?
.
-- remove
UPDATE xxx SET data = jsonb_merge(data,‘{"b":null}‘) WHERE id = 1;
SELECT * FROM xxx;
id | data
----+-----------------
1 | {"a": 1, "c": 4}
postgresql postgresql-9.4
share|improve this question | edited Mar 20 ‘15 at 10:23 | asked Mar 20 ‘15 at 6:06 Kokizzu 273312 |
up vote 3 down vote accepted |
Question 1
|
postgresql删除属性
标签:
热心网友 时间:2022-04-09 04:16
在postgres中当我们执行:drop/alter database XXX的时候的时候,可能会提示:
ERROR: database "testdb" is being accessed by other users
DETAIL: There are 3 other sessions using the database.
这个意思是说,删除数据库失败,因为这里还有3个链接连接到该数据库上,PostgreSQL在有进程连接到数据库时,对应的数据库是不运行被删除的。那么怎么办呢?答案就是:断开连接到这个数据库上的所有链接,再删除数据库。怎么断开呢?在PostgreSQL 9.2 及以上版本,执行下面的语句:
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname='testdb' AND pid<>pg_backend_pid();
执行上面的语句之后,在执行DROP操作,就可以删除数据库了。
pg_terminate_backend:用来终止与数据库的连接的进程id的函数。
pg_stat_activity:是一个系统表,用于存储服务进程的属性和状态。
pg_backend_pid():是一个系统函数,获取附加到当前会话的服务器进程的ID。
热心网友 时间:2022-04-09 05:34
你需要找到postgresql的swift库,就可以实现了