pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Code to automatically refresh sequences after loading data?
4+ messages / 2 participants
[nested] [flat]

* Code to automatically refresh sequences after loading data?
@ 2001-10-27 17:36 steve boyle <boylesa@dial.pipex.com>
  2001-10-29 19:18 ` Re: Code to automatically refresh sequences after loading data? Ross J. Reedstrom <reedstrm@rice.edu>
  0 siblings, 1 reply; 4+ messages in thread

From: steve boyle @ 2001-10-27 17:36 UTC (permalink / raw)
  To: pgsql-admin

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





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Code to automatically refresh sequences after loading data?
  2001-10-27 17:36 Code to automatically refresh sequences after loading data? steve boyle <boylesa@dial.pipex.com>
@ 2001-10-29 19:18 ` Ross J. Reedstrom <reedstrm@rice.edu>
  2001-10-29 23:16   ` Re: Code to automatically refresh sequences after loading data? Ross J. Reedstrom <reedstrm@rice.edu>
  0 siblings, 1 reply; 4+ messages in thread

From: Ross J. Reedstrom @ 2001-10-29 19:18 UTC (permalink / raw)
  To: steve boyle <boylesa@dial.pipex.com>; +Cc: pgsql-admin

On Sat, Oct 27, 2001 at 06:36:16PM +0100, steve boyle wrote:
> 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.

You need to go grovelling through pg_attrdef, looking for the sequence name
in the adsrc field. Then the adrelid field gives you the oid from pg_class
of the table, and the adnum gives you the ordinal for the column that has
this default, which is in pg_attribute.attnum.

Be careful of other tricky uses of sequences: recent discussion on one of 
these lists has been about isung one sequence to generate unique ids across
_multiple_ tables.

I usually keep a hand edited file around with my (also hand-edited) defining
schema, in which I have a bunch of:

SELECT setval('sequence_name_here',max(column_name)) from tablename;

Generated by a little awk/sed/grep of the schema, or from some SQL on the db.

Ah, this should be useful:

select 'SELECT setval(''"'||cs.relname||'"'', max("'||attname||'") FROM "'||c.relname||'";' from pg_class c, pg_class cs, pg_attribute a, pg_attrdef d where cs.relkind = 'S' and d.adsrc ~ cs.relname and c.oid = a.attrelid and c.oid = d.adrelid and d.adnum = a.attnum;

Here's what it does on a simple db of mine (excuse the wrap) all those
quotes are to make it MixEdCase proof.


                                       ?column?                                        
---------------------------------------------------------------------------------------
 SELECT setval('"people_peid_seq"', max("peid") FROM "people";
 SELECT setval('"other_programs_prog_id_seq"', max("prog_id") FROM "other_programs";
 SELECT setval('"other_courses_course_id_seq"', max("course_id") FROM "other_courses";


Ross


> 
> 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
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly

-- 
Ross Reedstrom, Ph.D.                                 reedstrm@rice.edu
Executive Director                                  phone: 713-348-6166
Gulf Coast Consortium for Bioinformatics              fax: 713-348-6182
Rice University MS-39
Houston, TX 77005



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Code to automatically refresh sequences after loading data?
  2001-10-27 17:36 Code to automatically refresh sequences after loading data? steve boyle <boylesa@dial.pipex.com>
  2001-10-29 19:18 ` Re: Code to automatically refresh sequences after loading data? Ross J. Reedstrom <reedstrm@rice.edu>
@ 2001-10-29 23:16   ` Ross J. Reedstrom <reedstrm@rice.edu>
  2001-10-30 11:38     ` Re: Code to automatically refresh sequences after loading data? steve boyle <boylesa@dial.pipex.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Ross J. Reedstrom @ 2001-10-29 23:16 UTC (permalink / raw)
  To: pgsql-admin

On Mon, Oct 29, 2001 at 01:18:15PM -0600, Ross J. Reedstrom wrote:
 
Need to fix that: I was short a right paren:

select 'SELECT setval(''"'||cs.relname||'"'', max("'||attname||'"))
FROM "'||c.relname||'";' from pg_class c, pg_class cs, pg_attribute a,
pg_attrdef d where cs.relkind = 'S' and d.adsrc ~ cs.relname and c.oid =
a.attrelid and c.oid = d.adrelid and d.adnum = a.attnum;

now you can do (in psql):

bioinfo=# \t
Showing only tuples.
bioinfo=# select 'SELECT setval(''"'||cs.relname||
'"'', max("'||attname||'")) FROM "'||c.relname||'";' 
from pg_class c, pg_class cs, pg_attribute a, pg_attrdef d 
where cs.relkind = 'S' and d.adsrc ~ cs.relname 
and c.oid = a.attrelid and c.oid = d.adrelid 
and d.adnum = a.attnum \g fix-serial.sql

bioinfo=# \i fix-serial.sql
     77

     78

     17

bioinfo=# 

i.e. use SQL to generate the SQL into a file, then read the commands back
in from that file.

Note that since this grovels around in systemtables, it can break with version
changes.

Ross

-- 
Ross Reedstrom, Ph.D.                                 reedstrm@rice.edu
Executive Director                                  phone: 713-348-6166
Gulf Coast Consortium for Bioinformatics              fax: 713-348-6182
Rice University MS-39
Houston, TX 77005



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Code to automatically refresh sequences after loading data?
  2001-10-27 17:36 Code to automatically refresh sequences after loading data? steve boyle <boylesa@dial.pipex.com>
  2001-10-29 19:18 ` Re: Code to automatically refresh sequences after loading data? Ross J. Reedstrom <reedstrm@rice.edu>
  2001-10-29 23:16   ` Re: Code to automatically refresh sequences after loading data? Ross J. Reedstrom <reedstrm@rice.edu>
@ 2001-10-30 11:38     ` steve boyle <boylesa@dial.pipex.com>
  0 siblings, 0 replies; 4+ messages in thread

From: steve boyle @ 2001-10-30 11:38 UTC (permalink / raw)
  To: pgsql-admin

Ross, I've used your code and created a function that seems to do the job.

The function is shown below.
--
-- Function:    sys_refresh_sequences()
-- Purpose:     Refreshes all sequences in the current database after data
load.
-- Notes:       Code for sequence_setvals provided by Ross J Reedstorm
--              Wrapper function added by Steve Boyle 30/10/2001
--
drop view sequence_setvals;
create view sequence_setvals as
select 'SELECT setval(''"'||cs.relname||'"'', max("'||attname||'")) FROM
"'||c.relname||'";' AS expr from pg_class c, pg_class cs, pg_attribute a,
pg_attrdef d where cs.relkind = 'S' and d.adsrc ~ cs.relname and c.oid =
a.attrelid and c.oid = d.adrelid and d.adnum = a.attnum;

drop function sys_refresh_sequences();
create function sys_refresh_sequences () returns integer as '
    DECLARE
        myseq RECORD;
    BEGIN
        FOR myseq IN select * from sequence_setvals LOOP
            RAISE NOTICE ''Executing - %'', myseq.expr;
            EXECUTE myseq.expr;
        END LOOP;

        return 1;
    END;
' language 'plpgsql';

""Ross J. Reedstrom"" <reedstrm@rice.edu> wrote in message
news:20011029171627.B24888@rice.edu...
> On Mon, Oct 29, 2001 at 01:18:15PM -0600, Ross J. Reedstrom wrote:
>
> Need to fix that: I was short a right paren:
>
> select 'SELECT setval(''"'||cs.relname||'"'', max("'||attname||'"))
> FROM "'||c.relname||'";' from pg_class c, pg_class cs, pg_attribute a,
> pg_attrdef d where cs.relkind = 'S' and d.adsrc ~ cs.relname and c.oid =
> a.attrelid and c.oid = d.adrelid and d.adnum = a.attnum;
>
> now you can do (in psql):
>
> bioinfo=# \t
> Showing only tuples.
> bioinfo=# select 'SELECT setval(''"'||cs.relname||
> '"'', max("'||attname||'")) FROM "'||c.relname||'";'
> from pg_class c, pg_class cs, pg_attribute a, pg_attrdef d
> where cs.relkind = 'S' and d.adsrc ~ cs.relname
> and c.oid = a.attrelid and c.oid = d.adrelid
> and d.adnum = a.attnum \g fix-serial.sql
>
> bioinfo=# \i fix-serial.sql
>      77
>
>      78
>
>      17
>
> bioinfo=#
>
> i.e. use SQL to generate the SQL into a file, then read the commands back
> in from that file.
>
> Note that since this grovels around in systemtables, it can break with
version
> changes.
>
> Ross
>
> --
> Ross Reedstrom, Ph.D.                                 reedstrm@rice.edu
> Executive Director                                  phone: 713-348-6166
> Gulf Coast Consortium for Bioinformatics              fax: 713-348-6182
> Rice University MS-39
> Houston, TX 77005
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2001-10-30 11:38 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-10-27 17:36 Code to automatically refresh sequences after loading data? steve boyle <boylesa@dial.pipex.com>
2001-10-29 19:18 ` Ross J. Reedstrom <reedstrm@rice.edu>
2001-10-29 23:16   ` Ross J. Reedstrom <reedstrm@rice.edu>
2001-10-30 11:38     ` steve boyle <boylesa@dial.pipex.com>

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