Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf58eac8

Browse files
committed
Code and docs review for ALTER TABLE INHERIT/NO INHERIT patch.
1 parente1fdd22 commitf58eac8

File tree

8 files changed

+290
-303
lines changed

8 files changed

+290
-303
lines changed

‎doc/src/sgml/ddl.sgml

Lines changed: 78 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.63 2006/09/20 21:30:20 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.64 2006/10/13 21:43:17 tgl Exp $ -->
22

33
<chapter id="ddl">
44
<title>Data Definition</title>
@@ -2061,53 +2061,54 @@ VALUES ('New York', NULL, NULL, 'NY');
20612061
</para>
20622062

20632063
<para>
2064-
Table inheritancecan be defined using the<xref linkend="sql-createtable"
2065-
endterm="sql-createtable-title"> statement using the
2066-
<command>INHERITS</command> keyword. However the related statement
2067-
<command>CREATE TABLE AS</command> does not allow inheritance to be
2068-
specified.
2064+
Table inheritanceis typically established when thechild table is
2065+
created, using the <literal>INHERITS</> clause of the
2066+
<xref linkend="sql-createtable" endterm="sql-createtable-title">
2067+
statement. However the related statement<command>CREATE TABLE AS</command>
2068+
does not allow inheritance to bespecified.
20692069
</para>
20702070

20712071
<para>
2072-
Alternatively a table which is already defined in a compatible way can have
2073-
a new parent added with <xref linkend="sql-altertable"
2074-
endterm="sql-altertable-title"> using the <command>INHERIT</command>
2075-
subform.To do this the new child table must already include columns with
2076-
the samename andtype as the columns of the parent. It must also include
2077-
check constraints with the samename and checkexpression as those of the
2072+
Alternatively, a table which is already defined in a compatible way can
2073+
havea new parentrelationshipadded, using the <literal>INHERIT</literal>
2074+
variant of <xref linkend="sql-altertable"endterm="sql-altertable-title">.
2075+
To do this the new child table must already include columns with
2076+
the samenames andtypes as the columns of the parent. It must also include
2077+
check constraints with the samenames and checkexpressions as those of the
20782078
parent. Similarly an inheritance link can be removed from a child using the
2079-
<command>ALTER TABLE</command> using the <command>NO INHERIT</command>
2080-
subform.
2079+
<literal>NO INHERIT</literal> variant of <command>ALTER TABLE</>.
2080+
Dynamically adding and removing inheritance links like this can be useful
2081+
when the inheritance relationship is being used for table
2082+
partitioning (see <xref linkend="ddl-partitioning">).
20812083
</para>
20822084

20832085
<para>
2084-
One convenient way to create a compatible tableto be a new child
2085-
isspecifythe <command>LIKE</command> clause in <command>CREATE
2086+
One convenient way to create a compatible tablethat will later be made
2087+
a new childisto usethe <literal>LIKE</literal> clause in <command>CREATE
20862088
TABLE</command>. This creates a new table with the same columns as
2087-
the source table. If there are any <command>CHECK</command>
2088-
constraints defined on the parent table, the <command>INCLUDING
2089-
CONSTRAINTS</command> option to <command>LIKE</command> may be
2090-
useful, as the new child must have constraints matching the parent
2091-
to be considered compatible. Alternatively a compatible table can
2092-
be created by first creating a new child using <command>CREATE
2093-
TABLE</command> then removing the inheritance link with
2094-
<command>ALTER TABLE</command>.
2089+
the source table. If there are any <literal>CHECK</literal>
2090+
constraints defined on the source table, the <literal>INCLUDING
2091+
CONSTRAINTS</literal> option to <literal>LIKE</literal> should be
2092+
specified, as the new child must have constraints matching the parent
2093+
to be considered compatible.
20952094
</para>
20962095

20972096
<para>
2098-
A parent table cannot be dropped while any
2099-
of its children remain. If you wish to remove a table and all of its
2097+
A parent table cannot be dropped while any of its children remain. Neither
2098+
can columns of child tables be dropped or altered if they are inherited
2099+
from any parent tables. If you wish to remove a table and all of its
21002100
descendants, one easy way is to drop the parent table with the
2101-
<literal>CASCADE</literal> option. Neither can columns of child tables be
2102-
dropped or altered if they are inherited from any parent tables.
2101+
<literal>CASCADE</literal> option.
21032102
</para>
21042103

21052104
<para>
21062105
<xref linkend="sql-altertable" endterm="sql-altertable-title"> will
2107-
propagate any changes in column data definitions and check constraints down
2108-
the inheritance hierarchy. <command>ALTER TABLE</command> follows the same
2109-
rules for duplicate column merging and rejection that apply during
2110-
<command>CREATE TABLE</command>.
2106+
propagate any changes in column data definitions and check
2107+
constraints down the inheritance hierarchy. Again, dropping
2108+
columns or constraints on parent tables is only possible when using
2109+
the <literal>CASCADE</literal> option. <command>ALTER
2110+
TABLE</command> follows the same rules for duplicate column merging
2111+
and rejection that apply during <command>CREATE TABLE</command>.
21112112
</para>
21122113

21132114
<sect2 id="ddl-inherit-caveats">
@@ -2162,16 +2163,6 @@ VALUES ('New York', NULL, NULL, 'NY');
21622163
not capital names. There is no good workaround for this case.
21632164
</para>
21642165
</listitem>
2165-
2166-
<listitem>
2167-
<para>
2168-
If a table is ever removed from the inheritance structure using
2169-
<command>ALTER TABLE</command> then all its columns will be marked as
2170-
being locally defined. This means <command>DROP COLUMN</command> on the
2171-
parent table will never cascade to drop those columns on the child
2172-
table. They must be dropped manually.
2173-
</para>
2174-
</listitem>
21752166
</itemizedlist>
21762167

21772168
These deficiencies will probably be fixed in some future release,
@@ -2222,37 +2213,31 @@ VALUES ('New York', NULL, NULL, 'NY');
22222213
<itemizedlist>
22232214
<listitem>
22242215
<para>
2225-
Query performance can be improved when partition constraints can be
2226-
combined with local indexes to reduce the number of records that need to
2227-
be accessed for a query. Whereas the alternative, adding those columns
2228-
to every index, increases space usage which can erase any
2229-
performance gain.
2230-
</para>
2231-
2232-
<para>
2233-
When most of the heavily accessed area of the table is in a single
2234-
partition or a small number of partitions. That partition and its
2235-
indexes are more likely to fit within memory than the index of the
2236-
entire table.
2216+
Query performance can be improved dramatically in certain situations,
2217+
particularly when most of the heavily accessed rows of the table are in a
2218+
single partition or a small number of partitions. The partitioning
2219+
substitutes for leading columns of indexes, reducing index size and
2220+
making it more likely that the heavily-used parts of the indexes
2221+
fit in memory.
22372222
</para>
22382223
</listitem>
22392224

22402225
<listitem>
22412226
<para>
22422227
When queries or updates access a large percentage of a a single
2243-
partition performance can be improved dramatically by taking advantage
2244-
of sequentialdisk accessofa single partition instead of using an
2245-
index and random access reads across the whole table.
2228+
partition, performance can be improved by taking advantage
2229+
of sequentialscanofthat partition instead of using an
2230+
index and random access readsscatteredacross the whole table.
22462231
</para>
22472232
</listitem>
22482233

22492234
<listitem>
22502235
<para>
2251-
Bulk loads and deletes may be accomplished bysimply removingoradding
2252-
one of thepartitions. <command>ALTER TABLE</>isfar faster than a bulk
2253-
and takes the same amount of time regardless of the amount of data being
2254-
added or removed.It also entirely avoids the <command>VACUUM</command>
2255-
overhead caused by a bulk <command>delete</>.
2236+
Bulk loads and deletes may be accomplished byaddingorremoving
2237+
partitions, if that requirementisplanned into the partitioning design.
2238+
<command>ALTER TABLE</> is far faster than a bulk operation.
2239+
It also entirely avoids the <command>VACUUM</command>
2240+
overhead caused by a bulk <command>DELETE</>.
22562241
</para>
22572242
</listitem>
22582243

@@ -2577,6 +2562,25 @@ DO INSTEAD
25772562
creating a new partition each month, so it may be wise to write a
25782563
script that generates the required DDL automatically.
25792564
</para>
2565+
2566+
<para>
2567+
Partitioning can also be arranged using a <literal>UNION ALL</literal>
2568+
view:
2569+
2570+
<programlisting>
2571+
CREATE VIEW measurement AS
2572+
SELECT * FROM measurement_y2004m02
2573+
UNION ALL SELECT * FROM measurement_y2004m03
2574+
...
2575+
UNION ALL SELECT * FROM measurement_y2005m11
2576+
UNION ALL SELECT * FROM measurement_y2005m12
2577+
UNION ALL SELECT * FROM measurement_y2006m01;
2578+
</programlisting>
2579+
2580+
However, the need to
2581+
recreate the view adds an extra step to adding and dropping
2582+
individual partitions of the dataset.
2583+
</para>
25802584
</sect2>
25812585

25822586
<sect2 id="ddl-partitioning-managing-partitions">
@@ -2589,15 +2593,15 @@ DO INSTEAD
25892593
add new partitions for new data. One of the most important
25902594
advantages of partitioning is precisely that it allows this
25912595
otherwise painful task to be executed nearly instantaneously by
2592-
manipulating the partition structure, rather than moving large
2593-
amounts of data around physically.
2596+
manipulating the partition structure, rather thanphysicallymoving large
2597+
amounts of data around.
25942598
</para>
25952599

25962600
<para>
2597-
The simplest option for removing old data istosimply drop the partition
2601+
The simplest option for removing old data is simply to drop the partition
25982602
that is no longer necessary:
25992603
<programlisting>
2600-
DROP TABLEmeasurement_y2003mm02;
2604+
DROP TABLEmeasurement_y2003m02;
26012605
</programlisting>
26022606
This can very quickly delete millions of records because it doesn't have
26032607
to individually delete every record.
@@ -2608,10 +2612,10 @@ DROP TABLE measurement_y2003mm02;
26082612
the partitioned table but retain access to it as a table in its own
26092613
right:
26102614
<programlisting>
2611-
ALTER TABLEmeasurement_y2003mm02 NO INHERIT measurement;
2615+
ALTER TABLEmeasurement_y2003m02 NO INHERIT measurement;
26122616
</programlisting>
26132617
This allows further operations to be performed on the data before
2614-
it is dropped. For example, this is often a useful time tobackup
2618+
it is dropped. For example, this is often a useful time toback up
26152619
the data using <command>COPY</>, <application>pg_dump</>, or
26162620
similar tools. It can also be a useful time to aggregate data
26172621
into smaller formats, perform other data manipulations, or run
@@ -2635,10 +2639,12 @@ CREATE TABLE measurement_y2006m02 (
26352639
transformed prior to it appearing in the partitioned table.
26362640

26372641
<programlisting>
2638-
CREATE TABLE measurement_y2006m02 (LIKE measurement INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
2639-
\COPY measurement_y2006m02 FROM 'measurement_y2006m02'
2640-
UPDATE ... ;
2641-
ALTER TABLE measurement_y2006m02 ADD CONSTRAINT y2006m02 CHECK ( logdate &gt;= DATE '2006-02-01' AND logdate &lt; DATE '2006-03-01' );
2642+
CREATE TABLE measurement_y2006m02
2643+
(LIKE measurement INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
2644+
ALTER TABLE measurement_y2006m02 ADD CONSTRAINT y2006m02
2645+
CHECK ( logdate &gt;= DATE '2006-02-01' AND logdate &lt; DATE '2006-03-01' );
2646+
\copy measurement_y2006m02 from 'measurement_y2006m02'
2647+
-- possibly some other data preparation work
26422648
ALTER TABLE measurement_y2006m02 INHERIT measurement;
26432649
</programlisting>
26442650
</para>
@@ -2670,38 +2676,8 @@ ALTER TABLE measurement_y2006m02 INHERIT measurement;
26702676
using a set of rules as suggested above.)
26712677
</para>
26722678
</listitem>
2673-
2674-
<listitem>
2675-
<para>
2676-
When using the <literal>LIKE</> option above to create new
2677-
partitions, <literal>CHECK</> constraints are not copied from
2678-
the parent. If there are any <literal>CHECK</> constraints
2679-
defined for the parent, they must be manually created in new
2680-
partitions before <command>ALTER TABLE</command> will allow them
2681-
to be added.
2682-
</para>
2683-
</listitem>
26842679
</itemizedlist>
26852680
</para>
2686-
2687-
<para>
2688-
Partitioning can also be arranged using a <literal>UNION ALL</literal>
2689-
view:
2690-
2691-
<programlisting>
2692-
CREATE VIEW measurement AS
2693-
SELECT * FROM measurement_y2004m02
2694-
UNION ALL SELECT * FROM measurement_y2004m03
2695-
...
2696-
UNION ALL SELECT * FROM measurement_y2005m11
2697-
UNION ALL SELECT * FROM measurement_y2005m12
2698-
UNION ALL SELECT * FROM measurement_y2006m01;
2699-
</programlisting>
2700-
2701-
However, the need to
2702-
recreate the view adds an extra step to adding and dropping
2703-
individual partitions of the dataset.
2704-
</para>
27052681
</sect2>
27062682

27072683
<sect2 id="ddl-partitioning-constraint-exclusion">

‎doc/src/sgml/ref/alter_table.sgml

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.90 2006/09/16 00:30:16 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.91 2006/10/13 21:43:18 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -294,28 +294,22 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
294294
<term><literal>INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
295295
<listitem>
296296
<para>
297-
This form adds a new parent table to the table. This won't add new
298-
columns to the child table, instead all columns of the parent table must
299-
already exist in the child table. They must have matching data types,
297+
This form adds the target table as a new child of the specified parent
298+
table. Subsequently, queries against the parent will include records
299+
of the target table. To be added as a child, the target table must
300+
already contain all the same columns as the parent (it could have
301+
additional columns, too). The columns must have matching data types,
300302
and if they have <literal>NOT NULL</literal> constraints in the parent
301303
then they must also have <literal>NOT NULL</literal> constraints in the
302304
child.
303305
</para>
304306

305307
<para>
306-
There must also be matching table constraints for all
307-
<literal>CHECK</literal>tableconstraints of the parent. Currently
308+
There must also be matchingchild-table constraints for all
309+
<literal>CHECK</literal> constraints of the parent. Currently
308310
<literal>UNIQUE</literal>, <literal>PRIMARY KEY</literal>, and
309-
<literal>FOREIGN KEY</literal> constraints are ignored however this may
310-
change in the future.
311-
</para>
312-
313-
<para>
314-
The easiest way to create a suitable table is to create a table using
315-
<literal>INHERITS</literal> and then remove it via <literal>NO
316-
INHERIT</literal>. Alternatively create a table using
317-
<literal>LIKE</literal> however note that <literal>LIKE</literal> does
318-
not create the necessary constraints.
311+
<literal>FOREIGN KEY</literal> constraints are not considered, but
312+
this may change in the future.
319313
</para>
320314
</listitem>
321315
</varlistentry>
@@ -324,7 +318,8 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
324318
<term><literal>NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
325319
<listitem>
326320
<para>
327-
This form removes a parent table from the list of parents of the table.
321+
This form removes the target table from the list of children of the
322+
specified parent table.
328323
Queries against the parent table will no longer include records drawn
329324
from the target table.
330325
</para>
@@ -392,6 +387,8 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
392387
You must own the table to use <command>ALTER TABLE</>.
393388
To change the schema of a table, you must also have
394389
<literal>CREATE</literal> privilege on the new schema.
390+
To add the table as a new child of a parent table, you must own the
391+
parent table as well.
395392
To alter the owner, you must also be a direct or indirect member of the new
396393
owning role, and that role must have <literal>CREATE</literal> privilege on
397394
the table's schema. (These restrictions enforce that altering the owner

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp