@@ -122,6 +122,24 @@ DefineSequence(CreateSeqStmt *seq)
122122 (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
123123 errmsg ("unlogged sequences are not supported" )));
124124
125+ /*
126+ * If if_not_exists was given and a relation with the same name already
127+ * exists, bail out. (Note: we needn't check this when not if_not_exists,
128+ * because DefineRelation will complain anyway.)
129+ */
130+ if (seq -> if_not_exists )
131+ {
132+ RangeVarGetAndCheckCreationNamespace (seq -> sequence , NoLock , & seqoid );
133+ if (OidIsValid (seqoid ))
134+ {
135+ ereport (NOTICE ,
136+ (errcode (ERRCODE_DUPLICATE_TABLE ),
137+ errmsg ("relation \"%s\" already exists, skipping" ,
138+ seq -> sequence -> relname )));
139+ return InvalidOid ;
140+ }
141+ }
142+
125143 /* Check and set all option values */
126144 init_params (seq -> options , true, & new , & owned_by );
127145
@@ -210,7 +228,7 @@ DefineSequence(CreateSeqStmt *seq)
210228 stmt -> options = NIL ;
211229 stmt -> oncommit = ONCOMMIT_NOOP ;
212230 stmt -> tablespacename = NULL ;
213- stmt -> if_not_exists = false ;
231+ stmt -> if_not_exists = seq -> if_not_exists ;
214232
215233 seqoid = DefineRelation (stmt , RELKIND_SEQUENCE , seq -> ownerId );
216234 Assert (seqoid != InvalidOid );
0 commit comments