Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jFSQV-0000n4-D6 for pgsql-general@arkaria.postgresql.org; Sat, 21 Mar 2020 00:55:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jFSQU-0006SH-5z for pgsql-general@arkaria.postgresql.org; Sat, 21 Mar 2020 00:55:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jFSQT-0006SA-Si for pgsql-general@lists.postgresql.org; Sat, 21 Mar 2020 00:55:45 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jFSQR-0000SM-ET for pgsql-general@lists.postgresql.org; Sat, 21 Mar 2020 00:55:45 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id F2DE35C0464; Fri, 20 Mar 2020 20:55:41 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Fri, 20 Mar 2020 20:55:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=q CqOCkGphXzuwJ5WulrN0MXxP1xc5PACauA6U+ufTFc=; b=bFCEVQ9gOUgyFZLdJ lNh14M+yaasqNtLA148TXyJC476pYNbcSgD/tFJnnFqfYYcBA0QSoqpc88qkbJYc 8QaMCtFJ9XNXTAds7FD5sH149hz8eNGeW0ZwCELhicB0l8ouhVP+1JSj/hpEYjzz TjNs7edpQ1XAiuUkaLtqx96Wj7y8ub7+kBm9HRUX7lq+0nGomuZEDKg6mMGFfdHR 4aKl1pKKwtfyuSEyMrD2DUznuDeOCrF3f49Scofl8wkDhXRlykXuLTemD5KGgl+s GmuFzkD/H73pfXwlBy/RBdxSNJVh2GVb1HLv7nbhHrjoQRYntf2wAUesDCYPlZhl iJ2Vw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=qCqOCkGphXzuwJ5WulrN0MXxP1xc5PACauA6U+ufT Fc=; b=mbpWMm8l1mO086ZU7TZNC2V0JUR2GyoGbdz7LhOLScKDhOO1Zx3FQ+MVu E84YoZUhEcpMmaUPNn5IlISN5sbYIb+YJRCmufj44fs6AwrCmYmCEq2wStCj3j33 gSiizq2WiLpt0YpC23ntldS7KY0V7NdnI56QUH3cgRwaUHWWrnAvkelEB5VSBrlS iutKNjW0/rS3dQKDc1aeMuXKVfFalf/0j4y2DQR8dW42m87eTeBjcZBu51YJLFiU 5lew1lmelBON0YzK8XZ313bHxopQ4UNYezGu8ZEgpqYUvvyLhEIHwclc1yfrStam vsBx2yuSAICNlB+HXoKAD6GzSYI5w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrudegvddgvdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepjedurddvuddvrdduvdelrdduleefnecuvehluhhsthgvrhfuihiivgep tdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklh grvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id B9FA93060FE7; Fri, 20 Mar 2020 20:55:40 -0400 (EDT) Subject: Re: Could postgres12 support millions of sequences? (like 10 million) To: pabloa98 Cc: Tom Lane , Michael Lewis , "David G. Johnston" , Rob Sargent , "pgsql-generallists.postgresql.org" References: <7CF69CD4-AE4A-4C2B-9621-4462B4AE1726@gmail.com> <10838.1584672974@sss.pgh.pa.us> <032b7a7f-e08a-fb7d-abf9-4a227f815f7a@aklaver.com> From: Adrian Klaver Message-ID: Date: Fri, 20 Mar 2020 17:55:40 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/20/20 2:13 PM, pabloa98 wrote: > > > On Fri, Mar 20, 2020 at 10:26 AM Adrian Klaver > > 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