@@ -23,7 +23,8 @@ PostgreSQL documentation
2323<synopsis>
2424CREATE FOREIGN TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable> ( [
2525 { <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ OPTIONS ( <replaceable class="parameter">option</replaceable> '<replaceable class="parameter">value</replaceable>' [, ... ] ) ] [ COLLATE <replaceable>collation</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
26- | <replaceable>table_constraint</replaceable> }
26+ | <replaceable>table_constraint</replaceable>
27+ | LIKE <replaceable>source_table</replaceable> [ <replaceable>like_option</replaceable> ... ] }
2728 [, ... ]
2829] )
2930[ INHERITS ( <replaceable>parent_table</replaceable> [, ... ] ) ]
@@ -57,6 +58,10 @@ CREATE FOREIGN TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name
5758 CHECK ( <replaceable class="parameter">expression</replaceable> ) [ NO INHERIT ] }
5859[ ENFORCED | NOT ENFORCED ]
5960
61+ <phrase>and <replaceable class="parameter">like_option</replaceable> is:</phrase>
62+
63+ { INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | ALL }
64+
6065<phrase>and <replaceable class="parameter">partition_bound_spec</replaceable> is:</phrase>
6166
6267IN ( <replaceable class="parameter">partition_bound_expr</replaceable> [, ...] ) |
@@ -191,6 +196,111 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
191196 </listitem>
192197 </varlistentry>
193198
199+ <varlistentry>
200+ <term><literal>LIKE <replaceable>source_table</replaceable> [ <replaceable>like_option</replaceable> ... ]</literal></term>
201+ <listitem>
202+ <para>
203+ The <literal>LIKE</literal> clause specifies a table from which
204+ the new table automatically copies all column names, their data types,
205+ and their not-null constraints.
206+ </para>
207+ <para>
208+ Unlike <literal>INHERITS</literal>, the new table and original table
209+ are completely decoupled after creation is complete. Changes to the
210+ original table will not be applied to the new table, and it is not
211+ possible to include data of the new table in scans of the original
212+ table.
213+ </para>
214+ <para>
215+ Also unlike <literal>INHERITS</literal>, columns and
216+ constraints copied by <literal>LIKE</literal> are not merged with similarly
217+ named columns and constraints.
218+ If the same name is specified explicitly or in another
219+ <literal>LIKE</literal> clause, an error is signaled.
220+ </para>
221+ <para>
222+ The optional <replaceable>like_option</replaceable> clauses specify
223+ which additional properties of the original table to copy. Specifying
224+ <literal>INCLUDING</literal> copies the property, specifying
225+ <literal>EXCLUDING</literal> omits the property.
226+ <literal>EXCLUDING</literal> is the default. If multiple specifications
227+ are made for the same kind of object, the last one is used. The
228+ available options are:
229+
230+ <variablelist>
231+ <varlistentry>
232+ <term><literal>INCLUDING COMMENTS</literal></term>
233+ <listitem>
234+ <para>
235+ Comments for the copied columns, constraints, and indexes will be
236+ copied. The default behavior is to exclude comments, resulting in
237+ the copied columns and constraints in the new table having no
238+ comments.
239+ </para>
240+ </listitem>
241+ </varlistentry>
242+
243+ <varlistentry>
244+ <term><literal>INCLUDING CONSTRAINTS</literal></term>
245+ <listitem>
246+ <para>
247+ <literal>CHECK</literal> constraints will be copied. No distinction
248+ is made between column constraints and table constraints. Not-null
249+ constraints are always copied to the new table.
250+ </para>
251+ </listitem>
252+ </varlistentry>
253+
254+ <varlistentry>
255+ <term><literal>INCLUDING DEFAULTS</literal></term>
256+ <listitem>
257+ <para>
258+ Default expressions for the copied column definitions will be
259+ copied. Otherwise, default expressions are not copied, resulting in
260+ the copied columns in the new table having null defaults. Note that
261+ copying defaults that call database-modification functions, such as
262+ <function>nextval</function>, may create a functional linkage
263+ between the original and new tables.
264+ </para>
265+ </listitem>
266+ </varlistentry>
267+
268+ <varlistentry>
269+ <term><literal>INCLUDING GENERATED</literal></term>
270+ <listitem>
271+ <para>
272+ Any generation expressions of copied column definitions will be
273+ copied. By default, new columns will be regular base columns.
274+ </para>
275+ </listitem>
276+ </varlistentry>
277+
278+ <varlistentry>
279+ <term><literal>INCLUDING STATISTICS</literal></term>
280+ <listitem>
281+ <para>
282+ Extended statistics are copied to the new table.
283+ </para>
284+ </listitem>
285+ </varlistentry>
286+
287+ <varlistentry>
288+ <term><literal>INCLUDING ALL</literal></term>
289+ <listitem>
290+ <para>
291+ <literal>INCLUDING ALL</literal> is an abbreviated form selecting
292+ all the available individual options. (It could be useful to write
293+ individual <literal>EXCLUDING</literal> clauses after
294+ <literal>INCLUDING ALL</literal> to select all but some specific
295+ options.)
296+ </para>
297+ </listitem>
298+ </varlistentry>
299+ </variablelist>
300+ </para>
301+ </listitem>
302+ </varlistentry>
303+
194304 <varlistentry>
195305 <term><literal>CONSTRAINT <replaceable class="parameter">constraint_name</replaceable></literal></term>
196306 <listitem>
@@ -450,6 +560,15 @@ CREATE FOREIGN TABLE measurement_y2016m07
450560 defined by <productname>PostgreSQL</productname>, is nonstandard.
451561 </para>
452562
563+ <refsect2>
564+ <title><literal>LIKE</literal> Clause</title>
565+
566+ <para>
567+ The <literal>LIKE</literal> clause is a
568+ <productname>PostgreSQL</productname> extension.
569+ </para>
570+ </refsect2>
571+
453572 </refsect1>
454573
455574 <refsect1>
0 commit comments