Repository navigation
feat(outputs.postgresql): Allow setting schema from a tag - #20086
Conversation
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
| // 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) | ||
| } |
There was a problem hiding this comment.
Wouldn't it be better to rather use a unique name for the temporary table including a schema identifier?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
|
Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip. 📦 Click here to get additional PR build artifactsArtifact URLs |
|
The |
srebhan
left a comment
There was a problem hiding this comment.
Thanks a lot @hsdfat!
@skartikey please check again as the code changed significantly and merge if you are okay with the new code!
|
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! |
|
The |
Summary
Adds
schema_tagandexclude_schema_tagtooutputs.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
schemais not changed. Instead, each batch is grouped by (schema, table), similar todatabase_tagin the influxdb output. Metrics without the tag, or with an empty value, go to the configuredschema. 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 DROPonly 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