Skip to content

Commit 2796a11

Browse files
authored
PG: Directly run ANALYZE for the test_analyze_progress (DataDog#21219)
* PG: Directly run ANALYZE for the test_analyze_progress Tests are currently using `vacuum analyze` to check metrics reporting analyze progress. If the vacuum phase takes more than 1s, the test will fail as the wait_for_value will exit and only a running vacuum will be present when metrics are collected. To avoid flakiness, we skip the vacuum phase and directly run an analyze. * PG: Only test backend transaction age on test app Don't check for metrics generated by datadog-agent app as background queries could interfer. Instead, we only check for queries from the 'test' app which should have a much more predictable behaviour. * PG: Use dedicated application_name for vacuum and tx age tests Queries could be left over by previous tests and possibly interfere with following tests. Use a different application_name to avoid interference between tests.
1 parent ce95045 commit 2796a11

2 files changed

Lines changed: 12 additions & 24 deletions

File tree

postgres/tests/test_pg_integration.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,10 @@ def test_activity_metrics_no_aggregations(aggregator, integration_check, pg_inst
440440
def test_activity_vacuum_excluded(aggregator, integration_check, pg_instance):
441441
pg_instance['collect_activity_metrics'] = True
442442
check = integration_check(pg_instance)
443+
app = 'test_activity_vacuum_excluded'
443444

444445
# Run vacuum in a thread
445-
thread = run_vacuum_thread(pg_instance, 'VACUUM (DISABLE_PAGE_SKIPPING, ANALYZE) persons', application_name='test')
446+
thread = run_vacuum_thread(pg_instance, 'VACUUM (DISABLE_PAGE_SKIPPING, ANALYZE) persons', application_name=app)
446447

447448
# Wait for vacuum to be running
448449
_wait_for_value(
@@ -451,7 +452,7 @@ def test_activity_vacuum_excluded(aggregator, integration_check, pg_instance):
451452
query="SELECT count(*) from pg_stat_activity WHERE backend_type = 'client backend' AND query ~* '^vacuum';",
452453
)
453454

454-
conn_increase_txid = _get_conn(pg_instance, user=USER_ADMIN, password=PASSWORD_ADMIN, application_name='test')
455+
conn_increase_txid = _get_conn(pg_instance, user=USER_ADMIN, password=PASSWORD_ADMIN, application_name=app)
455456
cur = conn_increase_txid.cursor()
456457
# Increase txid counter
457458
_increase_txid(cur)
@@ -463,7 +464,7 @@ def test_activity_vacuum_excluded(aggregator, integration_check, pg_instance):
463464
# Gather metrics
464465
check.run()
465466

466-
expected_tags = _get_expected_tags(check, pg_instance, db=DB_NAME, app='test', user=USER_ADMIN)
467+
expected_tags = _get_expected_tags(check, pg_instance, db=DB_NAME, app=app, user=USER_ADMIN)
467468
aggregator.assert_metric('postgresql.waiting_queries', value=1, count=1, tags=expected_tags)
468469
# Vacuum process with 3 xmin age should not be reported
469470
aggregator.assert_metric('postgresql.activity.backend_xmin_age', count=1, tags=expected_tags)
@@ -477,25 +478,21 @@ def test_activity_vacuum_excluded(aggregator, integration_check, pg_instance):
477478
thread.join()
478479

479480

480-
@pytest.mark.flaky(max_runs=10)
481481
def test_backend_transaction_age(aggregator, integration_check, pg_instance):
482482
pg_instance['collect_activity_metrics'] = True
483483
check = integration_check(pg_instance)
484484

485485
check.run()
486486

487-
dd_agent_tags = _get_expected_tags(check, pg_instance, db=DB_NAME, app='datadog-agent', user='datadog')
488-
test_tags = _get_expected_tags(check, pg_instance, db=DB_NAME, app='test', user='datadog')
489-
# No transaction in progress, we have 0
490-
if float(POSTGRES_VERSION) >= 9.6:
491-
aggregator.assert_metric('postgresql.activity.backend_xmin_age', value=0, count=1, tags=dd_agent_tags)
492-
else:
493-
aggregator.assert_metric('postgresql.activity.backend_xmin_age', count=0, tags=dd_agent_tags)
494-
aggregator.assert_metric('postgresql.activity.xact_start_age', count=1, tags=dd_agent_tags)
495-
496-
conn1 = _get_conn(pg_instance)
487+
app = 'test_backend_transaction_age'
488+
conn1 = _get_conn(pg_instance, application_name=app)
497489
cur = conn1.cursor()
498490

491+
test_tags = _get_expected_tags(check, pg_instance, db=DB_NAME, app=app, user='datadog')
492+
# No transaction in progress, nothing should be reported for test app
493+
aggregator.assert_metric('postgresql.activity.backend_xmin_age', count=0, tags=test_tags)
494+
aggregator.assert_metric('postgresql.activity.xact_start_age', count=0, tags=test_tags)
495+
499496
# Start a transaction in repeatable read to force pinning of backend_xmin
500497
cur.execute('BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;')
501498
# Force assignement of a txid and keep the transaction opened
@@ -510,16 +507,10 @@ def test_backend_transaction_age(aggregator, integration_check, pg_instance):
510507
if float(POSTGRES_VERSION) >= 9.6:
511508
aggregator.assert_metric('postgresql.activity.backend_xid_age', value=1, count=1, tags=test_tags)
512509
aggregator.assert_metric('postgresql.activity.backend_xmin_age', value=1, count=1, tags=test_tags)
513-
514-
aggregator.assert_metric('postgresql.activity.backend_xid_age', count=0, tags=dd_agent_tags)
515-
aggregator.assert_metric('postgresql.activity.backend_xmin_age', value=1, count=1, tags=dd_agent_tags)
516510
else:
517511
aggregator.assert_metric('postgresql.activity.backend_xid_age', count=0, tags=test_tags)
518512
aggregator.assert_metric('postgresql.activity.backend_xmin_age', count=0, tags=test_tags)
519513

520-
aggregator.assert_metric('postgresql.activity.backend_xid_age', count=0, tags=dd_agent_tags)
521-
aggregator.assert_metric('postgresql.activity.backend_xmin_age', count=0, tags=dd_agent_tags)
522-
523514
aggregator.assert_metric('postgresql.activity.xact_start_age', count=1, tags=test_tags)
524515

525516
with _get_conn(pg_instance) as conn2:
@@ -536,9 +527,6 @@ def test_backend_transaction_age(aggregator, integration_check, pg_instance):
536527
aggregator.assert_metric('postgresql.activity.backend_xid_age', value=2, count=1, tags=test_tags)
537528
aggregator.assert_metric('postgresql.activity.backend_xmin_age', value=2, count=1, tags=test_tags)
538529

539-
aggregator.assert_metric('postgresql.activity.backend_xid_age', count=0, tags=dd_agent_tags)
540-
aggregator.assert_metric('postgresql.activity.backend_xmin_age', value=2, count=1, tags=dd_agent_tags)
541-
542530
# Check that xact_start_age has a value greater than the trasaction_age lower bound
543531
aggregator.assert_metric('postgresql.activity.xact_start_age', count=1, tags=test_tags)
544532
assert_metric_at_least(

postgres/tests/test_progress_stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
def _check_analyze_progress(check, pg_instance, table):
34-
thread = run_vacuum_thread(pg_instance, f'VACUUM ANALYZE {table}')
34+
thread = run_vacuum_thread(pg_instance, f'ANALYZE {table}')
3535

3636
# Wait for vacuum to be reported
3737
_wait_for_value(

0 commit comments

Comments
 (0)