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

Commit0156840

Browse files
committed
Add more checks against altering typed tables
- Prohibit altering column type- Prohibit changing inheritance- Move checks from Exec to Prep phases in ALTER TABLE codebackpatched to 9.0
1 parent87e0b74 commit0156840

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.332 2010/07/06 19:18:56 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.333 2010/07/23 20:04:18 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -288,6 +288,7 @@ static void ATExecSetOptions(Relation rel, const char *colName,
288288
Node*options,boolisReset);
289289
staticvoidATExecSetStorage(Relationrel,constchar*colName,
290290
Node*newValue);
291+
staticvoidATPrepDropColumn(Relationrel,boolrecurse,AlterTableCmd*cmd);
291292
staticvoidATExecDropColumn(List**wqueue,Relationrel,constchar*colName,
292293
DropBehaviorbehavior,
293294
boolrecurse,boolrecursing,
@@ -327,7 +328,8 @@ static void ATExecEnableDisableTrigger(Relation rel, char *trigname,
327328
charfires_when,boolskip_system);
328329
staticvoidATExecEnableDisableRule(Relationrel,char*rulename,
329330
charfires_when);
330-
staticvoidATExecAddInherit(Relationrel,RangeVar*parent);
331+
staticvoidATPrepAddInherit(Relationchild_rel);
332+
staticvoidATExecAddInherit(Relationchild_rel,RangeVar*parent);
331333
staticvoidATExecDropInherit(Relationrel,RangeVar*parent);
332334
staticvoidcopy_relation_data(SMgrRelationrel,SMgrRelationdst,
333335
ForkNumberforkNum,boolistemp);
@@ -2499,10 +2501,8 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
24992501
break;
25002502
caseAT_DropColumn:/* DROP COLUMN */
25012503
ATSimplePermissions(rel, false);
2504+
ATPrepDropColumn(rel,recurse,cmd);
25022505
/* Recursion occurs during execution phase */
2503-
/* No command-specific prep needed except saving recurse flag */
2504-
if (recurse)
2505-
cmd->subtype=AT_DropColumnRecurse;
25062506
pass=AT_PASS_DROP;
25072507
break;
25082508
caseAT_AddIndex:/* ADD INDEX */
@@ -2579,6 +2579,12 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
25792579
/* No command-specific prep needed */
25802580
pass=AT_PASS_MISC;
25812581
break;
2582+
caseAT_AddInherit:/* INHERIT */
2583+
ATSimplePermissions(rel, false);
2584+
/* This command never recurses */
2585+
ATPrepAddInherit(rel);
2586+
pass=AT_PASS_MISC;
2587+
break;
25822588
caseAT_EnableTrig:/* ENABLE TRIGGER variants */
25832589
caseAT_EnableAlwaysTrig:
25842590
caseAT_EnableReplicaTrig:
@@ -2591,8 +2597,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
25912597
caseAT_EnableAlwaysRule:
25922598
caseAT_EnableReplicaRule:
25932599
caseAT_DisableRule:
2594-
caseAT_AddInherit:/* INHERIT / NO INHERIT */
2595-
caseAT_DropInherit:
2600+
caseAT_DropInherit:/* NO INHERIT */
25962601
ATSimplePermissions(rel, false);
25972602
/* These commands never recurse */
25982603
/* No command-specific prep needed */
@@ -3568,6 +3573,11 @@ static void
35683573
ATPrepAddColumn(List**wqueue,Relationrel,boolrecurse,
35693574
AlterTableCmd*cmd)
35703575
{
3576+
if (rel->rd_rel->reloftype)
3577+
ereport(ERROR,
3578+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
3579+
errmsg("cannot add column to typed table")));
3580+
35713581
/*
35723582
* Recurse to add the column to child classes, if requested.
35733583
*
@@ -3616,11 +3626,6 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
36163626
Form_pg_typetform;
36173627
Expr*defval;
36183628

3619-
if (rel->rd_rel->reloftype)
3620-
ereport(ERROR,
3621-
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
3622-
errmsg("cannot add column to typed table")));
3623-
36243629
attrdesc=heap_open(AttributeRelationId,RowExclusiveLock);
36253630

36263631
/*
@@ -4325,6 +4330,19 @@ ATExecSetStorage(Relation rel, const char *colName, Node *newValue)
43254330
* static pre-pass because it won't handle multiple inheritance situations
43264331
* correctly.)
43274332
*/
4333+
staticvoid
4334+
ATPrepDropColumn(Relationrel,boolrecurse,AlterTableCmd*cmd)
4335+
{
4336+
if (rel->rd_rel->reloftype)
4337+
ereport(ERROR,
4338+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
4339+
errmsg("cannot drop column from typed table")));
4340+
4341+
/* No command-specific prep needed except saving recurse flag */
4342+
if (recurse)
4343+
cmd->subtype=AT_DropColumnRecurse;
4344+
}
4345+
43284346
staticvoid
43294347
ATExecDropColumn(List**wqueue,Relationrel,constchar*colName,
43304348
DropBehaviorbehavior,
@@ -4337,11 +4355,6 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
43374355
List*children;
43384356
ObjectAddressobject;
43394357

4340-
if (rel->rd_rel->reloftype)
4341-
ereport(ERROR,
4342-
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
4343-
errmsg("cannot drop column from typed table")));
4344-
43454358
/* At top level, permission check was done in ATPrepCmd, else do it */
43464359
if (recursing)
43474360
ATSimplePermissions(rel, false);
@@ -5788,6 +5801,11 @@ ATPrepAlterColumnType(List **wqueue,
57885801
NewColumnValue*newval;
57895802
ParseState*pstate=make_parsestate(NULL);
57905803

5804+
if (rel->rd_rel->reloftype)
5805+
ereport(ERROR,
5806+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
5807+
errmsg("cannot alter column type of typed table")));
5808+
57915809
/* lookup the attribute so we can check inheritance status */
57925810
tuple=SearchSysCacheAttName(RelationGetRelid(rel),colName);
57935811
if (!HeapTupleIsValid(tuple))
@@ -7115,6 +7133,15 @@ ATExecEnableDisableRule(Relation rel, char *trigname,
71157133
* check constraints of the parent appear in the child and that they have the
71167134
* same data types and expressions.
71177135
*/
7136+
staticvoid
7137+
ATPrepAddInherit(Relationchild_rel)
7138+
{
7139+
if (child_rel->rd_rel->reloftype)
7140+
ereport(ERROR,
7141+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
7142+
errmsg("cannot change inheritance of typed table")));
7143+
}
7144+
71187145
staticvoid
71197146
ATExecAddInherit(Relationchild_rel,RangeVar*parent)
71207147
{

‎src/test/regress/expected/typed_table.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ SELECT * FROM get_all_persons();
2525
----+------
2626
(0 rows)
2727

28+
-- certain ALTER TABLE operations on typed tables are not allowed
2829
ALTER TABLE persons ADD COLUMN comment text;
2930
ERROR: cannot add column to typed table
3031
ALTER TABLE persons DROP COLUMN name;
3132
ERROR: cannot drop column from typed table
3233
ALTER TABLE persons RENAME COLUMN id TO num;
3334
ERROR: cannot rename column of typed table
35+
ALTER TABLE persons ALTER COLUMN name TYPE varchar;
36+
ERROR: cannot alter column type of typed table
37+
CREATE TABLE stuff (id int);
38+
ALTER TABLE persons INHERIT stuff;
39+
ERROR: cannot change inheritance of typed table
3440
CREATE TABLE personsx OF person_type (myname WITH OPTIONS NOT NULL); -- error
3541
ERROR: column "myname" does not exist
3642
CREATE TABLE persons2 OF person_type (
@@ -83,3 +89,4 @@ DETAIL: drop cascades to table persons
8389
drop cascades to function get_all_persons()
8490
drop cascades to table persons2
8591
drop cascades to table persons3
92+
DROP TABLE stuff;

‎src/test/regress/sql/typed_table.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ $$;
1313

1414
SELECT*FROM get_all_persons();
1515

16+
-- certain ALTER TABLE operations on typed tables are not allowed
1617
ALTERTABLE persons ADD COLUMN commenttext;
1718
ALTERTABLE persons DROP COLUMN name;
1819
ALTERTABLE persons RENAME COLUMN id TO num;
20+
ALTERTABLE persons ALTER COLUMN name TYPEvarchar;
21+
CREATETABLEstuff (idint);
22+
ALTERTABLE persons INHERIT stuff;
1923

2024
CREATETABLEpersonsx OF person_type (myname WITH OPTIONSNOT NULL);-- error
2125

@@ -40,3 +44,5 @@ CREATE TABLE persons4 OF person_type (
4044

4145
DROPTYPE person_type RESTRICT;
4246
DROPTYPE person_type CASCADE;
47+
48+
DROPTABLE stuff;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp