Fix counted rows display in table properties. Fixes #2836

This commit is contained in:
Murtuza Zabuawala 2017-11-02 09:35:44 +00:00 committed by Dave Page
parent 2dcd2cc77f
commit 897bf4857a
8 changed files with 47 additions and 6 deletions

View File

@ -195,6 +195,11 @@ class BrowserModule(PgAdminModule):
gettext("Show system objects?"), 'boolean', False,
category_label=gettext('Display')
)
self.table_row_count_threshold = self.preference.register(
'properties', 'table_row_count_threshold',
gettext("Count rows if estimated less than"), 'integer', 2000,
category_label=gettext('Properties')
)
def get_exposed_url_endpoints(self):
"""

View File

@ -21,6 +21,7 @@ from pgadmin.browser.server_groups.servers.utils import parse_priv_to_db
from pgadmin.utils.ajax import make_json_response, internal_server_error, \
make_response as ajax_response, gone
from .utils import BaseTableView
from pgadmin.utils.preferences import Preferences
class TableModule(SchemaChildModule):
@ -564,8 +565,42 @@ class TableView(BaseTableView, DataTypeReader, VacuumSettings):
if len(res['rows']) == 0:
return gone(gettext("The specified table could not be found."))
# We will check the threshold set by user before executing
# the query because that can cause performance issues
# with large result set
pref = Preferences.module('browser')
table_row_count_pref = pref.preference('table_row_count_threshold')
table_row_count_threshold = table_row_count_pref.get()
estimated_row_count = int(res['rows'][0].get('reltuples', 0))
# If estimated rows are greater than threshold then
if estimated_row_count and \
estimated_row_count > table_row_count_threshold:
res['rows'][0]['rows_cnt'] = str(table_row_count_threshold) + '+'
# If estimated rows is lower than threshold then calculate the count
elif estimated_row_count and \
table_row_count_threshold >= estimated_row_count:
SQL = render_template(
"/".join(
[self.table_template_path, 'get_table_row_count.sql']
), data=res['rows'][0]
)
status, count = self.conn.execute_scalar(SQL)
if not status:
return internal_server_error(errormsg=count)
res['rows'][0]['rows_cnt'] = count
# If estimated_row_count is zero then set the row count with same
elif not estimated_row_count:
res['rows'][0]['rows_cnt'] = estimated_row_count
return super(TableView, self).properties(
gid, sid, did, scid, tid, res)
gid, sid, did, scid, tid, res
)
@BaseTableView.check_precondition
def types(self, gid, sid, did, scid, tid=None, clid=None):

View File

@ -7,7 +7,7 @@ SELECT rel.oid, rel.relname AS name, rel.reltablespace AS spcoid,rel.relacl AS r
(select nspname FROM pg_namespace WHERE oid = {{scid}}::oid ) as parent_schema,
nsp.nspname as schema,
pg_get_userbyid(rel.relowner) AS relowner, rel.relhasoids, rel.relispartition,
rel.relhassubclass, rel.reltuples, des.description, con.conname, con.conkey,
rel.relhassubclass, rel.reltuples::bigint, des.description, con.conname, con.conkey,
EXISTS(select 1 FROM pg_trigger
JOIN pg_proc pt ON pt.oid=tgfoid AND pt.proname='logtrigger'
JOIN pg_proc pc ON pc.pronamespace=pt.pronamespace AND pc.proname='slonyversion'

View File

@ -7,7 +7,7 @@ SELECT rel.oid, rel.relname AS name, rel.reltablespace AS spcoid,rel.relacl AS r
(select nspname FROM pg_namespace WHERE oid = {{scid}}::oid ) as schema,
pg_get_userbyid(rel.relowner) AS relowner, rel.relhasoids, rel.relkind,
(CASE WHEN rel.relkind = 'p' THEN true ELSE false END) AS is_partitioned,
rel.relhassubclass, rel.reltuples, des.description, con.conname, con.conkey,
rel.relhassubclass, rel.reltuples::bigint, des.description, con.conname, con.conkey,
EXISTS(select 1 FROM pg_trigger
JOIN pg_proc pt ON pt.oid=tgfoid AND pt.proname='logtrigger'
JOIN pg_proc pc ON pc.pronamespace=pt.pronamespace AND pc.proname='slonyversion'

View File

@ -6,7 +6,7 @@ SELECT rel.oid, rel.relname AS name, rel.reltablespace AS spcoid,rel.relacl AS r
END) as spcname,
(select nspname FROM pg_namespace WHERE oid = {{scid}}::oid ) as schema,
pg_get_userbyid(rel.relowner) AS relowner, rel.relhasoids,
rel.relhassubclass, rel.reltuples, des.description, con.conname, con.conkey,
rel.relhassubclass, rel.reltuples::bigint, des.description, con.conname, con.conkey,
EXISTS(select 1 FROM pg_trigger
JOIN pg_proc pt ON pt.oid=tgfoid AND pt.proname='logtrigger'
JOIN pg_proc pc ON pc.pronamespace=pt.pronamespace AND pc.proname='slonyversion'

View File

@ -0,0 +1 @@
SELECT COUNT(*)::text FROM {{ conn|qtIdent(data.schema, data.name) }};

View File

@ -9,7 +9,7 @@ FROM (
END) as spcname,
(select nspname FROM pg_namespace WHERE oid = {{scid}}::oid ) as schema,
pg_get_userbyid(rel.relowner) AS relowner, rel.relhasoids,
rel.relhassubclass, rel.reltuples, des.description, con.conname, con.conkey,
rel.relhassubclass, rel.reltuples::bigint, des.description, con.conname, con.conkey,
EXISTS(select 1 FROM pg_trigger
JOIN pg_proc pt ON pt.oid=tgfoid AND pt.proname='logtrigger'
JOIN pg_proc pc ON pc.pronamespace=pt.pronamespace AND pc.proname='slonyversion'

View File

@ -9,7 +9,7 @@ FROM (
END) as spcname,
(select nspname FROM pg_namespace WHERE oid = {{scid}}::oid ) as schema,
pg_get_userbyid(rel.relowner) AS relowner, rel.relhasoids,
rel.relhassubclass, rel.reltuples, des.description, con.conname, con.conkey,
rel.relhassubclass, rel.reltuples::bigint, des.description, con.conname, con.conkey,
EXISTS(select 1 FROM pg_trigger
JOIN pg_proc pt ON pt.oid=tgfoid AND pt.proname='logtrigger'
JOIN pg_proc pc ON pc.pronamespace=pt.pronamespace AND pc.proname='slonyversion'