From 81d4c3ad749491ca0dabf4a43cfb9ecf719678a5 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Sun, 12 Oct 2025 02:27:55 +0500 Subject: [PATCH] Added get_pg_backend_state --- include/PgSQL_Connection.h | 1 + lib/PgSQL_Connection.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/PgSQL_Connection.h b/include/PgSQL_Connection.h index 49e17baef..60a393ebd 100644 --- a/include/PgSQL_Connection.h +++ b/include/PgSQL_Connection.h @@ -509,6 +509,7 @@ public: char get_transaction_status_char(); inline int get_backend_pid() { return (pgsql_conn) ? get_pg_backend_pid() : -1; } bool is_pipeline_active() { return (PQpipelineStatus(pgsql_conn) != PQ_PIPELINE_OFF); } + const char* get_pg_backend_state() const; static int char_to_encoding(const char* name) { return pg_char_to_encoding(name); diff --git a/lib/PgSQL_Connection.cpp b/lib/PgSQL_Connection.cpp index 494d300cc..044c3c0bc 100644 --- a/lib/PgSQL_Connection.cpp +++ b/lib/PgSQL_Connection.cpp @@ -2117,6 +2117,25 @@ const char* PgSQL_Connection::get_pg_transaction_status_str() { return "INVALID"; } +const char* PgSQL_Connection::get_pg_backend_state() const { + if (PQstatus(pgsql_conn) != CONNECTION_OK) + return "disconnected"; + + switch (PQtransactionStatus(pgsql_conn)) { + case PQTRANS_IDLE: + return "idle"; + case PQTRANS_ACTIVE: + return "active"; + case PQTRANS_INTRANS: + return "idle in transaction"; + case PQTRANS_INERROR: + return "idle in transaction (aborted)"; + case PQTRANS_UNKNOWN: + default: + return "unknown"; + } +} + bool PgSQL_Connection::handle_copy_out(const PGresult* result, uint64_t* processed_bytes) { if (new_result == true) {