Emery Lapinski
Emery Lapinski

Reputation: 1662

postgres lag has type but row does not. Why?

In the following query, the lag column gets the type of the table (temp_junk) but the row column gets generic type record (although it can be cast to temp_junk). Is this the way it is supposed to work? Or am I misunderstanding something?

drop table if exists temp_junk;
create temp table temp_junk
(
    val integer
);

insert into temp_junk
values (1), (2), (3), (4);

select row(tj.*), row(tj.*)::temp_junk, lag(tj.*) over ()
from temp_junk tj;

EDIT: I guess my question becomes, why is lag() smart enough to return a type but row() isn't?

Upvotes: 1

Views: 65

Answers (1)

Raphaël Althaus
Raphaël Althaus

Reputation: 60503

This is stated in doc

By default, the value created by a ROW expression is of an anonymous record type. If necessary, it can be cast to a named composite type — either the row type of a table, or a composite type created with CREATE TYPE AS. An explicit cast might be needed to avoid ambiguity.

So if it's not casted, you get an anonymous record type => record.

Upvotes: 1

Related Questions