Skip to content

fix(inputs.postgresql_extensible): Retry database version detection before failing - #19367

Closed
DoTuanAnh2k1 wants to merge 1 commit into
influxdata:masterfrom
DoTuanAnh2k1:fix/pg-extensible-version-detection-error
Closed

fix(inputs.postgresql_extensible): Retry database version detection before failing#19367
DoTuanAnh2k1 wants to merge 1 commit into
influxdata:masterfrom
DoTuanAnh2k1:fix/pg-extensible-version-detection-error

Conversation

@DoTuanAnh2k1

@DoTuanAnh2k1 DoTuanAnh2k1 commented Aug 2, 2026

Copy link
Copy Markdown

Summary

postgresql_extensible runs a server-version query at the start of every Gather to decide which version-gated queries to run. The error from that query was silently swallowed, defaulting the version to 0:

if err := p.service.DB.QueryRow(query).Scan(&dbVersion); err != nil {
    dbVersion = 0
}

With version 0 the min_version / max_version gating selects the wrong queries, so a query intended for another version gets executed and fails with a confusing error such as column p.num_dead_tuples does not exist (#18409). This happens when a pooled connection is closed without database/sql noticing.

The version query is now retried once on failure — dropping the stale connection so a fresh one is used (per @redbaron's review) — and the error is only returned if the retry also fails, instead of silently running version-mismatched queries. Two integration tests that connected to a bare localhost database (and only passed because the error was swallowed) are converted to the existing testcontainer helper so they run against a real database.

Checklist

Related issues

resolves #18409

@telegraf-tiger

telegraf-tiger Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Thanks so much for the pull request!
🤝 ✒️ Just a reminder that the CLA has not yet been signed, and we'll need it before merging. Please sign the CLA when you get a chance, then post a comment here saying !signed-cla

@telegraf-tiger telegraf-tiger Bot added fix pr to fix corresponding bug plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins labels Aug 2, 2026
@DoTuanAnh2k1

Copy link
Copy Markdown
Author

!signed-cla

@DoTuanAnh2k1
DoTuanAnh2k1 force-pushed the fix/pg-extensible-version-detection-error branch from 3e9b1d6 to e13ce2b Compare August 2, 2026 15:57
var dbVersion int
if err := p.service.DB.QueryRow(query).Scan(&dbVersion); err != nil {
dbVersion = 0
// Surface the error instead of proceeding with an unknown version (0),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should try again here. we see that version error query happens only when underneath connection was closed without database/sql noticing it. Error here will force connection to close, so new connection will be established on retry.

Alternative proposed in the ticket is to switch pool from database/sql to pgx pool: latter does internal connection healtcheck before running queries

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, thanks. Switched to retrying the version query once on failure so a stale pooled connection gets dropped and a fresh one is used, and only error out if the retry also fails. Left the pgx-pool switch out of scope for this fix.

…efore failing

The per-Gather server-version lookup swallowed any error and fell back to
version 0, which then selected the wrong set of version-gated queries and
produced confusing "column ... does not exist" failures (e.g. when a pooled
connection was closed without database/sql noticing).

Retry the version query once on failure so the stale connection is dropped and
a fresh one is used, and only return the error if the retry also fails, instead
of silently running version-mismatched queries.

Add regression tests for both paths. This also surfaced two integration tests
that connected to a bare localhost database and only passed because the error
was swallowed; convert them to the existing testcontainer helper so they run
against a real database.

Fixes influxdata#18409.
@DoTuanAnh2k1
DoTuanAnh2k1 force-pushed the fix/pg-extensible-version-detection-error branch from e13ce2b to f48fabb Compare August 3, 2026 14:46
@DoTuanAnh2k1 DoTuanAnh2k1 changed the title fix(inputs.postgresql_extensible): return error when database version detection fails fix(inputs.postgresql_extensible): retry database version detection before failing Aug 3, 2026
@telegraf-tiger

telegraf-tiger Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip.
Downloads for additional architectures and packages are available below.

⚠️ This pull request increases the Telegraf binary size by 5.03 % for linux amd64 (new size: 323.8 MB, nightly size 308.2 MB)

📦 Click here to get additional PR build artifacts

Artifact URLs

. DEB . RPM . TAR . GZ . ZIP
amd64.deb aarch64.rpm darwin_amd64.tar.gz windows_amd64.zip
arm64.deb armel.rpm darwin_arm64.tar.gz windows_arm64.zip
armel.deb armv6hl.rpm freebsd_amd64.tar.gz windows_i386.zip
armhf.deb i386.rpm freebsd_armv7.tar.gz
i386.deb ppc64le.rpm freebsd_i386.tar.gz
mips.deb riscv64.rpm linux_amd64.tar.gz
mipsel.deb s390x.rpm linux_arm64.tar.gz
ppc64el.deb x86_64.rpm linux_armel.tar.gz
riscv64.deb linux_armhf.tar.gz
s390x.deb linux_i386.tar.gz
linux_mips.tar.gz
linux_mipsel.tar.gz
linux_ppc64le.tar.gz
linux_riscv64.tar.gz
linux_s390x.tar.gz

@srebhan

srebhan commented Aug 4, 2026

Copy link
Copy Markdown
Member

@DoTuanAnh2k1 please restore the PR description template as we cannot review your contribution otherwise!

@srebhan srebhan self-assigned this Aug 4, 2026
@DoTuanAnh2k1

Copy link
Copy Markdown
Author

@srebhan done — restored the PR description template. Thanks!

@srebhan srebhan changed the title fix(inputs.postgresql_extensible): retry database version detection before failing fix(inputs.postgresql_extensible): Retry database version detection before failing Aug 13, 2026

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @DoTuanAnh2k1 for your contribution! Some minor comments from my side below...

Comment on lines -225 to 279
func TestPostgresqlSqlScript(t *testing.T) {
q := []query{{
Script: "testdata/test.sql",
MinVersion: 901,
Withdbname: false,
Tagvalue: "",
}}

addr := fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
)

p := &Postgresql{
Log: testutil.Logger{},
Config: postgresql.Config{
Address: config.NewSecret([]byte(addr)),
IsPgBouncer: false,
},
Databases: []string{"postgres"},
Query: q,
func TestPostgresqlSqlScriptIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
require.NoError(t, p.Init())

var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
defer p.Stop()
require.NoError(t, acc.GatherError(p.Gather))
queryRunner(t, []query{{
Script: "testdata/test.sql",
MinVersion: 901,
}})
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this change! This has nothing to do with what the PR title says. If you want to fix tests, please do so in a separate PR!

Comment on lines -260 to +290
addr := fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
)

p := &Postgresql{
Log: testutil.Logger{},
Config: postgresql.Config{
Address: config.NewSecret([]byte(addr)),
},
}
require.NoError(t, p.Init())

var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
defer p.Stop()
require.NoError(t, acc.GatherError(p.Gather))
// `pg_stat_database` includes `stats_reset`, one of the ignored columns.
acc := queryRunner(t, []query{{
Sqlquery: "select * from pg_stat_database",
MinVersion: 901,
}})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this change! This has nothing to do with what the PR title says. If you want to fix tests, please do so in a separate PR!

Comment on lines +26 to +27
mock.ExpectQuery("server_version_num").
WillReturnRows(sqlmock.NewRows([]string{"version"}).AddRow(1400))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mock.ExpectQuery("server_version_num").
WillReturnRows(sqlmock.NewRows([]string{"version"}).AddRow(1400))
mock.ExpectQuery("server_version_num").WillReturnRows(sqlmock.NewRows([]string{"version"}).AddRow(1400))

@srebhan srebhan added the waiting for response waiting for response from contributor label Aug 18, 2026
@telegraf-tiger

telegraf-tiger Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Hello! I am closing this issue due to inactivity. I hope you were able to resolve your problem, if not please try posting this question in our Community Slack or Community Forums or provide additional details in this issue and reqeust that it be re-opened. Thank you!

@telegraf-tiger telegraf-tiger Bot closed this Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix pr to fix corresponding bug plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins waiting for response waiting for response from contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[inputs.postgresql_extensible] incorrect version detection

3 participants