Skip to content

feat(outputs.postgresql): Allow setting schema from a tag - #20086

Merged
skartikey merged 2 commits into
influxdata:masterfrom
hsdfat:feat/pg-schema-tag-16747
Nov 16, 2026
Merged

skartikey merged 2 commits into
influxdata:masterfrom
hsdfat:feat/pg-schema-tag-16747

Conversation

@hsdfat

@hsdfat hsdfat commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds schema_tag and exclude_schema_tag to outputs.postgresql, so the schema can come from a tag, as asked in the issue.

Following the review of #17107, the schema is not taken from the first metric and the plugin's configured schema is not changed. Instead, each batch is grouped by (schema, table), similar to database_tag in the influxdb output. Metrics without the tag, or with an empty value, go to the configured schema. The table cache, column lookup, templates and tag cache are now keyed on schema and table, so two schemas with the same table name don't share state.

With tags_as_foreign_keys, the temp table used for tag-table writes is now dropped explicitly after use. Inside a savepoint, ON COMMIT DROP only fires when the outer transaction commits, so two schemas with the same table name in one batch would otherwise collide on <name>_temp.

The plugin does not create schemas. The README shows how to create them via the templates.

Unit tests cover the grouping, the fallback and the exclude option. New integration tests (TestWriteIntegration_schemaTag*, TestTableSourceIntegration_*) write to two schemas. I couldn't run the docker-based integration tests locally, so CI is their first run.

Checklist

Related issues

resolves #16747

Add the schema_tag and exclude_schema_tag options. Each batch is
grouped by (schema, table), and metrics without the tag fall back to
the configured schema. The table cache, the column lookup, the
templates and the tag cache are keyed on schema and table.

Resolves influxdata#16747
@telegraf-tiger telegraf-tiger Bot added area/postgresql feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins labels Nov 6, 2026

@skartikey skartikey left a comment

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.

@hsdfat Thanks for the contribution!

@skartikey skartikey added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Nov 12, 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 for your contribution @hsdfat! Just one question regarding the temporary tables from my side...

Comment on lines +509 to +513
// When called inside a savepoint, the temp table is only dropped when the outer transaction commits. Drop it
// explicitly, so that a tag table with the same name in another schema can be written in the same transaction.
if _, err := tx.Exec(ctx, "DROP TABLE "+identTemp.Sanitize()); err != nil {
return fmt.Errorf("dropping tags temp table: %w", err)
}

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.

Wouldn't it be better to rather use a unique name for the temporary table including a schema identifier?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I considered that and went with the explicit drop because of the identifier length limit. PostgreSQL truncates identifiers to 63 bytes, and a schema name and a table name can each be that long, so <schema>_<name>_temp can be cut. Two different schema and table pairs could then end up with the same temp table name again, and that would fail the same way as before. A unique name would need a hash or a counter to be safe.

The drop also removes the temp table as soon as the tags are written, so a batch that touches many schemas does not keep one temp table per schema until the outer transaction commits.

If you prefer the unique name anyway, I can switch to a short hash of schema and table in the name. Which one would you like?

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.

To be honest my fear is that dropping the table creates a "state". What if dropping the table fails for any reason? Furthermore, we are scratching table by creating/removing them in each transaction. I don't know PostgreSQL well enough to estimate the consequences of this but it seems odd.

So long story short: I would prefer using a hash or otherwise unique ID for the tables. Please include the _temp (or _tmp) suffix in the table name to make sure we don't collide with actual tables...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, changed in a24f8d7. The explicit DROP TABLE is gone and the temp table is now named telegraf_<hash>_temp, where the hash is FNV-64a over the schema and the tag table name. The name has a fixed length of 30 bytes, so it stays below the 63 byte identifier limit, and it keeps the _temp suffix. Cleanup is left to ON COMMIT DROP as before.

I added TestTagTempTableName as a unit test for the name. The integration tests need Docker, which I cannot run here, so I am relying on CI for TestWriteIntegration_schemaTag.

@telegraf-tiger

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 7.30 % for linux amd64 (new size: 330.7 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

@hsdfat

hsdfat commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

The test-integration failure on a24f8d7 is TestIntegration in plugins/inputs/mqtt_consumer ("Condition never satisfied"), which this PR does not touch. In the same job plugins/outputs/postgresql passed, including the schema tag tests.

@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 a lot @hsdfat!

@skartikey please check again as the code changed significantly and merge if you are okay with the new code!

@srebhan srebhan assigned skartikey and unassigned srebhan Nov 16, 2026
@skartikey

Copy link
Copy Markdown
Contributor

Rechecked, the hashed temp table name with the _temp suffix looks good to me and the schema tag tests pass in test-integration. The only red job is an unrelated mqtt_consumer integration flake. Thanks @hsdfat!

@skartikey
skartikey merged commit fba8e55 into influxdata:master Nov 16, 2026
26 of 27 checks passed
@github-actions github-actions Bot added this to the v1.41.0 milestone Nov 16, 2026
@hsdfat

hsdfat commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

The test-integration failure on the last run is TestIntegration in plugins/inputs/mqtt_consumer, which this change does not touch. The postgresql integration tests passed in the same job. Could someone re-run it?

@hsdfat
hsdfat deleted the feat/pg-schema-tag-16747 branch November 16, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/postgresql feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variable schema for postgressql output plugin

3 participants