Skip to content

Commit

Permalink
[DBM-2734] fix test_snapshot_xmin for pg > 13 (#15718)
Browse files Browse the repository at this point in the history
* fix test_snapshot_xmin for pg > 13

* reorder the steps to first collect metrics

* remove check.cancel()
  • Loading branch information
lu-zhengda authored Aug 30, 2023
1 parent a822589 commit 2e31e3c
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions postgres/tests/test_pg_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,37 @@ def test_common_metrics(aggregator, integration_check, pg_instance, is_aurora):


def test_snapshot_xmin(aggregator, integration_check, pg_instance):
# In the test we are going to first run the check to collect xmin & xmax
check = integration_check(pg_instance)
check.check(pg_instance)

# Once we have the metrics, we will run a simple query to collect the xmin
# The xmin we collect should be the same as the one collected by the check
with psycopg.connect(host=HOST, dbname=DB_NAME, user="postgres", password="datad0g") as conn:
with conn.cursor() as cur:
cur.execute('select txid_snapshot_xmin(txid_current_snapshot());')
if float(POSTGRES_VERSION) >= 13.0:
query = 'select pg_snapshot_xmin(pg_current_snapshot());'
else:
query = 'select txid_snapshot_xmin(txid_current_snapshot());'
cur.execute(query)
xmin = float(cur.fetchall()[0][0])
check = integration_check(pg_instance)
check.check(pg_instance)

expected_tags = _get_expected_tags(check, pg_instance)
aggregator.assert_metric('postgresql.snapshot.xmin', value=xmin, count=1, tags=expected_tags)
aggregator.assert_metric('postgresql.snapshot.xmax', value=xmin, count=1, tags=expected_tags)

# We then force the increase of the txid by 2
with psycopg.connect(host=HOST, dbname=DB_NAME, user="postgres", password="datad0g", autocommit=True) as conn:
with conn.cursor() as cur:
# Force increases of txid
cur.execute('select txid_current();')
cur.execute('select txid_current();')

check = integration_check(pg_instance)
if float(POSTGRES_VERSION) >= 13.0:
query = 'select pg_current_xact_id();'
else:
query = 'select txid_current();'
cur.execute(query)
cur.execute(query)

# Recollect the metrics
check.check(pg_instance)
aggregator.assert_metric('postgresql.snapshot.xmin', value=xmin + 2, count=1, tags=expected_tags)
aggregator.assert_metric('postgresql.snapshot.xmax', value=xmin + 2, count=1, tags=expected_tags)
Expand Down

0 comments on commit 2e31e3c

Please sign in to comment.