-- Function: smolid
-- Generates a random string of a specified size using the provided alphabet
-- size: The length of the generated string (default is 21)
-- alphabet: A string of characters from which the random string will be created (default is upper/lower case letters and digits)

CREATE OR REPLACE FUNCTION smolid(
    size INTEGER DEFAULT 21,
    alphabet TEXT DEFAULT 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
)
RETURNS TEXT AS $$
DECLARE
    result TEXT := '';
    i INTEGER;
BEGIN
    FOR i IN 1..size LOOP
        result := result || substr(alphabet, floor(random() * length(alphabet) + 1)::int, 1);
    END LOOP;
    RETURN result;
END;
$$ LANGUAGE plpgsql;