Frank van Vugt <ftm(dot)van(dot)vugt(at)foxi(dot)nl> writes:
> So it seems distinct applied to the second column causes it to lose knowledge
> on its type.
No, because it never had any: NULL is typeless (type UNKNOWN, to the
parser). In the straight INSERT this doesn't matter because we don't
have to resolve the type until we get up to the INSERT, and then we know
we want to insert into the value column. But to do a DISTINCT, the
parser has to assign datatypes to all the columns (to determine the
comparison rules). The default assumption for an UNKNOWN constant is
type TEXT. This is chosen based on the assumption that when someone
writes
select distinct 'foo';
they are probably expecting the system to treat 'foo' as a TEXT literal.
regards, tom lane