# PostgreSQL Variable Chain

**URL:** <https://community.grafana.com/t/postgresql-variable-chain/116935>\
**Category:** PostgreSQL\
**Tags:** dashboard-variables\
**Created:** [March 13, 2024, 9:20pm UTC](https://community.grafana.com/t/postgresql-variable-chain/116935 "2024-03-13T21:20:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpfeltham](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/lpfeltham/32/43316_2.png) [@lpfeltham](https://community.grafana.com/u/lpfeltham)\
**Post date:** [March 13, 2024, 9:20pm UTC](https://community.grafana.com/t/postgresql-variable-chain/116935/1 "2024-03-13T21:20:53Z")

</div>

Grafana Version:  
grafana v10.3.1 on docker container using grafana/grafana base image

I’m trying to filter a chained variable. The filter works and the chaining works as expected, but the psql query can’t handle the ‘All’ option and the UI leaves me no ability to correct this.

Trying to make this clear:  
Var1=container\_type  
var\_type=Custom  
values: type\_1, type\_2, type\_3

Var2=container\_id  
var\_type=PSQL\_Query  
Query:  
`SELECT grafana_label AS " __text", container_id AS "__ value" FROM containers WHERE container_type = ($container_type)`

Possible values (these DO appear and this WORKS):  
container\_type=type\_1  
Result: id-446, id-578, etc (id’s that match on container type)

When container\_type=All  
I get nothing for id’s…

DB structure:  
I have a table of containers with all the information I need on them to display specific metrics. We run metrics and I get those easily off of Prometheus, but the PSQL tables are mostly for historical references and my display.

NDA’s prevent me from taking photos, but hopefully I’m clear.  
No errors showed up. I can set the All parameter to a default custom value… but I can’t seem to get it to work with the query?

Suggestion: Allow the custom All section to be a separate query to get exactly what I want with ease. For now, I need a workaround.

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [March 13, 2024, 9:53pm UTC](https://community.grafana.com/t/postgresql-variable-chain/116935/2 "2024-03-13T21:53:00Z")

</div>

That’s not bug. You just don’t see how you can use all options, which Grafana has.

Var1=container\_type  
BUT: Custom all value: %

Var2=container\_id  
Query:  
SELECT grafana\_label AS “\_\_text”, container\_id AS “\_\_value” FROM containers WHERE container\_type LIKE ‘%${container\_type:raw}%’

So what kind of SQL will be generated when Var1=All?  
SELECT grafana\_label AS “\_\_text”, container\_id AS “\_\_value” FROM containers WHERE container\_type LIKE ‘%%%’

So what kind of SQL will be generated when Var1=type\_1?  
SELECT grafana\_label AS “\_\_text”, container\_id AS “\_\_value” FROM containers WHERE container\_type LIKE ‘%type\_1%’

Both are valid SQLs. Of course there can be many other variations how to achieve desired result and this is just one example.

---

<div class="post-metadata">

**Author:** ![lpfeltham](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/lpfeltham/32/43316_2.png) [@lpfeltham](https://community.grafana.com/u/lpfeltham)\
**Post date:** [March 13, 2024, 9:57pm UTC](https://community.grafana.com/t/postgresql-variable-chain/116935/3 "2024-03-13T21:57:30Z")

</div>

Correct, the query is still valid. However, that isn’t what I want. I apologize because I didn’t clarify that.  
I would like to run this when container\_type=‘All’  
`SELECT grafana_label AS " __text", container_id AS "__ value" FROM containers`

This would return all the ids in my db. I did try setting my default value for container\_type=%  
That didn’t seem to work.  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/3/e366801ac21f36c595b53e8bec24eb8c4a204053.png)

---

<div class="post-metadata">

**Author:** ![lpfeltham](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/lpfeltham/32/43316_2.png) [@lpfeltham](https://community.grafana.com/u/lpfeltham)\
**Post date:** [March 13, 2024, 10:12pm UTC](https://community.grafana.com/t/postgresql-variable-chain/116935/4 "2024-03-13T22:12:59Z")

</div>

Correction: it seems to work in the actual psql CLI. I’m going to try recreating my dashboard container.

IT WORKS! Thank you!  
Here’s my query:

```auto
SELECT grafana_label AS " __text", container_id AS "__ value" FROM containers WHERE container_type LIKE '${container_type_l:raw}'

```

Here’s the default val for container type:  
%  
(see image above)
