Re: Weird issue with truncation of values in array with some tables

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mike Martin <redtux1(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: Weird issue with truncation of values in array with some tables
Date: 2020-08-16 17:12:50
Message-ID: 989596.1597597970@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Mike Martin <redtux1(at)gmail(dot)com> writes:
> I have come across a weird issue with truncation of text in an array (in
> this case using pg_indexes view)
> This query truncates the second array element at 63 characters
> SELECT ARRAY[indexname,indexdef] FROM pg_indexes
> However reversing the order doesn't truncate
> SELECT ARRAY[indexdef,indexname] FROM pg_indexes
> Anyone know why this behaviour occurs?

indexname is of type name, indexdef is of type text, and the rules
for inferring the type of an array[] construct are such that the
first element's type wins in these cases.

regression=# SELECT pg_typeof(ARRAY[indexname,indexdef]) FROM pg_indexes limit 1;
pg_typeof
-----------
name[]
(1 row)

regression=# SELECT pg_typeof(ARRAY[indexdef,indexname]) FROM pg_indexes limit 1;
pg_typeof
-----------
text[]
(1 row)

You could insert an explicit cast to text to avoid the truncation
of indexdef to name:

regression=# SELECT pg_typeof(ARRAY[indexname::text,indexdef]) FROM pg_indexes limit 1;
pg_typeof
-----------
text[]
(1 row)

The documentation about that is here:
https://www.postgresql.org/docs/current/typeconv-union-case.html
although looking at this example it seems like that description isn't
telling the full truth.

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Mike Martin 2020-08-16 21:19:50 Re: Weird issue with truncation of values in array with some tables
Previous Message David G. Johnston 2020-08-16 15:13:20 Re: Weird issue with truncation of values in array with some tables