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

Commit c621721

Browse files
author
Neil Conway
committed
Improvements to the partitioning documentation. Original patch from Greg
Stark, additional fixes and editorialization by Neil Conway. Greg's patch description: Some more doc patches for partitioned tables. In particular replace the caveat that INCLUDING CONSTRAINTS doesn't exist and replace it with documentation of, well, INCLUDING CONSTRAINTS. Also, there was an instance of "LIKE WITH DEFAULTS" which is actually spelled "LIKE INCLUDING DEFAULTS".
1 parent b74c543 commit c621721

File tree

1 file changed

+83
-61
lines changed

1 file changed

+83
-61
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 83 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 $ -->
22

33
<chapter id="ddl">
44
<title>Data Definition</title>
@@ -2081,13 +2081,17 @@ VALUES ('New York', NULL, NULL, 'NY');
20812081
</para>
20822082

20832083
<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
20882092
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>.
20912095
</para>
20922096

20932097
<para>
@@ -2159,19 +2163,6 @@ VALUES ('New York', NULL, NULL, 'NY');
21592163
</para>
21602164
</listitem>
21612165

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-
21752166
<listitem>
21762167
<para>
21772168
If a table is ever removed from the inheritance structure using
@@ -2577,56 +2568,86 @@ DO INSTEAD
25772568
constraint for its partition.
25782569
</para>
25792570
</listitem>
2571+
</orderedlist>
2572+
</para>
25802573

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>
25852594

2595+
<para>
2596+
The simplest option for removing old data is to simply drop the partition
2597+
that is no longer necessary:
25862598
<programlisting>
2587-
ALTER TABLE measurement_y2003mm02 NO INHERIT measurement
2599+
DROP TABLE measurement_y2003mm02;
25882600
</programlisting>
2601+
This can very quickly delete millions of records because it doesn't have
2602+
to individually delete every record.
2603+
</para>
25892604

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>
25982619

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.
26062624

26072625
<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 &gt;= DATE '2006-02-01' AND logdate &lt 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);
26132629
</programlisting>
26142630

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.
26202635

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>
26262643
</para>
2644+
</sect2>
26272645

2646+
<sect2 id="ddl-partitioning-caveats">
2647+
<title>Caveats</title>
2648+
26282649
<para>
2629-
The following caveats apply:
2650+
The following caveats apply to partitioned tables:
26302651
<itemizedlist>
26312652
<listitem>
26322653
<para>
@@ -2651,11 +2672,12 @@ ALTER TABLE measurement_y2006m02 INHERIT MEASUREMENT;
26512672

26522673
<listitem>
26532674
<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.
26592681
</para>
26602682
</listitem>
26612683
</itemizedlist>

0 commit comments

Comments
 (0)