TimescaleDB exporter does not recover after a failed transaction
Check the bug
- I searched for an existing issue describing the same TimescaleDB transaction-recovery problem.
Describe the bug
When a TimescaleDB operation fails, the exporter catches the database exception and returns without rolling back the transaction.
Psycopg connections use transactions by default. After PostgreSQL rejects a statement, the connection remains in the INERROR transaction state until rollback() is called.
The exporter reuses this connection during subsequent refreshes. Consequently, every later database command fails with:
psycopg.errors.InFailedSqlTransaction:
current transaction is aborted, commands ignored until end of transaction blockA single transient schema, DDL, or insertion error can therefore disable the TimescaleDB exporter until Glances is restarted or the database connection is recreated.
The affected code is in:
glances/exports/glances_timescaledb/__init__.pyBoth table-creation and insertion errors can leave the shared connection in an aborted transaction.
To Reproduce
Check out a Glances revision without the transaction-recovery fix.
Ensure Docker is running.
Run the independent reproduction script:
./HASSAN_RADY/reproduce_timescaledb_aborted_transaction.shThe script:
- Starts an isolated PostgreSQL-compatible Docker container.
- Creates a table containing a
BIGINTcolumn. - Uses the actual Glances
Export.export()implementation. - Attempts to insert an invalid string into the
BIGINTcolumn. - Attempts a valid insert afterward using the same connection.
On the affected implementation, inspect the connection state after the invalid insert. It remains:
INERRORThe next valid refresh fails with an error similar to:
psycopg.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block
The same behavior can occur during normal Glances operation when a catalog query, table-creation statement, or insert fails.
Expected behavior
When a TimescaleDB operation fails, the exporter should:
- Log the original database error.
- Roll back the failed transaction.
- Return a failure result for the current export.
- Leave the connection in the
IDLEstate. - Allow later refreshes to continue using the connection.
A failed sample should not permanently disable the exporter. After an invalid insert, a subsequent valid insert using the same connection should succeed.
Screenshots
Not applicable.
Example transaction states from the reproduction:
Initial connection state: IDLE
State after failed insert: INERRORA successful fix should instead produce:
Initial connection state: IDLE
State after failed insert: IDLE
Connection state after valid insert: IDLEEnvironement (please complete the following information)
- Operating System (
lsb_release -aor OS name/version):To be completed - Glances & psutil versions: Glances installed from source; exact
glances -Voutput to be completed - How do you install Glances: From source
- Glances test:
To be completed with result of: glances --issue - Python version used by the reproducer: Python 3.11
- Psycopg version used by the reproducer: 3.3.5
- Database image:
timescale/timescaledb:2.30.1-pg16, withpostgres:16-alpineas a compatible fallback
Additional context
The exporter uses one persistent Psycopg connection for catalog queries, DDL, inserts, commits, and subsequent refreshes.
Closing a cursor does not roll back the connection transaction. Catching the exception and returning is therefore insufficient: an explicit connection.rollback() is required before the connection can execute another command.
The relevant failure pattern is equivalent to:
try:
cursor.executemany(insert_query, values)
except Exception as error:
logger.error("Cannot insert data: %s", error)
return # Connection remains in INERRORThe exception path should roll back before returning:
try:
cursor.executemany(insert_query, values)
connection.commit()
except Exception as error:
connection.rollback()
logger.error("Cannot insert data: %s", error)
return FalseBecause the connection is shared, the complete cursor/commit/rollback operation should also remain serialized so another export cannot use the connection while transaction recovery is taking place.
The independent reproduction script verifies recovery by causing one intentional failure and then performing valid inserts using the same connection.
Source: nicolargo/glances