pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: steve boyle <boylesa@dial.pipex.com>
To: pgsql-admin@postgresql.org
Subject: Code to automatically refresh sequences after loading data?
Date: Sat, 27 Oct 2001 18:36:16 +0100
Message-ID: <9rerbg$25uq$1@news.tht.net> (raw)

Has anyone got any code that will automatically carry out setvals for serial
fields after loading data.  I'm trying to write a function that will
identify and go through all sequences within the current database and 'fix'
the sequences to be set as the correct next value.

I cannot find any way of consistently identifiying the table/field that the
sequence has been defined over by interrogating the system catalogues.

The function so far is:

create function sys_refresh_sequences () returns integer as '
    DECLARE
        myfield RECORD;
        tblname text;
    BEGIN
        FOR myseq IN SELECT * FROM pg_class WHERE relkind=''S'' ORDER BY
relname LOOP
            myfield := substring(myseq.relname, 1,
char_length(myseq.relname)-4);
            ....
            ....
            RAISE NOTICE ''REFRESHING SEQUENCE % ON %'', myseq.relname,
myfield;
        END LOOP;

        return 1;
    END;
' language 'plpgsql';

Any pointers would be appreciated

Many thanks

Steve Boyle
boylesa@dial.pipex.com





view thread (4+ messages)  latest in thread

Message-ID: <9rerbg$25uq$1@news.tht.net>
Permalink:  ../9rerbg$25uq$1@news.tht.net/
Also on:    postgresql.org/message-id/9rerbg$25uq$1@news.tht.net

 · 

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: boylesa@dial.pipex.com
  Subject: Re: Code to automatically refresh sequences after loading data?
  In-Reply-To: <9rerbg$25uq$1@news.tht.net>
  References: <9rerbg$25uq$1@news.tht.net>

* 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