> Attempts to return the id of the last value to be inserted into a table.
> You can either provide a sequence name (preferred) or provide a table
> name with optional schema. The $catalog and $field arguments are always 
> ignored.
> The current value of the sequence is returned by a call to the
> 'currval' PostgreSQL function. This will fail if the sequence has not yet
> been used in the current database connection.


This suffers from the same problems that currval does when using
connection pools tho.
 
I previously suggested a function similar to last_insert_id in behaviour,
and have attached it to this email for reference.

Even so, this also suffers from the same problems when using a connection pool.

The solution I proposed, namely having the tuple returned by
inserts/updates (perhaps even deletes?) would only mean changing the
client library to handle this, and as an example, libpg could easily
figure out the OID of said tuple and return that if it's present for
PQExec() (for backwards compatibility just as it does today,) and add a
separate PQExecSelect() that instead returns the tuple(s) as if they had
been SELECTed.

-- 
John Hansen <[EMAIL PROTECTED]>
GeekNET
#include "postgres.h"
#include "fmgr.h"
#include "storage/relfilenode.h"
#include "commands/sequence.h"

static int64 _lastval = 0;

PG_FUNCTION_INFO_V1(nextval_new);
Datum nextval_new(PG_FUNCTION_ARGS) {
    _lastval = DatumGetInt64(nextval(fcinfo));
    PG_RETURN_INT64(_lastval);
}

PG_FUNCTION_INFO_V1(lastval);
Datum lastval(PG_FUNCTION_ARGS) {
    PG_RETURN_INT64(_lastval);
}
SET search_path = pg_catalog;

BEGIN;

DELETE FROM pg_catalog.pg_proc WHERE proname = 'nextval';

CREATE FUNCTION nextval(text)
RETURNS bigint
AS 'lastval.so','nextval_new'
LANGUAGE 'C';

COMMENT ON FUNCTION nextval(text) IS 'sequence next value';

CREATE FUNCTION lastval()
RETURNS bigint
AS 'lastval.so','lastval'
LANGUAGE 'C';

COMMENT ON FUNCTION lastval() IS 'sequence last value';

COMMIT;
# -----------------------------------------------------------------------------

lastval :
	gcc -I /usr/include/postgresql/server/ -I /usr/include/postgresql/ -shared -o lastval.so lastval.c
	strip lastval.so

install :
	install -s -m 755 lastval.so $(DESTDIR)/usr/lib/postgresql/lib/;

clean :
	rm -f *.o *~ core *.so;

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])

Reply via email to