I
I
Ivan2020-08-24 14:32:27
PostgreSQL
Ivan, 2020-08-24 14:32:27

Error in Postgres query?

Some problems with Query time

bash-4.2$ psql -qtAX -h "$1" -p "$2" -U "$3" -d "$4" -v tmax=$5 -f "/var/lib/zabbix/postgresql/pgsql.query.time.sql"
psql:/var/lib/zabbix/postgresql/pgsql.query.time.sql:31: ERROR:  syntax error at or near ")"
СТРОКА 22: ...'epoch' FROM (clock_timestamp() - query_start)) > )::integer...


I don't understand the error in the request itself, tell me what the problem is please

WITH T AS
        (SELECT db.datname,
                        coalesce(T.query_time_max, 0) query_time_max,
                        coalesce(T.tx_time_max, 0) tx_time_max,
                        coalesce(T.mro_time_max, 0) mro_time_max,
                        coalesce(T.query_time_sum, 0) query_time_sum,
                        coalesce(T.tx_time_sum, 0) tx_time_sum,
                        coalesce(T.mro_time_sum, 0) mro_time_sum,
                        coalesce(T.query_slow_count, 0) query_slow_count,
                        coalesce(T.tx_slow_count, 0) tx_slow_count,
                        coalesce(T.mro_slow_count, 0) mro_slow_count
        FROM pg_database db NATURAL
        LEFT JOIN (
                SELECT datname,
                        extract(epoch FROM now())::integer ts,
                        coalesce(max(extract('epoch' FROM (clock_timestamp() - query_start))::integer * (state NOT IN ('idle', 'idle in transaction', 'idle in transaction (aborted)') AND query !~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) query_time_max,
                        coalesce(max(extract('epoch' FROM (clock_timestamp() - query_start))::integer * (state NOT IN ('idle') AND query !~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) tx_time_max,
                        coalesce(max(extract('epoch' FROM (clock_timestamp() - query_start))::integer * (state NOT IN ('idle') AND query ~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) mro_time_max,
                        coalesce(sum(extract('epoch' FROM (clock_timestamp() - query_start))::integer * (state NOT IN ('idle', 'idle in transaction', 'idle in transaction (aborted)') AND query !~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) query_time_sum,
                        coalesce(sum(extract('epoch' FROM (clock_timestamp() - query_start))::integer * (state NOT IN ('idle') AND query !~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) tx_time_sum,
                        coalesce(sum(extract('epoch' FROM (clock_timestamp() - query_start))::integer * (state NOT IN ('idle') AND query ~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) mro_time_sum,

                        coalesce(sum((extract('epoch' FROM (clock_timestamp() - query_start)) > :tmax)::integer * (state NOT IN ('idle', 'idle in transaction', 'idle in transaction (aborted)') AND query !~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) query_slow_count,
                        coalesce(sum((extract('epoch' FROM (clock_timestamp() - query_start)) > :tmax)::integer * (state NOT IN ('idle') AND query !~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) tx_slow_count,                                                                                                   
                        coalesce(sum((extract('epoch' FROM (clock_timestamp() - query_start)) > :tmax)::integer * (state NOT IN ('idle') AND query ~* E'^(\\s*(--[^\\n]*\\n|/\\*.*\\*/|\\n))*(autovacuum|VACUUM|ANALYZE|REINDEX|CLUSTER|CREATE|ALTER|TRUNCATE|DROP)')::integer), 0) mro_slow_count                                                                                                                        
                FROM pg_stat_activity                                                                                                                                         
                WHERE pid <> pg_backend_pid()                                                                                                                                 
                GROUP BY 1) T                                                                                                                                                                                                              
        WHERE NOT db.datistemplate )
SELECT json_object_agg(datname, row_to_json(T))
FROM T

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2020-08-24
@melkij

I don't understand where the error is in the request itself.

FROM (clock_timestamp() - query_start)) > )::integer

More than what?
:tmax replaced with an empty string, look for where you lost your $5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question