From cd1b202612e3a3b11cf1aaf77cd9243a176b030e Mon Sep 17 00:00:00 2001 From: Dull Bananas Date: Thu, 19 Dec 2024 13:30:05 -0700 Subject: [PATCH] make random_smallint function work in postgresql 16 --- .../2024-12-18-200602_optimize_get_random_community/up.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/migrations/2024-12-18-200602_optimize_get_random_community/up.sql b/migrations/2024-12-18-200602_optimize_get_random_community/up.sql index 8157cc6cb..64c8af4e1 100644 --- a/migrations/2024-12-18-200602_optimize_get_random_community/up.sql +++ b/migrations/2024-12-18-200602_optimize_get_random_community/up.sql @@ -5,8 +5,10 @@ CREATE FUNCTION random_smallint () RETURNS smallint LANGUAGE sql - VOLATILE PARALLEL RESTRICTED RETURN random ( - -32768, 32767 + VOLATILE PARALLEL RESTRICTED RETURN + -- https://stackoverflow.com/questions/1400505/generate-a-random-number-in-the-range-1-10/1400752#1400752 + -- (`+ 1` makes the upper bound inclusive) + trunc ((random() * (32768 + 32767 + 1)) - 32768 ); ALTER TABLE community