ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL DBA(67) - PG 12 SQLJSON path

PostgreSQL DBA(67) - PG 12 SQLJSON path

翻译 PostgreSQL 作者:husthxd 时间:2019-08-01 14:33:48 0 删除 编辑

PostgreSQL 12提供了SQL/JSON函数用以兼容SQL 2016 SQL/JSON特性.
这些函数包括:


[local]:5432 pg12@testdb=# \df jsonb_path*
                                                                     List of functions
   Schema   |          Name          | Result data type |                                    Argument data types                                    | Type 
------------+------------------------+------------------+-------------------------------------------------------------------------------------------+------
 pg_catalog | jsonb_path_exists      | boolean          | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func
 pg_catalog | jsonb_path_exists_opr  | boolean          | jsonb, jsonpath                                                                           | func
 pg_catalog | jsonb_path_match       | boolean          | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func
 pg_catalog | jsonb_path_match_opr   | boolean          | jsonb, jsonpath                                                                           | func
 pg_catalog | jsonb_path_query       | SETOF jsonb      | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func
 pg_catalog | jsonb_path_query_array | jsonb            | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func
 pg_catalog | jsonb_path_query_first | jsonb            | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func
(7 rows)

简单试用:


[local]:5432 pg12@testdb=# CREATE TABLE characters (data jsonb);
         "weight" : 0.1
       },
       {"name" : "ring of strength",
        "weight" : 2.4
       }
     ],
     "arm_right" : "Sword of flame",
     "arm_left" : "Shield of faith"
   }
}');
CREATE TABLE
Time: 208.690 ms
[local]:5432 pg12@testdb=# INSERT INTO characters VALUES ('
pg12@testdb'# { "name" : "Yksdargortso",
pg12@testdb'#   "id" : 1,
pg12@testdb'#   "sex" : "male",
pg12@testdb'#   "hp" : 300,
pg12@testdb'#   "level" : 10,
pg12@testdb'#   "class" : "warrior",
pg12@testdb'#   "equipment" :
pg12@testdb'#    {
pg12@testdb'#      "rings" : [
pg12@testdb'#        { "name" : "ring of despair",
pg12@testdb'#          "weight" : 0.1
pg12@testdb'#        },
pg12@testdb'#        {"name" : "ring of strength",
pg12@testdb'#         "weight" : 2.4
pg12@testdb'#        }
pg12@testdb'#      ],
pg12@testdb'#      "arm_right" : "Sword of flame",
pg12@testdb'#      "arm_left" : "Shield of faith"
pg12@testdb'#    }
pg12@testdb'# }');
INSERT 0 1
Time: 3.881 ms
[local]:5432 pg12@testdb=# 
[local]:5432 pg12@testdb=# 
[local]:5432 pg12@testdb=# SELECT jsonb_path_query(data, '$.equipment.rings[0].name') AS ring_name FROM characters;
     ring_name     
-------------------
 "ring of despair"
(1 row)
Time: 10.081 ms
[local]:5432 pg12@testdb=# SELECT jsonb_path_query(data, '$.equipment.rings[0].*') AS data FROM characters;
       data        
-------------------
 "ring of despair"
 0.1
(2 rows)
Time: 0.687 ms
[local]:5432 pg12@testdb=# SELECT jsonb_path_query(data, '$.equipment.rings[*].weight.floor()') AS weight FROM characters;
 weight 
--------
 0
 2
(2 rows)

如果是PG 11或以下版本,则需要使用#>>等操作符实现


testdb=# select data#>>'{equipment,rings,0,name}' AS ring_name FROM characters;
    ring_name    
-----------------
 ring of despair
(1 row)

参考资料
Postgres 12 highlight - SQL/JSON path

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2652466/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 广义迷信
请登录后发表评论 登录
全部评论
ITPUB数据库版块资深版主,对Oracle、PostgreSQL有深入研究。现就职于广州云图数据技术有限公司,系统架构师。

注册时间:2007-12-28

  • 博文量
    1338
  • 访问量
    3804683