1- <!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.78 2007/12/02 19:20:32 tgl Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.79 2007/12/03 04:59:55 tgl Exp $ -->
22
33<chapter id="ddl">
44 <title>Data Definition</title>
@@ -2466,8 +2466,9 @@ CREATE TABLE measurement_y2008m01 ( ) INHERITS (measurement);
24662466
24672467 <listitem>
24682468 <para>
2469- We must add non-overlapping table constraints, so that our
2470- table creation script becomes:
2469+ We must provide non-overlapping table constraints. Rather than
2470+ just creating the partition tables as above, the table creation
2471+ script should really be:
24712472
24722473 <programlisting>
24732474CREATE TABLE measurement_y2006m02 (
@@ -2550,12 +2551,12 @@ CREATE TRIGGER insert_measurement_trigger
25502551CREATE OR REPLACE FUNCTION measurement_insert_trigger()
25512552RETURNS TRIGGER AS $$
25522553BEGIN
2553- IF ( logdate >= DATE '2006-02-01' AND logdate < DATE '2006-03-01' ) THEN
2554+ IF (NEW. logdate >= DATE '2006-02-01' ANDNEW. logdate < DATE '2006-03-01' ) THEN
25542555 INSERT INTO measurement_y2006m02 VALUES (NEW.*);
2555- ELSIF ( logdate >= DATE '2006-03-01' AND logdate < DATE '2006-04-01' ) THEN
2556+ ELSIF (NEW. logdate >= DATE '2006-03-01' ANDNEW. logdate < DATE '2006-04-01' ) THEN
25562557 INSERT INTO measurement_y2006m03 VALUES (NEW.*);
25572558 ...
2558- ELSIF ( logdate >= DATE '2008-01-01' AND logdate < DATE '2008-02-01' ) THEN
2559+ ELSIF (NEW. logdate >= DATE '2008-01-01' ANDNEW. logdate < DATE '2008-02-01' ) THEN
25592560 INSERT INTO measurement_y2008m01 VALUES (NEW.*);
25602561 ELSE
25612562 RAISE EXCEPTION 'Date out of range. Fix the measurement_insert_trigger() function!';
@@ -2576,6 +2577,15 @@ LANGUAGE plpgsql;
25762577 it doesn't need to be updated as often, since branches can be
25772578 added in advance of being needed.
25782579 </para>
2580+
2581+ <note>
2582+ <para>
2583+ In practice it might be best to check the newest partition first,
2584+ if most inserts go into that partition. For simplicity we have
2585+ shown the trigger's tests in the same order as in other parts
2586+ of this example.
2587+ </para>
2588+ </note>
25792589 </listitem>
25802590 </orderedlist>
25812591 </para>