1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.61 2006/09/05 21:08:33 tgl Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.62 2006/09/20 01:20:38 neilc Exp $ -->
2
2
3
3
<chapter id="ddl">
4
4
<title>Data Definition</title>
@@ -2081,13 +2081,17 @@ VALUES ('New York', NULL, NULL, 'NY');
2081
2081
</para>
2082
2082
2083
2083
<para>
2084
- One convenient way to create a compatible table to be a new child is using
2085
- the <command>LIKE</command> option of <command>CREATE TABLE</command>. This
2086
- creates a table with the same columns with the same type (however note the
2087
- caveat below regarding constraints). Alternatively a compatible table can
2084
+ One convenient way to create a compatible table to be a new child
2085
+ is specify the <command>LIKE</command> clause in <command>CREATE
2086
+ 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
2088
2092
be created by first creating a new child using <command>CREATE
2089
- TABLE</command> then removing the inheritance link with <command>ALTER
2090
- TABLE</command>.
2093
+ TABLE</command> then removing the inheritance link with
2094
+ <command>ALTER TABLE</command>.
2091
2095
</para>
2092
2096
2093
2097
<para>
@@ -2159,19 +2163,6 @@ VALUES ('New York', NULL, NULL, 'NY');
2159
2163
</para>
2160
2164
</listitem>
2161
2165
2162
- <listitem>
2163
- <para>
2164
- There is no convenient way to define a table compatible with a specific
2165
- parent including columns and constraints. The <command>LIKE</command>
2166
- option for <command>CREATE TABLE</command> does not copy constraints
2167
- which makes the tables it creates ineligible for being added using
2168
- <command>ALTER TABLE</command>. Matching check constraints must be added
2169
- manually or the table must be created as a child immediately, then if
2170
- needed removed from the inheritance structure temporarily to be added
2171
- again later.
2172
- </para>
2173
- </listitem>
2174
-
2175
2166
<listitem>
2176
2167
<para>
2177
2168
If a table is ever removed from the inheritance structure using
@@ -2577,56 +2568,86 @@ DO INSTEAD
2577
2568
constraint for its partition.
2578
2569
</para>
2579
2570
</listitem>
2571
+ </orderedlist>
2572
+ </para>
2580
2573
2581
- <listitem>
2582
- <para>
2583
- When the time comes to archive and remove the old data we first remove
2584
- it from the production table using:
2574
+ <para>
2575
+ As we can see, a complex partitioning scheme could require a
2576
+ substantial amount of DDL. In the above example we would be
2577
+ creating a new partition each month, so it may be wise to write a
2578
+ script that generates the required DDL automatically.
2579
+ </para>
2580
+
2581
+ <sect2 id="ddl-partitioning-managing-partitions">
2582
+ <title>Managing Partitions</title>
2583
+
2584
+ <para>
2585
+ Normally the set of partitions established when initially
2586
+ defining the table are not intended to remain static. It is
2587
+ common to want to remove old partitions of data and periodically
2588
+ add new partitions for new data. One of the most important
2589
+ advantages of partitioning is precisely that it allows this
2590
+ otherwise painful task to be executed nearly instantaneously by
2591
+ manipulating the partition structure, rather than moving large
2592
+ amounts of data around physically.
2593
+ </para>
2585
2594
2595
+ <para>
2596
+ The simplest option for removing old data is to simply drop the partition
2597
+ that is no longer necessary:
2586
2598
<programlisting>
2587
- ALTER TABLE measurement_y2003mm02 NO INHERIT measurement
2599
+ DROP TABLE measurement_y2003mm02;
2588
2600
</programlisting>
2601
+ This can very quickly delete millions of records because it doesn't have
2602
+ to individually delete every record.
2603
+ </para>
2589
2604
2590
- Then we can perform any sort of data modification necessary prior to
2591
- archiving without impacting the data viewed by the production system.
2592
- This could include, for example, deleting or compressing out redundant
2593
- data.
2594
- </para>
2595
- </listitem>
2596
- <listitem>
2597
- <para>
2605
+ <para>
2606
+ Another option that is often preferable is to remove the partition from
2607
+ the partitioned table but retain access to it as a table in its own
2608
+ right:
2609
+ <programlisting>
2610
+ ALTER TABLE measurement_y2003mm02 NO INHERIT measurement;
2611
+ </programlisting>
2612
+ This allows further operations to be performed on the data before
2613
+ it is dropped. For example, this is often a useful time to backup
2614
+ the data using <command>COPY</>, <application>pg_dump</>, or
2615
+ similar tools. It can also be a useful time to aggregate data
2616
+ into smaller formats, perform other data manipulations, or run
2617
+ reports.
2618
+ </para>
2598
2619
2599
- Similarly we can a new partition to handle new data. We can either
2600
- create an empty partition as the original partitions were created
2601
- above, or for some applications it's necessary to bulk load and clean
2602
- data for the new partition. If that operation involves multiple steps
2603
- by different processes it can be helpful to work with it in a fresh
2604
- table outside of the master partitioned table until it's ready to be
2605
- loaded:
2620
+ <para>
2621
+ Similarly we can add a new partition to handle new data. We can create an
2622
+ empty partition in the partitioned table just as the original partitions
2623
+ were created above.
2606
2624
2607
2625
<programlisting>
2608
- CREATE TABLE measurement_y2006m02 (LIKE measurement WITH DEFAULTS);
2609
- \COPY measurement_y2006m02 FROM 'measurement_y2006m02'
2610
- UPDATE ...
2611
- ALTER TABLE measurement_y2006m02 ADD CONSTRAINT y2006m02 CHECK ( logdate >= DATE '2006-02-01' AND logdate < DATE '2006-03-01' );
2612
- ALTER TABLE measurement_y2006m02 INHERIT MEASUREMENT;
2626
+ CREATE TABLE measurement_y2006m02 (
2627
+ CHECK ( logdate >= DATE '2006-02-01' AND logdate < DATE '2006-03-01' )
2628
+ ) INHERITS (measurement);
2613
2629
</programlisting>
2614
2630
2615
- </para>
2616
- </listitem>
2617
-
2618
- </orderedlist>
2619
- </para>
2631
+ As an alternative, it is sometimes more convenient to create the
2632
+ new table outside the partition structure, and make it a proper
2633
+ partition later. This allows the data to be loaded, checked, and
2634
+ transformed prior to it appearing in the partitioned table.
2620
2635
2621
- <para>
2622
- As we can see, a complex partitioning scheme could require a
2623
- substantial amount of DDL. In the above example we would be
2624
- creating a new partition each month, so it may be wise to write a
2625
- script that generates the required DDL automatically.
2636
+ <programlisting>
2637
+ CREATE TABLE measurement_y2006m02 (LIKE measurement INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
2638
+ \COPY measurement_y2006m02 FROM 'measurement_y2006m02'
2639
+ UPDATE ... ;
2640
+ ALTER TABLE measurement_y2006m02 ADD CONSTRAINT y2006m02 CHECK ( logdate >= DATE '2006-02-01' AND logdate < DATE '2006-03-01' );
2641
+ ALTER TABLE measurement_y2006m02 INHERIT measurement;
2642
+ </programlisting>
2626
2643
</para>
2644
+ </sect2>
2627
2645
2646
+ <sect2 id="ddl-partitioning-caveats">
2647
+ <title>Caveats</title>
2648
+
2628
2649
<para>
2629
- The following caveats apply:
2650
+ The following caveats apply to partitioned tables :
2630
2651
<itemizedlist>
2631
2652
<listitem>
2632
2653
<para>
@@ -2651,11 +2672,12 @@ ALTER TABLE measurement_y2006m02 INHERIT MEASUREMENT;
2651
2672
2652
2673
<listitem>
2653
2674
<para>
2654
- When using the <literal>LIKE</> option above to create new partitions
2655
- check constraints are not copied from the parent. If there are any check
2656
- constraints defined for the parent they must be manually created in new
2657
- partitions before <command>ALTER TABLE</command> will allow them to be
2658
- added.
2675
+ When using the <literal>LIKE</> option above to create new
2676
+ partitions, <literal>CHECK</> constraints are not copied from
2677
+ the parent. If there are any <literal>CHECK</> constraints
2678
+ defined for the parent, they must be manually created in new
2679
+ partitions before <command>ALTER TABLE</command> will allow them
2680
+ to be added.
2659
2681
</para>
2660
2682
</listitem>
2661
2683
</itemizedlist>
0 commit comments