Home Explore Blog Models CI



postgresql

12th chunk of `doc/src/sgml/ref/create_table.sgml`
2ca773599bbae1a7aac9494ade2ae20040faa1661fd2de360000000100000fa2
 applied when inserting
      a tuple through a partitioned table.
     </para>

     <para>
      Rows inserted into a partitioned table will be automatically routed to
      the correct partition.  If no suitable partition exists, an error will
      occur.
     </para>

     <para>
      Operations such as <command>TRUNCATE</command>
      which normally affect a table and all of its
      inheritance children will cascade to all partitions, but may also be
      performed on an individual partition.
     </para>

     <para>
      Note that creating a partition using <literal>PARTITION OF</literal>
      requires taking an <literal>ACCESS EXCLUSIVE</literal> lock on the
      parent partitioned table.  Likewise, dropping a partition
      with <command>DROP TABLE</command> requires taking
      an <literal>ACCESS EXCLUSIVE</literal> lock on the parent table.
      It is possible to use <link linkend="sql-altertable"><command>ALTER
      TABLE ATTACH/DETACH PARTITION</command></link> to perform these
      operations with a weaker lock, thus reducing interference with
      concurrent operations on the partitioned table.
     </para>

    </listitem>
   </varlistentry>

   <varlistentry id="sql-createtable-parms-like">
    <term><literal>LIKE <replaceable>source_table</replaceable> [ <replaceable>like_option</replaceable> ... ]</literal></term>
    <listitem>
     <para>
      The <literal>LIKE</literal> clause specifies a table from which
      the new table automatically copies all column names, their data types,
      and their not-null constraints.
     </para>
     <para>
      Unlike <literal>INHERITS</literal>, the new table and original table
      are completely decoupled after creation is complete.  Changes to the
      original table will not be applied to the new table, and it is not
      possible to include data of the new table in scans of the original
      table.
     </para>
      <para>
      Also unlike <literal>INHERITS</literal>, columns and
      constraints copied by <literal>LIKE</literal> are not merged with similarly
      named columns and constraints.
      If the same name is specified explicitly or in another
      <literal>LIKE</literal> clause, an error is signaled.
     </para>
     <para>
      The optional <replaceable>like_option</replaceable> clauses specify
      which additional properties of the original table to copy.  Specifying
      <literal>INCLUDING</literal> copies the property, specifying
      <literal>EXCLUDING</literal> omits the property.
      <literal>EXCLUDING</literal> is the default.  If multiple specifications
      are made for the same kind of object, the last one is used.  The
      available options are:

      <variablelist>
       <varlistentry id="sql-createtable-parms-like-opt-comments">
        <term><literal>INCLUDING COMMENTS</literal></term>
        <listitem>
         <para>
          Comments for the copied columns, constraints, and indexes will be
          copied.  The default behavior is to exclude comments, resulting in
          the copied columns and constraints in the new table having no
          comments.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry id="sql-createtable-parms-like-opt-compression">
        <term><literal>INCLUDING COMPRESSION</literal></term>
        <listitem>
         <para>
          Compression method of the columns will be copied.  The default
          behavior is to exclude compression methods, resulting in columns
          having the default compression method.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry id="sql-createtable-parms-like-opt-constraints">
        <term><literal>INCLUDING CONSTRAINTS</literal></term>
        <listitem>
         <para>
          <literal>CHECK</literal> constraints will be copied.  No distinction
          is made between column constraints and table constraints.  Not-null
          constraints are always copied to the

Title: CREATE TABLE Parameters: Partition Handling and LIKE Clause
Summary
This section discusses how rows are routed to correct partitions in a partitioned table and what happens when no suitable partition exists. It explains how operations like TRUNCATE affect partitions and mentions the exclusive lock needed for creating or dropping partitions, recommending ALTER TABLE ATTACH/DETACH for weaker locks. The section then details the LIKE clause, which copies column names, data types, and not-null constraints from a source table. Unlike INHERITS, the new table is decoupled from the original. The LIKE clause also offers options to include or exclude additional properties like comments, compression, and constraints.