Home Explore Blog CI



postgresql

55th chunk of `doc/src/sgml/ddl.sgml`
8d057fc23b193aaf645d7462b9f28e8b6b749095efddb1110000000100000fa0
 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 &gt;= DATE '2008-02-01' AND logdate &lt; 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

Title: Partition Management: Detaching, Attaching, and Creating New Partitions
Summary
This section details how to manage partitions in PostgreSQL. It explains how to detach partitions, allowing for operations like backups or data aggregation before removal. It also shows how to create and attach new partitions, outlining the benefits of pre-loading and transforming data in a separate table before attaching it as a partition. It also covers the locking implications of different commands (e.g., `DETACH PARTITION CONCURRENTLY` vs. `DETACH PARTITION`) and recommends using CHECK constraints before attaching partitions to avoid table scans, and dropping the constraint after attachment.