Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
fabiolimace / fn_password_md5.sql
Last active April 24, 2020 23:53
Function for generating password MD5 on PostgreSQL
/**
* Returns the password MD5.
*
* @param p_password the password string
* @param p_salt the salt string
* @param p_encode the output encoding: 'base64', 'hex', 'escape'.
*/
CREATE FUNCTION fn_password_md5(p_password VARCHAR, p_salt VARCHAR, p_encode VARCHAR) RETURNS VARCHAR AS $$
DECLARE
v_salt_hash BYTEA := NULL;
@fabiolimace
fabiolimace / fn_uuid_random_based.sql
Last active November 23, 2021 16:51
Function for generating random-based UUIDs (v4) on PostgreSQL
/**
* Generate a random-based UUID (v4)
*
* RFC-4122 compliant.
*
* Tags: uuid guid uuid-generator guid-generator generator random rfc4122 rfc-4122
*/
create or replace function fn_uuid_random_based() returns uuid as $$
declare
v_bytes bytea;
@fabiolimace
fabiolimace / fn_uuid_name_based.sql
Last active September 11, 2021 04:22
Function for generating name-based MD5 UUIDs (v3) on PostgreSQL
/**
* Returns a name-based UUID v3 (MD5)
*
* RFC-4122 compliant.
*
* Tags: uuid guid uuid-generator guid-generator generator name hash md5 sha1 rfc4122 rfc-4122
*
* @param p_name a string
* @param p_namespace a UUID string
@fabiolimace
fabiolimace / UUIDv6.sql
Last active October 30, 2024 19:52
Functions for generating UUIDv6 and UUIDv7 on PostgreSQL
/*
* MIT License
*
* Copyright (c) 2023-2024 Fabio Lima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@fabiolimace
fabiolimace / uuid1.sql
Last active April 14, 2023 00:05
Function for generating UUIDv1 on PostgreSQL
/**
* Returns a time-based UUID (UUIDv1).
*
* RFC-4122 compliant.
*
* Tags: uuid guid uuid-generator guid-generator generator time rfc4122 rfc-4122
*/
create or replace function uuid1() returns uuid as $$
declare
v_time timestamp with time zone:= null;
@fabiolimace
fabiolimace / select_uuid_random_based.sql
Last active December 29, 2020 23:40
Select statement for generating random UUIDs (v4) on PostgreSQL
/**
* Generate a random-based UUID (v4)
*
* RFC-4122 compliant.
*
* Tags: uuid guid uuid-generator guid-generator generator random rfc4122 rfc-4122
*/
select (overlay(overlay(md5(random()::text || random()::text || current_timestamp::text) placing '4' from 13) placing ( to_hex(8 + (extract(microseconds from statement_timestamp())::integer % 4)) ) from 17))::uuid;
@fabiolimace
fabiolimace / fn_tsid_milli.sql
Last active October 16, 2024 09:00
Function for generating Time Sortable ID with millisecond precision on PostgreSQL
/**
* Returns a Time Sortable ID with millisecond precision.
*
* Time component: 42 bits (2^42 = ~69 years)
*
* Random component: 22 bits (2^22 = 4,194,304)
*
* The time component is the count of milliseconds since 2020-01-01T00:00:00Z.
*
* Tags: tsid ulid snowflake id-generator generator time sortable sort order id
@fabiolimace
fabiolimace / fn_tsid_micro.sql
Last active November 29, 2022 17:37
Function for generating Time Sortable ID with microsecond precision on PostgreSQL
/**
* Returns a Time Sortable ID with microsecond precision.
*
* Time component: 52 bits (2^52 = ~71 years)
*
* Random component: 12 bits (2^12 = 4,096)
*
* The time component is the count of microseconds since 2020-01-01T00:00:00Z.
*
* Tags: tsid ulid snowflake id-generator generator time sortable sort order id
@fabiolimace
fabiolimace / find_database_objects.sql
Created June 12, 2020 12:53
Find database objects on PostgreSQL
-- Find table
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type='BASE TABLE'
AND table_name ILIKE '%tablename%';
@fabiolimace
fabiolimace / NanoCombCreator.java
Last active October 24, 2020 22:15
Generate COMB UUID/GUID with nanosecond resolution in Java
package your.package.name;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.UUID;
/**
* A UUID generator that creates a COMB GUID with nanoseconds resolution.
*
* It borrows the main idea from ULID and COMB generators: a concatenation of