Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abuhujair Javed
Postgres FD Implementation
Commits
0076f264
Commit
0076f264
authored
Aug 26, 2014
by
Heikki Linnakangas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement IF NOT EXISTS for CREATE SEQUENCE.
Fabrízio de Royes Mello
parent
57ca1d4f
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
49 additions
and
2 deletions
+49
-2
doc/src/sgml/ref/create_sequence.sgml
doc/src/sgml/ref/create_sequence.sgml
+13
-1
src/backend/commands/sequence.c
src/backend/commands/sequence.c
+19
-1
src/backend/nodes/copyfuncs.c
src/backend/nodes/copyfuncs.c
+1
-0
src/backend/nodes/equalfuncs.c
src/backend/nodes/equalfuncs.c
+1
-0
src/backend/parser/gram.y
src/backend/parser/gram.y
+11
-0
src/include/nodes/parsenodes.h
src/include/nodes/parsenodes.h
+1
-0
src/test/regress/expected/sequence.out
src/test/regress/expected/sequence.out
+2
-0
src/test/regress/sql/sequence.sql
src/test/regress/sql/sequence.sql
+1
-0
No files found.
doc/src/sgml/ref/create_sequence.sgml
View file @
0076f264
...
...
@@ -21,7 +21,7 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
CREATE [ TEMPORARY | TEMP ] SEQUENCE <replaceable class="parameter">name</replaceable> [ INCREMENT [ BY ] <replaceable class="parameter">increment</replaceable> ]
CREATE [ TEMPORARY | TEMP ]
[ IF NOT EXISTS ]
SEQUENCE <replaceable class="parameter">name</replaceable> [ INCREMENT [ BY ] <replaceable class="parameter">increment</replaceable> ]
[ MINVALUE <replaceable class="parameter">minvalue</replaceable> | NO MINVALUE ] [ MAXVALUE <replaceable class="parameter">maxvalue</replaceable> | NO MAXVALUE ]
[ START [ WITH ] <replaceable class="parameter">start</replaceable> ] [ CACHE <replaceable class="parameter">cache</replaceable> ] [ [ NO ] CYCLE ]
[ OWNED BY { <replaceable class="parameter">table_name</replaceable>.<replaceable class="parameter">column_name</replaceable> | NONE } ]
...
...
@@ -89,6 +89,18 @@ SELECT * FROM <replaceable>name</replaceable>;
</listitem>
</varlistentry>
<varlistentry>
<term><literal>IF NOT EXISTS</literal></term>
<listitem>
<para>
Do not throw an error if a relation with the same name already exists.
A notice is issued in this case. Note that there is no guarantee that
the existing relation is anything like the sequence that would have
been created - it might not even be a sequence.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">name</replaceable></term>
<listitem>
...
...
src/backend/commands/sequence.c
View file @
0076f264
...
...
@@ -122,6 +122,24 @@ DefineSequence(CreateSeqStmt *seq)
(
errcode
(
ERRCODE_FEATURE_NOT_SUPPORTED
),
errmsg
(
"unlogged sequences are not supported"
)));
/*
* If if_not_exists was given and a relation with the same name already
* exists, bail out. (Note: we needn't check this when not if_not_exists,
* because DefineRelation will complain anyway.)
*/
if
(
seq
->
if_not_exists
)
{
RangeVarGetAndCheckCreationNamespace
(
seq
->
sequence
,
NoLock
,
&
seqoid
);
if
(
OidIsValid
(
seqoid
))
{
ereport
(
NOTICE
,
(
errcode
(
ERRCODE_DUPLICATE_TABLE
),
errmsg
(
"relation
\"
%s
\"
already exists, skipping"
,
seq
->
sequence
->
relname
)));
return
InvalidOid
;
}
}
/* Check and set all option values */
init_params
(
seq
->
options
,
true
,
&
new
,
&
owned_by
);
...
...
@@ -210,7 +228,7 @@ DefineSequence(CreateSeqStmt *seq)
stmt
->
options
=
NIL
;
stmt
->
oncommit
=
ONCOMMIT_NOOP
;
stmt
->
tablespacename
=
NULL
;
stmt
->
if_not_exists
=
false
;
stmt
->
if_not_exists
=
seq
->
if_not_exists
;
seqoid
=
DefineRelation
(
stmt
,
RELKIND_SEQUENCE
,
seq
->
ownerId
);
Assert
(
seqoid
!=
InvalidOid
);
...
...
src/backend/nodes/copyfuncs.c
View file @
0076f264
...
...
@@ -3330,6 +3330,7 @@ _copyCreateSeqStmt(const CreateSeqStmt *from)
COPY_NODE_FIELD
(
sequence
);
COPY_NODE_FIELD
(
options
);
COPY_SCALAR_FIELD
(
ownerId
);
COPY_SCALAR_FIELD
(
if_not_exists
);
return
newnode
;
}
...
...
src/backend/nodes/equalfuncs.c
View file @
0076f264
...
...
@@ -1566,6 +1566,7 @@ _equalCreateSeqStmt(const CreateSeqStmt *a, const CreateSeqStmt *b)
COMPARE_NODE_FIELD
(
sequence
);
COMPARE_NODE_FIELD
(
options
);
COMPARE_SCALAR_FIELD
(
ownerId
);
COMPARE_SCALAR_FIELD
(
if_not_exists
);
return
true
;
}
...
...
src/backend/parser/gram.y
View file @
0076f264
...
...
@@ -3486,6 +3486,17 @@ CreateSeqStmt:
n->sequence = $4;
n->options = $5;
n->ownerId = InvalidOid;
n->if_not_exists = false;
$$ = (Node *)n;
}
| CREATE OptTemp SEQUENCE IF_P NOT EXISTS qualified_name OptSeqOptList
{
CreateSeqStmt *n = makeNode(CreateSeqStmt);
$7->relpersistence = $2;
n->sequence = $7;
n->options = $8;
n->ownerId = InvalidOid;
n->if_not_exists = true;
$$ = (Node *)n;
}
;
...
...
src/include/nodes/parsenodes.h
View file @
0076f264
...
...
@@ -1979,6 +1979,7 @@ typedef struct CreateSeqStmt
RangeVar
*
sequence
;
/* the sequence to create */
List
*
options
;
Oid
ownerId
;
/* ID of owner, or InvalidOid for default */
bool
if_not_exists
;
/* just do nothing if it already exists? */
}
CreateSeqStmt
;
typedef
struct
AlterSeqStmt
...
...
src/test/regress/expected/sequence.out
View file @
0076f264
...
...
@@ -91,6 +91,8 @@ SELECT nextval('serialTest2_f6_seq');
-- basic sequence operations using both text and oid references
CREATE SEQUENCE sequence_test;
CREATE SEQUENCE IF NOT EXISTS sequence_test;
NOTICE: relation "sequence_test" already exists, skipping
SELECT nextval('sequence_test'::text);
nextval
---------
...
...
src/test/regress/sql/sequence.sql
View file @
0076f264
...
...
@@ -59,6 +59,7 @@ SELECT nextval('serialTest2_f6_seq');
-- basic sequence operations using both text and oid references
CREATE
SEQUENCE
sequence_test
;
CREATE
SEQUENCE
IF
NOT
EXISTS
sequence_test
;
SELECT
nextval
(
'sequence_test'
::
text
);
SELECT
nextval
(
'sequence_test'
::
regclass
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment