66 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
9- *$Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.225 2002/03/31 06:26:31 tgl Exp $
9+ *$Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.226 2002/04/02 06:30:34 tgl Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
3535#include "rewrite/rewriteManip.h"
3636#include "utils/builtins.h"
3737#include "utils/fmgroids.h"
38+ #include "utils/lsyscache.h"
3839#include "utils/relcache.h"
3940#include "utils/syscache.h"
4041#ifdef MULTIBYTE
@@ -801,25 +802,43 @@ transformColumnDefinition(ParseState *pstate, CreateStmtContext *cxt,
801802if (is_serial )
802803{
803804char * sname ;
805+ char * snamespace ;
804806char * qstring ;
805807A_Const * snamenode ;
806808FuncCall * funccallnode ;
807- CreateSeqStmt * sequence ;
809+ CreateSeqStmt * seqstmt ;
810+
811+ /*
812+ * Determine name and namespace to use for the sequence.
813+ */
814+ sname = makeObjectName (cxt -> relation -> relname ,column -> colname ,"seq" );
815+ snamespace = get_namespace_name (RangeVarGetCreationNamespace (cxt -> relation ));
816+
817+ elog (NOTICE ,"%s will create implicit sequence '%s' for SERIAL column '%s.%s'" ,
818+ cxt -> stmtType ,sname ,cxt -> relation -> relname ,column -> colname );
819+
820+ /*
821+ * Build a CREATE SEQUENCE command to create the sequence object,
822+ * and add it to the list of things to be done before this
823+ * CREATE/ALTER TABLE.
824+ */
825+ seqstmt = makeNode (CreateSeqStmt );
826+ seqstmt -> sequence = makeRangeVar (snamespace ,sname );
827+ seqstmt -> options = NIL ;
828+
829+ cxt -> blist = lappend (cxt -> blist ,seqstmt );
808830
809831/*
810832 * Create appropriate constraints for SERIAL. We do this in full,
811833 * rather than shortcutting, so that we will detect any
812834 * conflicting constraints the user wrote (like a different
813835 * DEFAULT).
814- */
815- sname = makeObjectName ((cxt -> relation )-> relname ,column -> colname ,"seq" );
816-
817- /*
836+ *
818837 * Create an expression tree representing the function call
819838 * nextval('"sequencename"')
820839 */
821- qstring = palloc (strlen (sname )+ 2 + 1 );
822- sprintf (qstring ,"\"%s\"" ,sname );
840+ qstring = palloc (strlen (snamespace ) + strlen ( sname )+ 5 + 1 );
841+ sprintf (qstring ,"\"%s\".\"%s\"" , snamespace ,sname );
823842snamenode = makeNode (A_Const );
824843snamenode -> val .type = T_String ;
825844snamenode -> val .val .str = qstring ;
@@ -845,21 +864,6 @@ transformColumnDefinition(ParseState *pstate, CreateStmtContext *cxt,
845864constraint = makeNode (Constraint );
846865constraint -> contype = CONSTR_NOTNULL ;
847866column -> constraints = lappend (column -> constraints ,constraint );
848-
849- /*
850- * Build a CREATE SEQUENCE command to create the sequence object,
851- * and add it to the list of things to be done before this
852- * CREATE/ALTER TABLE.
853- */
854- sequence = makeNode (CreateSeqStmt );
855- sequence -> sequence = copyObject (cxt -> relation );
856- sequence -> sequence -> relname = pstrdup (sname );
857- sequence -> options = NIL ;
858-
859- elog (NOTICE ,"%s will create implicit sequence '%s' for SERIAL column '%s.%s'" ,
860- cxt -> stmtType ,sequence -> sequence -> relname , (cxt -> relation )-> relname ,column -> colname );
861-
862- cxt -> blist = lappend (cxt -> blist ,sequence );
863867}
864868
865869/* Process column constraints, if any... */