#3743·glances

TimescaleDB exporter does not recover after a failed transaction

Author: HassanRadyCreated Sep 25, 2026Updated Sep 25, 2026

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 block

A 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__.py

Both table-creation and insertion errors can leave the shared connection in an aborted transaction.

To Reproduce

  1. Check out a Glances revision without the transaction-recovery fix.

  2. Ensure Docker is running.

  3. Run the independent reproduction script:

    bash
    ./HASSAN_RADY/reproduce_timescaledb_aborted_transaction.sh

    The script:

    • Starts an isolated PostgreSQL-compatible Docker container.
    • Creates a table containing a BIGINT column.
    • Uses the actual Glances Export.export() implementation.
    • Attempts to insert an invalid string into the BIGINT column.
    • Attempts a valid insert afterward using the same connection.
  4. On the affected implementation, inspect the connection state after the invalid insert. It remains:

    INERROR
  5. The 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:

  1. Log the original database error.
  2. Roll back the failed transaction.
  3. Return a failure result for the current export.
  4. Leave the connection in the IDLE state.
  5. 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: INERROR

A successful fix should instead produce:

Initial connection state: IDLE
State after failed insert: IDLE
Connection state after valid insert: IDLE

Environement (please complete the following information)

  • Operating System (lsb_release -a or OS name/version): To be completed
  • Glances & psutil versions: Glances installed from source; exact glances -V output 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, with postgres:16-alpine as 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:

python
try:
    cursor.executemany(insert_query, values)
except Exception as error:
    logger.error("Cannot insert data: %s", error)
    return  # Connection remains in INERROR

The exception path should roll back before returning:

python
try:
    cursor.executemany(insert_query, values)
    connection.commit()
except Exception as error:
    connection.rollback()
    logger.error("Cannot insert data: %s", error)
    return False

Because 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.