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 1jFGvl-0002Gf-N2 for pgsql-general@arkaria.postgresql.org; Fri, 20 Mar 2020 12:39:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jFGvk-0005ve-2c for pgsql-general@arkaria.postgresql.org; Fri, 20 Mar 2020 12:39:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jFGvj-0005vA-IW for pgsql-general@lists.postgresql.org; Fri, 20 Mar 2020 12:39:15 +0000 Received: from mail-pj1-x1044.google.com ([2607:f8b0:4864:20::1044]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jFGvh-00013O-58 for pgsql-general@lists.postgresql.org; Fri, 20 Mar 2020 12:39:14 +0000 Received: by mail-pj1-x1044.google.com with SMTP id v13so2420846pjb.0 for ; Fri, 20 Mar 2020 05:39:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=fX6Zag/ukfoJwfydXLrUrdwkoBFMJNfM3qsBN+KuPoE=; b=CBn5JfU3EqvkwpK050cyCESQcdBaFGSF9G4sI6bJ+JmRTkGlzkhbyeBqqC4RAvv8rj CW8xuPHGZRWfCPKEuBE05vXjtQmDvnliMroxXzDMExEL0WKucxoL0lIdzOBSJ0SbrpuL BRiBTRLxwRDBkkCDJ7BbIXDXvWeFhQWDaouVtsM2eIHcI9ROdg+HTDDODkQ1ZQ86boCf 0oxq9Upe91vSmYdg7TlCyznvMR+kK5rL0y6p+qdq4n2pGpCVidvgm2yPtsitFMtSt6DJ 6UnVtc9JXZXQXIKgy9sbasKmAlYplxwRhxTCVImakQaSnS05AQ+AaEbrcuJo/2vSQ9yx bqBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=fX6Zag/ukfoJwfydXLrUrdwkoBFMJNfM3qsBN+KuPoE=; b=jaf/MevIeuOYD3JYNqIrwRSHvotz2fXmF0n7m8cdkqXG287DU6PqmLxX5gEfM/t4on FVF3LJNtAkG1VFd2oxdyWCDXodSFAqxMMbNLnyEaWOv09o5bVf1ifeqUhZ5A7LbIzLZ+ jSjAQ/wiXcHb6+aagLgCjZWJH0uwiBz+aRLVJFElxTdlHZBuFRk4WZpd2BllL/RIbuc1 /MMwyfaUkTTBj8UcIQhMGwvUFLoQgCyGnGzXKgmKOGex7Unif4TjsOkdIn0UqrjQ+g5z p9fXO1BbExJ5KOsnn9CxjsGttFXuv1+nci95VuWYeifzzqvnUAFSsnjuHZlLn43A9hUC fD5Q== X-Gm-Message-State: ANhLgQ0jgSabDoFM4hspkHaFVbM631jdEK4fjGmkfKinNXNusnnAxzuo 4aCBu4vqNZfjy2pyqX/p/TM= X-Google-Smtp-Source: ADFU+vvru962Q5UZYd8JJI5NS2vuQ9W7R04ODCKZNef3DccVLh9ZsEZyxnVCgrX4AbQqZ+oZiXiNOw== X-Received: by 2002:a17:90a:aa83:: with SMTP id l3mr9113721pjq.5.1584707951813; Fri, 20 Mar 2020 05:39:11 -0700 (PDT) Received: from roblaptop (121-200-4-90.79c804.syd.nbn.aussiebb.net. [121.200.4.90]) by smtp.gmail.com with ESMTPSA id q6sm4844546pja.34.2020.03.20.05.39.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Mar 2020 05:39:11 -0700 (PDT) Message-ID: Subject: Re: Could postgres12 support millions of sequences? (like 10 million) From: rob stone To: pabloa98 , "pgsql-generallists.postgresql.org" Date: Fri, 20 Mar 2020 23:39:01 +1100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.34.1-4 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, On Thu, 2020-03-19 at 14:36 -0700, pabloa98 wrote: > Hello, > > My schema requires a counter for each combination of 2 values. > Something like: > > CREATE TABLE counter( > group INT NOT NULL, > element INT NOT NULL, > seq_number INT NOT NULL default 0, > CONSTRAINT PRIMARY KEY (group, element) > ); > > For each entry in counter, aka for each (group, element) pair, the > model requires a seq_number. > > If I use a table "counter", I could still have counter collisions > between 2 transactions. I need truly sequence behavior. Is that > possible by using a table like "counter" table, where the counter > could be increased out of the transaction so it performs as a > sequence without having race conditions between concurrent > transactions? > > The other option is to create sequences for each new pair of (group, > element) using triggers. There are millions of pairs. So this > approach will generate millions of sequences. > > How a PostgreSQL database would behave having millions of sequences > in a schema? Would it degrade its performance? Is there any negative > impact? > > Regards > > Pablo > > > Have you tried the following:- BEGIN; SELECT (seq_number + 1) FROM counter WHERE group = $1 AND element = $2 FOR UPDATE; If that gives a NOTFOUND exception, then you need to insert into table counter using a seq_number = 1, or update it with (seq_number + 1). Then do the insert into whatever table needs the seq_number value. If no errors detected, then COMMIT else ROLLBACK. It is all in one transaction block. We use this approach to assign invoice numbers. You could pre-seed table counter when a group/element pair is created for the first time. HTH, Robert