From: Postgres DBA <postgres@nest.bistbn.com>
To: Marc Howard Zuckman <marc@fallon.classyad.com>
Cc: Thomas Good <tomg@admin.nrnet.org>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] abusing an aggregate funct
Date: Sun, 15 Nov 1998 16:04:08 +0200 (IST)
Message-ID: <Pine.BSF.4.02.9811151524210.9202-100000@nest.bistbn.com> (raw)
In-Reply-To: <Pine.LNX.4.02A.9811140953190.6343-100000@fallon.classyad.com>
On Sat, 14 Nov 1998, Marc Howard Zuckman wrote:
> On Sat, 14 Nov 1998, Thomas Good wrote:
>
> > Due to the sheer girth of the table the following query, called from
> > a data entry script, takes a bit of time to finish:
> >
> > SELECT max(rec_num) FROM crtrd1;
> >
> > I use this to setup `rec_num + 1' for my query number (p_key)...any
> > way I can speed this up a bit? I've run vacuum analyze but the
> > table size is doing me in...
> >
> Use a sequence to generate rec_num. See man create_sequence.
>
> You can then use curval('sequence_name') to get the last
> sequence number generated, or if you are performing an insert:
> insert into crtrd1 (p_key,otherdata) values (nextval('sequence_name',moredata);
>
> You could also create the table using nextval as the default
> value of p_key. You may wish to create a unique_index on
> p_key, but nextval is guaranteed not to generate duplicates
> unless you allow it to rollover.
>
>
Unfortunately, solution using sequences is not so good unless you don't
suppose to use access to that table from some simultaneously running
sessions. The problem is that every sessions accessing this additional
sequence with nextval() will get its own pool of values for the
sequence, so early or later you'll get some gaps in records numbering
because of at least on of two reasons:
a) usually the length of such a pool is set to limit that is
bigger then 1 and obviously, it's impossible to check the number of
inserts (and thus the number of nestval`s) during every sessions.
b) and in case of pool with 1-length some of your inserts may
fail, but nextavl would be called and next time you try insert you'll call
nextval again and will miss one or more values of sequence.
Also, you can't issue curval until you haven't call nextval at least once
during currunt session -- this is also source of gaps in values from
sequence assigned to records in the table. So, I think this method can
produce only approximation of real ammount of records:-(
Aleksey.
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-sql@postgresql.org
Cc: postgres@nest.bistbn.com, marc@fallon.classyad.com, tomg@admin.nrnet.org
Subject: Re: [SQL] abusing an aggregate funct
In-Reply-To: <Pine.BSF.4.02.9811151524210.9202-100000@nest.bistbn.com>
References: <Pine.LNX.4.02A.9811140953190.6343-100000@fallon.classyad.com> <Pine.BSF.4.02.9811151524210.9202-100000@nest.bistbn.com>
* 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