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 1mVVqy-0004fH-SY for pgsql-general@arkaria.postgresql.org; Wed, 29 Sep 2021 09:26:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mVVqx-0006bX-SH for pgsql-general@arkaria.postgresql.org; Wed, 29 Sep 2021 09:26:15 +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 1mVVqx-0006bO-J6 for pgsql-general@lists.postgresql.org; Wed, 29 Sep 2021 09:26:15 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mVVqs-0007EU-6j for pgsql-general@lists.postgresql.org; Wed, 29 Sep 2021 09:26:14 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 325285680; Wed, 29 Sep 2021 11:26:07 +0200 (CEST) Date: Wed, 29 Sep 2021 11:26:07 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Using a single sequence for all tables Message-ID: Mail-Followup-To: pgsql-general@lists.postgresql.org MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="RKb89e7BvlHr3sO0" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --RKb89e7BvlHr3sO0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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). PostgreSQL makes it easy to generate unique ids. Just declare the column as SERIAL (or IDENTITY GENERATED in newer versions). One problem with this approach is that the ids are only unique within the table, not across the database. If you accidentally use an id from one table to access another table, chances are that a row with that id will actually exist - it just won't be the row you wanted to access. So the solution is to use a single sequence for all the id columns. Instead of=20 create table t1( id serial primary key, data text ); create table t2( id serial primary key, t1 int references t1, data text ); you write create sequence global_seq; create table t1( id int primary key default nextval('global_seq'), data text ); create table t2( id int primary key default nextval('global_seq'), t1 int references t1, data text ); Then you insert data just like you normally would with INSERT ... RETURNING id. The difference is just that if you accidentally do an UPDATE or delete wit an id from another table, it won't do anything. 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. * 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! * Doesn't work with IDENTIY - those columns always use implicit sequences. * currval() is pretty useless with a global sequence. But I basically never use that anyway. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --RKb89e7BvlHr3sO0 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmFUMSoACgkQ8g5IURL+ KF1QCw//d069ZDK7Y9OL7MMJspuePkI2tXLSIXmxwTYDceOXayaxVrfutubB9jSu tR4q0Pg6Iph43zjDrdWw0hX34u4r3dFcx9v25yKbPYCnqbqDFskStvHG8Y1B5xo+ YHfbhSLa3au4OyOvhUlGQIE7bViZDZj+fBbbCAqxzKFBO45MQkq0nFYt4uQ9SiNB EmmIIwqbf2uM5oz5Q9TCnGBVydsDUm+Q10fv93f+or9fE3QmqdD0pWaYUXMPwGfn GRS7sgsndn5/JzXjKO2JeLv5rL1mfJwR9xudpOaV6GMX+cZREgwBhXD4WgRr4UQB /CQGHXgrNF8k8WBKz+Z6NDe/k0LR1fkeznFVjb0itqVXImC4Z8Q00EIAlB4siJcg 2UvTtyAsDFc/VGGV4nEb/CVRu1XrbIHk+d+SewhYJogPAe+ObjLJyNQFTgm1159t vQ2XqVeM4DXuNLGmAzC5z93f/C/yYzMXpxvmL7oaCvOPuWIfQoR7SeRD26jXJjWY pUbvsIVjanJl+VwfoqyoFR0nyMFaKOyviRja0bgcZS45wQ+j0jQYbfGqL8LiUknY e5VOkzLLKrbTqBuzCGLFwL/mxztU4WX8H4XHKepT+Usf+7fY56Bj/wpGWkeW/tSL RCateD+/0PvTHk9MQ6U2xquVQ1zKeGkONg65hPZCDHMJ7bESHT0= =9rvI -----END PGP SIGNATURE----- --RKb89e7BvlHr3sO0--