Skip to content

Commit 0156840

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 code backpatched to 9.0
1 parent 87e0b74 commit 0156840

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, bool isReset);
289289
static void ATExecSetStorage(Relation rel, const char *colName,
290290
Node *newValue);
291+
static void ATPrepDropColumn(Relation rel, bool recurse, AlterTableCmd *cmd);
291292
static void ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
292293
DropBehavior behavior,
293294
bool recurse, bool recursing,
@@ -327,7 +328,8 @@ static void ATExecEnableDisableTrigger(Relation rel, char *trigname,
327328
char fires_when, bool skip_system);
328329
static void ATExecEnableDisableRule(Relation rel, char *rulename,
329330
char fires_when);
330-
static void ATExecAddInherit(Relation rel, RangeVar *parent);
331+
static void ATPrepAddInherit(Relation child_rel);
332+
static void ATExecAddInherit(Relation child_rel, RangeVar *parent);
331333
static void ATExecDropInherit(Relation rel, RangeVar *parent);
332334
static void copy_relation_data(SMgrRelation rel, SMgrRelation dst,
333335
ForkNumber forkNum, bool istemp);
@@ -2499,10 +2501,8 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
24992501
break;
25002502
case AT_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
case AT_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+
case AT_AddInherit: /* INHERIT */
2583+
ATSimplePermissions(rel, false);
2584+
/* This command never recurses */
2585+
ATPrepAddInherit(rel);
2586+
pass = AT_PASS_MISC;
2587+
break;
25822588
case AT_EnableTrig: /* ENABLE TRIGGER variants */
25832589
case AT_EnableAlwaysTrig:
25842590
case AT_EnableReplicaTrig:
@@ -2591,8 +2597,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
25912597
case AT_EnableAlwaysRule:
25922598
case AT_EnableReplicaRule:
25932599
case AT_DisableRule:
2594-
case AT_AddInherit: /* INHERIT / NO INHERIT */
2595-
case AT_DropInherit:
2600+
case AT_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, Relation rel, bool recurse,
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_type tform;
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+
static void
4334+
ATPrepDropColumn(Relation rel, bool recurse, 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
static void
43294347
ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
43304348
DropBehavior behavior,
@@ -4337,11 +4355,6 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
43374355
List *children;
43384356
ObjectAddress object;
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+
static void
7137+
ATPrepAddInherit(Relation child_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
static void
71197146
ATExecAddInherit(Relation child_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
ALTER TABLE persons ADD COLUMN comment text;
1718
ALTER TABLE persons DROP COLUMN name;
1819
ALTER TABLE persons RENAME COLUMN id TO num;
20+
ALTER TABLE persons ALTER COLUMN name TYPE varchar;
21+
CREATE TABLE stuff (id int);
22+
ALTER TABLE persons INHERIT stuff;
1923

2024
CREATE TABLE personsx OF person_type (myname WITH OPTIONS NOT NULL); -- error
2125

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

4145
DROP TYPE person_type RESTRICT;
4246
DROP TYPE person_type CASCADE;
47+
48+
DROP TABLE stuff;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy