Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amandoabreu/cabe92c16f341e6c37d4f2bf965c225b to your computer and use it in GitHub Desktop.
Save amandoabreu/cabe92c16f341e6c37d4f2bf965c225b to your computer and use it in GitHub Desktop.
# Create function
CREATE OR REPLACE FUNCTION get_organization_id_by_user_id(p_user_id INTEGER)
RETURNS INTEGER AS $$
DECLARE
v_organization_id INTEGER;
BEGIN
SELECT organization
INTO v_organization_id
FROM users
WHERE id = p_user_id;
RETURN v_organization_id;
END;
$$ LANGUAGE plpgsql;
# Call it
select get_organization_id_by_user_id(1)
# View all functions
SELECT
n.nspname AS schema_name,
p.proname AS function_name,
pg_get_function_arguments(p.oid) AS function_arguments
FROM
pg_proc p
JOIN pg_namespace n ON p.pronamespace = n.oid
WHERE
n.nspname NOT LIKE 'pg_%' AND
n.nspname != 'information_schema'
ORDER BY
schema_name,
function_name;
# View function definition by name
SELECT prosrc FROM pg_proc WHERE proname = 'function_name';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment