Short version: I'm spending some time in pg_catalog, and want to display expanded definitions where char codes are used. For example "composite type" instead of "c" for pg_class.relkind. I've tried custom functions, and a lookup soup table. I'm hoping to get some suggestions and, likely, pointers to obvious things I've overlooked.
Postgres 11.5, deployment on RDS (no superuser.)
Longer version: For our project, I'm writing a fair few client-side code generators and reporting screens, which means I need to dig into pg_type, pg_class, pg_attribute and more. For what I assume are historical reasons, a lot of the table and column names in pg_catalog are....opaque. And a good number of fields include character codes that either need to be looked up or memorized. For example, pg_class.relkind holds one of the values I, S, c, f, i, m, p, r, t, or v. Eh? I could memorize what those mean and translate them in my head, but that's something the computer can do more easily. So, I figured I'd write a function:
CREATE OR REPLACE FUNCTION data.relkind_name (relkind text, out relkind_name text)
RETURNS text
AS $$
SELECT CASE
WHEN relkind = 'r' THEN 'table'
WHEN relkind = 'i' THEN 'index'
WHEN relkind = 'S' THEN 'sequence'
WHEN relkind = 't' THEN 'TOAST table'
WHEN relkind = 'v' THEN 'view'
WHEN relkind = 'm' THEN 'materialized view'
WHEN relkind = 'c' THEN 'composite type'
WHEN relkind = 'f' THEN 'foreign table'
WHEN relkind = 'p' THEN 'partitioned table'
WHEN relkind = 'I' THEN 'partitioned index'
ELSE 'Unexpected relkind ' || relkind
END;
$$ LANGUAGE sql;
ALTER FUNCTION data.relkind_name (relkind text, out relkind_name text) OWNER TO user_bender;
That's...fine. It works, but I don't love functions like this for a couple of reasons 1) The data is baked into code, not a data structure. So, you can't reuse/display/validate it in any way. 2)I'll need a custom function for each constant type. Which brings me to the next idea, a lookup soup table.
BEGIN;
DROP TABLE IF EXISTS data.constant CASCADE;
CREATE TABLE IF NOT EXISTS data.constant (
theme text NOT NULL DEFAULT NULL,
code text NOT NULL DEFAULT NULL,
label text NOT NULL DEFAULT NULL,
PRIMARY KEY (theme, code)
);
ALTER TABLE data.constant OWNER TO user_change_structure;
COMMIT;
Before going further, I'll stipulate that grab-all lookup tables are generally worthy of derision. It's not something I would do with dynamic, user-driven data. Because bad. So bad. But in this one, narrow, case, it seems like a solid idea:
The data is baked into Postgres and changes only with major releases, if then.
None of this data will ever go away, or at least not likely.
It's super easy to add a new set of constants, once I run into something of interest.
Here's some setup for a couple of the constants lists in pg_catalog:
INSERT INTO constant
(theme,code,label)
VALUES
('typcategory','A','Array types'),
('typcategory','B','Boolean types'),
('typcategory','C','Composite types'),
('typcategory','D','Date/time types'),
('typcategory','E','Enum types'),
('typcategory','G','Geometric types'),
('typcategory','I','Network address types'),
('typcategory','N','Numeric types'),
('typcategory','P','Pseudo-types'),
('typcategory','R','Range types'),
('typcategory','S','String types'),
('typcategory','T','Timespan types'),
('typcategory','U','User-defined types'),
('typcategory','V','Bit-string types'),
('typcategory','X','unknown type'),
('relkind','r','ordinary table'),
('relkind','i','index'),
('relkind','S','sequence'),
('relkind','t','TOAST table'),
('relkind','v','view'),
('relkind','m','materialized view'),
('relkind','c','composite type'),
('relkind','f','foreign table'),
('relkind','p','partitioned table'),
('relkind','I','partitioned index');
Since the data is in a table, you can do normal things in a normal way. Or even use Postgres' wonderful string_agg function:
select theme,
string_agg(code, ', ' order by code) as constants
from constant
group by theme
order by theme;
relkind I, S, c, f, i, m, p, r, t, v
typcategory A, B, C, D, E, G, I, N, P, R, S, T, U, V, X
Or a simple query to do the lookup:
-- I want a default/error result label if there is no match.
select coalesce((select label from constant where theme = 'relkind' and code = 'X'),
'Undefined')
Which can be wrapped into a function:
DROP FUNCTION IF EXISTS data.lookup (theme text, code text);
CREATE OR REPLACE FUNCTION data.lookup (theme text, code text)
RETURNS TEXT
AS $$
-- I want a default/error result label if there is no match, hence the subquery.
select coalesce(
(select label
from constant
where theme = $1 and
code = $2),
'Undefined')
$$ LANGUAGE sql;
ALTER FUNCTION data.lookup (theme text, code text) OWNER TO user_bender;
And then, finally, a query on a catalog table that gives human-readable results:
select relowner::regrole,
relnamespace::regnamespace,
relname,
lookup('relkind',relkind) as relkind_name,
reltype::regtype
from pg_class
You'll see from the above that I found some of the oid magic casting tools, and some of the system information functions. I'm looking to use the lookup() function to fill in some gaps.
I'd be grateful for commentary or suggestions, even if that amounts to "throw all of that out, there's a better way."
For the record, I checked out custom types, magic::castings, CREATE DOMAIN (doesn't apply), ENUM (doesn't apply and doesn't appeal.) I'm currently ruling out building a bunch of custom views as it feels like that would make reworking my code harder for the next person. (The one lookup function doesn't seem like too much to learn.)
immutable
for performance reasons). Easier to maintain, easier to understand and less code than your other solutions. I don't see why it shouldn't be "reusable"