BUG #18051: char(N) and varchar(N) behave opposite to the documentation - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #18051: char(N) and varchar(N) behave opposite to the documentation
Date
Msg-id 18051-3d2890ce9127a33a@postgresql.org
Whole thread Raw
Responses Re: BUG #18051: char(N) and varchar(N) behave opposite to the documentation
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      18051
Logged by:          Nicolas Gouteux
Email address:      nicolas.gouteux@sonarsource.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


pgsql-bugs by date:

Previous
From: "Euler Taveira"
Date:
Subject: Re: pg_restore 14 skips ACL COLUMN when --schema is used
Next
From: "David G. Johnston"
Date:
Subject: Re: BUG #18051: char(N) and varchar(N) behave opposite to the documentation