postgresql_extensible feature request: Allow for using variables in queries
Feature Request
Hi all. I started porting my custom monitoring queries for PostgreSQL from collectd to telegraf. Currently, it doesn't appear that there is an option to accomplish this with the current postgresql_extensible plugin in Telegraf.
The collectd postgresql plugin allows for using variable names in queries to represent items such as database name, schema name, table name, etc. This would make the postgresql_extensible feature much more valuable and flexible! While this plugin does allow for using whatever monitoring query that a user desires, it's only really useful for getting global DB statistics, and not for parsing out more specific information such as DB size, table/index size, performance stats on individual tables/indexes, etc.
I'm willing to help with testing this functionality in any way I can!
Perhaps we could add a subtable with query parameters, is this the sort of thing you are thinking about?
[[inputs.postgresql_extensible]]
[[inputs.postgresql_extensible.query]]
sqlquery="select foo from @dbname"
[[inputs.postgresql_extensible.query.params]]
dbname = "bar"
What I think would make more sense is this:
[[inputs.postgresql_extensible]]
[[inputs.postgresql_extensible.database]]
database="postgres"
password="password"
port="5432"
query="pg_db_size"
query="pg_stat_activity"
[[inputs.postgresql_extensible.query]]
name="pg_db_size"
query="SELECT pg_database_size(@dbname) AS size;"
[[inputs.postgresql_extensible.query]]
name="pg_stat_activity"
query="SELECT count(*) from pg_stat_activity where datname=@dbname;"
Having the flexability to monitor many databases with this plugin without needing to copy and paste each monitoring query multiple times can be seen in this example. Lets look at the output of this query:
SELECT * from pg_catalog.pg_tables
WHERE schemaname NOT ILIKE '%pg_catalog%'
AND schemaname <> 'information_schema';
The purpose of the above query is to list all tables in pg_catalog. Depending on the database we are currently connected to, the output will be completely different. Assuming this list of databases:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------+----------+----------+-------------+-------------+-----------------------
application_internal | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
web_prod | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
output for DB postgres:
postgres=# select * from pg_catalog.pg_tables where schemaname not ilike '%pg_catalog%' and schemaname <> 'information_schema';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+------------------+------------+------------+------------+----------+-------------+-------------
public | audit_internal | postgres | | f | f | f | f
public | monitoring_users | postgres | | f | f | f | f
(2 rows)
for application_internal:
application_internal=# select * from pg_catalog.pg_tables where schemaname not ilike '%pg_catalog%' and schemaname <> 'information_schema';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+-----------------------+------------+------------+------------+----------+-------------+-------------
public | current_transactions | postgres | | f | f | f | f
public | archived_transactions | postgres | | f | f | f | f
public | processing_log | postgres | | f | f | f | f
(3 rows)
for web_prod:
web_prod=# select * from pg_catalog.pg_tables where schemaname not ilike '%pg_catalog%' and schemaname <> 'information_schema';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+-------------+------------+------------+------------+----------+-------------+-------------
public | web_users | postgres | | f | f | f | f
public | web_history | postgres | | f | f | f | f
public | last_login | postgres | | f | f | f | f
(3 rows)
This is only one example demonstrating why items like dbname, schemaname, tablename need to be handled more flexibly by the plugin. If you need help testing any development versions of the plugin, I will absolutely help and provide feedback.
To me this feels like 2 separate goals. In the first you're wanting some variable thing, and in the second you want to run the same query against multiple databases.
In the variable thing, in your provided example, where does @dbname come from?
Note that you can already accomplish this:
# select datname,pg_database_size(datname) from pg_database where datistemplate=false;
datname | pg_database_size
----------+------------------
denver | 829494532632
postgres | 134461976
sessions | 655030808
# select datname,count(*) from pg_stat_activity group by datname;
datname | count
----------+-------
denver | 60
postgres | 1
sessions | 12
As for your second request, of running the same query against multiple databases, see #3219
@phemmer See above in @danielnelson's post with the reference to it. Those queries are simple examples I was using for a sample config file mockup of what a solution could look like. There are many more complicated queries where you absolutely need to specify a database name in order to query for specific data.
As I said, I am attempting to move to telegraf from collectd (which does support this kind of variable use in queries). Is this an unreasonable request?
In that case (where @dbname comes from), I personally don't see the benefit over the example queries I provided.
I'm just saying that aside from needing to connect to the different databases, I don't see a need for telegraf to support the variables. You can do things like joins, group bys, subqueries, etc to satisfy all the use cases I can think of. If you have a use case that can't be satisfied by such, can you provide it? I think it'll make it easier to determine the need for the request.
While other techniques can be used to parse the data out, it can substantially increase the overhead of the monitoring query.
In practice, a monitoring query needs to be as lightweight as possible (since it is being run at regular intervals), and must have minimal impact on overall database performance. Also, do you know of a pure sql solution for querying for table information concerning database1 while connected to database2? If so I'd love to know.
I'm a DBA and would love to be able to use telegraf for putting all of my metrics data into influx. If we can't specify database names without hardcoding them in each query and copy/pasting those queries for each DB to monitor, then I'm not sure there is a solution using telegraf for metrics collection that is maintainable (from a config file standpoint).
Actually doing all the operations in a single query is generally a lot better for the server. There are a lot of views (commonly provided by extensions) that are resource intensive. Gathering all the data in a single query is a lot better so that you're not hitting the view multiple times. Same goes for queries against a table, it's more efficient for the DB to only scan an index/table once.
Also, do you know of a pure sql solution for querying for table information concerning database1 while connected to database2? If so I'd love to know.
That's what #3219 is about. This is a separate issue.
@petercelentano Are there other variables you would need besides database name?
schema would be nice to have :)
Adding variables that are tied to a connection string (currently address) that are used by queries is not hard, but it seems to be of limited use unless we can also have multiple connection strings?
Not necessarily. My plan was to place my postgresql queries into one telegraf config file, and define each DB and the queries that need to be run against it in their own configs (one config file per DB). I assume having multiple connection strings would be necessary, since the way I intend to use the plugin, I will place each DB config in a separate config file.
You won't be able to split the config files, since a plugin only has access to the config it is defined in.
What do you think about this:
[[inputs.postgresql_extensible]]
# We could make all key/value pairs available in the query as @host @dbname
address = "host=localhost user=postgres sslmode=disable dbname=howdy"
# Extra params that will be expanded in the queries
[inputs.postgresql_extensible.params]
schema = "xyzzy"
# Just a normal old query with param binding
[[inputs.postgresql_extensible.query]]
sqlquery="select foo from @dbname"
# This query is loaded from file once on startup, and reloaded on SIGHUP. You can also use @params here
[[inputs.postgresql_extensible.query]]
script = "some/file.sql"
Hi,
Going through old bugs and checking status.
I think the previous comments on this issue have resolved some of the original concerns, is there anything still outstanding required here?
Thanks!
Hello! I am closing this issue due to inactivity. I hope you were able to resolve your problem, if not please try posting this question in our Community Slack or Community Page. Thank you!