Fix an oversight in 32025718755c4bbf100563fdc88e96225fc1b250
authorPavan Deolasee <[email protected]>
Mon, 20 Aug 2018 10:47:31 +0000 (16:17 +0530)
committerPavan Deolasee <[email protected]>
Mon, 20 Aug 2018 10:47:31 +0000 (16:17 +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 6d5fe23c5f6720883bb7c14710144894b23bed3b..d2f6c947f69c2ebe0e3364971faf5569ad75df5b 100644 (file)
@@ -346,6 +346,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++)
@@ -1353,7 +1356,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;
@@ -1361,20 +1364,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 1ac24603e8e781296afd12530ceeb97de0e1d726..e3202e39c073e2efb906b7cc32e9cce1785c56e4 100644 (file)
@@ -206,3 +206,128 @@ DETAIL:  drop cascades to table analyze_s1.test_a1
 drop cascades to table analyze_s1.test_a3
 drop schema "ANALYZE S2" cascade;
 NOTICE:  drop cascades to table "ANALYZE S2"."TEST A4"
+-- 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 0e848d6ab1c14f413ea03df33a7e4953e66c1c51..30351efe9f54b34383d1530313e18dfde0b57618 100644 (file)
@@ -135,3 +135,54 @@ drop table test_a1 cascade;
 drop table test_a2;
 drop schema analyze_s1 cascade;
 drop schema "ANALYZE S2" cascade;
+
+-- 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";