Skip to content
Permalink
Browse files

[postgres] Fixes #40162 : Don't use explain plan when estimating coun…

…t on table

(cherry picked from commit 6c1a9bc)
  • Loading branch information
troopa81 authored and nyalldawson committed Dec 21, 2020
1 parent a73eace commit 961a4b42dbd9170d7d776e0bc762db5ca4af0ff5
Showing with 19 additions and 2 deletions.
  1. +7 −1 src/providers/postgres/qgspostgresprovider.cpp
  2. +12 −1 tests/src/python/test_provider_postgres.py
@@ -3588,7 +3588,7 @@ long QgsPostgresProvider::featureCount() const
long num = -1;
if ( !mIsQuery && mUseEstimatedMetadata )
{
if ( connectionRO()->pgVersion() >= 90000 )
if ( relkind() == Relkind::View && connectionRO()->pgVersion() >= 90000 )
{
// parse explain output to estimate feature count
// we don't use pg_class reltuples because it returns 0 for view
@@ -3605,6 +3605,12 @@ long QgsPostgresProvider::featureCount() const
else
QgsLogger::warning( QStringLiteral( "Cannot parse JSON explain result to estimate feature count (%1) : %2" ).arg( sql, json ) );
}
else
{
sql = QStringLiteral( "SELECT reltuples::bigint FROM pg_catalog.pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) );
QgsPostgresResult result( connectionRO()->PQexec( sql ) );
num = result.PQgetvalue( 0, 0 ).toLong();
}
}
else
{
@@ -2296,6 +2296,17 @@ def testCheckTidPkOnViews(self):
for f in vl0.getFeatures():
self.assertNotEqual(f.attribute(0), NULL)

def testFeatureCountEstimatedOnTable(self):
"""
Test feature count on table when estimated data is enabled
"""
vl = QgsVectorLayer(
self.dbconn +
' sslmode=disable key=\'pk\' estimatedmetadata=true srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=',
'test', 'postgres')
self.assertTrue(vl.isValid())
self.assertTrue(vl.featureCount() > 0)

def testFeatureCountEstimatedOnView(self):
"""
Test feature count on view when estimated data is enabled
@@ -2308,7 +2319,7 @@ def testFeatureCountEstimatedOnView(self):
' sslmode=disable key=\'pk\' estimatedmetadata=true srid=4326 type=POINT table="qgis_test"."somedataview" (geom) sql=',
'test', 'postgres')
self.assertTrue(vl.isValid())
self.assertTrue(self.source.featureCount() > 0)
self.assertTrue(vl.featureCount() > 0)

def testIdentityPk(self):
"""Test a table with identity pk, see GH #29560"""

0 comments on commit 961a4b4

Please sign in to comment.
You can’t perform that action at this time.