> Hmmm,.... is mysql's last_insert_id behaviour really that much to ask
> for?

Come to think of it,.... maybe the attached last_insert_id() patch is a
better option. Actually copying the behaviour of mysql's counterpart.

... John
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	8 Nov 2004 01:48:33 -0000
***************
*** 6348,6353 ****
--- 6348,6356 ----
     <primary>currval</primary>
    </indexterm>
    <indexterm>
+    <primary>last_insert_id</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>last_insert_id</function>()</literal></entry>
+ 	<entry><type>int64</type></entry>
+ 	<entry>Return value generated by <function>nextval</function> in the current session for ANY sequence.</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,6470 ----
       </varlistentry>
  
       <varlistentry>
+       <term><function>last_insert_id</function></term>
+       <listitem>
+        <para>
+         Return the value of last number generated by <function>nextval</function> for ANY sequence.
+ 	called in the current session.
+        </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	8 Nov 2004 01:48:33 -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>last_insert_id</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	8 Nov 2004 01:48:33 -0000
***************
*** 68,73 ****
--- 68,74 ----
  
  static SeqTable seqtab = NULL;	/* Head of list of SeqTable items */
  
+ static int64 _last_insert_id = 0;
  
  static void init_sequence(RangeVar *relation,
  			  SeqTable *p_elm, Relation *p_rel);
***************
*** 407,412 ****
--- 408,414 ----
  	{
  		elm->last += elm->increment;
  		relation_close(seqrel, NoLock);
+ 		_last_insert_id = elm->last;
  		PG_RETURN_INT64(elm->last);
  	}
  
***************
*** 570,576 ****
  	WriteBuffer(buf);
  
  	relation_close(seqrel, NoLock);
! 
  	PG_RETURN_INT64(result);
  }
  
--- 572,579 ----
  	WriteBuffer(buf);
  
  	relation_close(seqrel, NoLock);
!     
! 	_last_insert_id = result;
  	PG_RETURN_INT64(result);
  }
  
***************
*** 608,613 ****
--- 611,622 ----
  	PG_RETURN_INT64(result);
  }
  
+ Datum
+ last_insert_id(PG_FUNCTION_ARGS)
+ {
+ 	PG_RETURN_INT64(_last_insert_id);
+ }
+ 
  /*
   * 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	8 Nov 2004 01:48:44 -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 (  last_insert_id		PGNSP PGUID 12 f f t f v 0 20 "" _null_	last_insert_id - _null_ ));
+ DESCR("last generated sequence 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	8 Nov 2004 01:48:44 -0000
***************
*** 82,87 ****
--- 82,88 ----
  
  extern Datum nextval(PG_FUNCTION_ARGS);
  extern Datum currval(PG_FUNCTION_ARGS);
+ extern Datum last_insert_id(PG_FUNCTION_ARGS);
  extern Datum setval(PG_FUNCTION_ARGS);
  extern Datum setval_and_iscalled(PG_FUNCTION_ARGS);
  
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to [EMAIL PROTECTED] so that your
      message can get through to the mailing list cleanly

Reply via email to