Задание 5: Режим информирования
В этом задании будет изучено включение режима информирования, информирование о превышении ограничений памяти и использования процессора.
Включение режима информирования
Переключим параметры расширения psql_resources_consumption_limits в режим информирования о превышении ограничений:
postgres@limdb=# ALTER SYSTEM SET psql_resources_consumption_limits.mem_exceeding_reaction = 'alert';
ALTER SYSTEM
postgres@limdb=# ALTER SYSTEM SET psql_resources_consumption_limits.cpu_exceeding_reaction = 'alert';
ALTER SYSTEM
Перечитаем конфигурацию:
postgres@limdb=# SELECT pg_reload_conf();
┌────────────────┐
│ pg_reload_conf │
├────────────────┤
│ t │
└────────────────┘
(1 row)
Проверим настройки:
postgres@limdb=# \dconfig psql_resources_consumption_limits.*_exceeding_reaction
List of configuration parameters
┌──────────────────────────────────────────────────────────┬───────┐
│ Parameter │ Value │
├──────────────────────────────────────────────────────────┼───────┤
│ psql_resources_consumption_limits.cpu_exceeding_reaction │ alert │
│ psql_resources_consumption_limits.mem_exceeding_reaction │ alert │
└──────────────────────────────────────────────────────────┴───────┘
(2 rows)
Имеется еще один параметр, настраивающий уровень сообщений, записываемых в журнал:
postgres@limdb=# \dconfig psql_resources_consumption_limits.*level
List of configuration parameters
┌───────────────────────────────────────────────────┬─────────┐
│ Parameter │ Value │
├───────────────────────────────────────────────────┼─────────┤
│ psql_resources_consumption_limits.alert_log_level │ warning │
└───────────────────────────────────────────────────┴─────────┘
(1 row)
Информирование о превышении ограничения памяти
Подключимся к СУБД от имени роли test1:
[postgres@ServerName ~]$ psql -U test1 -d limdb -h localhost
Password for user test1:
Pager is used for long output.
Border style is 2.
Line style is unicode.
psql (15.15)
Type "help" for help.
test1@limdb=> SELECT pg_backend_pid();
┌────────────────┐
│ pg_backend_pid │
├────────────────┤
│ 234181 │
└────────────────┘
(1 row)
В сеансе postgres получим информацию о сессии test1:
postgres@limdb=# SELECT * FROM psql_resources_consumption_limits.consumption \gx
┌─[ RECORD 1 ]────────┬─────────┐
│ session_id │ 234181 │
│ proc_id │ 234181 │
│ role_id │ 17875 │
│ rolname │ test1 │
│ log_level │ warning │
│ mem_current │ 22020 │
│ mem_current_session │ 22020 │
│ mem_max │ 22020 │
│ mem_max_session │ 22020 │
│ mem_limit │ 26624 │
│ mem_reaction │ alert │
│ cpu_current │ 0 │
│ cpu_current_session │ 0 │
│ cpu_max │ 0 │
│ cpu_max_session │ 0 │
│ cpu_limit │ 0 │
│ cpu_reaction │ alert │
└─────────────────────┴─────────┘
Выполним запрос, в результате которого ограничение на использование памяти будет превышено:
test1@limdb=> DO $$
DECLARE
data bytea[] := '{}';
i int;
BEGIN
FOR i IN 1..10 LOOP
data := array_append(data, repeat(chr(65), 1048576)::bytea);
END LOOP;
PERFORM pg_sleep(60);
END $$;
Сессия не сброшена.
Проверим использование ресурсов сессией:
postgres@limdb=# SELECT * FROM psql_resources_consumption_limits.consumption \gx
┌─[ RECORD 1 ]────────┬─────────┐
│ session_id │ 234181 │
│ proc_id │ 234181 │
│ role_id │ 17875 │
│ rolname │ test1 │
│ log_level │ warning │
│ mem_current │ 37660 │
│ mem_current_session │ 37660 │
│ mem_max │ 37660 │
│ mem_max_session │ 37660 │
│ mem_limit │ 26624 │
│ mem_reaction │ alert │
│ cpu_current │ 0 │
│ cpu_current_session │ 0 │
│ cpu_max │ 0 │
│ cpu_max_session │ 0 │
│ cpu_limit │ 0 │
│ cpu_reaction │ alert │
└─────────────────────┴─────────┘
Обратим внимание на то, что ограничение на использование памяти превышено.
Проверим, что записалось в журнал отчета:
postgres@limdb=# \q
[postgres@ServerName ~]$ less /pgerrorlogs/06/postgresql-{date}.log
...
WARNING: The process 234181 memory consumption 37660 (session 37660) has violated the memory limit 26624
WARNING: The process 234181 memory consumption 37660 (session 37660) has violated the memory limit 26624
WARNING: The process 234181 memory consumption 37660 (session 37660) has violated the memory limit 26624
WARNING: The process 234181 memory consumption 37660 (session 37660) has violated the memory limit 26624
WARNING: The process 234181 memory consumption 37660 (session 37660) has violated the memory limit 26624
WARNING: The process 234181 memory consumption 37660 (session 37660) has violated the memory limit 26624
В журнале отчета имеются сообщения о превышении ограничения использования памяти с уровнем WARNING.
Информирование о превышении ограничения использования процессора
Подключимся к СУБД в сеансе test2:
[postgres@ServerName ~]$ psql -U test2 -d limdb -h localhost
Password for user test2:
Pager is used for long output.
Border style is 2.
Line style is unicode.
psql (15.15)
Type "help" for help.
test2@limdb=> SELECT pg_backend_pid();
┌────────────────┐
│ pg_backend_pid │
├────────────────┤
│ 234450 │
└────────────────┘
(1 row)
Запустим нагрузочный скрипт:
test2@limdb=> do $$
begin
while true loop
end loop;
end $$;
Цикл бесконечный, командную строку вернуть не должен.
Проверим в сессии postgres информацию о расходуемых сессией test2 ресурсах:
[postgres@ServerName ~]$ psql -d limdb
postgres@limdb=# SELECT * FROM psql_resources_consumption_limits.consumption \gx
┌─[ RECORD 1 ]────────┬─────────┐
│ session_id │ 234450 │
│ proc_id │ 234450 │
│ role_id │ 17876 │
│ rolname │ test2 │
│ log_level │ warning │
│ mem_current │ 22012 │
│ mem_current_session │ 0 │
│ mem_max │ 0 │
│ mem_max_session │ 0 │
│ mem_limit │ 0 │
│ mem_reaction │ alert │
│ cpu_current │ 99 │
│ cpu_current_session │ 99 │
│ cpu_max │ 99 │
│ cpu_max_session │ 99 │
│ cpu_limit │ 4 │
│ cpu_reaction │ alert │
└─────────────────────┴─────────┘
Остановим цикл, нажав Ctrl-C:
test2@limdb=> ^C
^CCancel request sent
ERROR: canceling statement due to user request
CONTEXT: PL/pgSQL function inline_code_block line 3 at WHILE
Снова получим отчет об используемых ресурсах:
postgres@limdb=# SELECT * FROM psql_resources_consumption_limits.consumption \gx
┌─[ RECORD 1 ]────────┬─────────┐
│ session_id │ 234450 │
│ proc_id │ 234450 │
│ role_id │ 17876 │
│ rolname │ test2 │
│ log_level │ warning │
│ mem_current │ 22012 │
│ mem_current_session │ 0 │
│ mem_max │ 0 │
│ mem_max_session │ 0 │
│ mem_limit │ 0 │
│ mem_reaction │ alert │
│ cpu_current │ 0 │
│ cpu_current_session │ 0 │
│ cpu_max │ 99 │
│ cpu_max_session │ 99 │
│ cpu_limit │ 4 │
│ cpu_reaction │ alert │
└─────────────────────┴─────────┘
Исследуем журнал:
postgres@limdb=# \q
[postgres@ServerName ~]$ less /pgerrorlogs/06/postgresql-{date}.log
WARNING: The process 234450 cpu consumption 99 (session 99) has violated the cpu limit 4
ERROR: canceling statement due to user request
CONTEXT: PL/pgSQL function inline_code_block line 3 at WHILE
STATEMENT: do $$
begin
while true loop
end loop;
end $$;
Первое сообщение из приведенных в примере говорит, что процесс превысил ограничение на используемые ресурсы процессора.
Сброс и удаление объектов
Сбросим настройки в postgresql.auto.conf и удалим базу данных limdb:
[postgres@ServerName ~]$ psql -d postgres
Pager is used for long output.
Border style is 2.
Line style is unicode.
psql (15.15)
Type "help" for help.
postgres=# ALTER SYSTEM RESET ALL;
ALTER SYSTEM
postgres=# SELECT pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)
postgres=# DROP DATABASE limdb;
DROP DATABASE
postgres=# \q