From: Tom Lane Date: Thu, 25 Oct 2007 19:15:01 +0000 (+0000) Subject: Ugly patch to make ALTER SEQUENCE OWNED BY not affect the currval() state X-Git-Tag: REL8_2_6~36 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9f3309858f6520d0d7b71d40f563bf08af769259;p=postgresql Ugly patch to make ALTER SEQUENCE OWNED BY not affect the currval() state of the sequence. Since OWNED BY never existed before 8.2, this seems unlikely to create any compatibility issues. Other forms of ALTER SEQUENCE continue to do what they did before, namely update currval to match the sequence's actual last_val. That seems wrong on consideration, but we'll not change it in a minor release --- 8.3 will make that fix. --- diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index b73069cf1b..9404efa379 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.141 2006/10/06 17:13:58 petere Exp $ + * $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.141.2.1 2007/10/25 19:15:01 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -315,6 +315,7 @@ AlterSequence(AlterSeqStmt *stmt) Form_pg_sequence seq; FormData_pg_sequence new; List *owned_by; + int64 save_increment; /* open and AccessShareLock sequence */ relid = RangeVarGetRelid(stmt->sequence, false); @@ -325,6 +326,9 @@ AlterSequence(AlterSeqStmt *stmt) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS, stmt->sequence->relname); + /* hack to keep ALTER SEQUENCE OWNED BY from changing currval state */ + save_increment = elm->increment; + /* lock page' buffer and read tuple into new sequence structure */ seq = read_info(elm, seqrel, &buf); page = BufferGetPage(buf); @@ -338,10 +342,18 @@ AlterSequence(AlterSeqStmt *stmt) /* Now okay to update the on-disk tuple */ memcpy(seq, &new, sizeof(FormData_pg_sequence)); - /* Clear local cache so that we don't think we have cached numbers */ - elm->last = new.last_value; /* last returned number */ - elm->cached = new.last_value; /* last cached number (forget cached + if (owned_by) + { + /* Restore previous state of elm (assume nothing else changes) */ + elm->increment = save_increment; + } + else + { + /* Clear local cache so that we don't think we have cached numbers */ + elm->last = new.last_value; /* last returned number */ + elm->cached = new.last_value; /* last cached number (forget cached * values) */ + } START_CRIT_SECTION();