Database Administration
postgresql datatypes composite-types
Updated Thu, 15 Sep 2022 06:45:47 GMT

What type of rows does `SELECT my_table FROM my_table` return?


I sometimes encounter this SELECT statement (in JSON-related answers on stackexchanges):

SELECT my_table
FROM my_table;

It seems to turn a one-column result, with multiple rows.

What would be the name of the type of each row returned? A row value of my_table?

(I tried reading the documentation (sql-expressions) but, I could not find a specific answer.)




Solution

It's a named composite type: https://www.postgresql.org/docs/current/static/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS

When you create a table, a TYPE with the same name is created.

A row of that table is of that type.