473,767 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sequence question

Hi,
I have a question about sequences. I need a field to have values with
no holes in the sequence. However, the values do not need to be in order.

My users will draw a number or numbers from the sequence and write to
the field. Sometimes, however, these sequence numbers will be discarded
(after a transaction is complete), and thus available for use. During
the transaction, however, any drawn numbers need to be unavailable.
I would like the next user who draws a number to draw the lowest number
she can, starting with the holes in the sequence.

This continuous sequence is absolutely required by our company, as the
fact that the sequence has no holes is used to check for much more
serious problems.

So my question is:
what's the most effective way to get the next available number?

My present method is to do a query that finds the first and last number
in each of the holes, step through those holes, and then start
generating new numbers. Unfortunately, this involves doing a table scan
each time - before I generate the number, and does not produce the
transaction-safety I want.

Does anyone have any better ideas? Places I should look?

Thanks,

Eric
Nov 23 '05 #1
5 3288
Hi,

On Tue, 2004-10-19 at 01:16, Eric E wrote:
Hi,
I have a question about sequences. I need a field to have values with
no holes in the sequence. However, the values do not need to be in order.

My users will draw a number or numbers from the sequence and write to
the field. Sometimes, however, these sequence numbers will be discarded
(after a transaction is complete), and thus available for use. During
the transaction, however, any drawn numbers need to be unavailable.
I would like the next user who draws a number to draw the lowest number
she can, starting with the holes in the sequence.

This continuous sequence is absolutely required by our company, as the
fact that the sequence has no holes is used to check for much more
serious problems.
I would recheck this requirement. What should actually be achieved
with the check for no holes in the numbering?
Remember you can always enumerate using a set returning function
or by means of a temporary sequence for a query.
So my question is:
what's the most effective way to get the next available number?
There is none.
My present method is to do a query that finds the first and last number
in each of the holes, step through those holes, and then start
generating new numbers. Unfortunately, this involves doing a table scan
each time - before I generate the number, and does not produce the
transaction-safety I want.


You cannot eat the cake and keep it - either you have holes
or you have transaction security or you have bad performance
by locking the whole table on insert.

Regards
Tino

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
Hi Tino,
Many thanks for helping me.

I know that the sequence issue is a troubling one for many on the list.
Perhaps if I explain the need for a continuous sequence I can circumvent
some of that:

This database is for a laboratory, and the numbers in sequence
determine storage locations for a sample. Having a physical space in
our storage boxes tells us something has happened - the sample was used
up, broken, in use, etc - and account for that missing sample. If the
generated sequence has holes in it, we cannot tell if a sample is
properly not in the rack, or if that hole was simply generated by the
database. Allowing empties would also fill up limited box space with
spaces generated by the database.
If anyone has a brilliant idea for how a non-continuous sequence could
address the needs, I'd be delighted to hear it, but short of that I
think I have to keep this requirement.

One thought I had, and I'd love to hear what people think of this, is to
build a table of storage location numbers that are available for use.
That way the search for new numbers could be pushed off until some
convenient moment well after the user requests them.

Thanks again for any ideas.

Cheers,

Eric

Tino Wildenhain wrote:
Hi,

On Tue, 2004-10-19 at 01:16, Eric E wrote:

Hi,
I have a question about sequences. I need a field to have values with
no holes in the sequence. However, the values do not need to be in order.

My users will draw a number or numbers from the sequence and write to
the field. Sometimes, however, these sequence numbers will be discarded
(after a transaction is complete), and thus available for use. During
the transaction, however, any drawn numbers need to be unavailable.
I would like the next user who draws a number to draw the lowest number
she can, starting with the holes in the sequence.

This continuous sequence is absolutely required by our company, as the
fact that the sequence has no holes is used to check for much more
serious problems.


I would recheck this requirement. What should actually be achieved
with the check for no holes in the numbering?
Remember you can always enumerate using a set returning function
or by means of a temporary sequence for a query.
So my question is:
what's the most effective way to get the next available number?


There is none.
My present method is to do a query that finds the first and last number
in each of the holes, step through those holes, and then start
generating new numbers. Unfortunately, this involves doing a table scan
each time - before I generate the number, and does not produce the
transaction-safety I want.


You cannot eat the cake and keep it - either you have holes
or you have transaction security or you have bad performance
by locking the whole table on insert.

Regards
Tino


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3
On Wed, Oct 20, 2004 at 01:52:59PM -0400, Eric E wrote:
One thought I had, and I'd love to hear what people think of this, is to
build a table of storage location numbers that are available for use.
That way the search for new numbers could be pushed off until some
convenient moment well after the user requests them.


That very application is how I heard the idea for the "second method"
I sent in another email. In the case I was thinking of, someone used
it for room allocation. It worked pretty well, as long as you can
tolerate occasional periods where you _do_ have gaps.

A

--
Andrew Sullivan | aj*@crankycanuc k.ca
In the future this spectacle of the middle classes shocking the avant-
garde will probably become the textbook definition of Postmodernism.
--Brad Holland

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4
Hi,

Am Mi, den 20.10.2004 schrieb Eric E um 19:52:
Hi Tino,
Many thanks for helping me.

I know that the sequence issue is a troubling one for many on the list.
Perhaps if I explain the need for a continuous sequence I can circumvent
some of that:

This database is for a laboratory, and the numbers in sequence
determine storage locations for a sample. Having a physical space in
our storage boxes tells us something has happened - the sample was used
up, broken, in use, etc - and account for that missing sample. If the
generated sequence has holes in it, we cannot tell if a sample is
properly not in the rack, or if that hole was simply generated by the
database. Allowing empties would also fill up limited box space with
spaces generated by the database.
If anyone has a brilliant idea for how a non-continuous sequence could
address the needs, I'd be delighted to hear it, but short of that I
think I have to keep this requirement.


Maybe you skip the sequence thingy alltogether in this case and
use an approach like this:

initialize a table with all possible locations and mark them
as empty.

CREATE TABLE locations (location_id int2,taken bool);

(you might want to have a timestamp for changes too)

Whenever you change state of a location, do it like this
(perhaps in a function)

SELECT INTO loc_id location_id FROM locations WHERE taken
FOR UPDATE;
IF FOUND THEN
UPDATE location SET taken=true WHERE location_id=loc _id;
ELSE
RAISE EXCEPTION 'no free location anymore';

....

AND the other way round for freeing a location.
The SELECT ... FOR UPDATE should lock the candidate
position in the table so concurrent
transactions have to wait then then find another
free cell when they wake up.

Advantage: not a full table scan. Only the first
matching row should be used and locked.

Not this is only a rough sketch and you should
look for the actual syntax and more flesh for
the function.

Regards
Tino
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #5
Hmm.... that's a really intesting idea, Tino. Since we're probably
talking about 1000000 numbers max, a query on this table would work
fairly fast, and operationally simple. I'll think about that.

Thanks,

Eric
Tino Wildenhain wrote:
Hi,

Am Mi, den 20.10.2004 schrieb Eric E um 19:52:

Hi Tino,
Many thanks for helping me.

I know that the sequence issue is a troubling one for many on the list.
Perhaps if I explain the need for a continuous sequence I can circumvent
some of that:

This database is for a laboratory, and the numbers in sequence
determine storage locations for a sample. Having a physical space in
our storage boxes tells us something has happened - the sample was used
up, broken, in use, etc - and account for that missing sample. If the
generated sequence has holes in it, we cannot tell if a sample is
properly not in the rack, or if that hole was simply generated by the
database. Allowing empties would also fill up limited box space with
spaces generated by the database.
If anyone has a brilliant idea for how a non-continuous sequence could
address the needs, I'd be delighted to hear it, but short of that I
think I have to keep this requirement.


Maybe you skip the sequence thingy alltogether in this case and
use an approach like this:

initialize a table with all possible locations and mark them
as empty.

CREATE TABLE locations (location_id int2,taken bool);

(you might want to have a timestamp for changes too)

Whenever you change state of a location, do it like this
(perhaps in a function)

SELECT INTO loc_id location_id FROM locations WHERE taken
FOR UPDATE;
IF FOUND THEN
UPDATE location SET taken=true WHERE location_id=loc _id;
ELSE
RAISE EXCEPTION 'no free location anymore';

...

AND the other way round for freeing a location.
The SELECT ... FOR UPDATE should lock the candidate
position in the table so concurrent
transactions have to wait then then find another
free cell when they wake up.

Advantage: not a full table scan. Only the first
matching row should be used and locked.

Not this is only a rough sketch and you should
look for the actual syntax and more flesh for
the function.

Regards
Tino

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2632
by: Anthony Best | last post by:
I'm working on an idea that uses sequences. I'm going to create a table like this: id serial, sequence int, keyword varchar(32), text text for every keyword there will be a uniq sequence for it eg:
4
8219
by: j | last post by:
In a footnote in the c99 standard the following is labeled as undefined: a = i; And in the second clause of section 6.5 the following is stated: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value
18
1955
by: Andy Green | last post by:
Emphasis is on efficiancy and speed this is the excercise: The program should monitor a possibly infinite stream of characters from the keyboard (standard input). If it detects the sequence "aaa" it outputs a "0". If it detects the sequence "aba" it outputs a "1". DO NOT detect sequences within sequences. The program should exit cleanly when it detects an End Of Input. For example: The following sequence aababaaabaaa<End Of Input>...
1
632
by: Eric E | last post by:
Hi, I have a question about sequences. I need a field to have values with no holes in the sequence. However, the values do not need to be in order. My users will draw a number or numbers from the sequence and write to the field. Sometimes, however, these sequence numbers will be discarded (after a transaction is complete), and thus available for use. During the transaction, however, any drawn numbers need to be unavailable. I would...
3
4738
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema to not be sequenced. (PERL hashes do not retain order information) First, the w3 specs don't mention the sequence in any detail - its just there in the examples, which makes me wonder if removing the sequence tag is even supportable. Second,...
14
4802
by: pat270881 | last post by:
hello, I have to implement a sequence class, however the header file is predefined class sequence { public: // TYPEDEFS and MEMBER CONSTANTS
2
334
by: Kai-Uwe Bux | last post by:
Please consider #include <iostream> int main ( void ) { int a = 0; a = ( (a = 5), 4 ); // (*) std::cout << a << '\n'; }
3
2477
by: somenath | last post by:
Hi All, I have one question regarding the conditional operator. In the draft C99 standard it is mentioned that "1 The following are the sequence points described in 5.1.2.3: -- The call to a function, after the arguments have been evaluated (6.5.2.2). -- The end of the first operand of the following operators: logical AND && (6.5.13);
5
2162
by: majestik666 | last post by:
Am not sure if it's been asked before (did a search but didn't come up with anything related...). I'm trying to write some code to "collapse" image sequences into a single item , i.e.: image.1.exr image.2.exr .... image.100.exr
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9960
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7383
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3930
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.