Creating exponential sequences

From: Rod Kreisler <rod(at)23net(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Creating exponential sequences
Date: 2002-10-08 02:52:04
Message-ID: JNEGKNDJGBKLBDGPOPFOOEJLDCAA.rod@23net.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Is there any way to create an exponential sequence rather than incremental?

What I would like is a sequence that would start at 1 and grow exponentially
by 2. (i.e. 1,2,4,8,16,32....)

The advantages are, IMHO, obvious:

With a sequence "my_seq" declared as above create a table as follows:

create table example
(
"ID" int4 DEFAULT nextval('"my_seq"'::text) NOT NULL,
"description" varchar(32),
CONSTRAINT "example_pkey" PRIMARY KEY ("ID")
);
CREATE UNIQUE INDEX "example_description" ON "example" ("description");

When referenced by another table with a 1:many relationship, instead of
using a third table, the values can be stored in a single field using a sum
of the "ID"s and reference can be queried using a logical AND. Assuming the
referring table "example2" contains a field "example" which references the
above table with the pk on "example2" being "e2id":

select "description" from "example", "example2" where ("ID" & "example")!=0
and "e2id"=555;

Am I nuts? This seems so obvious but I've never seen it applied anywhere.
Of course, I'm by no means a db guru.

Of course, if I can't do it with a sequence, I could write a function....

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2002-10-08 03:13:08 Re: Creating exponential sequences
Previous Message DAVID KUCHARSKI 2002-10-07 22:20:50 Re: update question