new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Grafana dashboards · all subjects

variables & templating

34 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Loki template variables available

Loki data source supports template variables for creating dynamic dashboards.

MSSQL multi-value variables double-quoting since v11.3

Since Grafana v11.3, multi-value variables used with IN are automatically quoted. If you manually wrapped the variable in quotes (for example, WHERE col IN ('${var}')), values are now double-quoted (for example, ''value''), causing query failures. Remove manual quotes around the variable: use WHERE col IN ($var) instead of WHERE col IN ('${var}'). For single-value comparisons, use the sqlstring format: WHERE col = ${var:sqlstring}.

MSSQL template variable query requirements

Variable query syntax must be valid SQL that returns a single column. Verify the data source connection is working. Ensure the user has permission to access the tables referenced in the variable query. Test the query in the query editor before using it as a variable query.

Template variable time filter for InfluxDB

To scope template variables to the dashboard time range and prevent stale values, add a time condition to the variable query such as WHERE $timeFilter for InfluxQL or WHERE $__timeFilter(time) for SQL. Set the variable's Refresh option to On time range change.

Template variable escaping with raw format

Grafana escapes special characters in variable values when the variable is multi-value or used inside a regular expression. Use the raw format option such as ${path:raw} to interpolate the literal value without escaping.

Multi-select variable query handling

When a template variable has the Multi-value or Include all value option enabled, Grafana interpolates the selected values as a regular expression group, such as (server1|server2). For InfluxQL, use the =~ operator and wrap the variable in a regular expression: "hostname" =~ /^$host$/. For SQL, use the IN operator: host IN ($host).

MySQL template variables documentation link

For detailed information on using template variables with MySQL, refer to the MySQL template variables documentation.

PostgreSQL legacy key/value variable syntax

Alternatively to using Value field and Text field, you can use the legacy approach: return columns named __text and __value in your query. Example: SELECT hostname AS __text, id AS __value FROM host

Note: Duplicate values in PostgreSQL text column only match first entry

The values in the text column should be unique. If there are duplicates, Grafana uses only the first matching entry.

PostgreSQL nested variables with multi-value IN operator

You can create nested variables, where one variable depends on the value of another. For example, if you have a variable named region, you can configure a hosts variable to only show hosts from the selected region. If region is a multi-value variable, use the IN operator instead of = to match against multiple selected values. Example: SELECT hostname FROM host WHERE region IN($region)

Pitfall: Multi-value PostgreSQL variable with Include All Hidden defaults to first value

When using a multi-value PostgreSQL query variable with Include All option enabled and Display set to Hidden, the variable defaults to the first returned value, not All. This is because Grafana persists the active selection at dashboard save time, and a hidden variable has no UI for users to change it. To default a hidden variable to All, temporarily show the variable, select All in the drop-down, save the dashboard, then hide the variable and save again.

PostgreSQL __searchFilter macro for real-time search

Using __searchFilter in the query field filters the query results based on user input in the drop-down selection box. If nothing is entered, the default value for __searchFilter is %. You must enclose the __searchFilter expression in quotes because Grafana doesn't add them automatically. Example: SELECT hostname FROM host WHERE hostname LIKE '$__searchFilter'

PostgreSQL __searchFilter with LIMIT for large tables

For tables with a large number of rows, combine __searchFilter with LIMIT to keep the drop-down responsive. Example: SELECT DISTINCT hostname FROM host WHERE hostname LIKE '$__searchFilter' ORDER BY hostname LIMIT 100

Regex variable type filters or transforms values from another variable

You can use a Regex type variable to filter or transform values from another variable. For example, if you have a variable named server that returns values like web-prod-01, web-staging-01, and db-prod-01, you can create a regular expression variable to extract only the environment. Steps: Create a new variable with Type: Query. Set the query to return the raw values. In the Regex field, enter a pattern to extract or filter.

PostgreSQL regex variable extraction example

Example regex variable pattern /^web-(.+)-\d+$/ extracts prod and staging from web server names like web-prod-01 and web-staging-01.

PostgreSQL regex variable filtering example

You can use regular expression to filter a variable's options. For example, setting the Regex field to /prod/ on a variable that returns all server names limits the drop-down to only production servers.

PostgreSQL multi-property variables map different identifiers

PostgreSQL data source supports multi-property variables. Use them when the same logical concept has different identifiers in different contexts (for example, an environment called dev in one system and development in another). Instead of maintaining several variables in sync, you can map all of those values to one variable and reference the property you need in each panel or query.

PostgreSQL multi-property variable with Type: Custom

You can create a multi-property variable with Type: Custom. In Custom options > JSON, paste your own JSON array with the mapping. Each object in the array can have any number of properties; use text and value for the label and value shown in the drop-down, and add additional properties as needed.

PostgreSQL multi-property variable with Type: Query

You can create a multi-property variable with Type: Query. Write a SQL query that returns multiple columns. In the variable editor, set Value field and Text field to the columns that supply the value and the label for the drop-down. Add one column per property you want to reference; each column name becomes a property name. In panels and queries, reference a property with ${varName.columnName}.

PostgreSQL multi-property variable example

Example: A variable named env that lists environments with different identifiers per cloud. Query: SELECT name, id, aws_identifier AS env_aws, azure_identifier AS env_azure FROM environments. In the variable editor, set Text field to name and Value field to id. In a panel query you might use $env.env_aws for an AWS-related query and $env.env_azure for an Azure-related query.

Grafana automatically quotes multi-value template variables

Grafana automatically quotes template variable values only when the template variable is a multi-value variable. When using a multi-value variable, use the IN comparison operator instead of = to match against multiple values.

PostgreSQL variable syntax: [[varname]]

Grafana supports [[varname]] syntax for using variables in queries. Example with a template variable named hostname: SELECT atimestamp AS time, aint AS value FROM table WHERE $__timeFilter(atimestamp) AND hostname IN([[hostname]]) ORDER BY atimestamp ASC

PostgreSQL multi-value variable with IN clause adds quotes

When a variable has Multi-value enabled, Grafana automatically adds quotes around each selected value. For example, if the user selects web01 and web02, $hostname expands to 'web01','web02'. Use the IN operator to match. Example: SELECT $__timeGroupAlias("created_at", '5m'), count(*) AS requests FROM access_log WHERE $__timeFilter("created_at") AND hostname IN($hostname) GROUP BY time ORDER BY time

PostgreSQL multi-value variable IN clause works with single value

If only a single value is selected, the IN clause still works correctly—IN('web01') is equivalent to = 'web01'.

PostgreSQL numeric column variables use csv format to disable quoting

For numeric columns (such as IDs), disable quoting with the csv format option so values aren't wrapped in quotes. Example: SELECT name FROM host WHERE id IN(${host_id:csv}). If host_id has values 1, 2, and 3 selected, this expands to IN(1,2,3) instead of IN('1','2','3').

PostgreSQL csv format option disables quoting for multi-value variables

By default, Grafana formats multi-value variables as a quoted, comma-separated string. For example, if server01 and server02 are selected, the result is 'server01','server02'. To disable quoting, use the csv formatting option: ${servers:csv}. This outputs the values as an unquoted comma-separated list.

Query variable in PostgreSQL returns dynamic values

A query variable in PostgreSQL dynamically retrieves values from the data source using a SQL query. Query variables display results in a drop-down select box, allowing you to show measurement names, key names, or key values that users can select from.

PostgreSQL query variable editor features

The PostgreSQL data source includes a dedicated variable query editor with the full SQL editor supporting Code mode with auto-completion and syntax highlighting. The editor provides Value Field and Text Field drop-downs. After writing and running a query, the editor automatically detects returned columns and populates the field drop-downs to map which column provides the variable value and which provides the display label.

Steps to create a PostgreSQL query variable

To create a query variable: Navigate to the dashboard and click Edit. Click the Add new element icon (blue plus sign). Click Variable. Select Query as the variable type. Enter a Name for your variable (for example, host). Select an option in the Display drop-down list to control where on the dashboard the variable is displayed. Click Open variable editor to open the Query Variable dialog box. Select your PostgreSQL data source. Write a SQL query in the editor. Optionally set Value Field and Text Field to control which columns map to the variable value and display label.

PostgreSQL query variable example with single column

A simple query variable that returns all values from the hostname column: SELECT hostname FROM host

PostgreSQL query variable with multiple columns

A query variable can return multiple columns, and Grafana automatically generates a list using the values from those columns. Example: SELECT host.hostname, other_host.hostname2 FROM host JOIN other_host ON host.city = other_host.city

PostgreSQL time range dependent variable requires On Time Range Change refresh

To use time range dependent macros like $__timeFilter(column) in your query, you must set the template variable's refresh mode to On Time Range Change. Example: SELECT event_name FROM event_log WHERE $__timeFilter(time_column)

PostgreSQL key/value variables with Value and Text fields

You can create a key/value variable so the drop-down shows a user-friendly label (for example, hostname) while panel queries use a different value (for example, ID). Use the variable editor's Value field and Text field at the bottom of the query section to specify which query columns supply the value and the label. Your query can use any column names; you do not need __value or __text in the SQL. Example: SELECT hostname, id FROM host, then set Text field to hostname and Value field to id.

Filter and Group by feature renames Ad hoc filters

The Filter and Group by feature renames the Ad hoc filters variable and extends it by adding grouping for Prometheus and Loki data sources. In the dashboard schema, it is still referred to as "kind": "AdhocVariable" under the variables schema property.

Give your agent this brain