Creating exponential sequences - Mailing list pgsql-novice

From Rod Kreisler
Subject Creating exponential sequences
Date
Msg-id JNEGKNDJGBKLBDGPOPFOOEJLDCAA.rod@23net.net
Whole thread Raw
In response to Re: update question  (DAVID KUCHARSKI <dave@iemco.com>)
Responses Re: Creating exponential sequences  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Creating exponential sequences  (Rod Kreisler <rod@23net.net>)
List 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....



pgsql-novice by date:

Previous
From: DAVID KUCHARSKI
Date:
Subject: Re: update question
Next
From: Tom Lane
Date:
Subject: Re: Creating exponential sequences