PostgreSL json_populate_レコードとjson_populate_recordset関数


公式の例はこうです
select * from json_populate_record(null::myrowtype, '{"a": 1, "b": ["2", "a b"], "c": {"d": 4, "e": "a b c"}}');
select * from json_populate_recordset(null::myrowtype, '[{"a":1,"b":2},{"a":3,"b":4}]');

null::myrowtypeって何の鬼??ネット上の多くのチュートリアルでは、test、テーブル構造、json構造などのテーブルを作成します.以下のようにします.
select * from json_populate_record(null::test, '[{"a":1,"b":2},{"a":3,"b":4}]');
select * from json_populate_recordset(null::test, '[{"a":1,"b":2},{"a":3,"b":4}]');

テーブルの作成が面倒で管理が不便である.実際にはカスタムタイプで代用できます
drop type if exists json_test_columns;
--  json         
create type json_test_columns as (a int,b int,c int,d int);
--         
select * from json_populate_record(null::json_test_columns,'{"a":1,"b":2,"c":3,"d":4}');
select * from json_populate_recordset(null::json_test_columns,'[{"a":1,"b":2,"c":3,"d":4},{"a":2,"b":3,"c":4,"d":5}]');
--       ,      ,         ,
-- select * from json_populate_recordset(null::json_test_columns,'[[1,2,3,4],[2,3,4,5]]');
--    
select value->>0 as a,value->>1 as b,value->>2 as c,value->>3 as c from (select * from jsonb_array_elements('[[1,2,3,4],[2,3,4,5]]')) as tmp;