Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f27976c

Browse files
committed
Make length() disregard trailing spaces in char(n) values, per discussion
some time ago and recent patch from Gavin Sherry. Update documentation to point out that trailing spaces are insignificant in char(n).
1 parent 08b0e60 commit f27976c

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

doc/src/sgml/datatype.sgml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.140 2004/01/20 22:46:06 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.141 2004/02/01 06:27:48 tgl Exp $
33
-->
44

55
<chapter id="datatype">
@@ -901,6 +901,18 @@ CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
901901
management systems have it as well.
902902
</para>
903903

904+
<para>
905+
Values of type <type>character</type> are physically padded
906+
with spaces to the specified width <replaceable>n</>, and are
907+
stored and displayed that way. However, the padding spaces are
908+
treated as semantically insignificant. Trailing spaces are
909+
disregarded when comparing two values of type <type>character</type>,
910+
and they will be removed when converting a <type>character</type> value
911+
to one of the other string types. Note that trailing spaces
912+
<emphasis>are</> semantically significant in
913+
<type>character varying</type> and <type>text</type> values.
914+
</para>
915+
904916
<para>
905917
The storage requirement for data of these types is 4 bytes plus the
906918
actual string, and in case of <type>character</type> plus the
@@ -922,7 +934,11 @@ CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
922934
<para>
923935
There are no performance differences between these three types,
924936
apart from the increased storage size when using the blank-padded
925-
type.
937+
type. While <type>character(<replaceable>n</>)</type> has performance
938+
advantages in some other database systems, it has no such advantages in
939+
<productname>PostgreSQL</productname>. In most situations
940+
<type>text</type> or <type>character varying</type> should be used
941+
instead.
926942
</para>
927943
</tip>
928944

src/backend/utils/adt/varchar.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.103 2003/11/29 19:51:59 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.104 2004/02/01 06:27:48 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -510,14 +510,16 @@ Datum
510510
bpcharlen(PG_FUNCTION_ARGS)
511511
{
512512
BpChar *arg = PG_GETARG_BPCHAR_P(0);
513+
int len;
513514

514-
/* optimization for single byte encoding */
515-
if (pg_database_encoding_max_length() <= 1)
516-
PG_RETURN_INT32(VARSIZE(arg) - VARHDRSZ);
515+
/* get number of bytes, ignoring trailing spaces */
516+
len = bcTruelen(arg);
517+
518+
/* in multibyte encoding, convert to number of characters */
519+
if (pg_database_encoding_max_length() != 1)
520+
len = pg_mbstrlen_with_len(VARDATA(arg), len);
517521

518-
PG_RETURN_INT32(
519-
pg_mbstrlen_with_len(VARDATA(arg), VARSIZE(arg) - VARHDRSZ)
520-
);
522+
PG_RETURN_INT32(len);
521523
}
522524

523525
Datum

0 commit comments

Comments
 (0)