Re: Sequences

From: Alex Balashov <abalashov(at)evaristesys(dot)com>
To: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Re: Sequences
Date: 2018-12-04 19:17:51
Message-ID: 20181204191751.GL769@tlaquepaque.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Constructing dynamic SQL is always a bit tricky. Try define this
function:

---
CREATE OR REPLACE FUNCTION seqs_last_val()
RETURNS SETOF record
AS $$
DECLARE
_seqname varchar;
_r record;
BEGIN
SELECT INTO _r null::varchar AS seqname, -1::integer AS lastval;

FOR _seqname IN
SELECT sequence_name
FROM information_schema.sequences
WHERE sequence_schema = 'public'
LOOP
_r.seqname = _seqname;
EXECUTE format('SELECT last_value FROM %s', quote_ident(_seqname)) INTO _r.lastval;
RETURN NEXT _r;
END LOOP;

RETURN;
END
$$ LANGUAGE 'plpgsql';
---

Then run:

SELECT * FROM seqs_last_val() AS (seqname varchar, last_value integer);

-- Alex

On Tue, Dec 04, 2018 at 07:03:11PM +0000, Campbell, Lance wrote:
> PostgreSQL 10.x
>
> What query will give the name of all sequences in a database and the current or next value for each sequence?
>
> This will give me everything except for the next value in the sequence.
>
> SELECT * FROM information_schema.sequences;
>
> Thanks,
>
> Lance

--
Alex Balashov | Principal | Evariste Systems LLC

Tel: +1-706-510-6800 / +1-800-250-5920 (toll-free)
Web: http://www.evaristesys.com/, http://www.csrpswitch.com/

In response to

  • Sequences at 2018-12-04 19:03:11 from Campbell, Lance

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Alex Balashov 2018-12-04 19:26:50 Re: Sequences
Previous Message Campbell, Lance 2018-12-04 19:03:11 Sequences