Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 28D186421A7 for ; Fri, 4 Dec 2009 14:39:22 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 14932-07 for ; Fri, 4 Dec 2009 18:39:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.fmed.uba.ar (mail.fmed.uba.ar [157.92.152.1]) by mail.postgresql.org (Postfix) with ESMTP id 5AE9F638B9A for ; Fri, 4 Dec 2009 14:39:11 -0400 (AST) Received: from [157.92.152.105] (unknown [157.92.152.105]) by mail.fmed.uba.ar (Postfix) with ESMTP id 0DE1339C1E1; Fri, 4 Dec 2009 15:37:59 -0200 (ARST) Message-ID: <4B194EBD.4050803@fmed.uba.ar> Date: Fri, 04 Dec 2009 15:02:37 -0300 From: Gerardo Herzig User-Agent: Thunderbird 2.0.0.6 (X11/20070801) MIME-Version: 1.0 To: Uwe Maiwald CC: pgsql-sql@postgresql.org Subject: Re: get sequence name from table name References: <4B193E8E.6010603@kiss-net.de> In-Reply-To: <4B193E8E.6010603@kiss-net.de> Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.884 tagged_above=-10 required=5 tests=AWL=0.715, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/10 X-Sequence-Number: 33448 Uwe Maiwald wrote: > how to get the name of the sequence that is responsible for setting the > autoincrement value of a tables primary key column? > > i only have the name of the table and need to have an appropiate sql > statement. > > > i need this to write a program that loops through all tables of a > database and then ajusts the start values of the sequencees in case the > table has an automatic id value (serial/bigserial) > > > thanks, > Uwe > The information schema provides what you need. test=# create table testing (id serial); NOTICE: CREATE TABLE will create implicit sequence "testing_id_seq" for serial column "testing.id" test=# SELECT table_name, column_name, column_default from information_schema.columns where table_name='testing'; table_name | column_name | column_default ------------+-------------+------------------------------------- testing | id | nextval('testing_id_seq'::regclass) (1 row) You may need an extra work finding out which the primary keys are, look at the information schema docs [0]. Maybe you will also need the help of the system catalogs [1]. [0] http://www.postgresql.org/docs/8.3/static/information-schema.html [1] http://www.postgresql.org/docs/8.3/static/catalogs.html (as you can see, this docs are from the 8.3 version. Check yours) HTH Gerardo