pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: nicolas.gouteux@sonarsource.com
Subject: BUG #18051: char(N) and varchar(N) behave opposite to the documentation
Date: Wed, 09 Aug 2023 08:55:53 +0000
Message-ID: <18051-3d2890ce9127a33a@postgresql.org> (raw)

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



view thread (11+ messages)  latest in thread

Message-ID: <18051-3d2890ce9127a33a@postgresql.org>
Permalink:  ../18051-3d2890ce9127a33a@postgresql.org/
Also on:    postgresql.org/message-id/18051-3d2890ce9127a33a@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, nicolas.gouteux@sonarsource.com
  Subject: Re: BUG #18051: char(N) and varchar(N) behave opposite to the documentation
  In-Reply-To: <18051-3d2890ce9127a33a@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox