pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Campbell, Lance <lance@illinois.edu>
To: Alex Balashov <abalashov@evaristesys.com>
To: pgsql-admin@lists.postgresql.org <pgsql-admin@lists.postgresql.org>
Subject: Re: Sequences
Date: Tue, 4 Dec 2018 19:47:19 +0000
Message-ID: <F58557BC-FA88-48F8-A1CF-69547570FBA6@illinois.edu> (raw)
In-Reply-To: <20181204191751.GL769@tlaquepaque.localdomain>
References: <87E05184-8087-4225-BBCC-F60B74F18023@illinois.edu>
	<20181204191751.GL769@tlaquepaque.localdomain>

Thanks so much.  This was very helpful!  Thanks.

Lance

On 12/4/18, 1:18 PM, "Alex Balashov" <abalashov@evaristesys.com> wrote:

    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: <F58557BC-FA88-48F8-A1CF-69547570FBA6@illinois.edu>
Permalink:  ../F58557BC-FA88-48F8-A1CF-69547570FBA6@illinois.edu/
Also on:    postgresql.org/message-id/F58557BC-FA88-48F8-A1CF-69547570FBA6@illinois.edu

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: lance@illinois.edu, abalashov@evaristesys.com
  Subject: Re: Sequences
  In-Reply-To: <F58557BC-FA88-48F8-A1CF-69547570FBA6@illinois.edu>
  References: <87E05184-8087-4225-BBCC-F60B74F18023@illinois.edu> <20181204191751.GL769@tlaquepaque.localdomain> <F58557BC-FA88-48F8-A1CF-69547570FBA6@illinois.edu>

* 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