~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/ddl.sgml

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!-- doc/src/sgml/ddl.sgml -->
 
2
 
 
3
<chapter id="ddl">
 
4
 <title>Data Definition</title>
 
5
 
 
6
 <para>
 
7
  This chapter covers how one creates the database structures that
 
8
  will hold one's data.  In a relational database, the raw data is
 
9
  stored in tables, so the majority of this chapter is devoted to
 
10
  explaining how tables are created and modified and what features are
 
11
  available to control what data is stored in the tables.
 
12
  Subsequently, we discuss how tables can be organized into
 
13
  schemas, and how privileges can be assigned to tables.  Finally,
 
14
  we will briefly look at other features that affect the data storage,
 
15
  such as inheritance, views, functions, and triggers.
 
16
 </para>
 
17
 
 
18
 <sect1 id="ddl-basics">
 
19
  <title>Table Basics</title>
 
20
 
 
21
  <indexterm zone="ddl-basics">
 
22
   <primary>table</primary>
 
23
  </indexterm>
 
24
 
 
25
  <indexterm>
 
26
   <primary>row</primary>
 
27
  </indexterm>
 
28
 
 
29
  <indexterm>
 
30
   <primary>column</primary>
 
31
  </indexterm>
 
32
 
 
33
  <para>
 
34
   A table in a relational database is much like a table on paper: It
 
35
   consists of rows and columns.  The number and order of the columns
 
36
   is fixed, and each column has a name.  The number of rows is
 
37
   variable &mdash; it reflects how much data is stored at a given moment.
 
38
   SQL does not make any guarantees about the order of the rows in a
 
39
   table.  When a table is read, the rows will appear in an unspecified order,
 
40
   unless sorting is explicitly requested.  This is covered in <xref
 
41
   linkend="queries">.  Furthermore, SQL does not assign unique
 
42
   identifiers to rows, so it is possible to have several completely
 
43
   identical rows in a table.  This is a consequence of the
 
44
   mathematical model that underlies SQL but is usually not desirable.
 
45
   Later in this chapter we will see how to deal with this issue.
 
46
  </para>
 
47
 
 
48
  <para>
 
49
   Each column has a data type.  The data type constrains the set of
 
50
   possible values that can be assigned to a column and assigns
 
51
   semantics to the data stored in the column so that it can be used
 
52
   for computations.  For instance, a column declared to be of a
 
53
   numerical type will not accept arbitrary text strings, and the data
 
54
   stored in such a column can be used for mathematical computations.
 
55
   By contrast, a column declared to be of a character string type
 
56
   will accept almost any kind of data but it does not lend itself to
 
57
   mathematical calculations, although other operations such as string
 
58
   concatenation are available.
 
59
  </para>
 
60
 
 
61
  <para>
 
62
   <productname>PostgreSQL</productname> includes a sizable set of
 
63
   built-in data types that fit many applications.  Users can also
 
64
   define their own data types.  Most built-in data types have obvious
 
65
   names and semantics, so we defer a detailed explanation to <xref
 
66
   linkend="datatype">.  Some of the frequently used data types are
 
67
   <type>integer</type> for whole numbers, <type>numeric</type> for
 
68
   possibly fractional numbers, <type>text</type> for character
 
69
   strings, <type>date</type> for dates, <type>time</type> for
 
70
   time-of-day values, and <type>timestamp</type> for values
 
71
   containing both date and time.
 
72
  </para>
 
73
 
 
74
  <indexterm>
 
75
   <primary>table</primary>
 
76
   <secondary>creating</secondary>
 
77
  </indexterm>
 
78
 
 
79
  <para>
 
80
   To create a table, you use the aptly named <xref
 
81
   linkend="sql-createtable"> command.
 
82
   In this command you specify at least a name for the new table, the
 
83
   names of the columns and the data type of each column.  For
 
84
   example:
 
85
<programlisting>
 
86
CREATE TABLE my_first_table (
 
87
    first_column text,
 
88
    second_column integer
 
89
);
 
90
</programlisting>
 
91
   This creates a table named <literal>my_first_table</literal> with
 
92
   two columns.  The first column is named
 
93
   <literal>first_column</literal> and has a data type of
 
94
   <type>text</type>; the second column has the name
 
95
   <literal>second_column</literal> and the type <type>integer</type>.
 
96
   The table and column names follow the identifier syntax explained
 
97
   in <xref linkend="sql-syntax-identifiers">.  The type names are
 
98
   usually also identifiers, but there are some exceptions.  Note that the
 
99
   column list is comma-separated and surrounded by parentheses.
 
100
  </para>
 
101
 
 
102
  <para>
 
103
   Of course, the previous example was heavily contrived.  Normally,
 
104
   you would give names to your tables and columns that convey what
 
105
   kind of data they store.  So let's look at a more realistic
 
106
   example:
 
107
<programlisting>
 
108
CREATE TABLE products (
 
109
    product_no integer,
 
110
    name text,
 
111
    price numeric
 
112
);
 
113
</programlisting>
 
114
   (The <type>numeric</type> type can store fractional components, as
 
115
   would be typical of monetary amounts.)
 
116
  </para>
 
117
 
 
118
  <tip>
 
119
   <para>
 
120
    When you create many interrelated tables it is wise to choose a
 
121
    consistent naming pattern for the tables and columns.  For
 
122
    instance, there is a choice of using singular or plural nouns for
 
123
    table names, both of which are favored by some theorist or other.
 
124
   </para>
 
125
  </tip>
 
126
 
 
127
  <para>
 
128
   There is a limit on how many columns a table can contain.
 
129
   Depending on the column types, it is between 250 and 1600.
 
130
   However, defining a table with anywhere near this many columns is
 
131
   highly unusual and often a questionable design.
 
132
  </para>
 
133
 
 
134
  <indexterm>
 
135
   <primary>table</primary>
 
136
   <secondary>removing</secondary>
 
137
  </indexterm>
 
138
 
 
139
  <para>
 
140
   If you no longer need a table, you can remove it using the <xref
 
141
   linkend="sql-droptable"> command.
 
142
   For example:
 
143
<programlisting>
 
144
DROP TABLE my_first_table;
 
145
DROP TABLE products;
 
146
</programlisting>
 
147
   Attempting to drop a table that does not exist is an error.
 
148
   Nevertheless, it is common in SQL script files to unconditionally
 
149
   try to drop each table before creating it, ignoring any error
 
150
   messages, so that the script works whether or not the table exists.
 
151
   (If you like, you can use the <literal>DROP TABLE IF EXISTS</> variant
 
152
   to avoid the error messages, but this is not standard SQL.)
 
153
  </para>
 
154
 
 
155
  <para>
 
156
   If you need to modify a table that already exists, see <xref
 
157
   linkend="ddl-alter"> later in this chapter.
 
158
  </para>
 
159
 
 
160
  <para>
 
161
   With the tools discussed so far you can create fully functional
 
162
   tables.  The remainder of this chapter is concerned with adding
 
163
   features to the table definition to ensure data integrity,
 
164
   security, or convenience.  If you are eager to fill your tables with
 
165
   data now you can skip ahead to <xref linkend="dml"> and read the
 
166
   rest of this chapter later.
 
167
  </para>
 
168
 </sect1>
 
169
 
 
170
 <sect1 id="ddl-default">
 
171
  <title>Default Values</title>
 
172
 
 
173
  <indexterm zone="ddl-default">
 
174
   <primary>default value</primary>
 
175
  </indexterm>
 
176
 
 
177
  <para>
 
178
   A column can be assigned a default value.  When a new row is
 
179
   created and no values are specified for some of the columns, those
 
180
   columns will be filled with their respective default values.  A
 
181
   data manipulation command can also request explicitly that a column
 
182
   be set to its default value, without having to know what that value is.
 
183
   (Details about data manipulation commands are in <xref linkend="dml">.)
 
184
  </para>
 
185
 
 
186
  <para>
 
187
   <indexterm><primary>null value</primary><secondary>default value</secondary></indexterm>
 
188
   If no default value is declared explicitly, the default value is the
 
189
   null value.  This usually makes sense because a null value can
 
190
   be considered to represent unknown data.
 
191
  </para>
 
192
 
 
193
  <para>
 
194
   In a table definition, default values are listed after the column
 
195
   data type.  For example:
 
196
<programlisting>
 
197
CREATE TABLE products (
 
198
    product_no integer,
 
199
    name text,
 
200
    price numeric <emphasis>DEFAULT 9.99</emphasis>
 
201
);
 
202
</programlisting>
 
203
  </para>
 
204
 
 
205
  <para>
 
206
   The default value can be an expression, which will be
 
207
   evaluated whenever the default value is inserted
 
208
   (<emphasis>not</emphasis> when the table is created).  A common example
 
209
   is for a <type>timestamp</type> column to have a default of <literal>CURRENT_TIMESTAMP</>,
 
210
   so that it gets set to the time of row insertion.  Another common
 
211
   example is generating a <quote>serial number</> for each row.
 
212
   In <productname>PostgreSQL</productname> this is typically done by
 
213
   something like:
 
214
<programlisting>
 
215
CREATE TABLE products (
 
216
    product_no integer <emphasis>DEFAULT nextval('products_product_no_seq')</emphasis>,
 
217
    ...
 
218
);
 
219
</programlisting>
 
220
   where the <literal>nextval()</> function supplies successive values
 
221
   from a <firstterm>sequence object</> (see <xref
 
222
   linkend="functions-sequence">). This arrangement is sufficiently common
 
223
   that there's a special shorthand for it:
 
224
<programlisting>
 
225
CREATE TABLE products (
 
226
    product_no <emphasis>SERIAL</emphasis>,
 
227
    ...
 
228
);
 
229
</programlisting>
 
230
   The <literal>SERIAL</> shorthand is discussed further in <xref
 
231
   linkend="datatype-serial">.
 
232
  </para>
 
233
 </sect1>
 
234
 
 
235
 <sect1 id="ddl-constraints">
 
236
  <title>Constraints</title>
 
237
 
 
238
  <indexterm zone="ddl-constraints">
 
239
   <primary>constraint</primary>
 
240
  </indexterm>
 
241
 
 
242
  <para>
 
243
   Data types are a way to limit the kind of data that can be stored
 
244
   in a table.  For many applications, however, the constraint they
 
245
   provide is too coarse.  For example, a column containing a product
 
246
   price should probably only accept positive values.  But there is no
 
247
   standard data type that accepts only positive numbers.  Another issue is
 
248
   that you might want to constrain column data with respect to other
 
249
   columns or rows.  For example, in a table containing product
 
250
   information, there should be only one row for each product number.
 
251
  </para>
 
252
 
 
253
  <para>
 
254
   To that end, SQL allows you to define constraints on columns and
 
255
   tables.  Constraints give you as much control over the data in your
 
256
   tables as you wish.  If a user attempts to store data in a column
 
257
   that would violate a constraint, an error is raised.  This applies
 
258
   even if the value came from the default value definition.
 
259
  </para>
 
260
 
 
261
  <sect2>
 
262
   <title>Check Constraints</title>
 
263
 
 
264
   <indexterm>
 
265
    <primary>check constraint</primary>
 
266
   </indexterm>
 
267
 
 
268
   <indexterm>
 
269
    <primary>constraint</primary>
 
270
    <secondary>check</secondary>
 
271
   </indexterm>
 
272
 
 
273
   <para>
 
274
    A check constraint is the most generic constraint type.  It allows
 
275
    you to specify that the value in a certain column must satisfy a
 
276
    Boolean (truth-value) expression.  For instance, to require positive
 
277
    product prices, you could use:
 
278
<programlisting>
 
279
CREATE TABLE products (
 
280
    product_no integer,
 
281
    name text,
 
282
    price numeric <emphasis>CHECK (price &gt; 0)</emphasis>
 
283
);
 
284
</programlisting>
 
285
   </para>
 
286
 
 
287
   <para>
 
288
    As you see, the constraint definition comes after the data type,
 
289
    just like default value definitions.  Default values and
 
290
    constraints can be listed in any order.  A check constraint
 
291
    consists of the key word <literal>CHECK</literal> followed by an
 
292
    expression in parentheses.  The check constraint expression should
 
293
    involve the column thus constrained, otherwise the constraint
 
294
    would not make too much sense.
 
295
   </para>
 
296
 
 
297
   <indexterm>
 
298
    <primary>constraint</primary>
 
299
    <secondary>name</secondary>
 
300
   </indexterm>
 
301
 
 
302
   <para>
 
303
    You can also give the constraint a separate name.  This clarifies
 
304
    error messages and allows you to refer to the constraint when you
 
305
    need to change it.  The syntax is:
 
306
<programlisting>
 
307
CREATE TABLE products (
 
308
    product_no integer,
 
309
    name text,
 
310
    price numeric <emphasis>CONSTRAINT positive_price</emphasis> CHECK (price &gt; 0)
 
311
);
 
312
</programlisting>
 
313
    So, to specify a named constraint, use the key word
 
314
    <literal>CONSTRAINT</literal> followed by an identifier followed
 
315
    by the constraint definition.  (If you don't specify a constraint
 
316
    name in this way, the system chooses a name for you.)
 
317
   </para>
 
318
 
 
319
   <para>
 
320
    A check constraint can also refer to several columns.  Say you
 
321
    store a regular price and a discounted price, and you want to
 
322
    ensure that the discounted price is lower than the regular price:
 
323
<programlisting>
 
324
CREATE TABLE products (
 
325
    product_no integer,
 
326
    name text,
 
327
    price numeric CHECK (price &gt; 0),
 
328
    discounted_price numeric CHECK (discounted_price &gt; 0),
 
329
    <emphasis>CHECK (price &gt; discounted_price)</emphasis>
 
330
);
 
331
</programlisting>
 
332
   </para>
 
333
 
 
334
   <para>
 
335
    The first two constraints should look familiar.  The third one
 
336
    uses a new syntax.  It is not attached to a particular column,
 
337
    instead it appears as a separate item in the comma-separated
 
338
    column list.  Column definitions and these constraint
 
339
    definitions can be listed in mixed order.
 
340
   </para>
 
341
 
 
342
   <para>
 
343
    We say that the first two constraints are column constraints, whereas the
 
344
    third one is a table constraint because it is written separately
 
345
    from any one column definition.  Column constraints can also be
 
346
    written as table constraints, while the reverse is not necessarily
 
347
    possible, since a column constraint is supposed to refer to only the
 
348
    column it is attached to.  (<productname>PostgreSQL</productname> doesn't
 
349
    enforce that rule, but you should follow it if you want your table
 
350
    definitions to work with other database systems.)  The above example could
 
351
    also be written as:
 
352
<programlisting>
 
353
CREATE TABLE products (
 
354
    product_no integer,
 
355
    name text,
 
356
    price numeric,
 
357
    CHECK (price &gt; 0),
 
358
    discounted_price numeric,
 
359
    CHECK (discounted_price &gt; 0),
 
360
    CHECK (price &gt; discounted_price)
 
361
);
 
362
</programlisting>
 
363
    or even:
 
364
<programlisting>
 
365
CREATE TABLE products (
 
366
    product_no integer,
 
367
    name text,
 
368
    price numeric CHECK (price &gt; 0),
 
369
    discounted_price numeric,
 
370
    CHECK (discounted_price &gt; 0 AND price &gt; discounted_price)
 
371
);
 
372
</programlisting>
 
373
    It's a matter of taste.
 
374
   </para>
 
375
 
 
376
   <para>
 
377
    Names can be assigned to table constraints in the same way as
 
378
    column constraints:
 
379
<programlisting>
 
380
CREATE TABLE products (
 
381
    product_no integer,
 
382
    name text,
 
383
    price numeric,
 
384
    CHECK (price &gt; 0),
 
385
    discounted_price numeric,
 
386
    CHECK (discounted_price &gt; 0),
 
387
    <emphasis>CONSTRAINT valid_discount</> CHECK (price &gt; discounted_price)
 
388
);
 
389
</programlisting>
 
390
   </para>
 
391
 
 
392
   <indexterm>
 
393
    <primary>null value</primary>
 
394
    <secondary sortas="check constraints">with check constraints</secondary>
 
395
   </indexterm>
 
396
 
 
397
   <para>
 
398
    It should be noted that a check constraint is satisfied if the
 
399
    check expression evaluates to true or the null value.  Since most
 
400
    expressions will evaluate to the null value if any operand is null,
 
401
    they will not prevent null values in the constrained columns.  To
 
402
    ensure that a column does not contain null values, the not-null
 
403
    constraint described in the next section can be used.
 
404
   </para>
 
405
  </sect2>
 
406
 
 
407
  <sect2>
 
408
   <title>Not-Null Constraints</title>
 
409
 
 
410
   <indexterm>
 
411
    <primary>not-null constraint</primary>
 
412
   </indexterm>
 
413
 
 
414
   <indexterm>
 
415
    <primary>constraint</primary>
 
416
    <secondary>NOT NULL</secondary>
 
417
   </indexterm>
 
418
 
 
419
   <para>
 
420
    A not-null constraint simply specifies that a column must not
 
421
    assume the null value.  A syntax example:
 
422
<programlisting>
 
423
CREATE TABLE products (
 
424
    product_no integer <emphasis>NOT NULL</emphasis>,
 
425
    name text <emphasis>NOT NULL</emphasis>,
 
426
    price numeric
 
427
);
 
428
</programlisting>
 
429
   </para>
 
430
 
 
431
   <para>
 
432
    A not-null constraint is always written as a column constraint.  A
 
433
    not-null constraint is functionally equivalent to creating a check
 
434
    constraint <literal>CHECK (<replaceable>column_name</replaceable>
 
435
    IS NOT NULL)</literal>, but in
 
436
    <productname>PostgreSQL</productname> creating an explicit
 
437
    not-null constraint is more efficient.  The drawback is that you
 
438
    cannot give explicit names to not-null constraints created this
 
439
    way.
 
440
   </para>
 
441
 
 
442
   <para>
 
443
    Of course, a column can have more than one constraint.  Just write
 
444
    the constraints one after another:
 
445
<programlisting>
 
446
CREATE TABLE products (
 
447
    product_no integer NOT NULL,
 
448
    name text NOT NULL,
 
449
    price numeric NOT NULL CHECK (price &gt; 0)
 
450
);
 
451
</programlisting>
 
452
    The order doesn't matter.  It does not necessarily determine in which
 
453
    order the constraints are checked.
 
454
   </para>
 
455
 
 
456
   <para>
 
457
    The <literal>NOT NULL</literal> constraint has an inverse: the
 
458
    <literal>NULL</literal> constraint.  This does not mean that the
 
459
    column must be null, which would surely be useless.  Instead, this
 
460
    simply selects the default behavior that the column might be null.
 
461
    The <literal>NULL</literal> constraint is not present in the SQL
 
462
    standard and should not be used in portable applications.  (It was
 
463
    only added to <productname>PostgreSQL</productname> to be
 
464
    compatible with some other database systems.)  Some users, however,
 
465
    like it because it makes it easy to toggle the constraint in a
 
466
    script file.  For example, you could start with:
 
467
<programlisting>
 
468
CREATE TABLE products (
 
469
    product_no integer NULL,
 
470
    name text NULL,
 
471
    price numeric NULL
 
472
);
 
473
</programlisting>
 
474
    and then insert the <literal>NOT</literal> key word where desired.
 
475
   </para>
 
476
 
 
477
   <tip>
 
478
    <para>
 
479
     In most database designs the majority of columns should be marked
 
480
     not null.
 
481
    </para>
 
482
   </tip>
 
483
  </sect2>
 
484
 
 
485
  <sect2>
 
486
   <title>Unique Constraints</title>
 
487
 
 
488
   <indexterm>
 
489
    <primary>unique constraint</primary>
 
490
   </indexterm>
 
491
 
 
492
   <indexterm>
 
493
    <primary>constraint</primary>
 
494
    <secondary>unique</secondary>
 
495
   </indexterm>
 
496
 
 
497
   <para>
 
498
    Unique constraints ensure that the data contained in a column or a
 
499
    group of columns is unique with respect to all the rows in the
 
500
    table.  The syntax is:
 
501
<programlisting>
 
502
CREATE TABLE products (
 
503
    product_no integer <emphasis>UNIQUE</emphasis>,
 
504
    name text,
 
505
    price numeric
 
506
);
 
507
</programlisting>
 
508
    when written as a column constraint, and:
 
509
<programlisting>
 
510
CREATE TABLE products (
 
511
    product_no integer,
 
512
    name text,
 
513
    price numeric,
 
514
    <emphasis>UNIQUE (product_no)</emphasis>
 
515
);
 
516
</programlisting>
 
517
    when written as a table constraint.
 
518
   </para>
 
519
 
 
520
   <para>
 
521
    If a unique constraint refers to a group of columns, the columns
 
522
    are listed separated by commas:
 
523
<programlisting>
 
524
CREATE TABLE example (
 
525
    a integer,
 
526
    b integer,
 
527
    c integer,
 
528
    <emphasis>UNIQUE (a, c)</emphasis>
 
529
);
 
530
</programlisting>
 
531
    This specifies that the combination of values in the indicated columns
 
532
    is unique across the whole table, though any one of the columns
 
533
    need not be (and ordinarily isn't) unique.
 
534
   </para>
 
535
 
 
536
   <para>
 
537
    You can assign your own name for a unique constraint, in the usual way:
 
538
<programlisting>
 
539
CREATE TABLE products (
 
540
    product_no integer <emphasis>CONSTRAINT must_be_different</emphasis> UNIQUE,
 
541
    name text,
 
542
    price numeric
 
543
);
 
544
</programlisting>
 
545
   </para>
 
546
 
 
547
   <para>
 
548
    Adding a unique constraint will automatically create a unique btree
 
549
    index on the column or group of columns used in the constraint.
 
550
   </para>
 
551
 
 
552
   <indexterm>
 
553
    <primary>null value</primary>
 
554
    <secondary sortas="unique constraints">with unique constraints</secondary>
 
555
   </indexterm>
 
556
 
 
557
   <para>
 
558
    In general, a unique constraint is violated when there is more than
 
559
    one row in the table where the values of all of the
 
560
    columns included in the constraint are equal.
 
561
    However, two null values are not considered equal in this
 
562
    comparison.  That means even in the presence of a
 
563
    unique constraint it is possible to store duplicate
 
564
    rows that contain a null value in at least one of the constrained
 
565
    columns.  This behavior conforms to the SQL standard, but we have
 
566
    heard that other SQL databases might not follow this rule.  So be
 
567
    careful when developing applications that are intended to be
 
568
    portable.
 
569
   </para>
 
570
  </sect2>
 
571
 
 
572
  <sect2>
 
573
   <title>Primary Keys</title>
 
574
 
 
575
   <indexterm>
 
576
    <primary>primary key</primary>
 
577
   </indexterm>
 
578
 
 
579
   <indexterm>
 
580
    <primary>constraint</primary>
 
581
    <secondary>primary key</secondary>
 
582
   </indexterm>
 
583
 
 
584
   <para>
 
585
    Technically, a primary key constraint is simply a combination of a
 
586
    unique constraint and a not-null constraint.  So, the following
 
587
    two table definitions accept the same data:
 
588
<programlisting>
 
589
CREATE TABLE products (
 
590
    product_no integer UNIQUE NOT NULL,
 
591
    name text,
 
592
    price numeric
 
593
);
 
594
</programlisting>
 
595
 
 
596
<programlisting>
 
597
CREATE TABLE products (
 
598
    product_no integer <emphasis>PRIMARY KEY</emphasis>,
 
599
    name text,
 
600
    price numeric
 
601
);
 
602
</programlisting>
 
603
   </para>
 
604
 
 
605
   <para>
 
606
    Primary keys can also constrain more than one column; the syntax
 
607
    is similar to unique constraints:
 
608
<programlisting>
 
609
CREATE TABLE example (
 
610
    a integer,
 
611
    b integer,
 
612
    c integer,
 
613
    <emphasis>PRIMARY KEY (a, c)</emphasis>
 
614
);
 
615
</programlisting>
 
616
   </para>
 
617
 
 
618
   <para>
 
619
    A primary key indicates that a column or group of columns can be
 
620
    used as a unique identifier for rows in the table.  (This is a
 
621
    direct consequence of the definition of a primary key.  Note that
 
622
    a unique constraint does not, by itself, provide a unique identifier
 
623
    because it does not exclude null values.)  This is useful both for
 
624
    documentation purposes and for client applications.  For example,
 
625
    a GUI application that allows modifying row values probably needs
 
626
    to know the primary key of a table to be able to identify rows
 
627
    uniquely.
 
628
   </para>
 
629
 
 
630
   <para>
 
631
    Adding a primary key will automatically create a unique btree index
 
632
    on the column or group of columns used in the primary key.
 
633
   </para>
 
634
 
 
635
   <para>
 
636
    A table can have at most one primary key.  (There can be any number
 
637
    of unique and not-null constraints, which are functionally the same
 
638
    thing, but only one can be identified as the primary key.)
 
639
    Relational database theory
 
640
    dictates that every table must have a primary key.  This rule is
 
641
    not enforced by <productname>PostgreSQL</productname>, but it is
 
642
    usually best to follow it.
 
643
   </para>
 
644
  </sect2>
 
645
 
 
646
  <sect2 id="ddl-constraints-fk">
 
647
   <title>Foreign Keys</title>
 
648
 
 
649
   <indexterm>
 
650
    <primary>foreign key</primary>
 
651
   </indexterm>
 
652
 
 
653
   <indexterm>
 
654
    <primary>constraint</primary>
 
655
    <secondary>foreign key</secondary>
 
656
   </indexterm>
 
657
 
 
658
   <indexterm>
 
659
    <primary>referential integrity</primary>
 
660
   </indexterm>
 
661
 
 
662
   <para>
 
663
    A foreign key constraint specifies that the values in a column (or
 
664
    a group of columns) must match the values appearing in some row
 
665
    of another table.
 
666
    We say this maintains the <firstterm>referential
 
667
    integrity</firstterm> between two related tables.
 
668
   </para>
 
669
 
 
670
   <para>
 
671
    Say you have the product table that we have used several times already:
 
672
<programlisting>
 
673
CREATE TABLE products (
 
674
    product_no integer PRIMARY KEY,
 
675
    name text,
 
676
    price numeric
 
677
);
 
678
</programlisting>
 
679
    Let's also assume you have a table storing orders of those
 
680
    products.  We want to ensure that the orders table only contains
 
681
    orders of products that actually exist.  So we define a foreign
 
682
    key constraint in the orders table that references the products
 
683
    table:
 
684
<programlisting>
 
685
CREATE TABLE orders (
 
686
    order_id integer PRIMARY KEY,
 
687
    product_no integer <emphasis>REFERENCES products (product_no)</emphasis>,
 
688
    quantity integer
 
689
);
 
690
</programlisting>
 
691
    Now it is impossible to create orders with
 
692
    <structfield>product_no</structfield> entries that do not appear in the
 
693
    products table.
 
694
   </para>
 
695
 
 
696
   <para>
 
697
    We say that in this situation the orders table is the
 
698
    <firstterm>referencing</firstterm> table and the products table is
 
699
    the <firstterm>referenced</firstterm> table.  Similarly, there are
 
700
    referencing and referenced columns.
 
701
   </para>
 
702
 
 
703
   <para>
 
704
    You can also shorten the above command to:
 
705
<programlisting>
 
706
CREATE TABLE orders (
 
707
    order_id integer PRIMARY KEY,
 
708
    product_no integer <emphasis>REFERENCES products</emphasis>,
 
709
    quantity integer
 
710
);
 
711
</programlisting>
 
712
    because in absence of a column list the primary key of the
 
713
    referenced table is used as the referenced column(s).
 
714
   </para>
 
715
 
 
716
   <para>
 
717
    A foreign key can also constrain and reference a group of columns.
 
718
    As usual, it then needs to be written in table constraint form.
 
719
    Here is a contrived syntax example:
 
720
<programlisting>
 
721
CREATE TABLE t1 (
 
722
  a integer PRIMARY KEY,
 
723
  b integer,
 
724
  c integer,
 
725
  <emphasis>FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)</emphasis>
 
726
);
 
727
</programlisting>
 
728
    Of course, the number and type of the constrained columns need to
 
729
    match the number and type of the referenced columns.
 
730
   </para>
 
731
 
 
732
   <para>
 
733
    You can assign your own name for a foreign key constraint,
 
734
    in the usual way.
 
735
   </para>
 
736
 
 
737
   <para>
 
738
    A table can contain more than one foreign key constraint.  This is
 
739
    used to implement many-to-many relationships between tables.  Say
 
740
    you have tables about products and orders, but now you want to
 
741
    allow one order to contain possibly many products (which the
 
742
    structure above did not allow).  You could use this table structure:
 
743
<programlisting>
 
744
CREATE TABLE products (
 
745
    product_no integer PRIMARY KEY,
 
746
    name text,
 
747
    price numeric
 
748
);
 
749
 
 
750
CREATE TABLE orders (
 
751
    order_id integer PRIMARY KEY,
 
752
    shipping_address text,
 
753
    ...
 
754
);
 
755
 
 
756
CREATE TABLE order_items (
 
757
    product_no integer REFERENCES products,
 
758
    order_id integer REFERENCES orders,
 
759
    quantity integer,
 
760
    PRIMARY KEY (product_no, order_id)
 
761
);
 
762
</programlisting>
 
763
    Notice that the primary key overlaps with the foreign keys in
 
764
    the last table.
 
765
   </para>
 
766
 
 
767
   <indexterm>
 
768
    <primary>CASCADE</primary>
 
769
    <secondary>foreign key action</secondary>
 
770
   </indexterm>
 
771
 
 
772
   <indexterm>
 
773
    <primary>RESTRICT</primary>
 
774
    <secondary>foreign key action</secondary>
 
775
   </indexterm>
 
776
 
 
777
   <para>
 
778
    We know that the foreign keys disallow creation of orders that
 
779
    do not relate to any products.  But what if a product is removed
 
780
    after an order is created that references it?  SQL allows you to
 
781
    handle that as well.  Intuitively, we have a few options:
 
782
    <itemizedlist spacing="compact">
 
783
     <listitem><para>Disallow deleting a referenced product</para></listitem>
 
784
     <listitem><para>Delete the orders as well</para></listitem>
 
785
     <listitem><para>Something else?</para></listitem>
 
786
    </itemizedlist>
 
787
   </para>
 
788
 
 
789
   <para>
 
790
    To illustrate this, let's implement the following policy on the
 
791
    many-to-many relationship example above: when someone wants to
 
792
    remove a product that is still referenced by an order (via
 
793
    <literal>order_items</literal>), we disallow it.  If someone
 
794
    removes an order, the order items are removed as well:
 
795
<programlisting>
 
796
CREATE TABLE products (
 
797
    product_no integer PRIMARY KEY,
 
798
    name text,
 
799
    price numeric
 
800
);
 
801
 
 
802
CREATE TABLE orders (
 
803
    order_id integer PRIMARY KEY,
 
804
    shipping_address text,
 
805
    ...
 
806
);
 
807
 
 
808
CREATE TABLE order_items (
 
809
    product_no integer REFERENCES products <emphasis>ON DELETE RESTRICT</emphasis>,
 
810
    order_id integer REFERENCES orders <emphasis>ON DELETE CASCADE</emphasis>,
 
811
    quantity integer,
 
812
    PRIMARY KEY (product_no, order_id)
 
813
);
 
814
</programlisting>
 
815
   </para>
 
816
 
 
817
   <para>
 
818
    Restricting and cascading deletes are the two most common options.
 
819
    <literal>RESTRICT</literal> prevents deletion of a
 
820
    referenced row. <literal>NO ACTION</literal> means that if any
 
821
    referencing rows still exist when the constraint is checked, an error
 
822
    is raised; this is the default behavior if you do not specify anything.
 
823
    (The essential difference between these two choices is that
 
824
    <literal>NO ACTION</literal> allows the check to be deferred until
 
825
    later in the transaction, whereas <literal>RESTRICT</literal> does not.)
 
826
    <literal>CASCADE</> specifies that when a referenced row is deleted,
 
827
    row(s) referencing it should be automatically deleted as well.
 
828
    There are two other options:
 
829
    <literal>SET NULL</literal> and <literal>SET DEFAULT</literal>.
 
830
    These cause the referencing columns to be set to nulls or default
 
831
    values, respectively, when the referenced row is deleted.
 
832
    Note that these do not excuse you from observing any constraints.
 
833
    For example, if an action specifies <literal>SET DEFAULT</literal>
 
834
    but the default value would not satisfy the foreign key, the
 
835
    operation will fail.
 
836
   </para>
 
837
 
 
838
   <para>
 
839
    Analogous to <literal>ON DELETE</literal> there is also
 
840
    <literal>ON UPDATE</literal> which is invoked when a referenced
 
841
    column is changed (updated).  The possible actions are the same.
 
842
   </para>
 
843
 
 
844
   <para>
 
845
    Since a <command>DELETE</command> of a row from the referenced table
 
846
    or an <command>UPDATE</command> of a referenced column will require
 
847
    a scan of the referencing table for rows matching the old value, it
 
848
    is often a good idea to index the referencing columns.  Because this
 
849
    is not always needed, and there are many choices available on how
 
850
    to index, declaration of a foreign key constraint does not
 
851
    automatically create an index on the referencing columns.
 
852
   </para>
 
853
 
 
854
   <para>
 
855
    More information about updating and deleting data is in <xref
 
856
    linkend="dml">.
 
857
   </para>
 
858
 
 
859
   <para>
 
860
    Finally, we should mention that a foreign key must reference
 
861
    columns that either are a primary key or form a unique constraint.
 
862
    If the foreign key references a unique constraint, there are some
 
863
    additional possibilities regarding how null values are matched.
 
864
    These are explained in the reference documentation for
 
865
    <xref linkend="sql-createtable">.
 
866
   </para>
 
867
  </sect2>
 
868
 
 
869
  <sect2 id="ddl-constraints-exclusion">
 
870
   <title>Exclusion Constraints</title>
 
871
 
 
872
   <indexterm>
 
873
    <primary>exclusion constraint</primary>
 
874
   </indexterm>
 
875
 
 
876
   <indexterm>
 
877
    <primary>constraint</primary>
 
878
    <secondary>exclusion</secondary>
 
879
   </indexterm>
 
880
 
 
881
   <para>
 
882
    Exclusion constraints ensure that if any two rows are compared on
 
883
    the specified columns or expressions using the specified operators,
 
884
    at least one of these operator comparisons will return false or null.
 
885
    The syntax is:
 
886
<programlisting>
 
887
CREATE TABLE circles (
 
888
    c circle,
 
889
    EXCLUDE USING gist (c WITH &amp;&amp;)
 
890
);
 
891
</programlisting>
 
892
   </para>
 
893
 
 
894
   <para>
 
895
    See also <link linkend="SQL-CREATETABLE-EXCLUDE"><command>CREATE
 
896
    TABLE ... CONSTRAINT ... EXCLUDE</></link> for details.
 
897
   </para>
 
898
 
 
899
   <para>
 
900
    Adding an exclusion constraint will automatically create an index
 
901
    of the type specified in the constraint declaration.
 
902
   </para>
 
903
  </sect2>
 
904
 </sect1>
 
905
 
 
906
 <sect1 id="ddl-system-columns">
 
907
  <title>System Columns</title>
 
908
 
 
909
  <para>
 
910
   Every table has several <firstterm>system columns</> that are
 
911
   implicitly defined by the system.  Therefore, these names cannot be
 
912
   used as names of user-defined columns.  (Note that these
 
913
   restrictions are separate from whether the name is a key word or
 
914
   not; quoting a name will not allow you to escape these
 
915
   restrictions.)  You do not really need to be concerned about these
 
916
   columns; just know they exist.
 
917
  </para>
 
918
 
 
919
  <indexterm>
 
920
   <primary>column</primary>
 
921
   <secondary>system column</secondary>
 
922
  </indexterm>
 
923
 
 
924
  <variablelist>
 
925
   <varlistentry>
 
926
    <term><structfield>oid</></term>
 
927
    <listitem>
 
928
     <para>
 
929
      <indexterm>
 
930
       <primary>OID</primary>
 
931
       <secondary>column</secondary>
 
932
      </indexterm>
 
933
      The object identifier (object ID) of a row. This column is only
 
934
      present if the table was created using <literal>WITH
 
935
      OIDS</literal>, or if the <xref linkend="guc-default-with-oids">
 
936
      configuration variable was set at the time. This column is of type
 
937
      <type>oid</type> (same name as the column); see <xref
 
938
      linkend="datatype-oid"> for more information about the type.
 
939
     </para>
 
940
    </listitem>
 
941
   </varlistentry>
 
942
 
 
943
   <varlistentry>
 
944
    <term><structfield>tableoid</></term>
 
945
    <listitem>
 
946
     <indexterm>
 
947
      <primary>tableoid</primary>
 
948
     </indexterm>
 
949
 
 
950
     <para>
 
951
      The OID of the table containing this row.  This column is
 
952
      particularly handy for queries that select from inheritance
 
953
      hierarchies (see <xref linkend="ddl-inherit">), since without it,
 
954
      it's difficult to tell which individual table a row came from.  The
 
955
      <structfield>tableoid</structfield> can be joined against the
 
956
      <structfield>oid</structfield> column of
 
957
      <structname>pg_class</structname> to obtain the table name.
 
958
     </para>
 
959
    </listitem>
 
960
   </varlistentry>
 
961
 
 
962
   <varlistentry>
 
963
    <term><structfield>xmin</></term>
 
964
    <listitem>
 
965
     <indexterm>
 
966
      <primary>xmin</primary>
 
967
     </indexterm>
 
968
 
 
969
     <para>
 
970
      The identity (transaction ID) of the inserting transaction for
 
971
      this row version.  (A row version is an individual state of a
 
972
      row; each update of a row creates a new row version for the same
 
973
      logical row.)
 
974
     </para>
 
975
    </listitem>
 
976
   </varlistentry>
 
977
 
 
978
   <varlistentry>
 
979
    <term><structfield>cmin</></term>
 
980
    <listitem>
 
981
     <indexterm>
 
982
      <primary>cmin</primary>
 
983
     </indexterm>
 
984
 
 
985
     <para>
 
986
      The command identifier (starting at zero) within the inserting
 
987
      transaction.
 
988
     </para>
 
989
    </listitem>
 
990
   </varlistentry>
 
991
 
 
992
   <varlistentry>
 
993
    <term><structfield>xmax</></term>
 
994
    <listitem>
 
995
     <indexterm>
 
996
      <primary>xmax</primary>
 
997
     </indexterm>
 
998
 
 
999
     <para>
 
1000
      The identity (transaction ID) of the deleting transaction, or
 
1001
      zero for an undeleted row version.  It is possible for this column to
 
1002
      be nonzero in a visible row version. That usually indicates that the
 
1003
      deleting transaction hasn't committed yet, or that an attempted
 
1004
      deletion was rolled back.
 
1005
     </para>
 
1006
    </listitem>
 
1007
   </varlistentry>
 
1008
 
 
1009
   <varlistentry>
 
1010
    <term><structfield>cmax</></term>
 
1011
    <listitem>
 
1012
     <indexterm>
 
1013
      <primary>cmax</primary>
 
1014
     </indexterm>
 
1015
 
 
1016
     <para>
 
1017
      The command identifier within the deleting transaction, or zero.
 
1018
     </para>
 
1019
    </listitem>
 
1020
   </varlistentry>
 
1021
 
 
1022
   <varlistentry>
 
1023
    <term><structfield>ctid</></term>
 
1024
    <listitem>
 
1025
     <indexterm>
 
1026
      <primary>ctid</primary>
 
1027
     </indexterm>
 
1028
 
 
1029
     <para>
 
1030
      The physical location of the row version within its table.  Note that
 
1031
      although the <structfield>ctid</structfield> can be used to
 
1032
      locate the row version very quickly, a row's
 
1033
      <structfield>ctid</structfield> will change if it is
 
1034
      updated or moved by <command>VACUUM FULL</>.  Therefore
 
1035
      <structfield>ctid</structfield> is useless as a long-term row
 
1036
      identifier.  The OID, or even better a user-defined serial
 
1037
      number, should be used to identify logical rows.
 
1038
     </para>
 
1039
    </listitem>
 
1040
   </varlistentry>
 
1041
  </variablelist>
 
1042
 
 
1043
   <para>
 
1044
    OIDs are 32-bit quantities and are assigned from a single
 
1045
    cluster-wide counter.  In a large or long-lived database, it is
 
1046
    possible for the counter to wrap around.  Hence, it is bad
 
1047
    practice to assume that OIDs are unique, unless you take steps to
 
1048
    ensure that this is the case.  If you need to identify the rows in
 
1049
    a table, using a sequence generator is strongly recommended.
 
1050
    However, OIDs can be used as well, provided that a few additional
 
1051
    precautions are taken:
 
1052
 
 
1053
    <itemizedlist>
 
1054
     <listitem>
 
1055
      <para>
 
1056
       A unique constraint should be created on the OID column of each
 
1057
       table for which the OID will be used to identify rows.  When such
 
1058
       a unique constraint (or unique index) exists, the system takes
 
1059
       care not to generate an OID matching an already-existing row.
 
1060
       (Of course, this is only possible if the table contains fewer
 
1061
       than 2<superscript>32</> (4 billion) rows, and in practice the
 
1062
       table size had better be much less than that, or performance
 
1063
       might suffer.)
 
1064
      </para>
 
1065
     </listitem>
 
1066
     <listitem>
 
1067
      <para>
 
1068
       OIDs should never be assumed to be unique across tables; use
 
1069
       the combination of <structfield>tableoid</> and row OID if you
 
1070
       need a database-wide identifier.
 
1071
      </para>
 
1072
     </listitem>
 
1073
     <listitem>
 
1074
      <para>
 
1075
       Of course, the tables in question must be created <literal>WITH
 
1076
       OIDS</literal>.  As of <productname>PostgreSQL</productname> 8.1,
 
1077
       <literal>WITHOUT OIDS</> is the default.
 
1078
      </para>
 
1079
     </listitem>
 
1080
    </itemizedlist>
 
1081
   </para>
 
1082
 
 
1083
   <para>
 
1084
    Transaction identifiers are also 32-bit quantities.  In a
 
1085
    long-lived database it is possible for transaction IDs to wrap
 
1086
    around.  This is not a fatal problem given appropriate maintenance
 
1087
    procedures; see <xref linkend="maintenance"> for details.  It is
 
1088
    unwise, however, to depend on the uniqueness of transaction IDs
 
1089
    over the long term (more than one billion transactions).
 
1090
   </para>
 
1091
 
 
1092
   <para>
 
1093
    Command identifiers are also 32-bit quantities.  This creates a hard limit
 
1094
    of 2<superscript>32</> (4 billion) <acronym>SQL</acronym> commands
 
1095
    within a single transaction.  In practice this limit is not a
 
1096
    problem &mdash; note that the limit is on the number of
 
1097
    <acronym>SQL</acronym> commands, not the number of rows processed.
 
1098
    Also, as of <productname>PostgreSQL</productname> 8.3, only commands
 
1099
    that actually modify the database contents will consume a command
 
1100
    identifier.
 
1101
   </para>
 
1102
 </sect1>
 
1103
 
 
1104
 <sect1 id="ddl-alter">
 
1105
  <title>Modifying Tables</title>
 
1106
 
 
1107
  <indexterm zone="ddl-alter">
 
1108
   <primary>table</primary>
 
1109
   <secondary>modifying</secondary>
 
1110
  </indexterm>
 
1111
 
 
1112
  <para>
 
1113
   When you create a table and you realize that you made a mistake, or
 
1114
   the requirements of the application change, you can drop the
 
1115
   table and create it again.  But this is not a convenient option if
 
1116
   the table is already filled with data, or if the table is
 
1117
   referenced by other database objects (for instance a foreign key
 
1118
   constraint).  Therefore <productname>PostgreSQL</productname>
 
1119
   provides a family of commands to make modifications to existing
 
1120
   tables.  Note that this is conceptually distinct from altering
 
1121
   the data contained in the table: here we are interested in altering
 
1122
   the definition, or structure, of the table.
 
1123
  </para>
 
1124
 
 
1125
  <para>
 
1126
   You can:
 
1127
   <itemizedlist spacing="compact">
 
1128
    <listitem>
 
1129
     <para>Add columns</para>
 
1130
    </listitem>
 
1131
    <listitem>
 
1132
     <para>Remove columns</para>
 
1133
    </listitem>
 
1134
    <listitem>
 
1135
     <para>Add constraints</para>
 
1136
    </listitem>
 
1137
    <listitem>
 
1138
     <para>Remove constraints</para>
 
1139
    </listitem>
 
1140
    <listitem>
 
1141
     <para>Change default values</para>
 
1142
    </listitem>
 
1143
    <listitem>
 
1144
     <para>Change column data types</para>
 
1145
    </listitem>
 
1146
    <listitem>
 
1147
     <para>Rename columns</para>
 
1148
    </listitem>
 
1149
    <listitem>
 
1150
     <para>Rename tables</para>
 
1151
    </listitem>
 
1152
   </itemizedlist>
 
1153
 
 
1154
   All these actions are performed using the
 
1155
   <xref linkend="sql-altertable">
 
1156
   command, whose reference page contains details beyond those given
 
1157
   here.
 
1158
  </para>
 
1159
 
 
1160
  <sect2>
 
1161
   <title>Adding a Column</title>
 
1162
 
 
1163
   <indexterm>
 
1164
    <primary>column</primary>
 
1165
    <secondary>adding</secondary>
 
1166
   </indexterm>
 
1167
 
 
1168
   <para>
 
1169
    To add a column, use a command like:
 
1170
<programlisting>
 
1171
ALTER TABLE products ADD COLUMN description text;
 
1172
</programlisting>
 
1173
    The new column is initially filled with whatever default
 
1174
    value is given (null if you don't specify a <literal>DEFAULT</> clause).
 
1175
   </para>
 
1176
 
 
1177
   <para>
 
1178
    You can also define constraints on the column at the same time,
 
1179
    using the usual syntax:
 
1180
<programlisting>
 
1181
ALTER TABLE products ADD COLUMN description text CHECK (description &lt;&gt; '');
 
1182
</programlisting>
 
1183
    In fact all the options that can be applied to a column description
 
1184
    in <command>CREATE TABLE</> can be used here.  Keep in mind however
 
1185
    that the default value must satisfy the given constraints, or the
 
1186
    <literal>ADD</> will fail.  Alternatively, you can add
 
1187
    constraints later (see below) after you've filled in the new column
 
1188
    correctly.
 
1189
   </para>
 
1190
 
 
1191
  <tip>
 
1192
   <para>
 
1193
    Adding a column with a default requires updating each row of the
 
1194
    table (to store the new column value).  However, if no default is
 
1195
    specified, <productname>PostgreSQL</productname> is able to avoid
 
1196
    the physical update.  So if you intend to fill the column with
 
1197
    mostly nondefault values, it's best to add the column with no default,
 
1198
    insert the correct values using <command>UPDATE</>, and then add any
 
1199
    desired default as described below.
 
1200
   </para>
 
1201
  </tip>
 
1202
  </sect2>
 
1203
 
 
1204
  <sect2>
 
1205
   <title>Removing a Column</title>
 
1206
 
 
1207
   <indexterm>
 
1208
    <primary>column</primary>
 
1209
    <secondary>removing</secondary>
 
1210
   </indexterm>
 
1211
 
 
1212
   <para>
 
1213
    To remove a column, use a command like:
 
1214
<programlisting>
 
1215
ALTER TABLE products DROP COLUMN description;
 
1216
</programlisting>
 
1217
    Whatever data was in the column disappears.  Table constraints involving
 
1218
    the column are dropped, too.  However, if the column is referenced by a
 
1219
    foreign key constraint of another table,
 
1220
    <productname>PostgreSQL</productname> will not silently drop that
 
1221
    constraint.  You can authorize dropping everything that depends on
 
1222
    the column by adding <literal>CASCADE</>:
 
1223
<programlisting>
 
1224
ALTER TABLE products DROP COLUMN description CASCADE;
 
1225
</programlisting>
 
1226
    See <xref linkend="ddl-depend"> for a description of the general
 
1227
    mechanism behind this.
 
1228
   </para>
 
1229
  </sect2>
 
1230
 
 
1231
  <sect2>
 
1232
   <title>Adding a Constraint</title>
 
1233
 
 
1234
   <indexterm>
 
1235
    <primary>constraint</primary>
 
1236
    <secondary>adding</secondary>
 
1237
   </indexterm>
 
1238
 
 
1239
   <para>
 
1240
    To add a constraint, the table constraint syntax is used.  For example:
 
1241
<programlisting>
 
1242
ALTER TABLE products ADD CHECK (name &lt;&gt; '');
 
1243
ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);
 
1244
ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;
 
1245
</programlisting>
 
1246
    To add a not-null constraint, which cannot be written as a table
 
1247
    constraint, use this syntax:
 
1248
<programlisting>
 
1249
ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;
 
1250
</programlisting>
 
1251
   </para>
 
1252
 
 
1253
   <para>
 
1254
    The constraint will be checked immediately, so the table data must
 
1255
    satisfy the constraint before it can be added.
 
1256
   </para>
 
1257
  </sect2>
 
1258
 
 
1259
  <sect2>
 
1260
   <title>Removing a Constraint</title>
 
1261
 
 
1262
   <indexterm>
 
1263
    <primary>constraint</primary>
 
1264
    <secondary>removing</secondary>
 
1265
   </indexterm>
 
1266
 
 
1267
   <para>
 
1268
    To remove a constraint you need to know its name.  If you gave it
 
1269
    a name then that's easy.  Otherwise the system assigned a
 
1270
    generated name, which you need to find out.  The
 
1271
    <application>psql</application> command <literal>\d
 
1272
    <replaceable>tablename</replaceable></literal> can be helpful
 
1273
    here; other interfaces might also provide a way to inspect table
 
1274
    details.  Then the command is:
 
1275
<programlisting>
 
1276
ALTER TABLE products DROP CONSTRAINT some_name;
 
1277
</programlisting>
 
1278
    (If you are dealing with a generated constraint name like <literal>$2</>,
 
1279
    don't forget that you'll need to double-quote it to make it a valid
 
1280
    identifier.)
 
1281
   </para>
 
1282
 
 
1283
   <para>
 
1284
    As with dropping a column, you need to add <literal>CASCADE</> if you
 
1285
    want to drop a constraint that something else depends on.  An example
 
1286
    is that a foreign key constraint depends on a unique or primary key
 
1287
    constraint on the referenced column(s).
 
1288
   </para>
 
1289
 
 
1290
   <para>
 
1291
    This works the same for all constraint types except not-null
 
1292
    constraints. To drop a not null constraint use:
 
1293
<programlisting>
 
1294
ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;
 
1295
</programlisting>
 
1296
    (Recall that not-null constraints do not have names.)
 
1297
   </para>
 
1298
  </sect2>
 
1299
 
 
1300
  <sect2>
 
1301
   <title>Changing a Column's Default Value</title>
 
1302
 
 
1303
   <indexterm>
 
1304
    <primary>default value</primary>
 
1305
    <secondary>changing</secondary>
 
1306
   </indexterm>
 
1307
 
 
1308
   <para>
 
1309
    To set a new default for a column, use a command like:
 
1310
<programlisting>
 
1311
ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;
 
1312
</programlisting>
 
1313
    Note that this doesn't affect any existing rows in the table, it
 
1314
    just changes the default for future <command>INSERT</> commands.
 
1315
   </para>
 
1316
 
 
1317
   <para>
 
1318
    To remove any default value, use:
 
1319
<programlisting>
 
1320
ALTER TABLE products ALTER COLUMN price DROP DEFAULT;
 
1321
</programlisting>
 
1322
    This is effectively the same as setting the default to null.
 
1323
    As a consequence, it is not an error
 
1324
    to drop a default where one hadn't been defined, because the
 
1325
    default is implicitly the null value.
 
1326
   </para>
 
1327
  </sect2>
 
1328
 
 
1329
  <sect2>
 
1330
   <title>Changing a Column's Data Type</title>
 
1331
 
 
1332
   <indexterm>
 
1333
    <primary>column data type</primary>
 
1334
    <secondary>changing</secondary>
 
1335
   </indexterm>
 
1336
 
 
1337
   <para>
 
1338
    To convert a column to a different data type, use a command like:
 
1339
<programlisting>
 
1340
ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);
 
1341
</programlisting>
 
1342
    This will succeed only if each existing entry in the column can be
 
1343
    converted to the new type by an implicit cast.  If a more complex
 
1344
    conversion is needed, you can add a <literal>USING</> clause that
 
1345
    specifies how to compute the new values from the old.
 
1346
   </para>
 
1347
 
 
1348
   <para>
 
1349
    <productname>PostgreSQL</> will attempt to convert the column's
 
1350
    default value (if any) to the new type, as well as any constraints
 
1351
    that involve the column.  But these conversions might fail, or might
 
1352
    produce surprising results.  It's often best to drop any constraints
 
1353
    on the column before altering its type, and then add back suitably
 
1354
    modified constraints afterwards.
 
1355
   </para>
 
1356
  </sect2>
 
1357
 
 
1358
  <sect2>
 
1359
   <title>Renaming a Column</title>
 
1360
 
 
1361
   <indexterm>
 
1362
    <primary>column</primary>
 
1363
    <secondary>renaming</secondary>
 
1364
   </indexterm>
 
1365
 
 
1366
   <para>
 
1367
    To rename a column:
 
1368
<programlisting>
 
1369
ALTER TABLE products RENAME COLUMN product_no TO product_number;
 
1370
</programlisting>
 
1371
   </para>
 
1372
  </sect2>
 
1373
 
 
1374
  <sect2>
 
1375
   <title>Renaming a Table</title>
 
1376
 
 
1377
   <indexterm>
 
1378
    <primary>table</primary>
 
1379
    <secondary>renaming</secondary>
 
1380
   </indexterm>
 
1381
 
 
1382
   <para>
 
1383
    To rename a table:
 
1384
<programlisting>
 
1385
ALTER TABLE products RENAME TO items;
 
1386
</programlisting>
 
1387
   </para>
 
1388
  </sect2>
 
1389
 </sect1>
 
1390
 
 
1391
 <sect1 id="ddl-priv">
 
1392
  <title>Privileges</title>
 
1393
 
 
1394
  <indexterm zone="ddl-priv">
 
1395
   <primary>privilege</primary>
 
1396
  </indexterm>
 
1397
 
 
1398
  <indexterm>
 
1399
   <primary>permission</primary>
 
1400
   <see>privilege</see>
 
1401
  </indexterm>
 
1402
 
 
1403
  <indexterm zone="ddl-priv">
 
1404
   <primary>owner</primary>
 
1405
  </indexterm>
 
1406
 
 
1407
  <indexterm zone="ddl-priv">
 
1408
   <primary>GRANT</primary>
 
1409
  </indexterm>
 
1410
 
 
1411
  <indexterm zone="ddl-priv">
 
1412
   <primary>REVOKE</primary>
 
1413
  </indexterm>
 
1414
 
 
1415
  <para>
 
1416
   When an object is created, it is assigned an owner. The
 
1417
   owner is normally the role that executed the creation statement.
 
1418
   For most kinds of objects, the initial state is that only the owner
 
1419
   (or a superuser) can do anything with the object. To allow
 
1420
   other roles to use it, <firstterm>privileges</firstterm> must be
 
1421
   granted.
 
1422
   There are several different kinds of privilege: <literal>SELECT</>,
 
1423
   <literal>INSERT</>, <literal>UPDATE</>, <literal>DELETE</>,
 
1424
   <literal>TRUNCATE</>, <literal>REFERENCES</>, <literal>TRIGGER</>,
 
1425
   <literal>CREATE</>, <literal>CONNECT</>, <literal>TEMPORARY</>,
 
1426
   <literal>EXECUTE</>, and <literal>USAGE</>.
 
1427
   For more information on the different types of privileges supported by
 
1428
   <productname>PostgreSQL</productname>, see the
 
1429
   <xref linkend="sql-grant"> reference page.
 
1430
  </para>
 
1431
 
 
1432
  <para>
 
1433
   There are several different privileges: <literal>SELECT</>,
 
1434
   <literal>INSERT</>, <literal>UPDATE</>, <literal>DELETE</>,
 
1435
   <literal>TRUNCATE</>, <literal>REFERENCES</>, <literal>TRIGGER</>,
 
1436
   <literal>CREATE</>, <literal>CONNECT</>, <literal>TEMPORARY</>,
 
1437
   <literal>EXECUTE</>, and <literal>USAGE</>.
 
1438
   The privileges applicable to a particular
 
1439
   object vary depending on the object's type (table, function, etc).
 
1440
   For complete information on the different types of privileges
 
1441
   supported by <productname>PostgreSQL</productname>, refer to the
 
1442
   <xref linkend="sql-grant"> reference
 
1443
   page.  The following sections and chapters will also show you how
 
1444
   those privileges are used.
 
1445
  </para>
 
1446
 
 
1447
  <para>
 
1448
   The right to modify or destroy an object is always the privilege of
 
1449
   the owner only.
 
1450
  </para>
 
1451
 
 
1452
  <para>
 
1453
   An object can be assigned to a new owner with an <command>ALTER</command>
 
1454
   command of the appropriate kind for the object, e.g. <xref
 
1455
   linkend="sql-altertable">.  Superusers can always do
 
1456
   this; ordinary roles can only do it if they are both the current owner
 
1457
   of the object (or a member of the owning role) and a member of the new
 
1458
   owning role.
 
1459
  </para>
 
1460
 
 
1461
  <para>
 
1462
   To assign privileges, the <command>GRANT</command> command is
 
1463
   used. For example, if <literal>joe</literal> is an existing user, and
 
1464
   <literal>accounts</literal> is an existing table, the privilege to
 
1465
   update the table can be granted with:
 
1466
<programlisting>
 
1467
GRANT UPDATE ON accounts TO joe;
 
1468
</programlisting>
 
1469
   Writing <literal>ALL</literal> in place of a specific privilege grants all
 
1470
   privileges that are relevant for the object type.
 
1471
  </para>
 
1472
 
 
1473
  <para>
 
1474
   The special <quote>user</quote> name <literal>PUBLIC</literal> can
 
1475
   be used to grant a privilege to every user on the system.  Also,
 
1476
   <quote>group</> roles can be set up to help manage privileges when
 
1477
   there are many users of a database &mdash; for details see
 
1478
   <xref linkend="user-manag">.
 
1479
  </para>
 
1480
 
 
1481
  <para>
 
1482
   To revoke a privilege, use the fittingly named
 
1483
   <command>REVOKE</command> command:
 
1484
<programlisting>
 
1485
REVOKE ALL ON accounts FROM PUBLIC;
 
1486
</programlisting>
 
1487
   The special privileges of the object owner (i.e., the right to do
 
1488
   <command>DROP</>, <command>GRANT</>, <command>REVOKE</>, etc.)
 
1489
   are always implicit in being the owner,
 
1490
   and cannot be granted or revoked.  But the object owner can choose
 
1491
   to revoke his own ordinary privileges, for example to make a
 
1492
   table read-only for himself as well as others.
 
1493
  </para>
 
1494
 
 
1495
  <para>
 
1496
   Ordinarily, only the object's owner (or a superuser) can grant or
 
1497
   revoke privileges on an object.  However, it is possible to grant a
 
1498
   privilege <quote>with grant option</>, which gives the recipient
 
1499
   the right to grant it in turn to others.  If the grant option is
 
1500
   subsequently revoked then all who received the privilege from that
 
1501
   recipient (directly or through a chain of grants) will lose the
 
1502
   privilege.  For details see the <xref linkend="sql-grant"> and
 
1503
   <xref linkend="sql-revoke"> reference pages.
 
1504
  </para>
 
1505
 </sect1>
 
1506
 
 
1507
 <sect1 id="ddl-schemas">
 
1508
  <title>Schemas</title>
 
1509
 
 
1510
  <indexterm zone="ddl-schemas">
 
1511
   <primary>schema</primary>
 
1512
  </indexterm>
 
1513
 
 
1514
  <para>
 
1515
   A <productname>PostgreSQL</productname> database cluster
 
1516
   contains one or more named databases.  Users and groups of users are
 
1517
   shared across the entire cluster, but no other data is shared across
 
1518
   databases.  Any given client connection to the server can access
 
1519
   only the data in a single database, the one specified in the connection
 
1520
   request.
 
1521
  </para>
 
1522
 
 
1523
  <note>
 
1524
   <para>
 
1525
    Users of a cluster do not necessarily have the privilege to access every
 
1526
    database in the cluster.  Sharing of user names means that there
 
1527
    cannot be different users named, say, <literal>joe</> in two databases
 
1528
    in the same cluster; but the system can be configured to allow
 
1529
    <literal>joe</> access to only some of the databases.
 
1530
   </para>
 
1531
  </note>
 
1532
 
 
1533
  <para>
 
1534
   A database contains one or more named <firstterm>schemas</>, which
 
1535
   in turn contain tables.  Schemas also contain other kinds of named
 
1536
   objects, including data types, functions, and operators.  The same
 
1537
   object name can be used in different schemas without conflict; for
 
1538
   example, both <literal>schema1</> and <literal>myschema</> can
 
1539
   contain tables named <literal>mytable</>.  Unlike databases,
 
1540
   schemas are not rigidly separated: a user can access objects in any
 
1541
   of the schemas in the database he is connected to, if he has
 
1542
   privileges to do so.
 
1543
  </para>
 
1544
 
 
1545
  <para>
 
1546
   There are several reasons why one might want to use schemas:
 
1547
 
 
1548
   <itemizedlist>
 
1549
    <listitem>
 
1550
     <para>
 
1551
      To allow many users to use one database without interfering with
 
1552
      each other.
 
1553
     </para>
 
1554
    </listitem>
 
1555
 
 
1556
    <listitem>
 
1557
     <para>
 
1558
      To organize database objects into logical groups to make them
 
1559
      more manageable.
 
1560
     </para>
 
1561
    </listitem>
 
1562
 
 
1563
    <listitem>
 
1564
     <para>
 
1565
      Third-party applications can be put into separate schemas so
 
1566
      they do not collide with the names of other objects.
 
1567
     </para>
 
1568
    </listitem>
 
1569
   </itemizedlist>
 
1570
 
 
1571
   Schemas are analogous to directories at the operating system level,
 
1572
   except that schemas cannot be nested.
 
1573
  </para>
 
1574
 
 
1575
  <sect2 id="ddl-schemas-create">
 
1576
   <title>Creating a Schema</title>
 
1577
 
 
1578
   <indexterm zone="ddl-schemas-create">
 
1579
    <primary>schema</primary>
 
1580
    <secondary>creating</secondary>
 
1581
   </indexterm>
 
1582
 
 
1583
   <para>
 
1584
    To create a schema, use the <xref linkend="sql-createschema">
 
1585
    command.  Give the schema a name
 
1586
    of your choice.  For example:
 
1587
<programlisting>
 
1588
CREATE SCHEMA myschema;
 
1589
</programlisting>
 
1590
   </para>
 
1591
 
 
1592
   <indexterm>
 
1593
    <primary>qualified name</primary>
 
1594
   </indexterm>
 
1595
 
 
1596
   <indexterm>
 
1597
    <primary>name</primary>
 
1598
    <secondary>qualified</secondary>
 
1599
   </indexterm>
 
1600
 
 
1601
   <para>
 
1602
    To create or access objects in a schema, write a
 
1603
    <firstterm>qualified name</> consisting of the schema name and
 
1604
    table name separated by a dot:
 
1605
<synopsis>
 
1606
<replaceable>schema</><literal>.</><replaceable>table</>
 
1607
</synopsis>
 
1608
    This works anywhere a table name is expected, including the table
 
1609
    modification commands and the data access commands discussed in
 
1610
    the following chapters.
 
1611
    (For brevity we will speak of tables only, but the same ideas apply
 
1612
    to other kinds of named objects, such as types and functions.)
 
1613
   </para>
 
1614
 
 
1615
   <para>
 
1616
    Actually, the even more general syntax
 
1617
<synopsis>
 
1618
<replaceable>database</><literal>.</><replaceable>schema</><literal>.</><replaceable>table</>
 
1619
</synopsis>
 
1620
    can be used too, but at present this is just for <foreignphrase>pro
 
1621
    forma</> compliance with the SQL standard.  If you write a database name,
 
1622
    it must be the same as the database you are connected to.
 
1623
   </para>
 
1624
 
 
1625
   <para>
 
1626
    So to create a table in the new schema, use:
 
1627
<programlisting>
 
1628
CREATE TABLE myschema.mytable (
 
1629
 ...
 
1630
);
 
1631
</programlisting>
 
1632
   </para>
 
1633
 
 
1634
   <indexterm>
 
1635
    <primary>schema</primary>
 
1636
    <secondary>removing</secondary>
 
1637
   </indexterm>
 
1638
 
 
1639
   <para>
 
1640
    To drop a schema if it's empty (all objects in it have been
 
1641
    dropped), use:
 
1642
<programlisting>
 
1643
DROP SCHEMA myschema;
 
1644
</programlisting>
 
1645
    To drop a schema including all contained objects, use:
 
1646
<programlisting>
 
1647
DROP SCHEMA myschema CASCADE;
 
1648
</programlisting>
 
1649
    See <xref linkend="ddl-depend"> for a description of the general
 
1650
    mechanism behind this.
 
1651
   </para>
 
1652
 
 
1653
   <para>
 
1654
    Often you will want to create a schema owned by someone else
 
1655
    (since this is one of the ways to restrict the activities of your
 
1656
    users to well-defined namespaces).  The syntax for that is:
 
1657
<programlisting>
 
1658
CREATE SCHEMA <replaceable>schemaname</replaceable> AUTHORIZATION <replaceable>username</replaceable>;
 
1659
</programlisting>
 
1660
    You can even omit the schema name, in which case the schema name
 
1661
    will be the same as the user name.  See <xref
 
1662
    linkend="ddl-schemas-patterns"> for how this can be useful.
 
1663
   </para>
 
1664
 
 
1665
   <para>
 
1666
    Schema names beginning with <literal>pg_</> are reserved for
 
1667
    system purposes and cannot be created by users.
 
1668
   </para>
 
1669
  </sect2>
 
1670
 
 
1671
  <sect2 id="ddl-schemas-public">
 
1672
   <title>The Public Schema</title>
 
1673
 
 
1674
   <indexterm zone="ddl-schemas-public">
 
1675
    <primary>schema</primary>
 
1676
    <secondary>public</secondary>
 
1677
   </indexterm>
 
1678
 
 
1679
   <para>
 
1680
    In the previous sections we created tables without specifying any
 
1681
    schema names.  By default such tables (and other objects) are
 
1682
    automatically put into a schema named <quote>public</quote>.  Every new
 
1683
    database contains such a schema.  Thus, the following are equivalent:
 
1684
<programlisting>
 
1685
CREATE TABLE products ( ... );
 
1686
</programlisting>
 
1687
    and:
 
1688
<programlisting>
 
1689
CREATE TABLE public.products ( ... );
 
1690
</programlisting>
 
1691
   </para>
 
1692
  </sect2>
 
1693
 
 
1694
  <sect2 id="ddl-schemas-path">
 
1695
   <title>The Schema Search Path</title>
 
1696
 
 
1697
   <indexterm>
 
1698
    <primary>search path</primary>
 
1699
   </indexterm>
 
1700
 
 
1701
   <indexterm>
 
1702
    <primary>unqualified name</primary>
 
1703
   </indexterm>
 
1704
 
 
1705
   <indexterm>
 
1706
    <primary>name</primary>
 
1707
    <secondary>unqualified</secondary>
 
1708
   </indexterm>
 
1709
 
 
1710
   <para>
 
1711
    Qualified names are tedious to write, and it's often best not to
 
1712
    wire a particular schema name into applications anyway.  Therefore
 
1713
    tables are often referred to by <firstterm>unqualified names</>,
 
1714
    which consist of just the table name.  The system determines which table
 
1715
    is meant by following a <firstterm>search path</>, which is a list
 
1716
    of schemas to look in.  The first matching table in the search path
 
1717
    is taken to be the one wanted.  If there is no match in the search
 
1718
    path, an error is reported, even if matching table names exist
 
1719
    in other schemas in the database.
 
1720
   </para>
 
1721
 
 
1722
   <indexterm>
 
1723
    <primary>schema</primary>
 
1724
    <secondary>current</secondary>
 
1725
   </indexterm>
 
1726
 
 
1727
   <para>
 
1728
    The first schema named in the search path is called the current schema.
 
1729
    Aside from being the first schema searched, it is also the schema in
 
1730
    which new tables will be created if the <command>CREATE TABLE</>
 
1731
    command does not specify a schema name.
 
1732
   </para>
 
1733
 
 
1734
   <indexterm>
 
1735
    <primary>search_path</primary>
 
1736
   </indexterm>
 
1737
 
 
1738
   <para>
 
1739
    To show the current search path, use the following command:
 
1740
<programlisting>
 
1741
SHOW search_path;
 
1742
</programlisting>
 
1743
    In the default setup this returns:
 
1744
<screen>
 
1745
 search_path
 
1746
--------------
 
1747
 "$user",public
 
1748
</screen>
 
1749
    The first element specifies that a schema with the same name as
 
1750
    the current user is to be searched.  If no such schema exists,
 
1751
    the entry is ignored.  The second element refers to the
 
1752
    public schema that we have seen already.
 
1753
   </para>
 
1754
 
 
1755
   <para>
 
1756
    The first schema in the search path that exists is the default
 
1757
    location for creating new objects.  That is the reason that by
 
1758
    default objects are created in the public schema.  When objects
 
1759
    are referenced in any other context without schema qualification
 
1760
    (table modification, data modification, or query commands) the
 
1761
    search path is traversed until a matching object is found.
 
1762
    Therefore, in the default configuration, any unqualified access
 
1763
    again can only refer to the public schema.
 
1764
   </para>
 
1765
 
 
1766
   <para>
 
1767
    To put our new schema in the path, we use:
 
1768
<programlisting>
 
1769
SET search_path TO myschema,public;
 
1770
</programlisting>
 
1771
    (We omit the <literal>$user</literal> here because we have no
 
1772
    immediate need for it.)  And then we can access the table without
 
1773
    schema qualification:
 
1774
<programlisting>
 
1775
DROP TABLE mytable;
 
1776
</programlisting>
 
1777
    Also, since <literal>myschema</literal> is the first element in
 
1778
    the path, new objects would by default be created in it.
 
1779
   </para>
 
1780
 
 
1781
   <para>
 
1782
    We could also have written:
 
1783
<programlisting>
 
1784
SET search_path TO myschema;
 
1785
</programlisting>
 
1786
    Then we no longer have access to the public schema without
 
1787
    explicit qualification.  There is nothing special about the public
 
1788
    schema except that it exists by default.  It can be dropped, too.
 
1789
   </para>
 
1790
 
 
1791
   <para>
 
1792
    See also <xref linkend="functions-info"> for other ways to manipulate
 
1793
    the schema search path.
 
1794
   </para>
 
1795
 
 
1796
   <para>
 
1797
    The search path works in the same way for data type names, function names,
 
1798
    and operator names as it does for table names.  Data type and function
 
1799
    names can be qualified in exactly the same way as table names.  If you
 
1800
    need to write a qualified operator name in an expression, there is a
 
1801
    special provision: you must write
 
1802
<synopsis>
 
1803
<literal>OPERATOR(</><replaceable>schema</><literal>.</><replaceable>operator</><literal>)</>
 
1804
</synopsis>
 
1805
    This is needed to avoid syntactic ambiguity.  An example is:
 
1806
<programlisting>
 
1807
SELECT 3 OPERATOR(pg_catalog.+) 4;
 
1808
</programlisting>
 
1809
    In practice one usually relies on the search path for operators,
 
1810
    so as not to have to write anything so ugly as that.
 
1811
   </para>
 
1812
  </sect2>
 
1813
 
 
1814
  <sect2 id="ddl-schemas-priv">
 
1815
   <title>Schemas and Privileges</title>
 
1816
 
 
1817
   <indexterm zone="ddl-schemas-priv">
 
1818
    <primary>privilege</primary>
 
1819
    <secondary sortas="schemas">for schemas</secondary>
 
1820
   </indexterm>
 
1821
 
 
1822
   <para>
 
1823
    By default, users cannot access any objects in schemas they do not
 
1824
    own.  To allow that, the owner of the schema must grant the
 
1825
    <literal>USAGE</literal> privilege on the schema.  To allow users
 
1826
    to make use of the objects in the schema, additional privileges
 
1827
    might need to be granted, as appropriate for the object.
 
1828
   </para>
 
1829
 
 
1830
   <para>
 
1831
    A user can also be allowed to create objects in someone else's
 
1832
    schema.  To allow that, the <literal>CREATE</literal> privilege on
 
1833
    the schema needs to be granted.  Note that by default, everyone
 
1834
    has <literal>CREATE</literal> and <literal>USAGE</literal> privileges on
 
1835
    the schema
 
1836
    <literal>public</literal>.  This allows all users that are able to
 
1837
    connect to a given database to create objects in its
 
1838
    <literal>public</literal> schema.  If you do
 
1839
    not want to allow that, you can revoke that privilege:
 
1840
<programlisting>
 
1841
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
 
1842
</programlisting>
 
1843
    (The first <quote>public</quote> is the schema, the second
 
1844
    <quote>public</quote> means <quote>every user</quote>.  In the
 
1845
    first sense it is an identifier, in the second sense it is a
 
1846
    key word, hence the different capitalization; recall the
 
1847
    guidelines from <xref linkend="sql-syntax-identifiers">.)
 
1848
   </para>
 
1849
  </sect2>
 
1850
 
 
1851
  <sect2 id="ddl-schemas-catalog">
 
1852
   <title>The System Catalog Schema</title>
 
1853
 
 
1854
   <indexterm zone="ddl-schemas-catalog">
 
1855
    <primary>system catalog</primary>
 
1856
    <secondary>schema</secondary>
 
1857
   </indexterm>
 
1858
 
 
1859
   <para>
 
1860
    In addition to <literal>public</> and user-created schemas, each
 
1861
    database contains a <literal>pg_catalog</> schema, which contains
 
1862
    the system tables and all the built-in data types, functions, and
 
1863
    operators.  <literal>pg_catalog</> is always effectively part of
 
1864
    the search path.  If it is not named explicitly in the path then
 
1865
    it is implicitly searched <emphasis>before</> searching the path's
 
1866
    schemas.  This ensures that built-in names will always be
 
1867
    findable.  However, you can explicitly place
 
1868
    <literal>pg_catalog</> at the end of your search path if you
 
1869
    prefer to have user-defined names override built-in names.
 
1870
   </para>
 
1871
 
 
1872
   <para>
 
1873
    In <productname>PostgreSQL</productname> versions before 7.3,
 
1874
    table names beginning with <literal>pg_</> were reserved.  This is
 
1875
    no longer true: you can create such a table name if you wish, in
 
1876
    any non-system schema.  However, it's best to continue to avoid
 
1877
    such names, to ensure that you won't suffer a conflict if some
 
1878
    future version defines a system table named the same as your
 
1879
    table.  (With the default search path, an unqualified reference to
 
1880
    your table name would then be resolved as the system table instead.)
 
1881
    System tables will continue to follow the convention of having
 
1882
    names beginning with <literal>pg_</>, so that they will not
 
1883
    conflict with unqualified user-table names so long as users avoid
 
1884
    the <literal>pg_</> prefix.
 
1885
   </para>
 
1886
  </sect2>
 
1887
 
 
1888
  <sect2 id="ddl-schemas-patterns">
 
1889
   <title>Usage Patterns</title>
 
1890
 
 
1891
   <para>
 
1892
    Schemas can be used to organize your data in many ways.  There are
 
1893
    a few usage patterns that are recommended and are easily supported by
 
1894
    the default configuration:
 
1895
    <itemizedlist>
 
1896
     <listitem>
 
1897
      <para>
 
1898
       If you do not create any schemas then all users access the
 
1899
       public schema implicitly.  This simulates the situation where
 
1900
       schemas are not available at all.  This setup is mainly
 
1901
       recommended when there is only a single user or a few cooperating
 
1902
       users in a database.  This setup also allows smooth transition
 
1903
       from the non-schema-aware world.
 
1904
      </para>
 
1905
     </listitem>
 
1906
 
 
1907
     <listitem>
 
1908
      <para>
 
1909
       You can create a schema for each user with the same name as
 
1910
       that user.  Recall that the default search path starts with
 
1911
       <literal>$user</literal>, which resolves to the user name.
 
1912
       Therefore, if each user has a separate schema, they access their
 
1913
       own schemas by default.
 
1914
      </para>
 
1915
 
 
1916
      <para>
 
1917
       If you use this setup then you might also want to revoke access
 
1918
       to the public schema (or drop it altogether), so users are
 
1919
       truly constrained to their own schemas.
 
1920
      </para>
 
1921
     </listitem>
 
1922
 
 
1923
     <listitem>
 
1924
      <para>
 
1925
       To install shared applications (tables to be used by everyone,
 
1926
       additional functions provided by third parties, etc.), put them
 
1927
       into separate schemas.  Remember to grant appropriate
 
1928
       privileges to allow the other users to access them.  Users can
 
1929
       then refer to these additional objects by qualifying the names
 
1930
       with a schema name, or they can put the additional schemas into
 
1931
       their search path, as they choose.
 
1932
      </para>
 
1933
     </listitem>
 
1934
    </itemizedlist>
 
1935
   </para>
 
1936
  </sect2>
 
1937
 
 
1938
  <sect2 id="ddl-schemas-portability">
 
1939
   <title>Portability</title>
 
1940
 
 
1941
   <para>
 
1942
    In the SQL standard, the notion of objects in the same schema
 
1943
    being owned by different users does not exist.  Moreover, some
 
1944
    implementations do not allow you to create schemas that have a
 
1945
    different name than their owner.  In fact, the concepts of schema
 
1946
    and user are nearly equivalent in a database system that
 
1947
    implements only the basic schema support specified in the
 
1948
    standard.  Therefore, many users consider qualified names to
 
1949
    really consist of
 
1950
    <literal><replaceable>username</>.<replaceable>tablename</></literal>.
 
1951
    This is how <productname>PostgreSQL</productname> will effectively
 
1952
    behave if you create a per-user schema for every user.
 
1953
   </para>
 
1954
 
 
1955
   <para>
 
1956
    Also, there is no concept of a <literal>public</> schema in the
 
1957
    SQL standard.  For maximum conformance to the standard, you should
 
1958
    not use (perhaps even remove) the <literal>public</> schema.
 
1959
   </para>
 
1960
 
 
1961
   <para>
 
1962
    Of course, some SQL database systems might not implement schemas
 
1963
    at all, or provide namespace support by allowing (possibly
 
1964
    limited) cross-database access.  If you need to work with those
 
1965
    systems, then maximum portability would be achieved by not using
 
1966
    schemas at all.
 
1967
   </para>
 
1968
  </sect2>
 
1969
 </sect1>
 
1970
 
 
1971
 <sect1 id="ddl-inherit">
 
1972
  <title>Inheritance</title>
 
1973
 
 
1974
  <indexterm>
 
1975
   <primary>inheritance</primary>
 
1976
  </indexterm>
 
1977
 
 
1978
  <indexterm>
 
1979
   <primary>table</primary>
 
1980
   <secondary>inheritance</secondary>
 
1981
  </indexterm>
 
1982
 
 
1983
  <para>
 
1984
   <productname>PostgreSQL</productname> implements table inheritance,
 
1985
   which can be a useful tool for database designers.  (SQL:1999 and
 
1986
   later define a type inheritance feature, which differs in many
 
1987
   respects from the features described here.)
 
1988
  </para>
 
1989
 
 
1990
  <para>
 
1991
   Let's start with an example: suppose we are trying to build a data
 
1992
   model for cities.  Each state has many cities, but only one
 
1993
   capital. We want to be able to quickly retrieve the capital city
 
1994
   for any particular state. This can be done by creating two tables,
 
1995
   one for state capitals and one for cities that are not
 
1996
   capitals. However, what happens when we want to ask for data about
 
1997
   a city, regardless of whether it is a capital or not? The
 
1998
   inheritance feature can help to resolve this problem. We define the
 
1999
   <structname>capitals</structname> table so that it inherits from
 
2000
   <structname>cities</structname>:
 
2001
 
 
2002
<programlisting>
 
2003
CREATE TABLE cities (
 
2004
    name            text,
 
2005
    population      float,
 
2006
    altitude        int     -- in feet
 
2007
);
 
2008
 
 
2009
CREATE TABLE capitals (
 
2010
    state           char(2)
 
2011
) INHERITS (cities);
 
2012
</programlisting>
 
2013
 
 
2014
   In this case, the <structname>capitals</> table <firstterm>inherits</>
 
2015
   all the columns of its parent table, <structname>cities</>. State
 
2016
   capitals also have an extra column, <structfield>state</>, that shows
 
2017
   their state.
 
2018
  </para>
 
2019
 
 
2020
  <para>
 
2021
   In <productname>PostgreSQL</productname>, a table can inherit from
 
2022
   zero or more other tables, and a query can reference either all
 
2023
   rows of a table or all rows of a table plus all of its descendant tables.
 
2024
   The latter behavior is the default.
 
2025
   For example, the following query finds the names of all cities,
 
2026
   including state capitals, that are located at an altitude over
 
2027
   500 feet:
 
2028
 
 
2029
<programlisting>
 
2030
SELECT name, altitude
 
2031
    FROM cities
 
2032
    WHERE altitude &gt; 500;
 
2033
</programlisting>
 
2034
 
 
2035
   Given the sample data from the <productname>PostgreSQL</productname>
 
2036
   tutorial (see <xref linkend="tutorial-sql-intro">), this returns:
 
2037
 
 
2038
<programlisting>
 
2039
   name    | altitude
 
2040
-----------+----------
 
2041
 Las Vegas |     2174
 
2042
 Mariposa  |     1953
 
2043
 Madison   |      845
 
2044
</programlisting>
 
2045
  </para>
 
2046
 
 
2047
  <para>
 
2048
   On the other hand, the following query finds all the cities that
 
2049
   are not state capitals and are situated at an altitude over 500 feet:
 
2050
 
 
2051
<programlisting>
 
2052
SELECT name, altitude
 
2053
    FROM ONLY cities
 
2054
    WHERE altitude &gt; 500;
 
2055
 
 
2056
   name    | altitude
 
2057
-----------+----------
 
2058
 Las Vegas |     2174
 
2059
 Mariposa  |     1953
 
2060
</programlisting>
 
2061
  </para>
 
2062
 
 
2063
  <para>
 
2064
   Here the <literal>ONLY</literal> keyword indicates that the query
 
2065
   should apply only to <structname>cities</structname>, and not any tables
 
2066
   below <structname>cities</structname> in the inheritance hierarchy.  Many
 
2067
   of the commands that we have already discussed &mdash;
 
2068
   <command>SELECT</command>, <command>UPDATE</command> and
 
2069
   <command>DELETE</command> &mdash; support the
 
2070
   <literal>ONLY</literal> keyword.
 
2071
  </para>
 
2072
 
 
2073
  <para>
 
2074
   In some cases you might wish to know which table a particular row
 
2075
   originated from. There is a system column called
 
2076
   <structfield>tableoid</structfield> in each table which can tell you the
 
2077
   originating table:
 
2078
 
 
2079
<programlisting>
 
2080
SELECT c.tableoid, c.name, c.altitude
 
2081
FROM cities c
 
2082
WHERE c.altitude &gt; 500;
 
2083
</programlisting>
 
2084
 
 
2085
   which returns:
 
2086
 
 
2087
<programlisting>
 
2088
 tableoid |   name    | altitude
 
2089
----------+-----------+----------
 
2090
   139793 | Las Vegas |     2174
 
2091
   139793 | Mariposa  |     1953
 
2092
   139798 | Madison   |      845
 
2093
</programlisting>
 
2094
 
 
2095
   (If you try to reproduce this example, you will probably get
 
2096
   different numeric OIDs.)  By doing a join with
 
2097
   <structname>pg_class</> you can see the actual table names:
 
2098
 
 
2099
<programlisting>
 
2100
SELECT p.relname, c.name, c.altitude
 
2101
FROM cities c, pg_class p
 
2102
WHERE c.altitude &gt; 500 AND c.tableoid = p.oid;
 
2103
</programlisting>
 
2104
 
 
2105
   which returns:
 
2106
 
 
2107
<programlisting>
 
2108
 relname  |   name    | altitude
 
2109
----------+-----------+----------
 
2110
 cities   | Las Vegas |     2174
 
2111
 cities   | Mariposa  |     1953
 
2112
 capitals | Madison   |      845
 
2113
</programlisting>
 
2114
  </para>
 
2115
 
 
2116
  <para>
 
2117
   Inheritance does not automatically propagate data from
 
2118
   <command>INSERT</command> or <command>COPY</command> commands to
 
2119
   other tables in the inheritance hierarchy. In our example, the
 
2120
   following <command>INSERT</command> statement will fail:
 
2121
<programlisting>
 
2122
INSERT INTO cities (name, population, altitude, state)
 
2123
VALUES ('New York', NULL, NULL, 'NY');
 
2124
</programlisting>
 
2125
   We might hope that the data would somehow be routed to the
 
2126
   <structname>capitals</structname> table, but this does not happen:
 
2127
   <command>INSERT</command> always inserts into exactly the table
 
2128
   specified.  In some cases it is possible to redirect the insertion
 
2129
   using a rule (see <xref linkend="rules">).  However that does not
 
2130
   help for the above case because the <structname>cities</> table
 
2131
   does not contain the column <structfield>state</>, and so the
 
2132
   command will be rejected before the rule can be applied.
 
2133
  </para>
 
2134
 
 
2135
  <para>
 
2136
   All check constraints and not-null constraints on a parent table are
 
2137
   automatically inherited by its children.  Other types of constraints
 
2138
   (unique, primary key, and foreign key constraints) are not inherited.
 
2139
  </para>
 
2140
 
 
2141
  <para>
 
2142
   A table can inherit from more than one parent table, in which case it has
 
2143
   the union of the columns defined by the parent tables.  Any columns
 
2144
   declared in the child table's definition are added to these.  If the
 
2145
   same column name appears in multiple parent tables, or in both a parent
 
2146
   table and the child's definition, then these columns are <quote>merged</>
 
2147
   so that there is only one such column in the child table.  To be merged,
 
2148
   columns must have the same data types, else an error is raised.  The
 
2149
   merged column will have copies of all the check constraints coming from
 
2150
   any one of the column definitions it came from, and will be marked not-null
 
2151
   if any of them are.
 
2152
  </para>
 
2153
 
 
2154
  <para>
 
2155
   Table inheritance is typically established when the child table is
 
2156
   created, using the <literal>INHERITS</> clause of the
 
2157
   <xref linkend="sql-createtable">
 
2158
   statement.
 
2159
   Alternatively, a table which is already defined in a compatible way can
 
2160
   have a new parent relationship added, using the <literal>INHERIT</literal>
 
2161
   variant of <xref linkend="sql-altertable">.
 
2162
   To do this the new child table must already include columns with
 
2163
   the same names and types as the columns of the parent. It must also include
 
2164
   check constraints with the same names and check expressions as those of the
 
2165
   parent. Similarly an inheritance link can be removed from a child using the
 
2166
   <literal>NO INHERIT</literal> variant of <command>ALTER TABLE</>.
 
2167
   Dynamically adding and removing inheritance links like this can be useful
 
2168
   when the inheritance relationship is being used for table
 
2169
   partitioning (see <xref linkend="ddl-partitioning">).
 
2170
  </para>
 
2171
 
 
2172
  <para>
 
2173
   One convenient way to create a compatible table that will later be made
 
2174
   a new child is to use the <literal>LIKE</literal> clause in <command>CREATE
 
2175
   TABLE</command>. This creates a new table with the same columns as
 
2176
   the source table. If there are any <literal>CHECK</literal>
 
2177
   constraints defined on the source table, the <literal>INCLUDING
 
2178
   CONSTRAINTS</literal> option to <literal>LIKE</literal> should be
 
2179
   specified, as the new child must have constraints matching the parent
 
2180
   to be considered compatible.
 
2181
  </para>
 
2182
 
 
2183
  <para>
 
2184
   A parent table cannot be dropped while any of its children remain. Neither
 
2185
   can columns or check constraints of child tables be dropped or altered
 
2186
   if they are inherited
 
2187
   from any parent tables. If you wish to remove a table and all of its
 
2188
   descendants, one easy way is to drop the parent table with the
 
2189
   <literal>CASCADE</literal> option.
 
2190
  </para>
 
2191
 
 
2192
  <para>
 
2193
   <xref linkend="sql-altertable"> will
 
2194
   propagate any changes in column data definitions and check
 
2195
   constraints down the inheritance hierarchy.  Again, dropping
 
2196
   columns that are depended on by other tables is only possible when using
 
2197
   the <literal>CASCADE</literal> option. <command>ALTER
 
2198
   TABLE</command> follows the same rules for duplicate column merging
 
2199
   and rejection that apply during <command>CREATE TABLE</command>.
 
2200
  </para>
 
2201
 
 
2202
  <para>
 
2203
   Note how table access permissions are handled.  Querying a parent
 
2204
   table can automatically access data in child tables without further
 
2205
   access privilege checking.  This preserves the appearance that the
 
2206
   data is (also) in the parent table.  Accessing the child tables
 
2207
   directly is, however, not automatically allowed and would require
 
2208
   further privileges to be granted.
 
2209
  </para>
 
2210
 
 
2211
 <sect2 id="ddl-inherit-caveats">
 
2212
  <title>Caveats</title>
 
2213
 
 
2214
  <para>
 
2215
   Note that not all SQL commands are able to work on
 
2216
   inheritance hierarchies.  Commands that are used for data querying,
 
2217
   data modification, or schema modification
 
2218
   (e.g., <literal>SELECT</literal>, <literal>UPDATE</literal>, <literal>DELETE</literal>,
 
2219
   most variants of <literal>ALTER TABLE</literal>, but
 
2220
   not <literal>INSERT</literal> and <literal>ALTER TABLE ...
 
2221
   RENAME</literal>) typically default to including child tables and
 
2222
   support the <literal>ONLY</literal> notation to exclude them.
 
2223
   Commands that do database maintenance and tuning
 
2224
   (e.g., <literal>REINDEX</literal>, <literal>VACUUM</literal>)
 
2225
   typically only work on individual, physical tables and do no
 
2226
   support recursing over inheritance hierarchies.  The respective
 
2227
   behavior of each individual command is documented in the reference
 
2228
   part (<xref linkend="sql-commands">).
 
2229
  </para>
 
2230
 
 
2231
  <para>
 
2232
   A serious limitation of the inheritance feature is that indexes (including
 
2233
   unique constraints) and foreign key constraints only apply to single
 
2234
   tables, not to their inheritance children. This is true on both the
 
2235
   referencing and referenced sides of a foreign key constraint. Thus,
 
2236
   in the terms of the above example:
 
2237
 
 
2238
   <itemizedlist>
 
2239
    <listitem>
 
2240
     <para>
 
2241
      If we declared <structname>cities</>.<structfield>name</> to be
 
2242
      <literal>UNIQUE</> or a <literal>PRIMARY KEY</>, this would not stop the
 
2243
      <structname>capitals</> table from having rows with names duplicating
 
2244
      rows in <structname>cities</>.  And those duplicate rows would by
 
2245
      default show up in queries from <structname>cities</>.  In fact, by
 
2246
      default <structname>capitals</> would have no unique constraint at all,
 
2247
      and so could contain multiple rows with the same name.
 
2248
      You could add a unique constraint to <structname>capitals</>, but this
 
2249
      would not prevent duplication compared to <structname>cities</>.
 
2250
     </para>
 
2251
    </listitem>
 
2252
 
 
2253
    <listitem>
 
2254
     <para>
 
2255
      Similarly, if we were to specify that
 
2256
      <structname>cities</>.<structfield>name</> <literal>REFERENCES</> some
 
2257
      other table, this constraint would not automatically propagate to
 
2258
      <structname>capitals</>.  In this case you could work around it by
 
2259
      manually adding the same <literal>REFERENCES</> constraint to
 
2260
      <structname>capitals</>.
 
2261
     </para>
 
2262
    </listitem>
 
2263
 
 
2264
    <listitem>
 
2265
     <para>
 
2266
      Specifying that another table's column <literal>REFERENCES
 
2267
      cities(name)</> would allow the other table to contain city names, but
 
2268
      not capital names.  There is no good workaround for this case.
 
2269
     </para>
 
2270
    </listitem>
 
2271
   </itemizedlist>
 
2272
 
 
2273
   These deficiencies will probably be fixed in some future release,
 
2274
   but in the meantime considerable care is needed in deciding whether
 
2275
   inheritance is useful for your application.
 
2276
  </para>
 
2277
 
 
2278
  <note>
 
2279
   <title>Deprecated</title>
 
2280
   <para>
 
2281
     In releases of <productname>PostgreSQL</productname> prior to 7.1, the
 
2282
     default behavior was not to include child tables in queries. This was
 
2283
     found to be error prone and also in violation of the SQL
 
2284
     standard.  You can get the pre-7.1 behavior by turning off the
 
2285
     <xref linkend="guc-sql-inheritance"> configuration
 
2286
     option.
 
2287
   </para>
 
2288
  </note>
 
2289
 
 
2290
   </sect2>
 
2291
  </sect1>
 
2292
 
 
2293
  <sect1 id="ddl-partitioning">
 
2294
   <title>Partitioning</title>
 
2295
 
 
2296
   <indexterm>
 
2297
    <primary>partitioning</primary>
 
2298
   </indexterm>
 
2299
 
 
2300
   <indexterm>
 
2301
    <primary>table</primary>
 
2302
    <secondary>partitioning</secondary>
 
2303
   </indexterm>
 
2304
 
 
2305
   <para>
 
2306
    <productname>PostgreSQL</productname> supports basic table
 
2307
    partitioning. This section describes why and how to implement
 
2308
    partitioning as part of your database design.
 
2309
   </para>
 
2310
 
 
2311
   <sect2 id="ddl-partitioning-overview">
 
2312
     <title>Overview</title>
 
2313
 
 
2314
   <para>
 
2315
    Partitioning refers to splitting what is logically one large table
 
2316
    into smaller physical pieces.
 
2317
    Partitioning can provide several benefits:
 
2318
   <itemizedlist>
 
2319
    <listitem>
 
2320
     <para>
 
2321
      Query performance can be improved dramatically in certain situations,
 
2322
      particularly when most of the heavily accessed rows of the table are in a
 
2323
      single partition or a small number of partitions.  The partitioning
 
2324
      substitutes for leading columns of indexes, reducing index size and
 
2325
      making it more likely that the heavily-used parts of the indexes
 
2326
      fit in memory.
 
2327
     </para>
 
2328
    </listitem>
 
2329
 
 
2330
    <listitem>
 
2331
     <para>
 
2332
      When queries or updates access a large percentage of a single
 
2333
      partition, performance can be improved by taking advantage
 
2334
      of sequential scan of that partition instead of using an
 
2335
      index and random access reads scattered across the whole table.
 
2336
     </para>
 
2337
    </listitem>
 
2338
 
 
2339
    <listitem>
 
2340
     <para>
 
2341
      Bulk loads and deletes can be accomplished by adding or removing
 
2342
      partitions, if that requirement is planned into the partitioning design.
 
2343
      <command>ALTER TABLE</> is far faster than a bulk operation.
 
2344
      It also entirely avoids the <command>VACUUM</command>
 
2345
      overhead caused by a bulk <command>DELETE</>.
 
2346
     </para>
 
2347
    </listitem>
 
2348
 
 
2349
    <listitem>
 
2350
     <para>
 
2351
      Seldom-used data can be migrated to cheaper and slower storage media.
 
2352
     </para>
 
2353
    </listitem>
 
2354
   </itemizedlist>
 
2355
 
 
2356
    The benefits will normally be worthwhile only when a table would
 
2357
    otherwise be very large. The exact point at which a table will
 
2358
    benefit from partitioning depends on the application, although a
 
2359
    rule of thumb is that the size of the table should exceed the physical
 
2360
    memory of the database server.
 
2361
   </para>
 
2362
 
 
2363
   <para>
 
2364
    Currently, <productname>PostgreSQL</productname> supports partitioning
 
2365
    via table inheritance.  Each partition must be created as a child
 
2366
    table of a single parent table.  The parent table itself is normally
 
2367
    empty; it exists just to represent the entire data set.  You should be
 
2368
    familiar with inheritance (see <xref linkend="ddl-inherit">) before
 
2369
    attempting to set up partitioning.
 
2370
   </para>
 
2371
 
 
2372
   <para>
 
2373
    The following forms of partitioning can be implemented in
 
2374
    <productname>PostgreSQL</productname>:
 
2375
 
 
2376
    <variablelist>
 
2377
     <varlistentry>
 
2378
      <term>Range Partitioning</term>
 
2379
 
 
2380
      <listitem>
 
2381
       <para>
 
2382
        The table is partitioned into <quote>ranges</quote> defined
 
2383
        by a key column or set of columns, with no overlap between
 
2384
        the ranges of values assigned to different partitions.  For
 
2385
        example one might partition by date ranges, or by ranges of
 
2386
        identifiers for particular business objects.
 
2387
       </para>
 
2388
      </listitem>
 
2389
     </varlistentry>
 
2390
 
 
2391
     <varlistentry>
 
2392
      <term>List Partitioning</term>
 
2393
 
 
2394
      <listitem>
 
2395
       <para>
 
2396
        The table is partitioned by explicitly listing which key values
 
2397
        appear in each partition.
 
2398
       </para>
 
2399
      </listitem>
 
2400
     </varlistentry>
 
2401
    </variablelist>
 
2402
   </para>
 
2403
   </sect2>
 
2404
 
 
2405
   <sect2 id="ddl-partitioning-implementation">
 
2406
     <title>Implementing Partitioning</title>
 
2407
 
 
2408
    <para>
 
2409
     To set up a partitioned table, do the following:
 
2410
     <orderedlist spacing="compact">
 
2411
      <listitem>
 
2412
       <para>
 
2413
        Create the <quote>master</quote> table, from which all of the
 
2414
        partitions will inherit.
 
2415
       </para>
 
2416
       <para>
 
2417
        This table will contain no data.  Do not define any check
 
2418
        constraints on this table, unless you intend them to
 
2419
        be applied equally to all partitions.  There is no point
 
2420
        in defining any indexes or unique constraints on it, either.
 
2421
       </para>
 
2422
      </listitem>
 
2423
 
 
2424
      <listitem>
 
2425
       <para>
 
2426
        Create several <quote>child</quote> tables that each inherit from
 
2427
        the master table.  Normally, these tables will not add any columns
 
2428
        to the set inherited from the master.
 
2429
       </para>
 
2430
 
 
2431
       <para>
 
2432
        We will refer to the child tables as partitions, though they
 
2433
        are in every way normal <productname>PostgreSQL</> tables.
 
2434
       </para>
 
2435
      </listitem>
 
2436
 
 
2437
      <listitem>
 
2438
       <para>
 
2439
        Add table constraints to the partition tables to define the
 
2440
        allowed key values in each partition.
 
2441
       </para>
 
2442
 
 
2443
       <para>
 
2444
        Typical examples would be:
 
2445
<programlisting>
 
2446
CHECK ( x = 1 )
 
2447
CHECK ( county IN ( 'Oxfordshire', 'Buckinghamshire', 'Warwickshire' ))
 
2448
CHECK ( outletID &gt;= 100 AND outletID &lt; 200 )
 
2449
</programlisting>
 
2450
        Ensure that the constraints guarantee that there is no overlap
 
2451
        between the key values permitted in different partitions.  A common
 
2452
        mistake is to set up range constraints like:
 
2453
<programlisting>
 
2454
CHECK ( outletID BETWEEN 100 AND 200 )
 
2455
CHECK ( outletID BETWEEN 200 AND 300 )
 
2456
</programlisting>
 
2457
        This is wrong since it is not clear which partition the key value
 
2458
        200 belongs in.
 
2459
       </para>
 
2460
 
 
2461
       <para>
 
2462
        Note that there is no difference in
 
2463
        syntax between range and list partitioning; those terms are
 
2464
        descriptive only.
 
2465
       </para>
 
2466
      </listitem>
 
2467
 
 
2468
      <listitem>
 
2469
       <para>
 
2470
        For each partition, create an index on the key column(s),
 
2471
        as well as any other indexes you might want.  (The key index is
 
2472
        not strictly necessary, but in most scenarios it is helpful.
 
2473
        If you intend the key values to be unique then you should
 
2474
        always create a unique or primary-key constraint for each
 
2475
        partition.)
 
2476
       </para>
 
2477
      </listitem>
 
2478
 
 
2479
      <listitem>
 
2480
       <para>
 
2481
        Optionally, define a trigger or rule to redirect data inserted into
 
2482
        the master table to the appropriate partition.
 
2483
       </para>
 
2484
      </listitem>
 
2485
 
 
2486
      <listitem>
 
2487
       <para>
 
2488
        Ensure that the <xref linkend="guc-constraint-exclusion">
 
2489
        configuration parameter is not disabled in
 
2490
        <filename>postgresql.conf</>.
 
2491
        If it is, queries will not be optimized as desired.
 
2492
       </para>
 
2493
      </listitem>
 
2494
 
 
2495
     </orderedlist>
 
2496
    </para>
 
2497
 
 
2498
    <para>
 
2499
     For example, suppose we are constructing a database for a large
 
2500
     ice cream company. The company measures peak temperatures every
 
2501
     day as well as ice cream sales in each region. Conceptually,
 
2502
     we want a table like:
 
2503
 
 
2504
<programlisting>
 
2505
CREATE TABLE measurement (
 
2506
    city_id         int not null,
 
2507
    logdate         date not null,
 
2508
    peaktemp        int,
 
2509
    unitsales       int
 
2510
);
 
2511
</programlisting>
 
2512
 
 
2513
     We know that most queries will access just the last week's, month's or
 
2514
     quarter's data, since the main use of this table will be to prepare
 
2515
     online reports for management.
 
2516
     To reduce the amount of old data that needs to be stored, we
 
2517
     decide to only keep the most recent 3 years worth of data. At the
 
2518
     beginning of each month we will remove the oldest month's data.
 
2519
    </para>
 
2520
 
 
2521
    <para>
 
2522
     In this situation we can use partitioning to help us meet all of our
 
2523
     different requirements for the measurements table. Following the
 
2524
     steps outlined above, partitioning can be set up as follows:
 
2525
    </para>
 
2526
 
 
2527
    <para>
 
2528
     <orderedlist spacing="compact">
 
2529
      <listitem>
 
2530
       <para>
 
2531
        The master table is the <structname>measurement</> table, declared
 
2532
        exactly as above.
 
2533
       </para>
 
2534
      </listitem>
 
2535
 
 
2536
      <listitem>
 
2537
       <para>
 
2538
        Next we create one partition for each active month:
 
2539
 
 
2540
<programlisting>
 
2541
CREATE TABLE measurement_y2006m02 ( ) INHERITS (measurement);
 
2542
CREATE TABLE measurement_y2006m03 ( ) INHERITS (measurement);
 
2543
...
 
2544
CREATE TABLE measurement_y2007m11 ( ) INHERITS (measurement);
 
2545
CREATE TABLE measurement_y2007m12 ( ) INHERITS (measurement);
 
2546
CREATE TABLE measurement_y2008m01 ( ) INHERITS (measurement);
 
2547
</programlisting>
 
2548
 
 
2549
        Each of the partitions are complete tables in their own right,
 
2550
        but they inherit their definitions from the
 
2551
        <structname>measurement</> table.
 
2552
       </para>
 
2553
 
 
2554
       <para>
 
2555
        This solves one of our problems: deleting old data. Each
 
2556
        month, all we will need to do is perform a <command>DROP
 
2557
        TABLE</command> on the oldest child table and create a new
 
2558
        child table for the new month's data.
 
2559
       </para>
 
2560
      </listitem>
 
2561
 
 
2562
      <listitem>
 
2563
       <para>
 
2564
        We must provide non-overlapping table constraints.  Rather than
 
2565
        just creating the partition tables as above, the table creation
 
2566
        script should really be:
 
2567
 
 
2568
<programlisting>
 
2569
CREATE TABLE measurement_y2006m02 (
 
2570
    CHECK ( logdate &gt;= DATE '2006-02-01' AND logdate &lt; DATE '2006-03-01' )
 
2571
) INHERITS (measurement);
 
2572
CREATE TABLE measurement_y2006m03 (
 
2573
    CHECK ( logdate &gt;= DATE '2006-03-01' AND logdate &lt; DATE '2006-04-01' )
 
2574
) INHERITS (measurement);
 
2575
...
 
2576
CREATE TABLE measurement_y2007m11 (
 
2577
    CHECK ( logdate &gt;= DATE '2007-11-01' AND logdate &lt; DATE '2007-12-01' )
 
2578
) INHERITS (measurement);
 
2579
CREATE TABLE measurement_y2007m12 (
 
2580
    CHECK ( logdate &gt;= DATE '2007-12-01' AND logdate &lt; DATE '2008-01-01' )
 
2581
) INHERITS (measurement);
 
2582
CREATE TABLE measurement_y2008m01 (
 
2583
    CHECK ( logdate &gt;= DATE '2008-01-01' AND logdate &lt; DATE '2008-02-01' )
 
2584
) INHERITS (measurement);
 
2585
</programlisting>
 
2586
       </para>
 
2587
      </listitem>
 
2588
 
 
2589
      <listitem>
 
2590
       <para>
 
2591
        We probably need indexes on the key columns too:
 
2592
 
 
2593
<programlisting>
 
2594
CREATE INDEX measurement_y2006m02_logdate ON measurement_y2006m02 (logdate);
 
2595
CREATE INDEX measurement_y2006m03_logdate ON measurement_y2006m03 (logdate);
 
2596
...
 
2597
CREATE INDEX measurement_y2007m11_logdate ON measurement_y2007m11 (logdate);
 
2598
CREATE INDEX measurement_y2007m12_logdate ON measurement_y2007m12 (logdate);
 
2599
CREATE INDEX measurement_y2008m01_logdate ON measurement_y2008m01 (logdate);
 
2600
</programlisting>
 
2601
 
 
2602
        We choose not to add further indexes at this time.
 
2603
       </para>
 
2604
      </listitem>
 
2605
 
 
2606
      <listitem>
 
2607
       <para>
 
2608
        We want our application to be able to say <literal>INSERT INTO
 
2609
        measurement ...</> and have the data be redirected into the
 
2610
        appropriate partition table.  We can arrange that by attaching
 
2611
        a suitable trigger function to the master table.
 
2612
        If data will be added only to the latest partition, we can
 
2613
        use a very simple trigger function:
 
2614
 
 
2615
<programlisting>
 
2616
CREATE OR REPLACE FUNCTION measurement_insert_trigger()
 
2617
RETURNS TRIGGER AS $$
 
2618
BEGIN
 
2619
    INSERT INTO measurement_y2008m01 VALUES (NEW.*);
 
2620
    RETURN NULL;
 
2621
END;
 
2622
$$
 
2623
LANGUAGE plpgsql;
 
2624
</programlisting>
 
2625
 
 
2626
        After creating the function, we create a trigger which
 
2627
        calls the trigger function:
 
2628
 
 
2629
<programlisting>
 
2630
CREATE TRIGGER insert_measurement_trigger
 
2631
    BEFORE INSERT ON measurement
 
2632
    FOR EACH ROW EXECUTE PROCEDURE measurement_insert_trigger();
 
2633
</programlisting>
 
2634
 
 
2635
        We must redefine the trigger function each month so that it always
 
2636
        points to the current partition.  The trigger definition does
 
2637
        not need to be updated, however.
 
2638
       </para>
 
2639
 
 
2640
       <para>
 
2641
        We might want to insert data and have the server automatically
 
2642
        locate the partition into which the row should be added. We
 
2643
        could do this with a more complex trigger function, for example:
 
2644
 
 
2645
<programlisting>
 
2646
CREATE OR REPLACE FUNCTION measurement_insert_trigger()
 
2647
RETURNS TRIGGER AS $$
 
2648
BEGIN
 
2649
    IF ( NEW.logdate &gt;= DATE '2006-02-01' AND
 
2650
         NEW.logdate &lt; DATE '2006-03-01' ) THEN
 
2651
        INSERT INTO measurement_y2006m02 VALUES (NEW.*);
 
2652
    ELSIF ( NEW.logdate &gt;= DATE '2006-03-01' AND
 
2653
            NEW.logdate &lt; DATE '2006-04-01' ) THEN
 
2654
        INSERT INTO measurement_y2006m03 VALUES (NEW.*);
 
2655
    ...
 
2656
    ELSIF ( NEW.logdate &gt;= DATE '2008-01-01' AND
 
2657
            NEW.logdate &lt; DATE '2008-02-01' ) THEN
 
2658
        INSERT INTO measurement_y2008m01 VALUES (NEW.*);
 
2659
    ELSE
 
2660
        RAISE EXCEPTION 'Date out of range.  Fix the measurement_insert_trigger() function!';
 
2661
    END IF;
 
2662
    RETURN NULL;
 
2663
END;
 
2664
$$
 
2665
LANGUAGE plpgsql;
 
2666
</programlisting>
 
2667
 
 
2668
        The trigger definition is the same as before.
 
2669
        Note that each <literal>IF</literal> test must exactly match the
 
2670
        <literal>CHECK</literal> constraint for its partition.
 
2671
       </para>
 
2672
 
 
2673
       <para>
 
2674
        While this function is more complex than the single-month case,
 
2675
        it doesn't need to be updated as often, since branches can be
 
2676
        added in advance of being needed.
 
2677
       </para>
 
2678
 
 
2679
       <note>
 
2680
        <para>
 
2681
         In practice it might be best to check the newest partition first,
 
2682
         if most inserts go into that partition.  For simplicity we have
 
2683
         shown the trigger's tests in the same order as in other parts
 
2684
         of this example.
 
2685
        </para>
 
2686
       </note>
 
2687
      </listitem>
 
2688
     </orderedlist>
 
2689
    </para>
 
2690
 
 
2691
    <para>
 
2692
     As we can see, a complex partitioning scheme could require a
 
2693
     substantial amount of DDL. In the above example we would be
 
2694
     creating a new partition each month, so it might be wise to write a
 
2695
     script that generates the required DDL automatically.
 
2696
    </para>
 
2697
 
 
2698
   </sect2>
 
2699
 
 
2700
   <sect2 id="ddl-partitioning-managing-partitions">
 
2701
   <title>Managing Partitions</title>
 
2702
 
 
2703
   <para>
 
2704
     Normally the set of partitions established when initially
 
2705
     defining the table are not intended to remain static. It is
 
2706
     common to want to remove old partitions of data and periodically
 
2707
     add new partitions for new data. One of the most important
 
2708
     advantages of partitioning is precisely that it allows this
 
2709
     otherwise painful task to be executed nearly instantaneously by
 
2710
     manipulating the partition structure, rather than physically moving large
 
2711
     amounts of data around.
 
2712
   </para>
 
2713
 
 
2714
   <para>
 
2715
     The simplest option for removing old data is simply to drop the partition
 
2716
     that is no longer necessary:
 
2717
<programlisting>
 
2718
DROP TABLE measurement_y2006m02;
 
2719
</programlisting>
 
2720
     This can very quickly delete millions of records because it doesn't have
 
2721
     to individually delete every record.
 
2722
   </para>
 
2723
 
 
2724
   <para>
 
2725
     Another option that is often preferable is to remove the partition from
 
2726
     the partitioned table but retain access to it as a table in its own
 
2727
     right:
 
2728
<programlisting>
 
2729
ALTER TABLE measurement_y2006m02 NO INHERIT measurement;
 
2730
</programlisting>
 
2731
     This allows further operations to be performed on the data before
 
2732
     it is dropped. For example, this is often a useful time to back up
 
2733
     the data using <command>COPY</>, <application>pg_dump</>, or
 
2734
     similar tools. It might also be a useful time to aggregate data
 
2735
     into smaller formats, perform other data manipulations, or run
 
2736
     reports.
 
2737
   </para>
 
2738
 
 
2739
   <para>
 
2740
     Similarly we can add a new partition to handle new data. We can create an
 
2741
     empty partition in the partitioned table just as the original partitions
 
2742
     were created above:
 
2743
 
 
2744
<programlisting>
 
2745
CREATE TABLE measurement_y2008m02 (
 
2746
    CHECK ( logdate &gt;= DATE '2008-02-01' AND logdate &lt; DATE '2008-03-01' )
 
2747
) INHERITS (measurement);
 
2748
</programlisting>
 
2749
 
 
2750
     As an alternative, it is sometimes more convenient to create the
 
2751
     new table outside the partition structure, and make it a proper
 
2752
     partition later. This allows the data to be loaded, checked, and
 
2753
     transformed prior to it appearing in the partitioned table:
 
2754
 
 
2755
<programlisting>
 
2756
CREATE TABLE measurement_y2008m02
 
2757
  (LIKE measurement INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
 
2758
ALTER TABLE measurement_y2008m02 ADD CONSTRAINT y2008m02
 
2759
   CHECK ( logdate &gt;= DATE '2008-02-01' AND logdate &lt; DATE '2008-03-01' );
 
2760
\copy measurement_y2008m02 from 'measurement_y2008m02'
 
2761
-- possibly some other data preparation work
 
2762
ALTER TABLE measurement_y2008m02 INHERIT measurement;
 
2763
</programlisting>
 
2764
    </para>
 
2765
   </sect2>
 
2766
 
 
2767
   <sect2 id="ddl-partitioning-constraint-exclusion">
 
2768
   <title>Partitioning and Constraint Exclusion</title>
 
2769
 
 
2770
   <indexterm>
 
2771
    <primary>constraint exclusion</primary>
 
2772
   </indexterm>
 
2773
 
 
2774
   <para>
 
2775
    <firstterm>Constraint exclusion</> is a query optimization technique
 
2776
    that improves performance for partitioned tables defined in the
 
2777
    fashion described above.  As an example:
 
2778
 
 
2779
<programlisting>
 
2780
SET constraint_exclusion = on;
 
2781
SELECT count(*) FROM measurement WHERE logdate &gt;= DATE '2008-01-01';
 
2782
</programlisting>
 
2783
 
 
2784
    Without constraint exclusion, the above query would scan each of
 
2785
    the partitions of the <structname>measurement</> table. With constraint
 
2786
    exclusion enabled, the planner will examine the constraints of each
 
2787
    partition and try to prove that the partition need not
 
2788
    be scanned because it could not contain any rows meeting the query's
 
2789
    <literal>WHERE</> clause.  When the planner can prove this, it
 
2790
    excludes the partition from the query plan.
 
2791
   </para>
 
2792
 
 
2793
   <para>
 
2794
    You can use the <command>EXPLAIN</> command to show the difference
 
2795
    between a plan with <varname>constraint_exclusion</> on and a plan
 
2796
    with it off.  A typical unoptimized plan for this type of table setup is:
 
2797
 
 
2798
<programlisting>
 
2799
SET constraint_exclusion = off;
 
2800
EXPLAIN SELECT count(*) FROM measurement WHERE logdate &gt;= DATE '2008-01-01';
 
2801
 
 
2802
                                          QUERY PLAN
 
2803
-----------------------------------------------------------------------------------------------
 
2804
 Aggregate  (cost=158.66..158.68 rows=1 width=0)
 
2805
   -&gt;  Append  (cost=0.00..151.88 rows=2715 width=0)
 
2806
         -&gt;  Seq Scan on measurement  (cost=0.00..30.38 rows=543 width=0)
 
2807
               Filter: (logdate &gt;= '2008-01-01'::date)
 
2808
         -&gt;  Seq Scan on measurement_y2006m02 measurement  (cost=0.00..30.38 rows=543 width=0)
 
2809
               Filter: (logdate &gt;= '2008-01-01'::date)
 
2810
         -&gt;  Seq Scan on measurement_y2006m03 measurement  (cost=0.00..30.38 rows=543 width=0)
 
2811
               Filter: (logdate &gt;= '2008-01-01'::date)
 
2812
...
 
2813
         -&gt;  Seq Scan on measurement_y2007m12 measurement  (cost=0.00..30.38 rows=543 width=0)
 
2814
               Filter: (logdate &gt;= '2008-01-01'::date)
 
2815
         -&gt;  Seq Scan on measurement_y2008m01 measurement  (cost=0.00..30.38 rows=543 width=0)
 
2816
               Filter: (logdate &gt;= '2008-01-01'::date)
 
2817
</programlisting>
 
2818
 
 
2819
    Some or all of the partitions might use index scans instead of
 
2820
    full-table sequential scans, but the point here is that there
 
2821
    is no need to scan the older partitions at all to answer this query.
 
2822
    When we enable constraint exclusion, we get a significantly
 
2823
    cheaper plan that will deliver the same answer:
 
2824
 
 
2825
<programlisting>
 
2826
SET constraint_exclusion = on;
 
2827
EXPLAIN SELECT count(*) FROM measurement WHERE logdate &gt;= DATE '2008-01-01';
 
2828
                                          QUERY PLAN
 
2829
-----------------------------------------------------------------------------------------------
 
2830
 Aggregate  (cost=63.47..63.48 rows=1 width=0)
 
2831
   -&gt;  Append  (cost=0.00..60.75 rows=1086 width=0)
 
2832
         -&gt;  Seq Scan on measurement  (cost=0.00..30.38 rows=543 width=0)
 
2833
               Filter: (logdate &gt;= '2008-01-01'::date)
 
2834
         -&gt;  Seq Scan on measurement_y2008m01 measurement  (cost=0.00..30.38 rows=543 width=0)
 
2835
               Filter: (logdate &gt;= '2008-01-01'::date)
 
2836
</programlisting>
 
2837
   </para>
 
2838
 
 
2839
   <para>
 
2840
    Note that constraint exclusion is driven only by <literal>CHECK</>
 
2841
    constraints, not by the presence of indexes.  Therefore it isn't
 
2842
    necessary to define indexes on the key columns.  Whether an index
 
2843
    needs to be created for a given partition depends on whether you
 
2844
    expect that queries that scan the partition will generally scan
 
2845
    a large part of the partition or just a small part.  An index will
 
2846
    be helpful in the latter case but not the former.
 
2847
   </para>
 
2848
 
 
2849
   <para>
 
2850
    The default (and recommended) setting of
 
2851
    <xref linkend="guc-constraint-exclusion"> is actually neither
 
2852
    <literal>on</> nor <literal>off</>, but an intermediate setting
 
2853
    called <literal>partition</>, which causes the technique to be
 
2854
    applied only to queries that are likely to be working on partitioned
 
2855
    tables.  The <literal>on</> setting causes the planner to examine
 
2856
    <literal>CHECK</> constraints in all queries, even simple ones that
 
2857
    are unlikely to benefit.
 
2858
   </para>
 
2859
 
 
2860
   </sect2>
 
2861
 
 
2862
   <sect2 id="ddl-partitioning-alternatives">
 
2863
   <title>Alternative Partitioning Methods</title>
 
2864
 
 
2865
    <para>
 
2866
     A different approach to redirecting inserts into the appropriate
 
2867
     partition table is to set up rules, instead of a trigger, on the
 
2868
     master table.  For example:
 
2869
 
 
2870
<programlisting>
 
2871
CREATE RULE measurement_insert_y2006m02 AS
 
2872
ON INSERT TO measurement WHERE
 
2873
    ( logdate &gt;= DATE '2006-02-01' AND logdate &lt; DATE '2006-03-01' )
 
2874
DO INSTEAD
 
2875
    INSERT INTO measurement_y2006m02 VALUES (NEW.*);
 
2876
...
 
2877
CREATE RULE measurement_insert_y2008m01 AS
 
2878
ON INSERT TO measurement WHERE
 
2879
    ( logdate &gt;= DATE '2008-01-01' AND logdate &lt; DATE '2008-02-01' )
 
2880
DO INSTEAD
 
2881
    INSERT INTO measurement_y2008m01 VALUES (NEW.*);
 
2882
</programlisting>
 
2883
 
 
2884
     A rule has significantly more overhead than a trigger, but the overhead
 
2885
     is paid once per query rather than once per row, so this method might be
 
2886
     advantageous for bulk-insert situations.  In most cases, however, the
 
2887
     trigger method will offer better performance.
 
2888
    </para>
 
2889
 
 
2890
    <para>
 
2891
     Be aware that <command>COPY</> ignores rules.  If you want to
 
2892
     use <command>COPY</> to insert data, you'll need to copy into the correct
 
2893
     partition table rather than into the master.  <command>COPY</> does fire
 
2894
     triggers, so you can use it normally if you use the trigger approach.
 
2895
    </para>
 
2896
 
 
2897
    <para>
 
2898
     Another disadvantage of the rule approach is that there is no simple
 
2899
     way to force an error if the set of rules doesn't cover the insertion
 
2900
     date; the data will silently go into the master table instead.
 
2901
    </para>
 
2902
 
 
2903
    <para>
 
2904
     Partitioning can also be arranged using a <literal>UNION ALL</literal>
 
2905
     view, instead of table inheritance.  For example,
 
2906
 
 
2907
<programlisting>
 
2908
CREATE VIEW measurement AS
 
2909
          SELECT * FROM measurement_y2006m02
 
2910
UNION ALL SELECT * FROM measurement_y2006m03
 
2911
...
 
2912
UNION ALL SELECT * FROM measurement_y2007m11
 
2913
UNION ALL SELECT * FROM measurement_y2007m12
 
2914
UNION ALL SELECT * FROM measurement_y2008m01;
 
2915
</programlisting>
 
2916
 
 
2917
     However, the need to recreate the view adds an extra step to adding and
 
2918
     dropping individual partitions of the data set.  In practice this
 
2919
     method has little to recommend it compared to using inheritance.
 
2920
    </para>
 
2921
 
 
2922
   </sect2>
 
2923
 
 
2924
   <sect2 id="ddl-partitioning-caveats">
 
2925
   <title>Caveats</title>
 
2926
 
 
2927
   <para>
 
2928
    The following caveats apply to partitioned tables:
 
2929
   <itemizedlist>
 
2930
    <listitem>
 
2931
     <para>
 
2932
      There is no automatic way to verify that all of the
 
2933
      <literal>CHECK</literal> constraints are mutually
 
2934
      exclusive.  It is safer to create code that generates
 
2935
      partitions and creates and/or modifies associated objects than
 
2936
      to write each by hand.
 
2937
     </para>
 
2938
    </listitem>
 
2939
 
 
2940
    <listitem>
 
2941
     <para>
 
2942
      The schemes shown here assume that the partition key column(s)
 
2943
      of a row never change, or at least do not change enough to require
 
2944
      it to move to another partition.  An <command>UPDATE</> that attempts
 
2945
      to do that will fail because of the <literal>CHECK</> constraints.
 
2946
      If you need to handle such cases, you can put suitable update triggers
 
2947
      on the partition tables, but it makes management of the structure
 
2948
      much more complicated.
 
2949
     </para>
 
2950
    </listitem>
 
2951
 
 
2952
    <listitem>
 
2953
     <para>
 
2954
      If you are using manual <command>VACUUM</command> or
 
2955
      <command>ANALYZE</command> commands, don't forget that
 
2956
      you need to run them on each partition individually. A command like:
 
2957
<programlisting>
 
2958
ANALYZE measurement;
 
2959
</programlisting>
 
2960
      will only process the master table.
 
2961
     </para>
 
2962
    </listitem>
 
2963
 
 
2964
   </itemizedlist>
 
2965
   </para>
 
2966
 
 
2967
   <para>
 
2968
    The following caveats apply to constraint exclusion:
 
2969
 
 
2970
   <itemizedlist>
 
2971
    <listitem>
 
2972
     <para>
 
2973
      Constraint exclusion only works when the query's <literal>WHERE</>
 
2974
      clause contains constants.  A parameterized query will not be
 
2975
      optimized, since the planner cannot know which partitions the
 
2976
      parameter value might select at run time.  For the same reason,
 
2977
      <quote>stable</> functions such as <function>CURRENT_DATE</function>
 
2978
      must be avoided.
 
2979
     </para>
 
2980
    </listitem>
 
2981
 
 
2982
    <listitem>
 
2983
     <para>
 
2984
      Keep the partitioning constraints simple, else the planner may not be
 
2985
      able to prove that partitions don't need to be visited.  Use simple
 
2986
      equality conditions for list partitioning, or simple
 
2987
      range tests for range partitioning, as illustrated in the preceding
 
2988
      examples.  A good rule of thumb is that partitioning constraints should
 
2989
      contain only comparisons of the partitioning column(s) to constants
 
2990
      using B-tree-indexable operators.
 
2991
     </para>
 
2992
    </listitem>
 
2993
 
 
2994
    <listitem>
 
2995
     <para>
 
2996
      All constraints on all partitions of the master table are examined
 
2997
      during constraint exclusion, so large numbers of partitions are likely
 
2998
      to increase query planning time considerably.  Partitioning using
 
2999
      these techniques will work well with up to perhaps a hundred partitions;
 
3000
      don't try to use many thousands of partitions.
 
3001
     </para>
 
3002
    </listitem>
 
3003
 
 
3004
   </itemizedlist>
 
3005
   </para>
 
3006
  </sect2>
 
3007
 </sect1>
 
3008
 
 
3009
 <sect1 id="ddl-foreign-data">
 
3010
  <title>Foreign Data</title>
 
3011
 
 
3012
   <indexterm>
 
3013
    <primary>foreign data</primary>
 
3014
   </indexterm>
 
3015
   <indexterm>
 
3016
    <primary>foreign table</primary>
 
3017
   </indexterm>
 
3018
 
 
3019
   <para>
 
3020
    <productname>PostgreSQL</productname> implements portions of the SQL/MED
 
3021
    specification, allowing you to access data that resides outside
 
3022
    PostgreSQL using regular SQL queries.  Such data is referred to as
 
3023
    <firstterm>foreign data</>.  (Note that this usage is not to be confused
 
3024
    with foreign keys, which are a type of constraint within the database.)
 
3025
   </para>
 
3026
 
 
3027
   <para>
 
3028
    Foreign data is accessed with help from a
 
3029
    <firstterm>foreign data wrapper</firstterm>. A foreign data wrapper is a
 
3030
    library that can communicate with an external data source, hiding the
 
3031
    details of connecting to the data source and fetching data from it. There
 
3032
    are several foreign data wrappers available, which can for example read
 
3033
    plain data files residing on the server, or connect to another PostgreSQL
 
3034
    instance. If none of the existing foreign data wrappers suit your needs,
 
3035
    you can write your own; see <xref linkend="fdwhandler">.
 
3036
   </para>
 
3037
 
 
3038
   <para>
 
3039
    To access foreign data, you need to create a <firstterm>foreign server</>
 
3040
    object, which defines how to connect to a particular external data source,
 
3041
    according to the set of options used by a particular foreign data
 
3042
    wrapper. Then you need to create one or more <firstterm>foreign
 
3043
    tables</firstterm>, which define the structure of the remote data. A
 
3044
    foreign table can be used in queries just like a normal table, but a
 
3045
    foreign table has no storage in the PostgreSQL server.  Whenever it is
 
3046
    used, PostgreSQL asks the foreign data wrapper to fetch the data from the
 
3047
    external source.
 
3048
   </para>
 
3049
 
 
3050
   <para>
 
3051
    Currently, foreign tables are read-only.  This limitation may be fixed
 
3052
    in a future release.
 
3053
   </para>
 
3054
 </sect1>
 
3055
 
 
3056
 <sect1 id="ddl-others">
 
3057
  <title>Other Database Objects</title>
 
3058
 
 
3059
  <para>
 
3060
   Tables are the central objects in a relational database structure,
 
3061
   because they hold your data.  But they are not the only objects
 
3062
   that exist in a database.  Many other kinds of objects can be
 
3063
   created to make the use and management of the data more efficient
 
3064
   or convenient.  They are not discussed in this chapter, but we give
 
3065
   you a list here so that you are aware of what is possible:
 
3066
  </para>
 
3067
 
 
3068
  <itemizedlist>
 
3069
   <listitem>
 
3070
    <para>
 
3071
     Views
 
3072
    </para>
 
3073
   </listitem>
 
3074
 
 
3075
   <listitem>
 
3076
    <para>
 
3077
     Functions and operators
 
3078
    </para>
 
3079
   </listitem>
 
3080
 
 
3081
   <listitem>
 
3082
    <para>
 
3083
     Data types and domains
 
3084
    </para>
 
3085
   </listitem>
 
3086
 
 
3087
   <listitem>
 
3088
    <para>
 
3089
     Triggers and rewrite rules
 
3090
    </para>
 
3091
   </listitem>
 
3092
  </itemizedlist>
 
3093
 
 
3094
  <para>
 
3095
   Detailed information on
 
3096
   these topics appears in <xref linkend="server-programming">.
 
3097
  </para>
 
3098
 </sect1>
 
3099
 
 
3100
 <sect1 id="ddl-depend">
 
3101
  <title>Dependency Tracking</title>
 
3102
 
 
3103
  <indexterm zone="ddl-depend">
 
3104
   <primary>CASCADE</primary>
 
3105
   <secondary sortas="DROP">with DROP</secondary>
 
3106
  </indexterm>
 
3107
 
 
3108
  <indexterm zone="ddl-depend">
 
3109
   <primary>RESTRICT</primary>
 
3110
   <secondary sortas="DROP">with DROP</secondary>
 
3111
  </indexterm>
 
3112
 
 
3113
  <para>
 
3114
   When you create complex database structures involving many tables
 
3115
   with foreign key constraints, views, triggers, functions, etc. you
 
3116
   implicitly create a net of dependencies between the objects.
 
3117
   For instance, a table with a foreign key constraint depends on the
 
3118
   table it references.
 
3119
  </para>
 
3120
 
 
3121
  <para>
 
3122
   To ensure the integrity of the entire database structure,
 
3123
   <productname>PostgreSQL</productname> makes sure that you cannot
 
3124
   drop objects that other objects still depend on.  For example,
 
3125
   attempting to drop the products table we had considered in <xref
 
3126
   linkend="ddl-constraints-fk">, with the orders table depending on
 
3127
   it, would result in an error message such as this:
 
3128
<screen>
 
3129
DROP TABLE products;
 
3130
 
 
3131
NOTICE:  constraint orders_product_no_fkey on table orders depends on table products
 
3132
ERROR:  cannot drop table products because other objects depend on it
 
3133
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
 
3134
</screen>
 
3135
   The error message contains a useful hint: if you do not want to
 
3136
   bother deleting all the dependent objects individually, you can run:
 
3137
<screen>
 
3138
DROP TABLE products CASCADE;
 
3139
</screen>
 
3140
   and all the dependent objects will be removed.  In this case, it
 
3141
   doesn't remove the orders table, it only removes the foreign key
 
3142
   constraint.  (If you want to check what <command>DROP ... CASCADE</> will do,
 
3143
   run <command>DROP</> without <literal>CASCADE</> and read the <literal>NOTICE</> messages.)
 
3144
  </para>
 
3145
 
 
3146
  <para>
 
3147
   All drop commands in <productname>PostgreSQL</productname> support
 
3148
   specifying <literal>CASCADE</literal>.  Of course, the nature of
 
3149
   the possible dependencies varies with the type of the object.  You
 
3150
   can also write <literal>RESTRICT</literal> instead of
 
3151
   <literal>CASCADE</literal> to get the default behavior, which is to
 
3152
   prevent the dropping of objects that other objects depend on.
 
3153
  </para>
 
3154
 
 
3155
  <note>
 
3156
   <para>
 
3157
    According to the SQL standard, specifying either
 
3158
    <literal>RESTRICT</literal> or <literal>CASCADE</literal> is
 
3159
    required.  No database system actually enforces that rule, but
 
3160
    whether the default behavior is <literal>RESTRICT</literal> or
 
3161
    <literal>CASCADE</literal> varies across systems.
 
3162
   </para>
 
3163
  </note>
 
3164
 
 
3165
  <note>
 
3166
   <para>
 
3167
    Foreign key constraint dependencies and serial column dependencies
 
3168
    from <productname>PostgreSQL</productname> versions prior to 7.3
 
3169
    are <emphasis>not</emphasis> maintained or created during the
 
3170
    upgrade process.  All other dependency types will be properly
 
3171
    created during an upgrade from a pre-7.3 database.
 
3172
   </para>
 
3173
  </note>
 
3174
 </sect1>
 
3175
 
 
3176
</chapter>