Files
teleport/lib/srv/db/postgres/sql/delete-user.sql
T
STeve (Xin) Huang 4dce835487 Database Automatic User Provisioning support for Redshift (#33307)
* Database Automatic User Provisioning support for Redshift

* capitalize sql script

* DeleteUser to fallback to deactivate.

* add TPxxx code to RAISE messages

* fix missing space in scripts
2023-10-23 18:44:38 +00:00

22 lines
764 B
PL/PgSQL

create or replace procedure 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 %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 teleport_deactivate_user(username);
end;
end if;
end;$$;