diff --git a/instructions.txt b/instructions.txt index 0eec54b..0f50d3c 100644 --- a/instructions.txt +++ b/instructions.txt @@ -235,4 +235,13 @@ SET node_local_interface = ( FROM spock.node_interface WHERE if_name = 'node2_local' AND if_nodeid = 13534 -); \ No newline at end of file +); +|========================| +|WAY TO ENTER PSQL SHELL | +|========================| +psql "postgresql://postgres_exporter:postgres@172.16.10.178:5444/postgres" + +|=============================| +|ENDPONT TO DETECT A DEAD LOCK| +|=============================| + http://172.16.10.178:9188/metrics | grep -i deadlock \ No newline at end of file diff --git a/lockDetectServer.py b/lockDetectServer.py new file mode 100644 index 0000000..9ad7151 --- /dev/null +++ b/lockDetectServer.py @@ -0,0 +1,66 @@ +import time +import psycopg2 + +DB_CONFIG = { + "host": "172.16.10.178", + "port": 5444, + "dbname": "postgres", + "user": "postgres_exporter", + "password": "postgres" +} + +while True: + try: + conn = psycopg2.connect(**DB_CONFIG) + cur = conn.cursor() + + cur.execute(""" + SELECT + pid, + usename, + now() - query_start AS duration, + query + FROM pg_stat_activity + WHERE state = 'active' + AND pid <> pg_backend_pid() + AND query_start < now() - interval '30 seconds'; + """) + + long_queries = cur.fetchall() + + if long_queries: + print("WARNING: Long-running queries detected") + + for row in long_queries: + print(row) + else: + print("OK: No long-running queries") + + cur.execute(""" + SELECT + pid, + usename, + query, + pg_blocking_pids(pid) + FROM pg_stat_activity + WHERE cardinality(pg_blocking_pids(pid)) > 0; + """) + + blockers = cur.fetchall() + + if blockers: + print("WARNING: Blocking sessions detected") + for row in blockers: + print(row) + else: + print("OK: No blocking sessions") + + cur.close() + conn.close() + + except Exception as e: + print("ERROR:", e) + + time.sleep(10) + +