Re: pg_relation_size performance issue

From: Hans Guijt <hg(at)terma(dot)com>
To: Melvin Davidson <melvin6925(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: pg_relation_size performance issue
Date: 2015-06-04 14:24:35
Message-ID: 0C5DB93EFF1ECE43B9C09E2E9FE4A127735F11B2@Exch04.terma.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thanks, this is most helpful. I originally found that query somewhere on the internet and used it as-is.

If I drop the restriction on table name I get a list that also includes indexes, constraints, etc. Is there a way to restrict the returned set to tables only?

Hans Guijt

From: Melvin Davidson [mailto:melvin6925(at)gmail(dot)com]
Sent: 04 June 2015 16:08
To: Tom Lane
Cc: Hans Guijt; pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] pg_relation_size performance issue

Dammit pg_total_relation_size includes toast data. Thumb problems and to quick to hit send. :(

On Thu, Jun 4, 2015 at 10:07 AM, Melvin Davidson <melvin6925(at)gmail(dot)com<mailto:melvin6925(at)gmail(dot)com>> wrote:
Correction, pg_relation_size includes toast data.

On Thu, Jun 4, 2015 at 10:03 AM, Melvin Davidson <melvin6925(at)gmail(dot)com<mailto:melvin6925(at)gmail(dot)com>> wrote:
I'm not sure why you are adding toast to table size, since pg_relation_size already does that.

http://www.postgresql.org/docs/9.3/interactive/functions-admin.html
This query might work better and faster for you.

SELECT n.nspname as schema,
c.relname as table,
a.rolname as owner,
c.relfilenode as filename,
c.reltuples::integer,
pg_size_pretty(pg_relation_size( quote_ident( n.nspname ) || '.' || quote_ident( c.relname ) )) as size,
pg_size_pretty(pg_total_relation_size( quote_ident( n.nspname ) || '.' || quote_ident( c.relname ) )) as total_size,
pg_relation_size( quote_ident( n.nspname ) || '.' || quote_ident( c.relname ) ) as size_bytes,
pg_total_relation_size( quote_ident( n.nspname ) || '.' || quote_ident( c.relname ) ) as total_size_bytes,
CASE WHEN c.reltablespace = 0
THEN 'pg_default'
ELSE (SELECT t.spcname
FROM pg_tablespace t WHERE (t.oid = c.reltablespace) )
END as tablespace
FROM pg_class c
JOIN pg_namespace n ON (n.oid = c.relnamespace)
JOIN pg_authid a ON ( a.oid = c.relowner )
WHERE c.relname = 'sensor'
AND n.nspname = 'devtest';

On Thu, Jun 4, 2015 at 9:50 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us<mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us>> wrote:
Hans Guijt <hg(at)terma(dot)com<mailto:hg(at)terma(dot)com>> writes:
> I have a Postgres 9.3.7 database, freshly created on Ubuntu 14 LTS 64 bit, and at this time almost completely empty. I'm attempting to find the size of a table, using the following code:
> SELECT
> pg_relation_size (stat.relid),
> CASE WHEN cl.reltoastrelid = 0 THEN
> 0
> ELSE
> pg_relation_size (cl.reltoastrelid) + COALESCE ((
> SELECT SUM (pg_relation_size (indexrelid)) FROM pg_index WHERE indrelid=cl.reltoastrelid
> ), 0)::int8
> END,
> COALESCE ((SELECT SUM (pg_relation_size (indexrelid)) FROM pg_index WHERE indrelid=stat.relid), 0)::int8
> FROM pg_stat_all_tables stat
> JOIN pg_class cl ON cl.oid=stat.relid
> JOIN pg_namespace ns ON cl.relnamespace=ns.oid
> WHERE UPPER (cl.relname) = UPPER ('sensor')
> AND UPPER (ns.nspname) = UPPER ('devtest')

Getting rid of the useless join to pg_stat_all_tables would probably help;
there's a lot of computation in that view.

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org<mailto:pgsql-general(at)postgresql(dot)org>)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

--
Melvin Davidson
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you. [Image removed by sender.]

--
Melvin Davidson
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you. [Image removed by sender.]

--
Melvin Davidson
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you. [Image removed by sender.]

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Melvin Davidson 2015-06-04 14:31:24 Re: pg_relation_size performance issue
Previous Message Melvin Davidson 2015-06-04 14:08:04 Re: pg_relation_size performance issue