pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: pabloa98 <pabloa98@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Michael Lewis <mlewis@entrata.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Could postgres12 support millions of sequences? (like 10 million)
Date: Fri, 20 Mar 2020 17:55:40 -0700
Message-ID: <dfeaaa15-81c3-7a13-6209-b4db3db83c64@aklaver.com> (raw)
In-Reply-To: <CAEjudX4eeD321J9ngOcJjDN4CaRMfPsfw4gsL2Y6Bpo3uOLdTg@mail.gmail.com>
References: <CAEjudX5UdEYJwSTYir07rUX10u3AdEyht_K=GWk_CyOChSFguQ@mail.gmail.com>
	<B6FC3723-FF9D-4E5E-8506-3BF9421825DF@gmail.com>
	<CAEjudX5T5E_two14i_i19uGMkSs_mT4JG4hhXWoY3Z5M+GaXRQ@mail.gmail.com>
	<7CF69CD4-AE4A-4C2B-9621-4462B4AE1726@gmail.com>
	<CAEjudX5=jxdxZmuSXgBCM+nHNhR=0OAcZGB5UaRB1JZe1YRNtA@mail.gmail.com>
	<CAKFQuwZ==ri5_m2geFA-GPOdfnVggmJRu3zEi+1EwJdJA=9AeQ@mail.gmail.com>
	<CAHOFxGqsZJM5s9x3ELVtq_qRQj1odZ_L-SNKqdYyyBbd=1+_nw@mail.gmail.com>
	<10838.1584672974@sss.pgh.pa.us>
	<CAEjudX6nQNte77FgK1ch_k6kGnEcjoTUU-pF3=9VpzuYqycwqw@mail.gmail.com>
	<e9337b4c-cbd7-7ad9-ea01-5af415139ad1@aklaver.com>
	<032b7a7f-e08a-fb7d-abf9-4a227f815f7a@aklaver.com>
	<CAEjudX4eeD321J9ngOcJjDN4CaRMfPsfw4gsL2Y6Bpo3uOLdTg@mail.gmail.com>

On 3/20/20 2:13 PM, pabloa98 wrote:
> 
> 
> On Fri, Mar 20, 2020 at 10:26 AM Adrian Klaver 
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     On 3/20/20 9:59 AM, Adrian Klaver wrote:
>      > On 3/19/20 10:31 PM, pabloa98 wrote:
>      >> I see.
>      >>
>      >> Any suggestion? It should behave like a sequence in the sense that
>      >> concurrent transitions will get different numbers from this
>      >> alternative sequence like solution.
>      >>
>      >> In our case, we will need to do a call nextval('some_seq') (or
>      >> similar) from different processes no more than twice every minute.
>      >>
>      >>
>      >> It would be nice to have a sequence data type. Correct me if I am
>      >> wrong, but It seems to me that a sequence data type would cost the
>      >> same or less than the current sequence implementation.
>      >>
>      >> The schema would be more clear too. We could have a table like:
>      >>
>      >> CREATE TABLE pair(
>      >> group INT NOT NULL,
>      >> element INT NOT NULL,
>      >> seq SEQUENCE INCREMENT 1
>      >>                START 1
>      >>                CACHE 1
>      >>                MINVALUE 1
>      >>                MAXVALUE 99999999
>      >>          NOT NULL,
>      >> CONSTRAINT PRIMARY KEY (group, element)
>      >> );
>      >>
>      >> And then:
>      >>
>      >> INSERT INTO event(group, element, code)
>      >> VALUES (
>      >>           1,
>      >>           1,
>      >>           ( SELECT seq.nextval('code_1_1_seq') FROM pair p WHERE
>      >> p.group=1 and p.code=1 )
>      >> );
>      >>
>      >> Or perhaps storing all the sequences in the same table as rows will
>      >> have the same behavior.
>      >
>      > If code is just something to show the sequence of insertion for
>     group,
>      > element combinations then maybe something like below:
>      >
>      > CREATE TABLE event(
>      > group_id INT NOT NULL, --changed as group is reserved word
>      > element INT NOT NULL,
>      > insert_ts timestamptz NOT NULL DEFAULT clock_timestamp(),
>      > PRIMARY KEY(group_id, element, insert_ts)
>      > );
>      >
>      >
>      > insert into event(group_id, element) VALUES
>      > (1, 1),
>      > (1, 1),
>      > (1, 1),
>      > (2, 1),
>      > (1, 1),
>      > (1, 3),
>      > (1, 1),
>      > (1, 3),
>      > (2, 1),
>      > (2, 1);
>      >
>      >
>      > select * from event ;
>      >   group_id | element |           insert_ts
>      > ----------+---------+--------------------------------
>      >          1 |       1 | 03/20/2020 09:51:12.675926 PDT
>      >          1 |       1 | 03/20/2020 09:51:12.675985 PDT
>      >          1 |       1 | 03/20/2020 09:51:12.675991 PDT
>      >          2 |       1 | 03/20/2020 09:51:12.675996 PDT
>      >          1 |       1 | 03/20/2020 09:51:12.676 PDT
>      >          1 |       3 | 03/20/2020 09:51:12.676004 PDT
>      >          1 |       1 | 03/20/2020 09:51:12.676008 PDT
>      >          1 |       3 | 03/20/2020 09:51:12.676012 PDT
>      >          2 |       1 | 03/20/2020 09:51:12.676016 PDT
>      >          2 |       1 | 03/20/2020 09:51:12.67602 PDT
>      > (10 rows)
>      >
>      >
>      > select group_id, element, row_number() OVER (partition by (group_id,
>      > element) order by (group_id, element)) AS code from event;
>      >   group_id | element | code
>      > ----------+---------+------
>      >          1 |       1 |    1
>      >          1 |       1 |    2
>      >          1 |       1 |    3
>      >          1 |       1 |    4
>      >          1 |       1 |    5
>      >          1 |       3 |    1
>      >          1 |       3 |    2
>      >          2 |       1 |    1
>      >          2 |       1 |    2
>      >          2 |       1 |    3
>      > (10 rows)
> 
>     Oops the above actually returned:
> 
>     select group_id, element, row_number() OVER (partition by (group_id,
>     element) order by (group_id, element)) AS code, insert_ts from event;
>        group_id | element | code |           insert_ts
>     ----------+---------+------+--------------------------------
>               1 |       1 |    1 | 03/20/2020 09:51:12.675926 PDT
>               1 |       1 |    2 | 03/20/2020 09:51:12.675985 PDT
>               1 |       1 |    3 | 03/20/2020 09:51:12.675991 PDT
>               1 |       1 |    4 | 03/20/2020 09:51:12.676008 PDT
>               1 |       1 |    5 | 03/20/2020 09:51:12.676 PDT
>               1 |       3 |    1 | 03/20/2020 09:51:12.676004 PDT
>               1 |       3 |    2 | 03/20/2020 09:51:12.676012 PDT
>               2 |       1 |    1 | 03/20/2020 09:51:12.67602 PDT
>               2 |       1 |    2 | 03/20/2020 09:51:12.676016 PDT
>               2 |       1 |    3 | 03/20/2020 09:51:12.675996 PDT
>     (10 rows)
> 
> 
>     Needs to be:
> 
>     select group_id, element, row_number() OVER (partition by (group_id,
>     element) order by (group_id, element, insert_ts)) AS code, insert_ts
>     from event;
>        group_id | element | code |           insert_ts
>     ----------+---------+------+--------------------------------
>               1 |       1 |    1 | 03/20/2020 09:51:12.675926 PDT
>               1 |       1 |    2 | 03/20/2020 09:51:12.675985 PDT
>               1 |       1 |    3 | 03/20/2020 09:51:12.675991 PDT
>               1 |       1 |    4 | 03/20/2020 09:51:12.676 PDT
>               1 |       1 |    5 | 03/20/2020 09:51:12.676008 PDT
>               1 |       3 |    1 | 03/20/2020 09:51:12.676004 PDT
>               1 |       3 |    2 | 03/20/2020 09:51:12.676012 PDT
>               2 |       1 |    1 | 03/20/2020 09:51:12.675996 PDT
>               2 |       1 |    2 | 03/20/2020 09:51:12.676016 PDT
>               2 |       1 |    3 | 03/20/2020 09:51:12.67602 PDT
>     (10 rows)
> 
> 
> 
> Clever. :D
> I will use it on other things.
> 
> The problem for this specific case is that if someone deletes a row, 
> several codes will change. For this problem, codes should not be changed.

A new requirement.

To get a better response I would suggest posting a detailed model of 
what you are after and why?

> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (58+ messages)  latest in thread

Message-ID: <dfeaaa15-81c3-7a13-6209-b4db3db83c64@aklaver.com>
Permalink:  ../dfeaaa15-81c3-7a13-6209-b4db3db83c64@aklaver.com/
Also on:    postgresql.org/message-id/dfeaaa15-81c3-7a13-6209-b4db3db83c64@aklaver.com

 · 

reply

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-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, pabloa98@gmail.com, tgl@sss.pgh.pa.us, mlewis@entrata.com, david.g.johnston@gmail.com, robjsargent@gmail.com
  Subject: Re: Could postgres12 support millions of sequences? (like 10 million)
  In-Reply-To: <dfeaaa15-81c3-7a13-6209-b4db3db83c64@aklaver.com>
  References: <CAEjudX5UdEYJwSTYir07rUX10u3AdEyht_K=GWk_CyOChSFguQ@mail.gmail.com> <B6FC3723-FF9D-4E5E-8506-3BF9421825DF@gmail.com> <CAEjudX5T5E_two14i_i19uGMkSs_mT4JG4hhXWoY3Z5M+GaXRQ@mail.gmail.com> <7CF69CD4-AE4A-4C2B-9621-4462B4AE1726@gmail.com> <CAEjudX5=jxdxZmuSXgBCM+nHNhR=0OAcZGB5UaRB1JZe1YRNtA@mail.gmail.com> <CAKFQuwZ==ri5_m2geFA-GPOdfnVggmJRu3zEi+1EwJdJA=9AeQ@mail.gmail.com> <CAHOFxGqsZJM5s9x3ELVtq_qRQj1odZ_L-SNKqdYyyBbd=1+_nw@mail.gmail.com> <10838.1584672974@sss.pgh.pa.us> <CAEjudX6nQNte77FgK1ch_k6kGnEcjoTUU-pF3=9VpzuYqycwqw@mail.gmail.com> <e9337b4c-cbd7-7ad9-ea01-5af415139ad1@aklaver.com> <032b7a7f-e08a-fb7d-abf9-4a227f815f7a@aklaver.com> <CAEjudX4eeD321J9ngOcJjDN4CaRMfPsfw4gsL2Y6Bpo3uOLdTg@mail.gmail.com> <dfeaaa15-81c3-7a13-6209-b4db3db83c64@aklaver.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