BUG #18051: char(N) and varchar(N) behave opposite to the documentation

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: nicolas(dot)gouteux(at)sonarsource(dot)com
Subject: BUG #18051: char(N) and varchar(N) behave opposite to the documentation
Date: 2023-08-09 08:55:53
Message-ID: 18051-3d2890ce9127a33a@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 18051
Logged by: Nicolas Gouteux
Email address: nicolas(dot)gouteux(at)sonarsource(dot)com
PostgreSQL version: 13.7
Operating system: Linux
Description:

Hi

Since SQL inception, char(N) dataype is supposed to add padding blanks up to
N characters, whereas varchar(N) is supposed to truncate extra trailing
white spaces.

This is confirmed by the documentation:
If the string to be stored is shorter than the declared length, values of
type character will be space-padded
https://www.postgresql.org/docs/13/datatype-character.html

However, the following snippet exhibits the exact opposite behavior:

create table if not exists ngx_char (
charcol char(10) not null,
varcharcol varchar(10) not null
);
truncate table ngx_char;
insert into ngx_char (charcol, varcharcol) values ('A', 'A');
insert into ngx_char (charcol, varcharcol) values ('A ', 'A ');
select charcol || 'B', varcharcol || 'C' from ngx_char;
select * from ngx_char where charcol = varcharcol;

Output:

?column?,?column?
AB,AC
AB,A C

This is very strange! Is it me?

Thanks

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David G. Johnston 2023-08-09 12:57:36 Re: BUG #18051: char(N) and varchar(N) behave opposite to the documentation
Previous Message Euler Taveira 2023-08-09 01:31:57 Re: pg_restore 14 skips ACL COLUMN when --schema is used