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

Commitcd902b3

Browse files
committed
Change the rules for inherited CHECK constraints to be essentially the same
as those for inherited columns; that is, it's no longer allowed for a childtable to not have a check constraint matching one that exists on a parent.This satisfies the principle of least surprise (rows selected from the parentwill always appear to meet its check constraints) and eliminates somelongstanding bogosity in pg_dump, which formerly had to guess about whethercheck constraints were really inherited or not.The implementation involves adding conislocal and coninhcount columns topg_constraint (paralleling attislocal and attinhcount in pg_attribute)and refactoring various ALTER TABLE actions to be more like those forcolumns.Alex Hunsaker, Nikhil Sontakke, Tom Lane
1 parentf8df836 commitcd902b3

File tree

25 files changed

+1385
-569
lines changed

25 files changed

+1385
-569
lines changed

‎doc/src/sgml/catalogs.sgml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.165 2008/04/14 17:05:32 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.166 2008/05/09 23:32:03 tgl Exp $ -->
22
<!--
33
Documentation of the system catalogs, directed toward PostgreSQL developers
44
-->
@@ -1907,6 +1907,26 @@
19071907
<entry>Foreign key match type</entry>
19081908
</row>
19091909

1910+
<row>
1911+
<entry><structfield>conislocal</structfield></entry>
1912+
<entry><type>bool</type></entry>
1913+
<entry></entry>
1914+
<entry>
1915+
This constraint is defined locally in the relation. Note that a
1916+
constraint can be locally defined and inherited simultaneously
1917+
</entry>
1918+
</row>
1919+
1920+
<row>
1921+
<entry><structfield>coninhcount</structfield></entry>
1922+
<entry><type>int4</type></entry>
1923+
<entry></entry>
1924+
<entry>
1925+
The number of direct ancestors this constraint has. A constraint with
1926+
a nonzero number of ancestors cannot be dropped nor renamed
1927+
</entry>
1928+
</row>
1929+
19101930
<row>
19111931
<entry><structfield>conkey</structfield></entry>
19121932
<entry><type>int2[]</type></entry>

‎doc/src/sgml/ddl.sgml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.81 2008/01/13 17:58:54 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.82 2008/05/09 23:32:03 tgl Exp $ -->
22

33
<chapter id="ddl">
44
<title>Data Definition</title>
@@ -2107,7 +2107,8 @@ VALUES ('New York', NULL, NULL, 'NY');
21072107

21082108
<para>
21092109
A parent table cannot be dropped while any of its children remain. Neither
2110-
can columns of child tables be dropped or altered if they are inherited
2110+
can columns or check constraints of child tables be dropped or altered
2111+
if they are inherited
21112112
from any parent tables. If you wish to remove a table and all of its
21122113
descendants, one easy way is to drop the parent table with the
21132114
<literal>CASCADE</literal> option.
@@ -2117,7 +2118,7 @@ VALUES ('New York', NULL, NULL, 'NY');
21172118
<xref linkend="sql-altertable" endterm="sql-altertable-title"> will
21182119
propagate any changes in column data definitions and check
21192120
constraints down the inheritance hierarchy. Again, dropping
2120-
columnsor constraintsonparent tables is only possible when using
2121+
columnsthat are dependedonby other tables is only possible when using
21212122
the <literal>CASCADE</literal> option. <command>ALTER
21222123
TABLE</command> follows the same rules for duplicate column merging
21232124
and rejection that apply during <command>CREATE TABLE</command>.

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

Lines changed: 12 additions & 3 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.98 2007/11/28 15:42:31 petere Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.99 2008/05/09 23:32:03 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -713,7 +713,8 @@ ALTER TABLE table ALTER COLUMN anycol TYPE anytype;
713713
The <literal>TRIGGER</>, <literal>CLUSTER</>, <literal>OWNER</>,
714714
and <literal>TABLESPACE</> actions never recurse to descendant tables;
715715
that is, they always act as though <literal>ONLY</> were specified.
716-
Adding a constraint can recurse only for <literal>CHECK</> constraints.
716+
Adding a constraint can recurse only for <literal>CHECK</> constraints,
717+
and is required to do so for such constraints.
717718
</para>
718719

719720
<para>
@@ -804,7 +805,7 @@ ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
804805
</para>
805806

806807
<para>
807-
To add a check constraint to a table:
808+
To add a check constraint to a table and all its children:
808809
<programlisting>
809810
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
810811
</programlisting>
@@ -817,6 +818,14 @@ ALTER TABLE distributors DROP CONSTRAINT zipchk;
817818
</programlisting>
818819
</para>
819820

821+
<para>
822+
To remove a check constraint from a table only:
823+
<programlisting>
824+
ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
825+
</programlisting>
826+
(The check constraint remains in place for any child tables.)
827+
</para>
828+
820829
<para>
821830
To add a foreign key constraint to a table:
822831
<programlisting>

‎doc/src/sgml/ref/create_table.sgml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_table.sgml,v 1.109 2007/07/17 05:02:00 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_table.sgml,v 1.110 2008/05/09 23:32:04 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -210,16 +210,25 @@ and <replaceable class="PARAMETER">table_constraint</replaceable> is:
210210
the new table. If the column name list of the new table
211211
contains a column name that is also inherited, the data type must
212212
likewise match the inherited column(s), and the column
213-
definitions are merged into one. However, inherited and new
214-
column declarations of the same name need not specify identical
215-
constraints: all constraints provided from any declaration are
216-
merged together and all are applied to the new table. If the
213+
definitions are merged into one. If the
217214
new table explicitly specifies a default value for the column,
218215
this default overrides any defaults from inherited declarations
219216
of the column. Otherwise, any parents that specify default
220217
values for the column must all specify the same default, or an
221218
error will be reported.
222219
</para>
220+
221+
<para>
222+
<literal>CHECK</> constraints are merged in essentially the same way as
223+
columns: if multiple parent tables and/or the new table definition
224+
contain identically-named <literal>CHECK</> constraints, these
225+
constraints must all have the same check expression, or an error will be
226+
reported. Constraints having the same name and expression will
227+
be merged into one copy. Notice that an unnamed <literal>CHECK</>
228+
constraint in the new table will never be merged, since a unique name
229+
will always be chosen for it.
230+
</para>
231+
223232
<!--
224233
<para>
225234
<productname>PostgreSQL</> automatically allows the

‎src/backend/access/common/tupdesc.c

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.122 2008/01/01 19:45:46 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.123 2008/05/09 23:32:04 tgl Exp $
1212
*
1313
* NOTES
1414
* some of the executor utility code such as "ExecTypeFromTL" should be
@@ -505,20 +505,18 @@ BuildDescForRelation(List *schema)
505505
AttrNumberattnum;
506506
ListCell*l;
507507
TupleDescdesc;
508-
AttrDefault*attrdef=NULL;
509-
TupleConstr*constr= (TupleConstr*)palloc0(sizeof(TupleConstr));
508+
boolhas_not_null;
510509
char*attname;
511510
Oidatttypid;
512511
int32atttypmod;
513512
intattdim;
514-
intndef=0;
515513

516514
/*
517515
* allocate a new tuple descriptor
518516
*/
519517
natts=list_length(schema);
520518
desc=CreateTemplateTupleDesc(natts, false);
521-
constr->has_not_null= false;
519+
has_not_null= false;
522520

523521
attnum=0;
524522

@@ -547,52 +545,25 @@ BuildDescForRelation(List *schema)
547545
atttypid,atttypmod,attdim);
548546

549547
/* Fill in additional stuff not handled by TupleDescInitEntry */
550-
if (entry->is_not_null)
551-
constr->has_not_null= true;
552548
desc->attrs[attnum-1]->attnotnull=entry->is_not_null;
553-
554-
/*
555-
* Note we copy only pre-cooked default expressions. Digestion of raw
556-
* ones is someone else's problem.
557-
*/
558-
if (entry->cooked_default!=NULL)
559-
{
560-
if (attrdef==NULL)
561-
attrdef= (AttrDefault*)palloc(natts*sizeof(AttrDefault));
562-
attrdef[ndef].adnum=attnum;
563-
attrdef[ndef].adbin=pstrdup(entry->cooked_default);
564-
ndef++;
565-
desc->attrs[attnum-1]->atthasdef= true;
566-
}
567-
549+
has_not_null |=entry->is_not_null;
568550
desc->attrs[attnum-1]->attislocal=entry->is_local;
569551
desc->attrs[attnum-1]->attinhcount=entry->inhcount;
570552
}
571553

572-
if (constr->has_not_null||ndef>0)
554+
if (has_not_null)
573555
{
574-
desc->constr=constr;
556+
TupleConstr*constr=(TupleConstr*)palloc0(sizeof(TupleConstr));
575557

576-
if (ndef>0)/* DEFAULTs */
577-
{
578-
if (ndef<natts)
579-
constr->defval= (AttrDefault*)
580-
repalloc(attrdef,ndef*sizeof(AttrDefault));
581-
else
582-
constr->defval=attrdef;
583-
constr->num_defval=ndef;
584-
}
585-
else
586-
{
587-
constr->defval=NULL;
588-
constr->num_defval=0;
589-
}
558+
constr->has_not_null= true;
559+
constr->defval=NULL;
560+
constr->num_defval=0;
590561
constr->check=NULL;
591562
constr->num_check=0;
563+
desc->constr=constr;
592564
}
593565
else
594566
{
595-
pfree(constr);
596567
desc->constr=NULL;
597568
}
598569

‎src/backend/bootstrap/bootparse.y

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.91 2008/01/01 19:45:48 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.92 2008/05/09 23:32:04 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -206,6 +206,7 @@ Boot_CreateStmt:
206206
$6,
207207
BOOTSTRAP_SUPERUSERID,
208208
tupdesc,
209+
NIL,
209210
RELKIND_RELATION,
210211
$3,
211212
true,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp