Support retrieving value from any sequence
| От | Thom Brown | 
|---|---|
| Тема | Support retrieving value from any sequence | 
| Дата | |
| Msg-id | CAA-aLv5ntC3TOmduvEgLRH+R08r3GtZ8e3a3j5rcjwuwygDjzw@mail.gmail.com обсуждение исходный текст | 
| Ответы | Re: Support retrieving value from any sequence Re: Support retrieving value from any sequence | 
| Список | 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.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;
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 по дате отправления: