counting tables query
The following query, while very fast, appears to be getting run hundreds of times per second on db1:
SELECT COUNT(*)
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind in (?,?,?)
AND c.relname = ? AND n.nspname = ANY (current_schema(?));
Is this really necessary? I had initially thought this was a monitoring query, but I can't imagine why even the monitoring system would need to count the number of tables over and over again every second. I'm wondering if perhaps this is some kind of automated check in the application which is looking to see if the number of tables it expects is different from the number of tables which are there, but surely that doesn't actually need to be run prior to each and every query sent to the database (if that's, indeed, what's happening).
Another query which is run extremely often is:
SELECT
a.attname,
format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid),
a.attnotnull,
a.atttypid,
a.atttypmod
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = ?::regclass
AND a.attnum > ?
AND NOT a.attisdropped ORDER BY a.attnum;
This is also run hundreds of times a second.
Edited by Stephen Frost