PostgreSQL – How to Get Row Type of a Table as Text

postgresql

I want to do this:

json_populate_record(null::schematable, json)

But schematable is a text variable contaning schema.table

I have tried this:

json_populate_record(pg_typeof(schematable), json)

but pg_typeof() returns regtype not rowtype.

How do I get a rowtype of a dynamic text argument?

Best Answer

There is no way to do this, since a type name is always an identifier, and only constants (literals) can be parameters.

The only resort is to use dynamic SQL like

EXECUTE format('json_populate_record(null::%I, $1)', 'schematable') USING json;