pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #11875: char() not behaving as documented
2+ messages / 2 participants
[nested] [flat]

* BUG #11875: char() not behaving as documented
@ 2014-11-04 19:55  b.wood@niwa.co.nz
  0 siblings, 1 reply; 2+ messages in thread

From: b.wood@niwa.co.nz @ 2014-11-04 19:55 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      11875
Logged by:          Brent Wood
Email address:      b.wood@niwa.co.nz
PostgreSQL version: 9.2.0
Operating system:   Linux
Description:        

According to the docs
http://www.postgresql.org/docs/9.3/static/datatype-character.html)(:
" If the string to be stored is shorter than the declared length, values of
type character will be space-padded; values of type character varying will
simply store the shorter string."

Yet chars are not being padded, in fact they lose trailing spaces which are
retained by varchars. They also return length()'s less than the defined
length... which should not be the case for a padded string as defined in the
documentation.

fish=# create table test(var3 varchar(3),cha3 char(3));
CREATE TABLE
fish=# insert into test values('1','1');
INSERT 0 1
fish=# insert into test values('2 ','2 '); -- one space
INSERT 0 1
fish=# insert into test values('3  ','3  '); --two spaces
INSERT 0 1
fish=# select var3||':' as var3, cha3||':' as char3 from test;
 var3 | char3
------+-------
 1:   | 1:
 2 :  | 2:
 3  : | 3:
(3 rows)
test=# select length(var3) as v_lgth, length(cha3) as c_length from test;
 v_lgth | c_length
--------+----------
      1 |        1
      2 |        1
      3 |        1

So, in summary, varchar stores whatever feed to it and keeps trailing spaces
to max length, char type will trim off trailing spaces, and store a string
shorter than the specified length.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #11875: char() not behaving as documented
@ 2014-11-04 20:36  Tom Lane <tgl@sss.pgh.pa.us>
  parent: b.wood@niwa.co.nz
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2014-11-04 20:36 UTC (permalink / raw)
  To: b.wood@niwa.co.nz; +Cc: pgsql-bugs

b.wood@niwa.co.nz writes:
> According to the docs
> http://www.postgresql.org/docs/9.3/static/datatype-character.html)(:
> " If the string to be stored is shorter than the declared length, values of
> type character will be space-padded; values of type character varying will
> simply store the shorter string."

Quite.

> Yet chars are not being padded

Yes they are.  You are neglecting the fact that a coercion from char to
varchar/text strips trailing blanks, so that a test like this:

> fish=# select var3||':' as var3, cha3||':' as char3 from test;

proves nothing at all about how many trailing spaces there were in
cha3.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2014-11-04 20:36 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-11-04 19:55 BUG #11875: char() not behaving as documented b.wood@niwa.co.nz
2014-11-04 20:36 ` Tom Lane <tgl@sss.pgh.pa.us>

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