it as a table in its own
right. This has two forms:
<programlisting>
ALTER TABLE measurement DETACH PARTITION measurement_y2006m02;
ALTER TABLE measurement DETACH PARTITION measurement_y2006m02 CONCURRENTLY;
</programlisting>
These allow further operations to be performed on the data before
it is dropped. For example, this is often a useful time to back up
the data using <command>COPY</command>, <application>pg_dump</application>, or
similar tools. It might also be a useful time to aggregate data
into smaller formats, perform other data manipulations, or run
reports. The first form of the command requires an
<literal>ACCESS EXCLUSIVE</literal> lock on the parent table.
Adding the <literal>CONCURRENTLY</literal> qualifier as in the second
form allows the detach operation to require only
<literal>SHARE UPDATE EXCLUSIVE</literal> lock on the parent table, but see
<link linkend="sql-altertable-detach-partition"><literal>ALTER TABLE ... DETACH PARTITION</literal></link>
for details on the restrictions.
</para>
<para>
Similarly we can add a new partition to handle new data. We can create an
empty partition in the partitioned table just as the original partitions
were created above:
<programlisting>
CREATE TABLE measurement_y2008m02 PARTITION OF measurement
FOR VALUES FROM ('2008-02-01') TO ('2008-03-01')
TABLESPACE fasttablespace;
</programlisting>
As an alternative to creating a new partition, it is sometimes more
convenient to create a new table separate from the partition structure
and attach it as a partition later. This allows new data to be loaded,
checked, and transformed prior to it appearing in the partitioned table.
Moreover, the <literal>ATTACH PARTITION</literal> operation requires
only a <literal>SHARE UPDATE EXCLUSIVE</literal> lock on the
partitioned table rather than the <literal>ACCESS EXCLUSIVE</literal>
lock required by <command>CREATE TABLE ... PARTITION OF</command>,
so it is more friendly to concurrent operations on the partitioned table;
see <link linkend="sql-altertable-attach-partition"><literal>ALTER TABLE ... ATTACH PARTITION</literal></link>
for additional details. The
<link linkend="sql-createtable-parms-like"><literal>CREATE TABLE ... LIKE</literal></link>
option can be helpful to avoid tediously repeating the parent table's
definition; for example:
<programlisting>
CREATE TABLE measurement_y2008m02
(LIKE measurement INCLUDING DEFAULTS INCLUDING CONSTRAINTS)
TABLESPACE fasttablespace;
ALTER TABLE measurement_y2008m02 ADD CONSTRAINT y2008m02
CHECK ( logdate >= DATE '2008-02-01' AND logdate < DATE '2008-03-01' );
\copy measurement_y2008m02 from 'measurement_y2008m02'
-- possibly some other data preparation work
ALTER TABLE measurement ATTACH PARTITION measurement_y2008m02
FOR VALUES FROM ('2008-02-01') TO ('2008-03-01' );
</programlisting>
</para>
<para>
Note that when running the <command>ATTACH PARTITION</command> command,
the table will be scanned to validate the partition constraint while
holding an <literal>ACCESS EXCLUSIVE</literal> lock on that partition.
As shown above, it is recommended to avoid this scan by creating a
<literal>CHECK</literal> constraint matching the expected partition
constraint on the table prior to attaching it. Once the
<command>ATTACH PARTITION</command> is complete, it is recommended to drop
the now-redundant <literal>CHECK</literal> constraint.
If the table being attached is itself a partitioned table, then each of its
sub-partitions will be recursively locked and scanned until either a
suitable <literal>CHECK</literal> constraint is encountered or the leaf
partitions are reached.
</para>
<para>
Similarly, if the partitioned table has a <literal>DEFAULT</literal>
partition, it is recommended to