Skip to content

Commit e97b8f2

Browse files
author
Neil Conway
committed
Add CREATE TRIGGER, CREATE INDEX, and CREATE SEQUENCE to the list of
expressions supported by CREATE SCHEMA. Also added the beginning of some regression tests for CREATE SCHEMA; plenty more work is needed here.
1 parent 4cdf51e commit e97b8f2

File tree

7 files changed

+155
-31
lines changed

7 files changed

+155
-31
lines changed

doc/src/sgml/ref/create_schema.sgml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_schema.sgml,v 1.9 2003/11/29 19:51:38 pgsql Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_schema.sgml,v 1.10 2004/01/11 04:58:17 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -84,11 +84,13 @@ CREATE SCHEMA AUTHORIZATION <replaceable class="parameter">username</replaceable
8484
<term><replaceable class="parameter">schema_element</replaceable></term>
8585
<listitem>
8686
<para>
87-
An SQL statement defining an object to be created within the schema.
88-
Currently, only <command>CREATE TABLE</>, <command>CREATE VIEW</>,
89-
and <command>GRANT</> are accepted as clauses within
90-
<command>CREATE SCHEMA</>. Other kinds of objects may be created
91-
in separate commands after the schema is created.
87+
An SQL statement defining an object to be created within the
88+
schema. Currently, only <command>CREATE
89+
TABLE</>, <command>CREATE VIEW</>, <command>CREATE
90+
INDEX</>, <command>CREATE SEQUENCE</>, <command>CREATE
91+
TRIGGER</> and <command>GRANT</> are accepted as clauses
92+
within <command>CREATE SCHEMA</>. Other kinds of objects may
93+
be created in separate commands after the schema is created.
9294
</para>
9395
</listitem>
9496
</varlistentry>

src/backend/parser/analyze.c

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.294 2004/01/10 23:28:45 neilc Exp $
9+
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.295 2004/01/11 04:58:17 neilc Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -54,8 +54,11 @@ typedef struct
5454
const char *stmtType; /* "CREATE SCHEMA" or "ALTER SCHEMA" */
5555
char *schemaname; /* name of schema */
5656
char *authid; /* owner of schema */
57+
List *sequences; /* CREATE SEQUENCE items */
5758
List *tables; /* CREATE TABLE items */
5859
List *views; /* CREATE VIEW items */
60+
List *indexes; /* CREATE INDEX items */
61+
List *triggers; /* CREATE TRIGGER items */
5962
List *grants; /* GRANT items */
6063
List *fwconstraints; /* Forward referencing FOREIGN KEY
6164
* constraints */
@@ -3152,13 +3155,28 @@ transformColumnType(ParseState *pstate, ColumnDef *column)
31523155
ReleaseSysCache(ctype);
31533156
}
31543157

3158+
static void
3159+
setSchemaName(char *context_schema, char **stmt_schema_name)
3160+
{
3161+
if (*stmt_schema_name == NULL)
3162+
*stmt_schema_name = context_schema;
3163+
else if (strcmp(context_schema, *stmt_schema_name) != 0)
3164+
ereport(ERROR,
3165+
(errcode(ERRCODE_INVALID_SCHEMA_DEFINITION),
3166+
errmsg("CREATE specifies a schema (%s) "
3167+
"different from the one being created (%s)",
3168+
*stmt_schema_name, context_schema)));
3169+
}
3170+
31553171
/*
31563172
* analyzeCreateSchemaStmt -
31573173
* analyzes the "create schema" statement
31583174
*
31593175
* Split the schema element list into individual commands and place
3160-
* them in the result list in an order such that there are no
3161-
* forward references (e.g. GRANT to a table created later in the list).
3176+
* them in the result list in an order such that there are no forward
3177+
* references (e.g. GRANT to a table created later in the list). Note
3178+
* that the logic we use for determining forward references is
3179+
* presently quite incomplete.
31623180
*
31633181
* SQL92 also allows constraints to make forward references, so thumb through
31643182
* the table columns and move forward references to a posterior alter-table
@@ -3168,7 +3186,7 @@ transformColumnType(ParseState *pstate, ColumnDef *column)
31683186
* but we can't analyze the later commands until we've executed the earlier
31693187
* ones, because of possible inter-object references.
31703188
*
3171-
* Note: Called from commands/command.c
3189+
* Note: Called from commands/schemacmds.c
31723190
*/
31733191
List *
31743192
analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
@@ -3180,9 +3198,12 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
31803198
cxt.stmtType = "CREATE SCHEMA";
31813199
cxt.schemaname = stmt->schemaname;
31823200
cxt.authid = stmt->authid;
3201+
cxt.sequences = NIL;
31833202
cxt.tables = NIL;
31843203
cxt.views = NIL;
3204+
cxt.indexes = NIL;
31853205
cxt.grants = NIL;
3206+
cxt.triggers = NIL;
31863207
cxt.fwconstraints = NIL;
31873208
cxt.alters = NIL;
31883209
cxt.blist = NIL;
@@ -3198,23 +3219,24 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
31983219

31993220
switch (nodeTag(element))
32003221
{
3222+
case T_CreateSeqStmt:
3223+
{
3224+
CreateSeqStmt *elp = (CreateSeqStmt *) element;
3225+
3226+
setSchemaName(cxt.schemaname, &elp->sequence->schemaname);
3227+
cxt.sequences = lappend(cxt.sequences, element);
3228+
}
3229+
break;
3230+
32013231
case T_CreateStmt:
32023232
{
32033233
CreateStmt *elp = (CreateStmt *) element;
32043234

3205-
if (elp->relation->schemaname == NULL)
3206-
elp->relation->schemaname = cxt.schemaname;
3207-
else if (strcmp(cxt.schemaname, elp->relation->schemaname) != 0)
3208-
ereport(ERROR,
3209-
(errcode(ERRCODE_INVALID_SCHEMA_DEFINITION),
3210-
errmsg("CREATE specifies a schema (%s)"
3211-
" different from the one being created (%s)",
3212-
elp->relation->schemaname, cxt.schemaname)));
3235+
setSchemaName(cxt.schemaname, &elp->relation->schemaname);
32133236

32143237
/*
32153238
* XXX todo: deal with constraints
32163239
*/
3217-
32183240
cxt.tables = lappend(cxt.tables, element);
32193241
}
32203242
break;
@@ -3223,23 +3245,33 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
32233245
{
32243246
ViewStmt *elp = (ViewStmt *) element;
32253247

3226-
if (elp->view->schemaname == NULL)
3227-
elp->view->schemaname = cxt.schemaname;
3228-
else if (strcmp(cxt.schemaname, elp->view->schemaname) != 0)
3229-
ereport(ERROR,
3230-
(errcode(ERRCODE_INVALID_SCHEMA_DEFINITION),
3231-
errmsg("CREATE specifies a schema (%s)"
3232-
" different from the one being created (%s)",
3233-
elp->view->schemaname, cxt.schemaname)));
3248+
setSchemaName(cxt.schemaname, &elp->view->schemaname);
32343249

32353250
/*
32363251
* XXX todo: deal with references between views
32373252
*/
3238-
32393253
cxt.views = lappend(cxt.views, element);
32403254
}
32413255
break;
32423256

3257+
case T_IndexStmt:
3258+
{
3259+
IndexStmt *elp = (IndexStmt *) element;
3260+
3261+
setSchemaName(cxt.schemaname, &elp->relation->schemaname);
3262+
cxt.indexes = lappend(cxt.indexes, element);
3263+
}
3264+
break;
3265+
3266+
case T_CreateTrigStmt:
3267+
{
3268+
CreateTrigStmt *elp = (CreateTrigStmt *) element;
3269+
3270+
setSchemaName(cxt.schemaname, &elp->relation->schemaname);
3271+
cxt.triggers = lappend(cxt.triggers, element);
3272+
}
3273+
break;
3274+
32433275
case T_GrantStmt:
32443276
cxt.grants = lappend(cxt.grants, element);
32453277
break;
@@ -3251,8 +3283,11 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
32513283
}
32523284

32533285
result = NIL;
3286+
result = nconc(result, cxt.sequences);
32543287
result = nconc(result, cxt.tables);
32553288
result = nconc(result, cxt.views);
3289+
result = nconc(result, cxt.indexes);
3290+
result = nconc(result, cxt.triggers);
32563291
result = nconc(result, cxt.grants);
32573292

32583293
return result;

src/backend/parser/gram.y

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.445 2004/01/10 23:28:45 neilc Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.446 2004/01/11 04:58:17 neilc Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -815,6 +815,9 @@ OptSchemaEltList:
815815
*/
816816
schema_stmt:
817817
CreateStmt
818+
| IndexStmt
819+
| CreateSeqStmt
820+
| CreateTrigStmt
818821
| GrantStmt
819822
| ViewStmt
820823
;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--
2+
-- Regression tests for schemas (namespaces)
3+
--
4+
CREATE SCHEMA test_schema_1
5+
CREATE UNIQUE INDEX abc_a_idx ON abc (a)
6+
CREATE VIEW abc_view AS
7+
SELECT a+1 AS a, b+1 AS b FROM abc
8+
CREATE TABLE abc (
9+
a serial,
10+
b int UNIQUE
11+
);
12+
NOTICE: CREATE TABLE will create implicit sequence "abc_a_seq" for "serial" column "abc.a"
13+
NOTICE: CREATE TABLE / UNIQUE will create implicit index "abc_b_key" for table "abc"
14+
-- verify that the objects were created
15+
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
16+
(SELECT oid FROM pg_namespace WHERE nspname = 'test_schema_1');
17+
count
18+
-------
19+
5
20+
(1 row)
21+
22+
INSERT INTO test_schema_1.abc DEFAULT VALUES;
23+
INSERT INTO test_schema_1.abc DEFAULT VALUES;
24+
INSERT INTO test_schema_1.abc DEFAULT VALUES;
25+
SELECT * FROM test_schema_1.abc;
26+
a | b
27+
---+---
28+
1 |
29+
2 |
30+
3 |
31+
(3 rows)
32+
33+
SELECT * FROM test_schema_1.abc_view;
34+
a | b
35+
---+---
36+
2 |
37+
3 |
38+
4 |
39+
(3 rows)
40+
41+
DROP SCHEMA test_schema_1 CASCADE;
42+
NOTICE: drop cascades to view test_schema_1.abc_view
43+
NOTICE: drop cascades to rule _RETURN on view test_schema_1.abc_view
44+
NOTICE: drop cascades to table test_schema_1.abc
45+
-- verify that the objects were dropped
46+
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
47+
(SELECT oid FROM pg_namespace WHERE nspname = 'test_schema_1');
48+
count
49+
-------
50+
0
51+
(1 row)
52+

src/test/regress/parallel_schedule

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ignore: random
6060
# ----------
6161
# The fourth group of parallel test
6262
# ----------
63-
test: select_into select_distinct select_distinct_on select_implicit select_having subselect union case join aggregates transactions random portals arrays btree_index hash_index update
63+
test: select_into select_distinct select_distinct_on select_implicit select_having subselect union case join aggregates transactions random portals arrays btree_index hash_index update namespace
6464

6565
test: privileges
6666
test: misc

src/test/regress/serial_schedule

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $PostgreSQL: pgsql/src/test/regress/serial_schedule,v 1.23 2003/11/29 19:52:14 pgsql Exp $
1+
# $PostgreSQL: pgsql/src/test/regress/serial_schedule,v 1.24 2004/01/11 04:58:17 neilc Exp $
22
# This should probably be in an order similar to parallel_schedule.
33
test: boolean
44
test: char
@@ -73,6 +73,7 @@ test: arrays
7373
test: btree_index
7474
test: hash_index
7575
test: update
76+
test: namespace
7677
test: privileges
7778
test: misc
7879
test: select_views

src/test/regress/sql/namespace.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--
2+
-- Regression tests for schemas (namespaces)
3+
--
4+
5+
CREATE SCHEMA test_schema_1
6+
CREATE UNIQUE INDEX abc_a_idx ON abc (a)
7+
8+
CREATE VIEW abc_view AS
9+
SELECT a+1 AS a, b+1 AS b FROM abc
10+
11+
CREATE TABLE abc (
12+
a serial,
13+
b int UNIQUE
14+
);
15+
16+
-- verify that the objects were created
17+
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
18+
(SELECT oid FROM pg_namespace WHERE nspname = 'test_schema_1');
19+
20+
INSERT INTO test_schema_1.abc DEFAULT VALUES;
21+
INSERT INTO test_schema_1.abc DEFAULT VALUES;
22+
INSERT INTO test_schema_1.abc DEFAULT VALUES;
23+
24+
SELECT * FROM test_schema_1.abc;
25+
SELECT * FROM test_schema_1.abc_view;
26+
27+
DROP SCHEMA test_schema_1 CASCADE;
28+
29+
-- verify that the objects were dropped
30+
SELECT COUNT(*) FROM pg_class WHERE relnamespace =
31+
(SELECT oid FROM pg_namespace WHERE nspname = 'test_schema_1');

0 commit comments

Comments
 (0)