Stepochkin
Stepochkin

Reputation: 313

How to show postgresql expression type?

I would like to show any expression type name. Something like 'type' function in python.

To work somewhat like these:

select type(1);
'int'

select type(ROW(1, 'abc'));
'row(int, text)'

select type(select * from t1);
'setof t1'

Is there anything like this in postgresql?

Upvotes: 19

Views: 2927

Answers (1)

user330315
user330315

Reputation:

It's called pg_typeof() although it's not exactly what you want

select pg_typeof(1), ROW(1, 'abc');

Returns

pg_typeof | row    
----------+--------
integer   | (1,abc)

You can't however use pg_typeof(select * from t1), not even with a limit 1 because the function requires a single expression as its input, not multiple columns. You could however do something like: pg_typeof((select some_column from t1))

Upvotes: 22

Related Questions