pgsql – return row containing data from multiple rows

Advertisements I have a table formatted like this (obviously, simplified) : session_id timestamp action message 4de88be3-2316-4efa-8e17-58a2365534d9 2:04 4363d58b-c9fe-43a1-b636-c65822329aa3 initial 4de88be3-2316-4efa-8e17-58a2365534d9 2:05 d4294aaf-3fee-4154-a3de-b2f9c05a0cf1 queued 4de88be3-2316-4efa-8e17-58a2365534d9 2:10 dc40eaec-2aed-4b24-9b8e-ff1e25194036 connected 4de88be3-2316-4efa-8e17-58a2365534d9 2:32 dd93f0d4-7db9-4a68-876b-956db9300841 hangup noting that multiple sessions could be happening at the same time of course, I’m trying to figure out how to write a query to… Read More pgsql – return row containing data from multiple rows

Output PGSQL commands from inside of an anonymous block to a SQL file

Advertisements I have tried to output some pgsql commands – specifically grants – to an SQL file. My code looks like this: DO $$ DECLARE rec record; BEGIN FOR rec in SELECT pu.usename, pr.rolname, tp.privilege_type, tp.table_name FROM pg_user pu JOIN pg_auth_members ON pu.usesysid = pg_auth_members.member JOIN pg_roles pr ON pr.oid = pg_auth_members.roleid LEFT JOIN information_schema.table_privileges… Read More Output PGSQL commands from inside of an anonymous block to a SQL file

PLPGSQL: Query JSONB Array size

Advertisements How do I query array size of a JSONB object? DO $$ DECLARE p_obj JSONB; array_size INT; BEGIN p_obj = ‘[{"a":"foo"},{"b":"bar"},{"c":"baz"}]’::jsonb; — This prints out NOTICE: [{"a": "foo"}, {"b": "bar"}, {"c": "baz"}] array_size = json_array_length(p_obj); RAISE NOTICE ‘%’, p_obj; RAISE NOTICE ‘%’, array_size; END; $$ LANGUAGE PLPGSQL; This results in: ERROR: function json_array_length(jsonb) does… Read More PLPGSQL: Query JSONB Array size