/
kirillilych
/
NIR
Обзор
Документация
Войти
/
kirillilych
/
NIR
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
src/primary/postgres_replication_update_current_timestamp.py
40 строк
1 KB
Ilichev Kirill
scripts for timestamp and lag
21 июн 2025, 22:52
21 июн 2025, 22:52
98b7471
Код
Авторство
О чём код?
import psycopg2 import time DB_CONFIG = { "host": "localhost", "database": "postgres", "user": "postgres", "password": "1234", "port": "5432" } SERVER_IDS = [1, 2] def update_timestamp(): conn = psycopg2.connect(**DB_CONFIG) cursor = conn.cursor() try: while True: query_set_ts = f'INSERT INTO replication_lag_timestamp (server_id, ts, is_active) VALUES (1, CURRENT_TIMESTAMP, TRUE), (2, CURRENT_TIMESTAMP, TRUE) ON CONFLICT (server_id) DO UPDATE SET ts = EXCLUDED.ts, is_active = TRUE;' cursor.execute(query_set_ts) conn.commit() time.sleep(0.01) except KeyboardInterrupt: query_set_inactive = f'UPDATE replication_lag_timestamp SET is_active = FALSE WHERE server_id = 1 OR server_id = 2;' cursor.execute(query_set_inactive) conn.commit() print("\nScript was interrupted by keyboard") except Exception as e: print(f"Error: {e}") finally: cursor.close() conn.close() print("PostgreSQL connection closed") if __name__ == "__main__": update_timestamp()