Support retrieving value from any sequence - Mailing list pgsql-hackers

From Thom Brown
Subject Support retrieving value from any sequence
Date
Msg-id CAA-aLv5ntC3TOmduvEgLRH+R08r3GtZ8e3a3j5rcjwuwygDjzw@mail.gmail.com
Whole thread Raw
Responses Re: Support retrieving value from any sequence  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: Support retrieving value from any sequence  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
Hi all,

When using currval() to find the current value of all sequences, it chokes on those that aren't initialised.  This is expected and documented as behaving in this manner.  However, I think it would be useful to also support retrieving the current value of a sequence, regardless of whether it's been used.  As this wouldn't be to get a sequence value for the current session, but all sessions, this would ideally get the real current value.

The use-case I have in mind is for finding out how close to the 32-bit integer limit sequences have reached.  At the moment, this isn't possible without creating a custom function to go fetch the last_value from the specified sequence.

So would it be desirable to have a function which accepts a sequence regclass as a parameter, and returns the last_value from the sequence?

Effectively, the same result as what this provides:

CREATE FUNCTION lastval(tablename regclass) RETURNS bigint AS $$
DECLARE
  last_value bigint;
BEGIN
  EXECUTE format('SELECT last_value FROM %I ', tablename) INTO last_value USING tablename;
  RETURN last_value;
END
$$ LANGUAGE plpgsql;

Thom

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: TABLESAMPLE patch is really in pretty sad shape
Next
From: Simon Riggs
Date:
Subject: Re: TABLESAMPLE patch is really in pretty sad shape