The following bug has been logged on the website:
Bug reference: 8513
Logged by: Tommi Korhonen
Email address: tommi.korhonen@thl.fi
PostgreSQL version: 8.4.17
Operating system: Redhat Linux
Description:
Example:
create table t1(a integer);
create table t2(b integer);
create table t3(c integer);
select null from t1
union all
select null from t2
union all
select c from t3;
produces an error
ERROR: UNION types text and integer cannot be matched
LINE 5: select c from t3;
but
select null from t2
union all
select c from t3;
works correctly.
So it seems that if there are more than one nulls before field c in union
PostreSQL thinks merged field's data type is text.