 WHERE
   d.oid = b.reldatabase
 GROUP BY
-  1;
+  datname;
 $sql$
 );
 

'buffercache_by_db',
9.2,
$sql$
select
  (extract(epoch from now()) * 1e9)::int8 as epoch_ns,
  SELECT datname,
  count(*) * 8192
FROM
  pg_buffercache AS b,

'buffercache_by_type',
9.2,
$sql$
SELECT
  (extract(epoch from now()) * 1e9)::int8 as epoch_ns,
  CASE
    WHEN relkind = 'r' THEN 'Table'   -- TODO all relkinds covered?
    WHEN relkind = 'i' THEN 'Index'
    WHEN relkind = 't' THEN 'Toast'
    WHEN relkind = 'm' THEN 'Materialized view'
    ELSE 'Other'
  END,
  count(*) * 8192
FROM
  pg_buffercache AS b,
  pg_class AS d
WHERE
  d.oid = b.relfilenode
GROUP BY 
  relkind;
$sql$
);

