Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUGCl-0006k9-W4 for pgsql-admin@arkaria.postgresql.org; Tue, 04 Dec 2018 19:18:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUGCj-0003aE-Ja for pgsql-admin@arkaria.postgresql.org; Tue, 04 Dec 2018 19:17:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUGCj-0003a7-8d for pgsql-admin@lists.postgresql.org; Tue, 04 Dec 2018 19:17:57 +0000 Received: from allegro-1.evaristesys.com ([209.51.167.66] helo=evaristesys.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUGCg-0006VT-C2 for pgsql-admin@lists.postgresql.org; Tue, 04 Dec 2018 19:17:56 +0000 Received: from [208.71.233.170] (helo=tlaquepaque.localdomain) by evaristesys.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1gUGCe-0004CM-QK for pgsql-admin@lists.postgresql.org; Tue, 04 Dec 2018 14:17:52 -0500 Date: Tue, 4 Dec 2018 14:17:51 -0500 From: Alex Balashov To: pgsql-admin@lists.postgresql.org Subject: Re: Sequences Message-ID: <20181204191751.GL769@tlaquepaque.localdomain> References: <87E05184-8087-4225-BBCC-F60B74F18023@illinois.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87E05184-8087-4225-BBCC-F60B74F18023@illinois.edu> Organization: Evariste Systems User-Agent: Mutt/1.11.1 (2018-12-01) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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/