qonto / postgresql-partition-manager

Simplified PostgreSQL partioning management
MIT License
6 stars 1 forks source link

Simplify min_uuid_v7 #2

Closed dverite closed 5 months ago

dverite commented 5 months ago

After reviewing min_uuid_v7 I understood the bug setting the UUID version bits in my first attempt. I suggest this fixed version:

CREATE OR REPLACE FUNCTION min_uuid_v7(min_timestamp timestamp) RETURNS uuid
LANGUAGE sql immutable strict
AS $function$
  SELECT encode(
      overlay('\x00000000000070000000000000000000'::bytea
          placing substring(int8send(floor(extract(epoch from min_timestamp) * 1000)::bigint) from 3)
          from 1 for 6)
      , 'hex')::uuid;
$function$;

The value simply starts with the version bits already set before overlaying with the timestamp. set_bit calls are not needed.

vmercierfr commented 5 months ago

Thanks for this improvement, I adopted it for the initial version in init branch