~ubuntu-branches/ubuntu/trusty/postgresql-9.3/trusty-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Character Types</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.3.13 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Data Types"
HREF="datatype.html"><LINK
REL="PREVIOUS"
TITLE="Monetary Types"
HREF="datatype-money.html"><LINK
REL="NEXT"
TITLE="Binary Data Types"
HREF="datatype-binary.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2016-05-09T21:13:26"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.3.13 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Monetary Types"
HREF="datatype-money.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="datatype.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 8. Data Types</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Binary Data Types"
HREF="datatype-binary.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="DATATYPE-CHARACTER"
>8.3. Character Types</A
></H1
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-CHARACTER-TABLE"
></A
><P
><B
>Table 8-4. Character Types</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>Name</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="TYPE"
>character varying(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
>, <TT
CLASS="TYPE"
>varchar(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
></TD
><TD
>variable-length with limit</TD
></TR
><TR
><TD
><TT
CLASS="TYPE"
>character(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
>, <TT
CLASS="TYPE"
>char(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
></TD
><TD
>fixed-length, blank padded</TD
></TR
><TR
><TD
><TT
CLASS="TYPE"
>text</TT
></TD
><TD
>variable unlimited length</TD
></TR
></TBODY
></TABLE
></DIV
><P
>    <A
HREF="datatype-character.html#DATATYPE-CHARACTER-TABLE"
>Table 8-4</A
> shows the
    general-purpose character types available in
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.
   </P
><P
>    <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> defines two primary character types:
    <TT
CLASS="TYPE"
>character varying(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
> and
    <TT
CLASS="TYPE"
>character(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
>, where <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>
    is a positive integer.  Both of these types can store strings up to
    <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
> characters (not bytes) in length.  An attempt to store a
    longer string into a column of these types will result in an
    error, unless the excess characters are all spaces, in which case
    the string will be truncated to the maximum length. (This somewhat
    bizarre exception is required by the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>
    standard.) If the string to be stored is shorter than the declared
    length, values of type <TT
CLASS="TYPE"
>character</TT
> will be space-padded;
    values of type <TT
CLASS="TYPE"
>character varying</TT
> will simply store the
    shorter
    string.
   </P
><P
>    If one explicitly casts a value to <TT
CLASS="TYPE"
>character
    varying(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
> or
    <TT
CLASS="TYPE"
>character(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
>, then an over-length
    value will be truncated to <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
> characters without
    raising an error. (This too is required by the
    <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard.)
   </P
><P
>    The notations <TT
CLASS="TYPE"
>varchar(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
> and
    <TT
CLASS="TYPE"
>char(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
> are aliases for <TT
CLASS="TYPE"
>character
    varying(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
> and
    <TT
CLASS="TYPE"
>character(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
>, respectively.
    <TT
CLASS="TYPE"
>character</TT
> without length specifier is equivalent to
    <TT
CLASS="TYPE"
>character(1)</TT
>. If <TT
CLASS="TYPE"
>character varying</TT
> is used
    without length specifier, the type accepts strings of any size. The
    latter is a <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extension.
   </P
><P
>    In addition, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> provides the
    <TT
CLASS="TYPE"
>text</TT
> type, which stores strings of any length.
    Although the type <TT
CLASS="TYPE"
>text</TT
> is not in the
    <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard, several other SQL database
    management systems have it as well.
   </P
><P
>    Values of type <TT
CLASS="TYPE"
>character</TT
> are physically padded
    with spaces to the specified width <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>, and are
    stored and displayed that way.  However, the padding spaces are
    treated as semantically insignificant.  Trailing spaces are
    disregarded when comparing two values of type <TT
CLASS="TYPE"
>character</TT
>,
    and they will be removed when converting a <TT
CLASS="TYPE"
>character</TT
> value
    to one of the other string types.  Note that trailing spaces
    <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>are</I
></SPAN
> semantically significant in
    <TT
CLASS="TYPE"
>character varying</TT
> and <TT
CLASS="TYPE"
>text</TT
> values, and
    when using pattern matching, e.g. <TT
CLASS="LITERAL"
>LIKE</TT
>,
    regular expressions.
   </P
><P
>    The storage requirement for a short string (up to 126 bytes) is 1 byte
    plus the actual string, which includes the space padding in the case of
    <TT
CLASS="TYPE"
>character</TT
>.  Longer strings have 4 bytes of overhead instead
    of 1.  Long strings are compressed by the system automatically, so
    the physical requirement on disk might be less. Very long values are also
    stored in background tables so that they do not interfere with rapid
    access to shorter column values. In any case, the longest
    possible character string that can be stored is about 1 GB. (The
    maximum value that will be allowed for <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
> in the data
    type declaration is less than that. It wouldn't be useful to
    change this because with multibyte character encodings the number of
    characters and bytes can be quite different. If you desire to
    store long strings with no specific upper limit, use
    <TT
CLASS="TYPE"
>text</TT
> or <TT
CLASS="TYPE"
>character varying</TT
> without a length
    specifier, rather than making up an arbitrary length limit.)
   </P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
>     There is no performance difference among these three types,
     apart from increased storage space when using the blank-padded
     type, and a few extra CPU cycles to check the length when storing into
     a length-constrained column.  While
     <TT
CLASS="TYPE"
>character(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
> has performance
     advantages in some other database systems, there is no such advantage in
     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>; in fact
     <TT
CLASS="TYPE"
>character(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
> is usually the slowest of
     the three because of its additional storage costs.  In most situations
     <TT
CLASS="TYPE"
>text</TT
> or <TT
CLASS="TYPE"
>character varying</TT
> should be used
     instead.
    </P
></BLOCKQUOTE
></DIV
><P
>    Refer to <A
HREF="sql-syntax-lexical.html#SQL-SYNTAX-STRINGS"
>Section 4.1.2.1</A
> for information about
    the syntax of string literals, and to <A
HREF="functions.html"
>Chapter 9</A
>
    for information about available operators and functions. The
    database character set determines the character set used to store
    textual values; for more information on character set support,
    refer to <A
HREF="multibyte.html"
>Section 22.3</A
>.
   </P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN5298"
></A
><P
><B
>Example 8-1. Using the Character Types</B
></P
><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE test1 (a character(4));
INSERT INTO test1 VALUES ('ok');
SELECT a, char_length(a) FROM test1; -- <A
NAME="CO.DATATYPE-CHAR"
><B
>(1)</B
></A
>
<SAMP
CLASS="COMPUTEROUTPUT"
>  a   | char_length
------+-------------
 ok   |           2</SAMP
>

CREATE TABLE test2 (b varchar(5));
INSERT INTO test2 VALUES ('ok');
INSERT INTO test2 VALUES ('good      ');
INSERT INTO test2 VALUES ('too long');
<SAMP
CLASS="COMPUTEROUTPUT"
>ERROR:  value too long for type character varying(5)</SAMP
>
INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
SELECT b, char_length(b) FROM test2;
<SAMP
CLASS="COMPUTEROUTPUT"
>   b   | char_length
-------+-------------
 ok    |           2
 good  |           5
 too l |           5</SAMP
></PRE
><DIV
CLASS="CALLOUTLIST"
><DL
COMPACT="COMPACT"
><DT
><A
HREF="datatype-character.html#CO.DATATYPE-CHAR"
><B
>(1)</B
></A
></DT
><DD
>       The <CODE
CLASS="FUNCTION"
>char_length</CODE
> function is discussed in
       <A
HREF="functions-string.html"
>Section 9.4</A
>.
      </DD
></DL
></DIV
></DIV
><P
>    There are two other fixed-length character types in
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, shown in <A
HREF="datatype-character.html#DATATYPE-CHARACTER-SPECIAL-TABLE"
>Table 8-5</A
>. The <TT
CLASS="TYPE"
>name</TT
>
    type exists <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>only</I
></SPAN
> for the storage of identifiers
    in the internal system catalogs and is not intended for use by the general user. Its
    length is currently defined as 64 bytes (63 usable characters plus
    terminator) but should be referenced using the constant
    <TT
CLASS="SYMBOL"
>NAMEDATALEN</TT
> in <TT
CLASS="LITERAL"
>C</TT
> source code.
    The length is set at compile time (and
    is therefore adjustable for special uses); the default maximum
    length might change in a future release. The type <TT
CLASS="TYPE"
>"char"</TT
>
    (note the quotes) is different from <TT
CLASS="TYPE"
>char(1)</TT
> in that it
    only uses one byte of storage. It is internally used in the system
    catalogs as a simplistic enumeration type.
   </P
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-CHARACTER-SPECIAL-TABLE"
></A
><P
><B
>Table 8-5. Special Character Types</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><THEAD
><TR
><TH
>Name</TH
><TH
>Storage Size</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="TYPE"
>"char"</TT
></TD
><TD
>1 byte</TD
><TD
>single-byte internal type</TD
></TR
><TR
><TD
><TT
CLASS="TYPE"
>name</TT
></TD
><TD
>64 bytes</TD
><TD
>internal type for object names</TD
></TR
></TBODY
></TABLE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="datatype-money.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="datatype-binary.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Monetary Types</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="datatype.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Binary Data Types</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>