BUG #18962: Type Conversion incorrect when performing UNION of queries. - Mailing list pgsql-bugs

From David G. Johnston
Subject BUG #18962: Type Conversion incorrect when performing UNION of queries.
Date
Msg-id CAKFQuwaGuUtTBOd4BiGYmuj+sq8d7T4_SVoKhDbpBCV7ubNJTg@mail.gmail.com
Whole thread Raw
In response to BUG #18962: Type Conversion incorrect when performing UNION of queries.  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #18962: Type Conversion incorrect when performing UNION of queries.
List pgsql-bugs
On Thursday, June 19, 2025, PG Bug reporting form <noreply@postgresql.org> wrote:
The following bug has been logged on the website:

Bug reference:      18962
Logged by:          Sundar Mudupalli
Email address:      sundar@mudupalli.com
PostgreSQL version: 17.5
Operating system:   Ubuntu I believe (using a Cloud SQL instance in GC
Description:       

Take the following table definitions:
```
create table public.test_table_1 as
SELECT
   'AA'::character(2) AS text_type
UNION ALL
SELECT
   'BB'::character(2) AS text_type
UNION ALL
SELECT
   'CC'::TEXT AS text_type
;
create table public.test_table_2 (
        char_fixed character,
        char_fixed_len character(5),
        char_var character varying(5),
        text_column text );
```
Based on the [type conversion rules for
Union](https://www.postgresql.org/docs/current/typeconv-union-case.html),
the column `text_type` in `test_table_1` should resolve to the postgres data
type `TEXT`.

We typically choose to suggest just avoiding “character” instead of trying to document why the observed behavior is correct…

In short, "character no length restriction" is actually the documented type "bpchar".
"character length restricted" is effectively a domain over "bpchar" - rule 2
"text" implicitly converts to "bpchar" - rule 5 vice-versa exception
Thus the final result is "bpchar".

David J.

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #18962: Type Conversion incorrect when performing UNION of queries.
Next
From: Junwang Zhao
Date:
Subject: Re: BUG #18959: Name collisions of expression indexes during parallel Index creations on a pratitioned table.