8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.151 2008/05/16 23:36:04 tgl Exp $
11
+ * $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.152 2008/05/17 01:20:39 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -91,7 +91,7 @@ static Relation open_share_lock(SeqTable seq);
91
91
static void init_sequence (Oid relid , SeqTable * p_elm , Relation * p_rel );
92
92
static Form_pg_sequence read_info (SeqTable elm , Relation rel , Buffer * buf );
93
93
static void init_params (List * options , bool isInit ,
94
- Form_pg_sequence new , Form_pg_sequence old , List * * owned_by );
94
+ Form_pg_sequence new , List * * owned_by );
95
95
static void do_setval (Oid relid , int64 next , bool iscalled );
96
96
static void process_owned_by (Relation seqrel , List * owned_by );
97
97
@@ -119,7 +119,7 @@ DefineSequence(CreateSeqStmt *seq)
119
119
NameData name ;
120
120
121
121
/* Check and set all option values */
122
- init_params (seq -> options , true, & new , NULL , & owned_by );
122
+ init_params (seq -> options , true, & new , & owned_by );
123
123
124
124
/*
125
125
* Create relation (and fill value[] and null[] for the tuple)
@@ -357,8 +357,11 @@ AlterSequenceInternal(Oid relid, List *options)
357
357
seq = read_info (elm , seqrel , & buf );
358
358
page = BufferGetPage (buf );
359
359
360
- /* Fill workspace with appropriate new info */
361
- init_params (options , false, & new , seq , & owned_by );
360
+ /* Copy old values of options into workspace */
361
+ memcpy (& new , seq , sizeof (FormData_pg_sequence ));
362
+
363
+ /* Check and set new values */
364
+ init_params (options , false, & new , & owned_by );
362
365
363
366
/* Clear local cache so that we don't think we have cached numbers */
364
367
/* Note that we do not change the currval() state */
@@ -989,9 +992,10 @@ read_info(SeqTable elm, Relation rel, Buffer *buf)
989
992
*/
990
993
static void
991
994
init_params (List * options , bool isInit ,
992
- Form_pg_sequence new , Form_pg_sequence old , List * * owned_by )
995
+ Form_pg_sequence new , List * * owned_by )
993
996
{
994
- DefElem * last_value = NULL ;
997
+ DefElem * start_value = NULL ;
998
+ DefElem * restart_value = NULL ;
995
999
DefElem * increment_by = NULL ;
996
1000
DefElem * max_value = NULL ;
997
1001
DefElem * min_value = NULL ;
@@ -1001,12 +1005,6 @@ init_params(List *options, bool isInit,
1001
1005
1002
1006
* owned_by = NIL ;
1003
1007
1004
- /* Copy old values of options into workspace */
1005
- if (old != NULL )
1006
- memcpy (new , old , sizeof (FormData_pg_sequence ));
1007
- else
1008
- memset (new , 0 , sizeof (FormData_pg_sequence ));
1009
-
1010
1008
foreach (option , options )
1011
1009
{
1012
1010
DefElem * defel = (DefElem * ) lfirst (option );
@@ -1021,27 +1019,19 @@ init_params(List *options, bool isInit,
1021
1019
}
1022
1020
else if (strcmp (defel -> defname , "start" ) == 0 )
1023
1021
{
1024
- if (!isInit )
1025
- ereport (ERROR ,
1026
- (errcode (ERRCODE_SYNTAX_ERROR ),
1027
- errmsg ("use RESTART not START in ALTER SEQUENCE" )));
1028
- if (last_value )
1022
+ if (start_value )
1029
1023
ereport (ERROR ,
1030
1024
(errcode (ERRCODE_SYNTAX_ERROR ),
1031
1025
errmsg ("conflicting or redundant options" )));
1032
- last_value = defel ;
1026
+ start_value = defel ;
1033
1027
}
1034
1028
else if (strcmp (defel -> defname , "restart" ) == 0 )
1035
1029
{
1036
- if (isInit )
1037
- ereport (ERROR ,
1038
- (errcode (ERRCODE_SYNTAX_ERROR ),
1039
- errmsg ("use START not RESTART in CREATE SEQUENCE" )));
1040
- if (last_value )
1030
+ if (restart_value )
1041
1031
ereport (ERROR ,
1042
1032
(errcode (ERRCODE_SYNTAX_ERROR ),
1043
1033
errmsg ("conflicting or redundant options" )));
1044
- last_value = defel ;
1034
+ restart_value = defel ;
1045
1035
}
1046
1036
else if (strcmp (defel -> defname , "maxvalue" ) == 0 )
1047
1037
{
@@ -1145,30 +1135,15 @@ init_params(List *options, bool isInit,
1145
1135
bufm , bufx )));
1146
1136
}
1147
1137
1148
- /* START/RESTART [WITH] */
1149
- if (last_value != NULL )
1150
- {
1151
- if (last_value -> arg != NULL )
1152
- new -> last_value = defGetInt64 (last_value );
1153
- else
1154
- {
1155
- Assert (old != NULL );
1156
- new -> last_value = old -> start_value ;
1157
- }
1158
- if (isInit )
1159
- new -> start_value = new -> last_value ;
1160
- new -> is_called = false;
1161
- new -> log_cnt = 1 ;
1162
- }
1138
+ /* START WITH */
1139
+ if (start_value != NULL )
1140
+ new -> start_value = defGetInt64 (start_value );
1163
1141
else if (isInit )
1164
1142
{
1165
1143
if (new -> increment_by > 0 )
1166
1144
new -> start_value = new -> min_value ; /* ascending seq */
1167
1145
else
1168
1146
new -> start_value = new -> max_value ; /* descending seq */
1169
- new -> last_value = new -> start_value ;
1170
- new -> is_called = false;
1171
- new -> log_cnt = 1 ;
1172
1147
}
1173
1148
1174
1149
/* crosscheck START */
@@ -1197,7 +1172,24 @@ init_params(List *options, bool isInit,
1197
1172
bufs , bufm )));
1198
1173
}
1199
1174
1200
- /* must crosscheck RESTART separately */
1175
+ /* RESTART [WITH] */
1176
+ if (restart_value != NULL )
1177
+ {
1178
+ if (restart_value -> arg != NULL )
1179
+ new -> last_value = defGetInt64 (restart_value );
1180
+ else
1181
+ new -> last_value = new -> start_value ;
1182
+ new -> is_called = false;
1183
+ new -> log_cnt = 1 ;
1184
+ }
1185
+ else if (isInit )
1186
+ {
1187
+ new -> last_value = new -> start_value ;
1188
+ new -> is_called = false;
1189
+ new -> log_cnt = 1 ;
1190
+ }
1191
+
1192
+ /* crosscheck RESTART (or current value, if changing MIN/MAX) */
1201
1193
if (new -> last_value < new -> min_value )
1202
1194
{
1203
1195
char bufs [100 ],
0 commit comments