Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rGCQa-00EByr-6p for pgsql-general@arkaria.postgresql.org; Thu, 21 Dec 2023 06:21:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rGCQY-0045WU-U5 for pgsql-general@arkaria.postgresql.org; Thu, 21 Dec 2023 06:21:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rGCQY-0045WL-J6 for pgsql-general@lists.postgresql.org; Thu, 21 Dec 2023 06:21:02 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rGCQR-00BSA6-VZ for pgsql-general@postgresql.org; Thu, 21 Dec 2023 06:21:01 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 3BL6KsSk594222; Thu, 21 Dec 2023 01:20:54 -0500 From: Tom Lane To: Phillip Diffley cc: pgsql-general@postgresql.org Subject: Re: How to generate random bigint In-reply-to: References: Comments: In-reply-to Phillip Diffley message dated "Thu, 21 Dec 2023 00:06:39 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <594220.1703139654.1@sss.pgh.pa.us> Date: Thu, 21 Dec 2023 01:20:54 -0500 Message-ID: <594221.1703139654@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Phillip Diffley writes: > Postgres's random() function generates a random double. That can be > converted to a random int for smaller integers, but a double can't > represent all of the values in a bigint. Is there a recommended way to > generate a random bigint in Postgres? Doesn't look like there's anything directly exposed for that. Since PG v13 you could use gen_random_uuid, if you're careful to extract only bits that aren't fixed by the v4 UUID spec. pgcrypto's pg_random_bytes() function offers another some-assembly-required solution that'd work considerably further back. Or you could make a custom C function that leverages pg_strong_random(). regards, tom lane