> Hi list,
>
> attached, cvs context diff that adds currval_isset('sequence_name');
>
Updated patch attached, didn't think to update the docs. Thanks oicu!
Kind Regards,
John Hansen
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.221
diff -c -r1.221 func.sgml
*** doc/src/sgml/func.sgml 26 Oct 2004 22:16:11 -0000 1.221
--- doc/src/sgml/func.sgml 7 Nov 2004 22:49:04 -0000
***************
*** 6348,6353 ****
--- 6348,6356 ----
<primary>currval</primary>
</indexterm>
<indexterm>
+ <primary>currval_isset</primary>
+ </indexterm>
+ <indexterm>
<primary>setval</primary>
</indexterm>
***************
*** 6382,6387 ****
--- 6385,6395 ----
<entry>Return value most recently obtained with <function>nextval</function></entry>
</row>
<row>
+ <entry><literal><function>currval_isset</function>(<type>text</type>)</literal></entry>
+ <entry><type>bool</type></entry>
+ <entry>True if <function>nextval</function> has been called in the current session, indicating that <function>currval</function> would return a value.</entry>
+ </row>
+ <row>
<entry><literal><function>setval</function>(<type>text</type>, <type>bigint</type>)</literal></entry>
<entry><type>bigint</type></entry>
<entry>Set sequence's current value</entry>
***************
*** 6447,6452 ****
--- 6455,6472 ----
</varlistentry>
<varlistentry>
+ <term><function>currval_isset</function></term>
+ <listitem>
+ <para>
+ Return true if <function>nextval</function> for this sequence has been
+ called in the current session. Notice that because this is returning
+ a response based on a session-local value, it gives a predictable answer
+ even if other sessions are executing <function>nextval</function> meanwhile.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><function>setval</function></term>
<listitem>
<para>
Index: doc/src/sgml/ref/create_sequence.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/ref/create_sequence.sgml,v
retrieving revision 1.41
diff -c -r1.41 create_sequence.sgml
*** doc/src/sgml/ref/create_sequence.sgml 12 Jul 2004 05:36:56 -0000 1.41
--- doc/src/sgml/ref/create_sequence.sgml 7 Nov 2004 22:49:04 -0000
***************
*** 49,55 ****
<para>
After a sequence is created, you use the functions
<function>nextval</function>,
! <function>currval</function>, and
<function>setval</function>
to operate on the sequence. These functions are documented in
<xref linkend="functions-sequence">.
--- 49,56 ----
<para>
After a sequence is created, you use the functions
<function>nextval</function>,
! <function>currval</function>,
! <function>currval_isset</function>, and
<function>setval</function>
to operate on the sequence. These functions are documented in
<xref linkend="functions-sequence">.
Index: src/backend/commands/sequence.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/commands/sequence.c,v
retrieving revision 1.117
diff -c -r1.117 sequence.c
*** src/backend/commands/sequence.c 16 Sep 2004 16:58:28 -0000 1.117
--- src/backend/commands/sequence.c 7 Nov 2004 22:49:04 -0000
***************
*** 608,613 ****
--- 608,642 ----
PG_RETURN_INT64(result);
}
+ Datum
+ currval_isset(PG_FUNCTION_ARGS)
+ {
+ text *seqin = PG_GETARG_TEXT_P(0);
+ RangeVar *sequence;
+ SeqTable elm;
+ Relation seqrel;
+
+ sequence = makeRangeVarFromNameList(textToQualifiedNameList(seqin,
+ "currval"));
+
+ /* open and AccessShareLock sequence */
+ init_sequence(sequence, &elm, &seqrel);
+
+ if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_SELECT) != ACLCHECK_OK)
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ errmsg("permission denied for sequence %s",
+ sequence->relname)));
+
+ if (elm->increment == 0) { /* nextval/read_info were not called */
+ relation_close(seqrel, NoLock);
+ PG_RETURN_BOOL(false);
+ }
+
+ relation_close(seqrel, NoLock);
+ PG_RETURN_BOOL(true);
+ }
+
/*
* Main internal procedure that handles 2 & 3 arg forms of SETVAL.
*
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.348
diff -c -r1.348 pg_proc.h
*** src/include/catalog/pg_proc.h 7 Oct 2004 18:38:50 -0000 1.348
--- src/include/catalog/pg_proc.h 7 Nov 2004 22:49:05 -0000
***************
*** 2034,2039 ****
--- 2034,2041 ----
DESCR("sequence next value");
DATA(insert OID = 1575 ( currval PGNSP PGUID 12 f f t f v 1 20 "25" _null_ currval - _null_ ));
DESCR("sequence current value");
+ DATA(insert OID = 1295 ( currval_isset PGNSP PGUID 12 f f t f v 1 16 "25" _null_ currval_isset - _null_ ));
+ DESCR("sequence has current value");
DATA(insert OID = 1576 ( setval PGNSP PGUID 12 f f t f v 2 20 "25 20" _null_ setval - _null_ ));
DESCR("set sequence value");
DATA(insert OID = 1765 ( setval PGNSP PGUID 12 f f t f v 3 20 "25 20 16" _null_ setval_and_iscalled - _null_ ));
Index: src/include/commands/sequence.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/commands/sequence.h,v
retrieving revision 1.29
diff -c -r1.29 sequence.h
*** src/include/commands/sequence.h 29 Aug 2004 04:13:05 -0000 1.29
--- src/include/commands/sequence.h 7 Nov 2004 22:49:05 -0000
***************
*** 82,87 ****
--- 82,88 ----
extern Datum nextval(PG_FUNCTION_ARGS);
extern Datum currval(PG_FUNCTION_ARGS);
+ extern Datum currval_isset(PG_FUNCTION_ARGS);
extern Datum setval(PG_FUNCTION_ARGS);
extern Datum setval_and_iscalled(PG_FUNCTION_ARGS);
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster