-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkeyboard.sql
50 lines (45 loc) · 1.22 KB
/
keyboard.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
create or replace function keyboard_init()
returns text
language plpgsql
as $$
declare
password text;
q int := 0;
ks_id text;
start_time timestamp(3) with time zone;
begin
select now() into start_time;
select random_text_simple(8) || ';' into password;
raise info 'Let''s connect keyboard. Open another psql process and enter "%"', password;
loop
select count(*) into q from postgres_log where log_time >= start_time and query = password;
if q > 0 then
select session_id into ks_id from postgres_log where query = password;
exit;
end if;
perform pg_sleep(1);
end loop;
raise info 'Keyboard connected';
return ks_id;
end
$$;
create or replace function keyboard_read(keyboard_session_id text)
returns text
language plpgsql
as $$
declare
start_time timestamp(3) with time zone;
q int;
result text;
begin
select now() into start_time;
loop
select query into result from postgres_log where log_time >= start_time and session_id = keyboard_session_id order by log_time desc limit 1;
if result is not null then
exit;
end if;
perform pg_sleep(1);
end loop;
return result;
end
$$;