@@ -62,14 +62,18 @@ func (qm *QueryManager) GetAllDatabases(conn *dbconn.DBConn) ([]string, error) {
6262
6363// GetUserTables retrieves user tables from the database
6464// example output:
65- // map[public.t1:{"Partition":0,"RelTuples":1000} public.t2:{"Partition":0,"RelTuples":2000}]
65+ // map[public.t1:{"Partition":0,"RelTuples":1000,"IsReplicated":false } public.t2:{"Partition":0,"RelTuples":2000,"IsReplicated":true }]
6666func (tm * QueryManager ) GetUserTables (conn * dbconn.DBConn ) (map [string ]option.TableStatistics , error ) {
6767 var query string
6868
6969 if (conn .Version .IsGPDB () && conn .Version .AtLeast ("7" )) || conn .Version .IsCBDBFamily () {
7070 query = `
7171 SELECT
72- quote_ident(n.nspname) AS schema, quote_ident(c.relname) as name, 0 as partition, cast(c.reltuples as bigint) AS relTuples
72+ quote_ident(n.nspname) AS schema,
73+ quote_ident(c.relname) as name,
74+ 0 as partition,
75+ cast(c.reltuples as bigint) AS relTuples,
76+ CASE WHEN p.policytype = 'r' THEN true ELSE false END as isReplicated
7377 FROM
7478 pg_class c
7579 JOIN pg_namespace n ON (c.relnamespace=n.oid)
@@ -81,11 +85,15 @@ func (tm *QueryManager) GetUserTables(conn *dbconn.DBConn) (map[string]option.Ta
8185 AND c.relkind <> 'm'
8286 ORDER BY c.relpages DESC
8387 `
84- } else {
88+ } else if conn . Version . IsGPDB () && conn . Version . AtLeast ( "6" ) {
8589 query = `
8690 SELECT t.* FROM (
8791 SELECT
88- quote_ident(n.nspname) AS schema, quote_ident(c.relname) as name, 0 as partition, cast(c.reltuples as bigint) AS relTuples
92+ quote_ident(n.nspname) AS schema,
93+ quote_ident(c.relname) as name,
94+ 0 as partition,
95+ cast(c.reltuples as bigint) AS relTuples,
96+ CASE WHEN p.policytype = 'r' THEN true ELSE false END as isReplicated
8997 FROM
9098 pg_class c
9199 JOIN pg_namespace n ON (c.relnamespace=n.oid)
@@ -99,7 +107,48 @@ func (tm *QueryManager) GetUserTables(conn *dbconn.DBConn) (map[string]option.Ta
99107 ORDER BY c.relpages DESC ) t
100108 UNION ALL
101109 SELECT
102- quote_ident(n.nspname) AS schema, quote_ident(cparent.relname) AS name, 0 as partition, cast(cparent.reltuples as bigint) AS relTuples
110+ quote_ident(n.nspname) AS schema,
111+ quote_ident(cparent.relname) AS name,
112+ 0 as partition,
113+ cast(cparent.reltuples as bigint) AS relTuples,
114+ false as isReplicated
115+ FROM pg_partition p
116+ JOIN pg_partition_rule r ON p.oid = r.paroid
117+ JOIN pg_class cparent ON cparent.oid = r.parchildrelid
118+ JOIN pg_namespace n ON cparent.relnamespace=n.oid
119+ JOIN (SELECT parrelid AS relid, max(parlevel) AS pl
120+ FROM pg_partition GROUP BY parrelid) AS levels ON p.parrelid = levels.relid
121+ WHERE
122+ r.parchildrelid != 0
123+ AND p.parlevel = levels.pl
124+ AND n.nspname NOT IN ('gpexpand', 'pg_bitmapindex', 'information_schema', 'gp_toolkit')
125+ AND n.nspname NOT LIKE 'pg_temp_%' AND cparent.relstorage NOT IN ('v', 'x', 'f')`
126+ } else {
127+ query = `
128+ SELECT t.* FROM (
129+ SELECT
130+ quote_ident(n.nspname) AS schema,
131+ quote_ident(c.relname) as name,
132+ 0 as partition,
133+ cast(c.reltuples as bigint) AS relTuples,
134+ false as isReplicated
135+ FROM
136+ pg_class c
137+ JOIN pg_namespace n ON (c.relnamespace=n.oid)
138+ WHERE
139+ c.oid NOT IN ( SELECT parchildrelid as oid FROM pg_partition_rule )
140+ AND c.oid NOT IN ( SELECT parrelid as oid FROM pg_partition )
141+ AND n.nspname NOT IN ('gpexpand', 'pg_bitmapindex', 'information_schema', 'gp_toolkit')
142+ AND n.nspname NOT LIKE 'pg_temp_%' AND c.relstorage NOT IN ('v', 'x', 'f')
143+ AND c.relkind <> 'm'
144+ ORDER BY c.relpages DESC ) t
145+ UNION ALL
146+ SELECT
147+ quote_ident(n.nspname) AS schema,
148+ quote_ident(cparent.relname) AS name,
149+ 0 as partition,
150+ cast(cparent.reltuples as bigint) AS relTuples,
151+ false as isReplicated
103152 FROM pg_partition p
104153 JOIN pg_partition_rule r ON p.oid = r.paroid
105154 JOIN pg_class cparent ON cparent.oid = r.parchildrelid
@@ -123,7 +172,7 @@ func (tm *QueryManager) GetUserTables(conn *dbconn.DBConn) (map[string]option.Ta
123172 results := make (map [string ]option.TableStatistics )
124173 for _ , t := range tables {
125174 k := t .Schema + "." + t .Name
126- results [k ] = option.TableStatistics {Partition : t .Partition , RelTuples : t .RelTuples }
175+ results [k ] = option.TableStatistics {Partition : t .Partition , RelTuples : t .RelTuples , IsReplicated : t . IsReplicated }
127176 }
128177
129178 return results , nil
0 commit comments