Files
teleport/lib/srv/db/postgres/sql/delete-user.sql
T
Gabriel Corado 0daeb99f90 Use pg_temp to store PostgreSQL auto provisioning procedures (#44255)
* refactor(postgres): use pg_temp to store auto user procedures

* refactor(postgres): code review suggestions

* test(postgres): replace logrus with slog

* chore(postgres): move switch statement to a map

* test(postgres): with wrong slog call

* test(postgres): use slog context functions
2024-07-18 18:29:29 +00:00

22 lines
790 B
PL/PgSQL

CREATE OR REPLACE PROCEDURE pg_temp.teleport_delete_user(username varchar, inout state varchar default 'TP003')
LANGUAGE plpgsql
AS $$
DECLARE
role_ varchar;
BEGIN
-- Only drop if the user doesn't have other active sessions.
IF EXISTS (SELECT usename FROM pg_stat_activity WHERE usename = username) THEN
RAISE NOTICE 'User has active connections';
ELSE
BEGIN
EXECUTE FORMAT('DROP USER IF EXISTS %I', username);
EXCEPTION
WHEN SQLSTATE '2BP01' THEN
state := 'TP004';
-- Drop user/role will fail if user has dependent objects.
-- In this scenario, fallback into disabling the user.
CALL pg_temp.teleport_deactivate_user(username);
END;
END IF;
END;$$;