Skip to content

Instantly share code, notes, and snippets.

@joelonsql
joelonsql / create_or_lookup_function.sql
Last active January 13, 2023 16:41
Content-Addressed Functions in PostgreSQL
--
-- Content-Addressed Functions
--
-- Inspired by Unison's concept of using a hash of a function's syntax tree
-- as its name, sometimes referred to as "content-addressed naming",
-- we can avoid the need to create/drop lots of different temp function,
-- by using the hash of its definition as its name,
-- and simply reuse the function for all tests that need the same function.
--
-- While Unison uses 512-bit SHA3, we use sha224() since the maximum length
const std = @import("std");
const bigint = std.math.big.int.Managed;
pub fn main() !void {
var a = try bigint.initSet(std.heap.c_allocator, 5);
try a.pow(&a, try std.math.powi(u32, 4, try std.math.powi(u32, 3, 2)));
defer a.deinit();
var as = try a.toString(std.heap.c_allocator, 10, .lower);
defer std.heap.c_allocator.free(as);
@joelonsql
joelonsql / tarjan.sql
Created December 7, 2022 13:05
Tarjan's strongly connected components algorithm implemented in PL/pgSQL
CREATE OR REPLACE FUNCTION tarjan(from_nodes int4range[], to_nodes int[])
RETURNS SETOF int[]
LANGUAGE plpgsql
AS
$$
--
-- https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
--
DECLARE
current_index integer := 0;
@joelonsql
joelonsql / PostgreSQL-EXTENSIONs.md
Last active June 5, 2025 13:56
1000+ PostgreSQL EXTENSIONs

🗺🐘 1000+ PostgreSQL EXTENSIONs

This is a list of URLs to PostgreSQL EXTENSION repos, listed in alphabetical order of parent repo, with active forks listed under each parent.

⭐️ >= 10 stars
⭐️⭐️ >= 100 stars
⭐️⭐️⭐️ >= 1000 stars
Numbers of stars might not be up-to-date.

@joelonsql
joelonsql / JOIN FOREIGN.md
Last active February 8, 2023 23:29
SQL language proposal: JOIN FOREIGN

SQL language proposal: JOIN FOREIGN

The idea is to improve the SQL language, specifically the join syntax, for the special but common case when joining on foreign key columns.

The problem

Example below taken from PostgreSQL documentation [1]

In SQL-89, we didn't have any JOIN syntax yet, so queries were written in this way:

@joelonsql
joelonsql / hyperloglog-demo.sql
Created June 2, 2021 08:55
HyperLogLog demo
--
-- Here comes some general advise on how to use HyperLogLog
-- for someone who wants to implement a YouTube-like service
-- using the awesome https://github.com/citusdata/postgresql-hll PostgreSQL extension
--
CREATE TABLE counter (
id text NOT NULL,
sketch hll NOT NULL DEFAULT hll_empty(),
PRIMARY KEY (id)
@joelonsql
joelonsql / guess_language.sql
Created May 22, 2021 15:08
PostgreSQL guess_language(text) one-liner SQL-function
CREATE OR REPLACE FUNCTION guess_language(string text)
RETURNS text
LANGUAGE sql
AS
$$
SELECT cfgname::text FROM pg_ts_config WHERE cfgname <> 'simple' ORDER BY length(to_tsvector(cfgname::regconfig,string)) LIMIT 1
$$;
--
-- Demo testing some random sentences from Wikipedia:
@joelonsql
joelonsql / easter.sql
Last active December 7, 2020 21:18
PL/pgSQL vs Subqueries vs LATERAL
CREATE OR REPLACE FUNCTION easter_plpgsql(year integer)
RETURNS date
LANGUAGE plpgsql
AS $$
-- https://github.com/christopherthompson81/pgsql_holidays/blob/master/utils/easter.pgsql
DECLARE
g CONSTANT integer := Year % 19;
c CONSTANT integer := Year / 100;
h CONSTANT integer := (c - c/4 - (8*c + 13)/25 + 19*g + 15) % 30;
i CONSTANT integer := h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 - g)/11));
@joelonsql
joelonsql / easter.sql
Created December 7, 2020 20:59
Easter functions PL/pgSQL vs Subqueries vs LATERAL
CREATE OR REPLACE FUNCTION Easter(Year integer)
RETURNS date
LANGUAGE plpgsql
IMMUTABLE
AS $$
-- https://github.com/christopherthompson81/pgsql_holidays/blob/master/utils/easter.pgsql
DECLARE
g CONSTANT integer := Year % 19;
c CONSTANT integer := Year / 100;
h CONSTANT integer := (c - c/4 - (8*c + 13)/25 + 19*g + 15) % 30;
@joelonsql
joelonsql / decode_cbor.sql
Last active January 13, 2024 04:36
Decode WebAuthn CBOR Using PostgreSQL Recursive CTE
CREATE OR REPLACE FUNCTION decode_cbor(cbor bytea)
RETURNS TABLE (
item integer,
map_item_count integer,
text_string text,
bytes bytea
)
LANGUAGE sql
AS $$
/*