Skip to content

SQL output plugin attempts to add existing columns when table_update_template is set #18192

@ishaankalra

Description

@ishaankalra

Relevant telegraf.conf

[[outputs.sql]]
  driver = "clickhouse"
  data_source_name = "clickhouse://..."
  table_update_template = "ALTER TABLE {TABLE} ADD COLUMN {COLUMN}"
  
  [outputs.sql.convert]
    conversion_style = "literal"
    integer = "Int64"
    text = "String"
    timestamp = "DateTime"

System info

Telegraf version: master branch
Database: ClickHouse (also affects other databases)

Steps to reproduce

  1. Configure SQL output plugin with table_update_template set
  2. Ensure tables already exist in the database with columns
  3. Start Telegraf and write metrics to existing tables
  4. Observe error: "column already exists" when plugin tries to add existing columns

Expected behavior

Plugin should detect existing columns and only add new columns that don't exist.

Actual behavior

Plugin attempts to add ALL columns (including existing ones) to the table, causing errors like:

creating column failed: code: 15, message: Cannot add column \`timestamp\`: column with this name already exists

Additional info

The issue occurs because when `tableExists()` discovers an existing table, it creates an empty column cache but never populates it with actual columns from the database. This causes `createColumn()` to think no columns exist and attempts to add them all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions