Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mVWft-0007Rq-Jr for pgsql-general@arkaria.postgresql.org; Wed, 29 Sep 2021 10:18:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mVWfs-0001rb-Fg for pgsql-general@arkaria.postgresql.org; Wed, 29 Sep 2021 10:18:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mVWfs-0001rS-5t for pgsql-general@lists.postgresql.org; Wed, 29 Sep 2021 10:18:52 +0000 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mVWfp-0000Cu-Ma for pgsql-general@lists.postgresql.org; Wed, 29 Sep 2021 10:18:50 +0000 Received: by mail-wr1-x430.google.com with SMTP id k7so3241161wrd.13 for ; Wed, 29 Sep 2021 03:18:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=MniUccXTEgvnso5JiisHeOA5Kd76I/Xrc+3QWtIHCXQ=; b=7v02JZI7I3EbT4P4//cpZl9uJqP8FmwgeW4uuVc+DwDHziUcMvtKEf0hcBmP93Tl9h 1Q22/oHaE4/pju/NZyFWPRrLjkbwJtHgH9Aa64HwCmYX2KWoI3JxRwjs1gojP2vsU9NK bbT6p1Z2XJ1zI1x3uK2lUZbE8wyzhzTOuaARXl8lFcs31glild6DcuLI2wqjo2mIonif bXwDxDIMXXT2PEn7MwcJew0u84noKKMNaI2l63rwkuPIfqczmZj5VJcQ984HXQWLOI3r D6ua6hbDbMKfPRRH/OtaZthcA4w0BVJtaCvQLo3LsDjgahOQ4IfIKto76DOWO+Tkx4S2 F2Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=MniUccXTEgvnso5JiisHeOA5Kd76I/Xrc+3QWtIHCXQ=; b=Rt50bbCydo7XDIRCm3tkwatkLt/gUXVOzetEuQYiZ+lHmf6/qIUDZYt1jkm9qsDimr JX6kClRWqrR+cQ0OJ1xGvFdGtU40uyphPEscBMV6tXIBKpon8Ke508vwT4wfkspxcOqt xNNsmB4wtaDci6x/N8C1Rsk/cfcOZ1m91Y4NSfyZexysznREDpr7//W4eWfUjKIIb/vF sBMkWnFHkSJTsca8GROeTB3tnLA9ev1jvdaHc1zOAXa6GsPPsOSHVxu/9BgIaMYzvecL ka0o1JGhU47gT8MpylunmjO0soBtcXeN3AWDFuJFxXFoCuQe7k/hwfOdQ0TsIrkxKqRu 4HmA== X-Gm-Message-State: AOAM530q9SEP3fuDtH7QewxPko9+9wUFd7Xjp2Wzp/7EkobxPjqP7tRa JL8zV+nLWfAiOR7naq0YSRw7bm8+xLr7aA== X-Google-Smtp-Source: ABdhPJz03vMyonLHw4ZJhYfoWmXwKdoFmY2jCXJetMiBY1ui0yjLSYGBFSCEedOSIYwOhtMyRb//hw== X-Received: by 2002:a5d:46ca:: with SMTP id g10mr5645263wrs.269.1632910727950; Wed, 29 Sep 2021 03:18:47 -0700 (PDT) Received: from localhost.localdomain (dynamic-e1vwedg39w2800ozs-pd01.res.v6.highway.a1.net. [2001:871:5c:82aa:58c4:7079:3d10:be88]) by smtp.gmail.com with ESMTPSA id m29sm1830948wrb.89.2021.09.29.03.18.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 03:18:47 -0700 (PDT) Message-ID: Subject: Re: Using a single sequence for all tables From: Laurenz Albe To: "Peter J. Holzer" , pgsql-general@lists.postgresql.org Date: Wed, 29 Sep 2021 12:18:47 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-1.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2021-09-29 at 11:26 +0200, Peter J. Holzer wrote: > I discovered this technique back in my Oracle days but it dropped out of > my toolbox when I switched to PostgreSQL. Recently I had reason to > revisit it, so I thought I should share it (trivial though it is). > > So the solution is to use a single sequence for all the id columns. > > Possible drawbacks: > >  * The ids will grow faster, and they will be large even on small >    tables. It may be a bit irritating if you have a table with just 5 >    rows and the ids are 5, 6, 7, 12654, 345953. That's why use use "bigint". >  * Bottleneck? Using a single sequence was said to be a performance >    bottleneck in Oracle. I didn't notice a performance difference then >    and I doubt it would be one in PostgreSQL, but if in doubt, measure! That's no problem if you define the sequence with a CACHE value above 1, so that not every "nextval" call hits the sequence. >  * Doesn't work with IDENTIY - those columns always use implicit >    sequences. Right. >  * currval() is pretty useless with a global sequence. But I basically >    never use that anyway. Same here. I think identity columns are a Good Thing, particularly when CREATED ALWAYS, and I don't see the advantage of a database-wide unique identifier. But if it gives you a warm fuzzy feeling, go fot it :^) Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com