"Keyed" sequence?
This is probably crazy talk, but in Postgresql is there any way to have a "keyed" sequence? That is, I have a table with a list of departments. While *relatively* static, technically speaking this is a dynamic list - someone certainly could decide to add a department, or combine two departments into one new one, whatever. In any case, I would ideally like to have a sequence per department, but since the list is dynamic it's not as simple as creating 33 independent sequences and then having a lookup table somewhere, although I guess I could implement something of the sort with triggers.
Is there any elegant way of accomplishing this? Something like SELECT nextval('department_seq['ops'])?
-----------------------------------------------
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------
Attachments:
Israel Brewster.vcftext/directory; name="Israel Brewster.vcf"Download
On 04/28/2016 08:30 PM, Israel Brewster wrote:
This is probably crazy talk, but in Postgresql is there any way to have
a "keyed" sequence? That is, I have a table with a list of departments.
While *relatively* static, technically speaking this is a dynamic list -
someone certainly could decide to add a department, or combine two
departments into one new one, whatever. In any case, I would ideally
like to have a sequence per department, but since the list is dynamic
it's not as simple as creating 33 independent sequences and then having
a lookup table somewhere, although I guess I could implement something
of the sort with triggers.
What would be the point of this? Why not just one sequence for all
departments?
--
Vik Fearing +33 6 46 75 15 36
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
On Apr 28, 2016, at 10:39 AM, Vik Fearing <vik@2ndquadrant.fr> wrote:
On 04/28/2016 08:30 PM, Israel Brewster wrote:
This is probably crazy talk, but in Postgresql is there any way to have
a "keyed" sequence? That is, I have a table with a list of departments.
While *relatively* static, technically speaking this is a dynamic list -
someone certainly could decide to add a department, or combine two
departments into one new one, whatever. In any case, I would ideally
like to have a sequence per department, but since the list is dynamic
it's not as simple as creating 33 independent sequences and then having
a lookup table somewhere, although I guess I could implement something
of the sort with triggers.What would be the point of this? Why not just one sequence for all
departments?
continuity and appearance, not to mention simple logical progression. In this case, the sequence is being used to generate a PO number. Company style indicates that a PO number is a department code followed by a unique number. With one sequence for all departments, you could (will) end up with discontinuous PO numbers in any given department. It would be nice if, after issuing PO number 15-1, the next PO in department 15 was 2, if for no other reason than the accounting department could easily see that they aren't missing any. With one sequence, there will quite likely not be a PO number 2 for any given department, so that department has no easy way to keep track of their PO's based on PO number.
--
Vik Fearing +33 6 46 75 15 36
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
On 04/28/2016 11:48 AM, Israel Brewster wrote:
On Apr 28, 2016, at 10:39 AM, Vik Fearing <vik@2ndquadrant.fr> wrote:
On 04/28/2016 08:30 PM, Israel Brewster wrote:
This is probably crazy talk, but in Postgresql is there any way to have
a "keyed" sequence? That is, I have a table with a list of departments.
While *relatively* static, technically speaking this is a dynamic list -
someone certainly could decide to add a department, or combine two
departments into one new one, whatever. In any case, I would ideally
like to have a sequence per department, but since the list is dynamic
it's not as simple as creating 33 independent sequences and then having
a lookup table somewhere, although I guess I could implement something
of the sort with triggers.What would be the point of this? Why not just one sequence for all
departments?continuity and appearance, not to mention simple logical progression. In this case, the sequence is being used to generate a PO number. Company style indicates that a PO number is a department code followed by a unique number. With one sequence for all departments, you could (will) end up with discontinuous PO numbers in any given department. It would be nice if, after issuing PO number 15-1, the next PO in department 15 was 2, if for no other reason than the accounting department could easily see that they aren't missing any. With one sequence, there will quite likely not be a PO number 2 for any given department, so that department has no easy way to keep track of their PO's based on PO number.
Here is a similar idea:
/messages/by-id/44E376F6.7010802@seaworthysys.com
--
Vik Fearing +33 6 46 75 15 36
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
--
Adrian Klaver
adrian.klaver@aklaver.com
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
On Thu, Apr 28, 2016 at 1:48 PM, Israel Brewster <israel@ravnalaska.net>
wrote:
On Apr 28, 2016, at 10:39 AM, Vik Fearing <vik@2ndquadrant.fr> wrote:
On 04/28/2016 08:30 PM, Israel Brewster wrote:
This is probably crazy talk, but in Postgresql is there any way to have
a "keyed" sequence? That is, I have a table with a list of departments.
While *relatively* static, technically speaking this is a dynamic list -
someone certainly could decide to add a department, or combine two
departments into one new one, whatever. In any case, I would ideally
like to have a sequence per department, but since the list is dynamic
it's not as simple as creating 33 independent sequences and then having
a lookup table somewhere, although I guess I could implement something
of the sort with triggers.What would be the point of this? Why not just one sequence for all
departments?continuity and appearance, not to mention simple logical progression. In
this case, the sequence is being used to generate a PO number. Company
style indicates that a PO number is a department code followed by a unique
number. With one sequence for all departments, you could (will) end up with
discontinuous PO numbers in any given department. It would be nice if,
after issuing PO number 15-1, the next PO in department 15 was 2, if for no
other reason than the accounting department could easily see that they
aren't missing any. With one sequence, there will quite likely not be a PO
number 2 for any given department, so that department has no easy way to
keep track of their PO's based on PO number.
Have you looked at an UPDATE ... RETURNING ... to generate a new PO
number? I don't know the structure of the table involved, but something
like the following occurs to me:
UPDATE table SET DEPT_LAST_PO = 1 + DEPT_LAST_PO RETURNING DEPT_LAST_PO;
example transcript:
# create table dept (dept_name text, dept_last_po int default 0);
CREATE TABLE
# insert into dept values('finance'); -- default dept_last_po to 0
INSERT 0 1
# update dept set dept_last_po = 1 + dept_last_po where dept_name='finance'
returning dept_last_po;
dept_last_po
--------------
1
(1 row)
UPDATE 1
--
Vik Fearing +33 6 46 75 15 36
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support
--
The unfacts, did we have them, are too imprecisely few to warrant our
certitude.
Maranatha! <><
John McKown
On 04/28/2016 08:48 PM, Israel Brewster wrote:
On Apr 28, 2016, at 10:39 AM, Vik Fearing <vik@2ndquadrant.fr> wrote:
What would be the point of this? Why not just one sequence for all
departments?continuity and appearance, not to mention simple logical progression. In this case, the sequence is being used to generate a PO number. Company style indicates that a PO number is a department code followed by a unique number. With one sequence for all departments, you could (will) end up with discontinuous PO numbers in any given department. It would be nice if, after issuing PO number 15-1, the next PO in department 15 was 2, if for no other reason than the accounting department could easily see that they aren't missing any. With one sequence, there will quite likely not be a PO number 2 for any given department, so that department has no easy way to keep track of their PO's based on PO number.
You're not guaranteed that even with individual sequences.
What' you're looking for is a gapless sequence, which is best simulated
with a table. In your case, I'd just add a column to your existing
departments table holding the next number to use.
It'll kill your performance, but if aesthetics are that important to you...
--
Vik Fearing +33 6 46 75 15 36
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
På torsdag 28. april 2016 kl. 20:30:20, skrev Israel Brewster <
israel@ravnalaska.net <mailto:israel@ravnalaska.net>>:
This is probably crazy talk, but in Postgresql is there any way to have a
"keyed" sequence? That is, I have a table with a list of departments. While
*relatively* static, technically speaking this is a dynamic list - someone
certainly could decide to add a department, or combine two departments into one
new one, whatever. In any case, I would ideally like to have a sequence per
department, but since the list is dynamic it's not as simple as creating 33
independent sequences and then having a lookup table somewhere, although I
guess I could implement something of the sort with triggers.
Is there any elegant way of accomplishing this? Something like SELECT
nextval('department_seq['ops'])?
Maybe this will help you:
http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram
Then you can do:
"id" bigint NOT NULL DEFAULT insta5.next_id(),
-- Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com>
<https://www.visena.com>
-----------------------------------------------
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------
Attachments:
Israel Brewster.vcftext/directory; name="Israel Brewster.vcf"Download
On Thu, Apr 28, 2016 at 12:09 PM, Israel Brewster <israel@ravnalaska.net>
wrote:
It'll kill your performance, but if aesthetics are that important to
you...
They're not *that* important. I was just asking if there was a way to do
this easily.
While the performance argument might be true it is heavily dependent upon
concurrency. I'm doubting a PO system in the typical company has enough
concurrency, and is sensitivity enough to small delays,
that giving up the benefit of sequential numbering would be a worthwhile
trade-off.
David J.
On Apr 28, 2016, at 11:21 AM, David G. Johnston <david.g.johnston@gmail.com> wrote:
On Thu, Apr 28, 2016 at 12:09 PM, Israel Brewster <israel@ravnalaska.net <mailto:israel@ravnalaska.net>> wrote:
It'll kill your performance, but if aesthetics are that important to you...
They're not *that* important. I was just asking if there was a way to do this easily.
While the performance argument might be true it is heavily dependent upon concurrency. I'm doubting a PO system in the typical company has enough concurrency, and is sensitivity enough to small delays, that giving up the benefit of sequential numbering would be a worthwhile trade-off.
I'm thinking the same - especially considering that we aren't exactly a huge company.
David J.
-----------------------------------------------
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------
Adrian Klaver wrote:
On 04/28/2016 11:48 AM, Israel Brewster wrote:
On Apr 28, 2016, at 10:39 AM, Vik Fearing <vik@2ndquadrant.fr> wrote:
On 04/28/2016 08:30 PM, Israel Brewster wrote:
This is probably crazy talk, but in Postgresql is there any way to have
a "keyed" sequence? That is, I have a table with a list of departments.
While *relatively* static, technically speaking this is a dynamic
list -
someone certainly could decide to add a department, or combine two
departments into one new one, whatever. In any case, I would ideally
like to have a sequence per department, but since the list is dynamic
it's not as simple as creating 33 independent sequences and then having
a lookup table somewhere, although I guess I could implement something
of the sort with triggers.What would be the point of this? Why not just one sequence for all
departments?continuity and appearance, not to mention simple logical progression.
In this case, the sequence is being used to generate a PO number.
Company style indicates that a PO number is a department code followed
by a unique number. With one sequence for all departments, you could
(will) end up with discontinuous PO numbers in any given department.
It would be nice if, after issuing PO number 15-1, the next PO in
department 15 was 2, if for no other reason than the accounting
department could easily see that they aren't missing any. With one
sequence, there will quite likely not be a PO number 2 for any given
department, so that department has no easy way to keep track of their
PO's based on PO number.Here is a similar idea:
/messages/by-id/44E376F6.7010802@seaworthysys.com
BAM!! You beat me to it!!
I have repeated that pattern multiple times and it is the exact use case
the OP has.
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general