Problem definition:
- I have a Postgres table
A
holding an arbitrary amount of different columns with different types, a single serial PK and millions of rows. Example:
id | val1 | val2 | val3
---+------+-------+-----
1 | 45.2 | 52.6 | 222
2 | 5.32 | 12.34 | 193
- I have another table
B
, mapping tableA
column names to free text string names (unique).
value_name | col_name
-----------+---------
"Value 1" | "val1"
"Value 2" | "val2"
"Value 3" | "val3"
- I want to run select queries on table
A
to retrieve data by free text names (instead of column names). A single query should return the data.
Something like this would be great:
SELECT id
, alias('Value 2', 'A')
, alias('Value 3', 'A')
from A
or
SELECT *
from alias('Value 2', 'Value 3', 'A')
would return
id | val2 | val3
---+-------+-----
1 | 52.6 | 222
2 | 12.34 | 193
(I don't mind returning id
always without explicitly asking for it, but I do need an ability to query the arbitrary amount of columns and return their original column names)
Solution flexibility
- The solution can include Postgres functions, rules, extensions, triggers - anything which does not change the basic structure of table
A
. - The solution can suggest any structure for table
B
. - Postgres version 11
Tnx!!