Fix an oversight in 32025718755c4bbf100563fdc88e96225fc1b250
authorPavan Deolasee <[email protected]>
Mon, 20 Aug 2018 10:47:31 +0000 (16:17 +0530)
committerPavan Deolasee <[email protected]>
Thu, 6 Sep 2018 08:37:43 +0000 (14:07 +0530)
In passing, add test cases and fix other issues around fetching table sizes
from remote nodes. For example, temp handling and names requiring quoting was
broken for long too.

Per report by Virendra Kumar and further tests by me.

src/backend/utils/adt/dbsize.c
src/test/regress/expected/xl_misc.out
src/test/regress/sql/xl_misc.sql

index 33a4620effd72e9a4c4ea58ab69f4b30e1b7d74b..f3c3b67a32dc57386fd4aac311e840a4b91f5567 100644 (file)
@@ -337,6 +337,9 @@ calculate_relation_size(RelFileNode *rfn, BackendId backend, ForkNumber forknum)
        char            pathname[MAXPGPATH];
        unsigned int segcount = 0;
 
+       if (OidIsValid(MyCoordId))
+               backend = InvalidBackendId;
+
        relationpath = relpathbackend(*rfn, backend, forknum);
 
        for (segcount = 0;; segcount++)
@@ -1185,7 +1188,7 @@ pgxc_exec_sizefunc(Oid relOid, char *funcname, char *extra_arg)
 {
        int             numnodes;
        Oid            *nodelist;
-       char           *relname = NULL;
+       const char         *relname = NULL;
        StringInfoData  buf;
        Relation        rel;
        bool                    istemp;
@@ -1193,20 +1196,25 @@ pgxc_exec_sizefunc(Oid relOid, char *funcname, char *extra_arg)
        rel = relation_open(relOid, AccessShareLock);
        istemp = (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP);
 
+       /* get relation name including any needed schema prefix and quoting */
+       if (rel->rd_locator_info)
+               relname = RelationGetRelationName(rel);
+
        initStringInfo(&buf);
        /* get relation name including any needed schema prefix and quoting */
        if (!extra_arg)
                appendStringInfo(&buf, "SELECT pg_catalog.%s(c.oid) "
                                "FROM pg_class c JOIN pg_namespace nc "
-                               "       ON c.oid = nc.oid ", funcname);
+                               "       ON c.relnamespace = nc.oid ", funcname);
        else
                appendStringInfo(&buf, "SELECT pg_catalog.%s(c.oid, '%s') "
                                "FROM pg_class c JOIN pg_namespace nc "
-                               "       ON c.oid = nc.oid ", funcname, extra_arg);
+                               "       ON c.relnamespace = nc.oid ", funcname, extra_arg);
 
        if (!istemp)
                appendStringInfo(&buf, "WHERE relname = '%s' AND nc.nspname = '%s'",
-                               relname, get_namespace_name(rel->rd_rel->relnamespace));
+                               relname,
+                               get_namespace_name(rel->rd_rel->relnamespace));
        else
                appendStringInfo(&buf, "WHERE relname = '%s' AND "
                                "       nc.oid = pg_my_temp_schema()", relname);
index 6a130357453cfcb890c4a12057aae113bcbd2295..2666151bc1ffbe2378a44476e7e58c3329e84e3c 100644 (file)
@@ -162,3 +162,128 @@ select array_agg(c.*) from "XLTEST_type" c where c.primary = 1;
 (1 row)
 
 drop table "XL.Schema"."XLTEST_type";
+-- size functions
+create table tabsize (a int);
+insert into tabsize values (1);
+select pg_relation_size('tabsize');                    -- only one node should have one heap page
+ pg_relation_size 
+------------------
+             8192
+(1 row)
+
+select pg_total_relation_size('tabsize');      -- no indexes or toast
+ pg_total_relation_size 
+------------------------
+                   8192
+(1 row)
+
+insert into tabsize values (2), (3);
+select pg_relation_size('tabsize');                    -- both nodes should have one heap page each
+ pg_relation_size 
+------------------
+            16384
+(1 row)
+
+select pg_total_relation_size('tabsize');      -- no indexes or toast
+ pg_total_relation_size 
+------------------------
+                  16384
+(1 row)
+
+create index testindx ON tabsize(a);
+select pg_total_relation_size('tabsize');      -- index size gets added
+ pg_total_relation_size 
+------------------------
+                  49152
+(1 row)
+
+alter table tabsize add column b text default 'x';             -- toast table
+select pg_total_relation_size('tabsize');      -- toast table size gets added
+ pg_total_relation_size 
+------------------------
+                  65536
+(1 row)
+
+create index testindx_b ON tabsize(b);
+select pg_total_relation_size('tabsize');      -- another index on the table
+ pg_total_relation_size 
+------------------------
+                  98304
+(1 row)
+
+-- check materialized view
+create materialized view tabsize_mv1 as select a from tabsize;
+select pg_total_relation_size('tabsize_mv1');
+ pg_total_relation_size 
+------------------------
+                   8192
+(1 row)
+
+create materialized view tabsize_mv2 as select a, b from tabsize;
+select pg_total_relation_size('tabsize_mv2');
+ pg_total_relation_size 
+------------------------
+                  16384
+(1 row)
+
+drop table tabsize cascade;
+NOTICE:  drop cascades to 2 other objects
+DETAIL:  drop cascades to materialized view tabsize_mv1
+drop cascades to materialized view tabsize_mv2
+-- check temp table
+create temp table tabsize (a int);
+insert into tabsize values (1), (2), (3);
+select pg_relation_size('tabsize');                    -- both nodes should have one heap page each
+ pg_relation_size 
+------------------
+            16384
+(1 row)
+
+select pg_total_relation_size('tabsize');      -- no indexes or toast
+ pg_total_relation_size 
+------------------------
+                  16384
+(1 row)
+
+create index testindx ON tabsize(a);
+select pg_total_relation_size('tabsize');      -- index size gets added
+ pg_total_relation_size 
+------------------------
+                  49152
+(1 row)
+
+drop table tabsize;
+-- check replicated tables
+create table tabsize (a int) distribute by replication;
+insert into tabsize values (1), (2), (3);
+select pg_relation_size('tabsize');
+ pg_relation_size 
+------------------
+            16384
+(1 row)
+
+select pg_total_relation_size('tabsize');
+ pg_total_relation_size 
+------------------------
+                  16384
+(1 row)
+
+drop table tabsize;
+-- check schema qualified, special names etc
+create schema "schema_SIZE";
+create table "schema_SIZE"."tab_SIZE" (a int);
+insert into "schema_SIZE"."tab_SIZE" values (1), (2), (3);
+select pg_relation_size('"schema_SIZE"."tab_SIZE"');
+ pg_relation_size 
+------------------
+            16384
+(1 row)
+
+set search_path to "schema_SIZE";
+select pg_relation_size('"tab_SIZE"');
+ pg_relation_size 
+------------------
+            16384
+(1 row)
+
+drop table "schema_SIZE"."tab_SIZE";
index 3ad64c5b96ddcc6ac1ed4fc648a47c66169ba473..c1123614100af8214728fd41ade4df3c417f0c0b 100644 (file)
@@ -93,3 +93,53 @@ select array_agg(c.*) from "XLTEST_type" c where c.primary = 1;
 
 drop table "XL.Schema"."XLTEST_type";
 
+-- size functions
+create table tabsize (a int);
+insert into tabsize values (1);
+select pg_relation_size('tabsize');                    -- only one node should have one heap page
+select pg_total_relation_size('tabsize');      -- no indexes or toast
+insert into tabsize values (2), (3);
+select pg_relation_size('tabsize');                    -- both nodes should have one heap page each
+select pg_total_relation_size('tabsize');      -- no indexes or toast
+
+create index testindx ON tabsize(a);
+select pg_total_relation_size('tabsize');      -- index size gets added
+
+alter table tabsize add column b text default 'x';             -- toast table
+select pg_total_relation_size('tabsize');      -- toast table size gets added
+create index testindx_b ON tabsize(b);
+select pg_total_relation_size('tabsize');      -- another index on the table
+
+-- check materialized view
+create materialized view tabsize_mv1 as select a from tabsize;
+select pg_total_relation_size('tabsize_mv1');
+create materialized view tabsize_mv2 as select a, b from tabsize;
+select pg_total_relation_size('tabsize_mv2');
+
+drop table tabsize cascade;
+
+-- check temp table
+create temp table tabsize (a int);
+insert into tabsize values (1), (2), (3);
+select pg_relation_size('tabsize');                    -- both nodes should have one heap page each
+select pg_total_relation_size('tabsize');      -- no indexes or toast
+
+create index testindx ON tabsize(a);
+select pg_total_relation_size('tabsize');      -- index size gets added
+drop table tabsize;
+
+-- check replicated tables
+create table tabsize (a int) distribute by replication;
+insert into tabsize values (1), (2), (3);
+select pg_relation_size('tabsize');
+select pg_total_relation_size('tabsize');
+drop table tabsize;
+
+-- check schema qualified, special names etc
+create schema "schema_SIZE";
+create table "schema_SIZE"."tab_SIZE" (a int);
+insert into "schema_SIZE"."tab_SIZE" values (1), (2), (3);
+select pg_relation_size('"schema_SIZE"."tab_SIZE"');
+set search_path to "schema_SIZE";
+select pg_relation_size('"tab_SIZE"');
+drop table "schema_SIZE"."tab_SIZE";