1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.63 2006/09/20 21:30:20 tgl Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.64 2006/10/13 21:43:17 tgl Exp $ -->
2
2
3
3
<chapter id="ddl">
4
4
<title>Data Definition</title>
@@ -2061,53 +2061,54 @@ VALUES ('New York', NULL, NULL, 'NY');
2061
2061
</para>
2062
2062
2063
2063
<para>
2064
- Table inheritancecan be defined using the<xref linkend="sql-createtable"
2065
- endterm="sql-createtable-title"> statement using the
2066
- <command>INHERITS</command> keyword. However the related statement
2067
- <command>CREATE TABLE AS</command> does not allow inheritance to be
2068
- specified.
2064
+ Table inheritanceis typically established when thechild table is
2065
+ created, using the <literal>INHERITS</> clause of the
2066
+ <xref linkend="sql-createtable" endterm="sql-createtable-title">
2067
+ statement. However the related statement <command>CREATE TABLE AS</command>
2068
+ does not allow inheritance to be specified.
2069
2069
</para>
2070
2070
2071
2071
<para>
2072
- Alternatively a table which is already defined in a compatible way can have
2073
- a new parent added with <xref linkend="sql-altertable"
2074
- endterm="sql-altertable-title"> using the <command>INHERIT</command>
2075
- subform. To do this the new child table must already include columns with
2076
- the samename andtype as the columns of the parent. It must also include
2077
- check constraints with the samename and checkexpression as those of the
2072
+ Alternatively, a table which is already defined in a compatible way can
2073
+ have a new parentrelationship added, using the <literal>INHERIT</literal>
2074
+ variant of <xref linkend="sql-altertable" endterm="sql-altertable-title">.
2075
+ To do this the new child table must already include columns with
2076
+ the samenames andtypes as the columns of the parent. It must also include
2077
+ check constraints with the samenames and checkexpressions as those of the
2078
2078
parent. Similarly an inheritance link can be removed from a child using the
2079
- <command>ALTER TABLE</command> using the <command>NO INHERIT</command>
2080
- subform.
2079
+ <literal>NO INHERIT</literal> variant of <command>ALTER TABLE</>.
2080
+ Dynamically adding and removing inheritance links like this can be useful
2081
+ when the inheritance relationship is being used for table
2082
+ partitioning (see <xref linkend="ddl-partitioning">).
2081
2083
</para>
2082
2084
2083
2085
<para>
2084
- One convenient way to create a compatible tableto be a new child
2085
- isspecify the <command >LIKE</command > clause in <command>CREATE
2086
+ One convenient way to create a compatible tablethat will later be made
2087
+ a new child isto use the <literal >LIKE</literal > clause in <command>CREATE
2086
2088
TABLE</command>. This creates a new table with the same columns as
2087
- the source table. If there are any <command>CHECK</command>
2088
- constraints defined on the parent table, the <command>INCLUDING
2089
- CONSTRAINTS</command> option to <command>LIKE</command> may be
2090
- useful, as the new child must have constraints matching the parent
2091
- to be considered compatible. Alternatively a compatible table can
2092
- be created by first creating a new child using <command>CREATE
2093
- TABLE</command> then removing the inheritance link with
2094
- <command>ALTER TABLE</command>.
2089
+ the source table. If there are any <literal>CHECK</literal>
2090
+ constraints defined on the source table, the <literal>INCLUDING
2091
+ CONSTRAINTS</literal> option to <literal>LIKE</literal> should be
2092
+ specified, as the new child must have constraints matching the parent
2093
+ to be considered compatible.
2095
2094
</para>
2096
2095
2097
2096
<para>
2098
- A parent table cannot be dropped while any
2099
- of its children remain. If you wish to remove a table and all of its
2097
+ A parent table cannot be dropped while any of its children remain. Neither
2098
+ can columns of child tables be dropped or altered if they are inherited
2099
+ from any parent tables. If you wish to remove a table and all of its
2100
2100
descendants, one easy way is to drop the parent table with the
2101
- <literal>CASCADE</literal> option. Neither can columns of child tables be
2102
- dropped or altered if they are inherited from any parent tables.
2101
+ <literal>CASCADE</literal> option.
2103
2102
</para>
2104
2103
2105
2104
<para>
2106
2105
<xref linkend="sql-altertable" endterm="sql-altertable-title"> will
2107
- propagate any changes in column data definitions and check constraints down
2108
- the inheritance hierarchy. <command>ALTER TABLE</command> follows the same
2109
- rules for duplicate column merging and rejection that apply during
2110
- <command>CREATE TABLE</command>.
2106
+ propagate any changes in column data definitions and check
2107
+ constraints down the inheritance hierarchy. Again, dropping
2108
+ columns or constraints on parent tables is only possible when using
2109
+ the <literal>CASCADE</literal> option. <command>ALTER
2110
+ TABLE</command> follows the same rules for duplicate column merging
2111
+ and rejection that apply during <command>CREATE TABLE</command>.
2111
2112
</para>
2112
2113
2113
2114
<sect2 id="ddl-inherit-caveats">
@@ -2162,16 +2163,6 @@ VALUES ('New York', NULL, NULL, 'NY');
2162
2163
not capital names. There is no good workaround for this case.
2163
2164
</para>
2164
2165
</listitem>
2165
-
2166
- <listitem>
2167
- <para>
2168
- If a table is ever removed from the inheritance structure using
2169
- <command>ALTER TABLE</command> then all its columns will be marked as
2170
- being locally defined. This means <command>DROP COLUMN</command> on the
2171
- parent table will never cascade to drop those columns on the child
2172
- table. They must be dropped manually.
2173
- </para>
2174
- </listitem>
2175
2166
</itemizedlist>
2176
2167
2177
2168
These deficiencies will probably be fixed in some future release,
@@ -2222,37 +2213,31 @@ VALUES ('New York', NULL, NULL, 'NY');
2222
2213
<itemizedlist>
2223
2214
<listitem>
2224
2215
<para>
2225
- Query performance can be improved when partition constraints can be
2226
- combined with local indexes to reduce the number of records that need to
2227
- be accessed for a query. Whereas the alternative, adding those columns
2228
- to every index, increases space usage which can erase any
2229
- performance gain.
2230
- </para>
2231
-
2232
- <para>
2233
- When most of the heavily accessed area of the table is in a single
2234
- partition or a small number of partitions. That partition and its
2235
- indexes are more likely to fit within memory than the index of the
2236
- entire table.
2216
+ Query performance can be improved dramatically in certain situations,
2217
+ particularly when most of the heavily accessed rows of the table are in a
2218
+ single partition or a small number of partitions. The partitioning
2219
+ substitutes for leading columns of indexes, reducing index size and
2220
+ making it more likely that the heavily-used parts of the indexes
2221
+ fit in memory.
2237
2222
</para>
2238
2223
</listitem>
2239
2224
2240
2225
<listitem>
2241
2226
<para>
2242
2227
When queries or updates access a large percentage of a a single
2243
- partition performance can be improved dramatically by taking advantage
2244
- of sequentialdisk access ofa single partition instead of using an
2245
- index and random access reads across the whole table.
2228
+ partition, performance can be improved by taking advantage
2229
+ of sequentialscan ofthat partition instead of using an
2230
+ index and random access readsscattered across the whole table.
2246
2231
</para>
2247
2232
</listitem>
2248
2233
2249
2234
<listitem>
2250
2235
<para>
2251
- Bulk loads and deletes may be accomplished bysimply removing oradding
2252
- one of the partitions. <command>ALTER TABLE</> isfar faster than a bulk
2253
- and takes the same amount of time regardless of the amount of data being
2254
- added or removed. It also entirely avoids the <command>VACUUM</command>
2255
- overhead caused by a bulk <command>delete </>.
2236
+ Bulk loads and deletes may be accomplished byadding orremoving
2237
+ partitions, if that requirement isplanned into the partitioning design.
2238
+ <command>ALTER TABLE</> is far faster than a bulk operation.
2239
+ It also entirely avoids the <command>VACUUM</command>
2240
+ overhead caused by a bulk <command>DELETE </>.
2256
2241
</para>
2257
2242
</listitem>
2258
2243
@@ -2577,6 +2562,25 @@ DO INSTEAD
2577
2562
creating a new partition each month, so it may be wise to write a
2578
2563
script that generates the required DDL automatically.
2579
2564
</para>
2565
+
2566
+ <para>
2567
+ Partitioning can also be arranged using a <literal>UNION ALL</literal>
2568
+ view:
2569
+
2570
+ <programlisting>
2571
+ CREATE VIEW measurement AS
2572
+ SELECT * FROM measurement_y2004m02
2573
+ UNION ALL SELECT * FROM measurement_y2004m03
2574
+ ...
2575
+ UNION ALL SELECT * FROM measurement_y2005m11
2576
+ UNION ALL SELECT * FROM measurement_y2005m12
2577
+ UNION ALL SELECT * FROM measurement_y2006m01;
2578
+ </programlisting>
2579
+
2580
+ However, the need to
2581
+ recreate the view adds an extra step to adding and dropping
2582
+ individual partitions of the dataset.
2583
+ </para>
2580
2584
</sect2>
2581
2585
2582
2586
<sect2 id="ddl-partitioning-managing-partitions">
@@ -2589,15 +2593,15 @@ DO INSTEAD
2589
2593
add new partitions for new data. One of the most important
2590
2594
advantages of partitioning is precisely that it allows this
2591
2595
otherwise painful task to be executed nearly instantaneously by
2592
- manipulating the partition structure, rather than moving large
2593
- amounts of data around physically .
2596
+ manipulating the partition structure, rather thanphysically moving large
2597
+ amounts of data around.
2594
2598
</para>
2595
2599
2596
2600
<para>
2597
- The simplest option for removing old data isto simply drop the partition
2601
+ The simplest option for removing old data is simply to drop the partition
2598
2602
that is no longer necessary:
2599
2603
<programlisting>
2600
- DROP TABLEmeasurement_y2003mm02 ;
2604
+ DROP TABLEmeasurement_y2003m02 ;
2601
2605
</programlisting>
2602
2606
This can very quickly delete millions of records because it doesn't have
2603
2607
to individually delete every record.
@@ -2608,10 +2612,10 @@ DROP TABLE measurement_y2003mm02;
2608
2612
the partitioned table but retain access to it as a table in its own
2609
2613
right:
2610
2614
<programlisting>
2611
- ALTER TABLEmeasurement_y2003mm02 NO INHERIT measurement;
2615
+ ALTER TABLEmeasurement_y2003m02 NO INHERIT measurement;
2612
2616
</programlisting>
2613
2617
This allows further operations to be performed on the data before
2614
- it is dropped. For example, this is often a useful time tobackup
2618
+ it is dropped. For example, this is often a useful time toback up
2615
2619
the data using <command>COPY</>, <application>pg_dump</>, or
2616
2620
similar tools. It can also be a useful time to aggregate data
2617
2621
into smaller formats, perform other data manipulations, or run
@@ -2635,10 +2639,12 @@ CREATE TABLE measurement_y2006m02 (
2635
2639
transformed prior to it appearing in the partitioned table.
2636
2640
2637
2641
<programlisting>
2638
- CREATE TABLE measurement_y2006m02 (LIKE measurement INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
2639
- \COPY measurement_y2006m02 FROM 'measurement_y2006m02'
2640
- UPDATE ... ;
2641
- ALTER TABLE measurement_y2006m02 ADD CONSTRAINT y2006m02 CHECK ( logdate >= DATE '2006-02-01' AND logdate < DATE '2006-03-01' );
2642
+ CREATE TABLE measurement_y2006m02
2643
+ (LIKE measurement INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
2644
+ ALTER TABLE measurement_y2006m02 ADD CONSTRAINT y2006m02
2645
+ CHECK ( logdate >= DATE '2006-02-01' AND logdate < DATE '2006-03-01' );
2646
+ \copy measurement_y2006m02 from 'measurement_y2006m02'
2647
+ -- possibly some other data preparation work
2642
2648
ALTER TABLE measurement_y2006m02 INHERIT measurement;
2643
2649
</programlisting>
2644
2650
</para>
@@ -2670,38 +2676,8 @@ ALTER TABLE measurement_y2006m02 INHERIT measurement;
2670
2676
using a set of rules as suggested above.)
2671
2677
</para>
2672
2678
</listitem>
2673
-
2674
- <listitem>
2675
- <para>
2676
- When using the <literal>LIKE</> option above to create new
2677
- partitions, <literal>CHECK</> constraints are not copied from
2678
- the parent. If there are any <literal>CHECK</> constraints
2679
- defined for the parent, they must be manually created in new
2680
- partitions before <command>ALTER TABLE</command> will allow them
2681
- to be added.
2682
- </para>
2683
- </listitem>
2684
2679
</itemizedlist>
2685
2680
</para>
2686
-
2687
- <para>
2688
- Partitioning can also be arranged using a <literal>UNION ALL</literal>
2689
- view:
2690
-
2691
- <programlisting>
2692
- CREATE VIEW measurement AS
2693
- SELECT * FROM measurement_y2004m02
2694
- UNION ALL SELECT * FROM measurement_y2004m03
2695
- ...
2696
- UNION ALL SELECT * FROM measurement_y2005m11
2697
- UNION ALL SELECT * FROM measurement_y2005m12
2698
- UNION ALL SELECT * FROM measurement_y2006m01;
2699
- </programlisting>
2700
-
2701
- However, the need to
2702
- recreate the view adds an extra step to adding and dropping
2703
- individual partitions of the dataset.
2704
- </para>
2705
2681
</sect2>
2706
2682
2707
2683
<sect2 id="ddl-partitioning-constraint-exclusion">