pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alex Balashov <abalashov@evaristesys.com>
To: pgsql-admin@lists.postgresql.org
Subject: Re: Sequences
Date: Tue, 4 Dec 2018 14:17:51 -0500
Message-ID: <20181204191751.GL769@tlaquepaque.localdomain> (raw)
In-Reply-To: <87E05184-8087-4225-BBCC-F60B74F18023@illinois.edu>
References: <87E05184-8087-4225-BBCC-F60B74F18023@illinois.edu>

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/




view thread (14+ messages)  latest in thread

Message-ID: <20181204191751.GL769@tlaquepaque.localdomain>
Permalink:  ../20181204191751.GL769@tlaquepaque.localdomain/
Also on:    postgresql.org/message-id/20181204191751.GL769@tlaquepaque.localdomain

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: abalashov@evaristesys.com, pgsql-admin@lists.postgresql.org
  Subject: Re: Sequences
  In-Reply-To: <20181204191751.GL769@tlaquepaque.localdomain>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox