

# Using the Amazon Redshift data source
<a name="Redshift-using-the-data-source"></a>

## IAM policies
<a name="Redshift-policies"></a>

 Grafana needs permissions granted using IAM to be able to read Redshift metrics. You can attach these permissions to IAM roles and utilize Grafana's built-in support for assuming roles. The built-in Amazon Grafana Redshift access policy is defined in the [AWS managed policy: AmazonGrafanaRedshiftAccess](security-iam-awsmanpol.md#security-iam-awsmanpol-AmazonGrafanaRedshiftAccess) section. 

## Query Amazon Redshift data
<a name="Redshift-query"></a>

 Amazon Redshift data source provides a standard SQL query editor. Amazon Managed Grafana includes some macros to help with writing more complex timeseries queries. 

Macros


|  Macro  |  Description  |  Output example  | 
| --- | --- | --- | 
|  \$1\$1\$1timeEpoch(column)  |  \$1\$1\$1timeEpoch will be replaced by an expression to convert to a UNIX timestamp and rename the column to time  |  UNIX\$1TIMESTAMP(dateColumn) as "time"  | 
|  \$1\$1\$1timeFilter(column)  |  \$1\$1\$1timeFiltercreates a conditional that filters the data (using column) based on the time range of the panel  |  time BETWEEN '2017-07-18T11:15:52Z' AND '2017-07-18T11:15:52Z'  | 
|  \$1\$1\$1timeFrom()  |  \$1\$1\$1timeFrom outputs the current starting time of the range of the panel with quotes  | '2017-07-18T11:15:52Z' | 
|  \$1\$1\$1timeTo()  |  \$1\$1\$1timeTo outputs the current ending time of the range of the panel with quotes  | '2017-07-18T11:15:52Z' | 
|  \$1\$1\$1timeGroup(column, '1m')  |  \$1\$1\$1timeGroup groups timestamps so that there is only 1 point for every period on the graph  | floor(extract(epoch from time)/60)\$160 AS "time" | 
|  \$1\$1\$1schema  |  \$1\$1\$1schema  uses the selected schema  | public | 
|  \$1\$1\$1table  |   \$1\$1\$1table outputs a table from the given \$1\$1\$1schema (it uses the public schema by default)  | sales | 
|  \$1\$1\$1column  |  \$1\$1\$1column outputs a column from the current \$1\$1\$1table  | date  | 
|  \$1\$1\$1unixEpochFilter(column)  |  \$1\$1\$1unixEpochFilter be replaced by a time range filter using the specified column name with times represented as Unix timestamp  |   column >= 1624406400 AND column <= 1624410000  | 
|  \$1\$1\$1unixEpochGroup(column)  |  \$1\$1\$1unixEpochGroup is the same as \$1\$1\$1timeGroup but for times stored as Unix timestamp  | floor(time/60)\$160 AS "time" | 

**Visualization**

Most queries in Redshift are best represented by a table visualization. Any query will display data in a table. If it can be queried, then it can be put in a table. 

This example returns results for a table visualization: 

```
SELECT {column_1}, {column_2} FROM {table};
```

**Time series and graph visualizations **

For time series and graph visualizations, there are a few requirements: 
+ A column with a `date` or a `datetime` type must be selected.
+ The `date` column must be in ascending order (using `ORDER BY column ASC`).
+ You must select a numeric column.

To make a more reasonable graph, be sure to use the `$__timeFilter` and `$__timeGroup` macros.

**Example timeseries query:**

```
SELECT
  avg(execution_time) AS average_execution_time,
  $__timeGroup(start_time, 'hour'),
  query_type
FROM
  account_usage.query_history
WHERE
  $__timeFilter(start_time)
group by
  query_type,start_time
order by
  start_time,query_type ASC;
```

**Fill mode**

Grafana also autocompletes frames without a value with some default. To configure this value, change the **Fill Value** in the query editor. 

**Inspecting the query**

Because Grafana supports macros that Redshift does not, the fully rendered query, which can be copied and pasted directly into Redshift, is visible in the Query Inspector. To view the full interpolated query, choose the **Query Inspector** menu, and the full query is visible on the **Query** tab.

## Templates and variables
<a name="using-redshift-templates-variables"></a>

For more information about how to add a new Redshift query varialble, see [Adding a query variable](variables-types.md#add-a-query-variable). Use your Redshift data source as your data source for the available queries.

Any value queried from a Amazon Redshift table can be used as a variable. Be sure to avoid selecting too many values, as this can cause performance issues. 

After creating a variable, you can use it in your Redshift queries by using [Variable syntax](templates-and-variables.md#variable-syntax). For more information about variables, see [Templates and variables](templates-and-variables.md).

## Annotations
<a name="using-redshift-annotations"></a>

[Annotations](dashboard-annotations.md) allows you to overlay rich event information on top of graphs. You can add annotations by selecting the panel or by adding annotation queries using the **Annotations** view, opened from the **Dashboard** menu. 

Example query to automatically add annotations:

```
SELECT
  time as time,
  environment as tags,
  humidity as text
FROM
  $__table
WHERE
  $__timeFilter(time) and humidity > 95
```

The following table represents the values of the columns taken into account to render annotations:


|  Name  |  Description  | 
| --- | --- | 
|  Time  |  The name of the date or time field. Could be a column with a native SQL date or time data type or epoch value.  | 
|  Timeend  |  Optional name of the end date or time field. Could be a column with a native SQL dateor time data type or epoch value.  | 
|  Text  |  Event description field.  | 
|  Tags  |  Optional field name to use for event tags as a comma separated string.  | 