Skip to content

Commit b0490ff

Browse files
author
jef
committedApr 1, 2010
use other approach to avoid estimate_extents on not analyzed tables
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13221 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

2 files changed

+28
-110
lines changed

2 files changed

+28
-110
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 23 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -234,28 +234,6 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
234234

235235
// Kick off the long running threads
236236

237-
#ifdef POSTGRESQL_THREADS
238-
QgsDebugMsg( "About to touch mExtentThread" );
239-
mExtentThread.setConnInfo( mUri.connectionInfo );
240-
mExtentThread.setTableName( mTableName );
241-
mExtentThread.setSqlWhereClause( sqlWhereClause );
242-
mExtentThread.setGeometryColumn( geometryColumn );
243-
mExtentThread.setCallback( this );
244-
QgsDebugMsg( "About to start mExtentThread" );
245-
mExtentThread.start();
246-
QgsDebugMsg( "Main thread just dispatched mExtentThread" );
247-
248-
QgsDebugMsg( "About to touch mCountThread" );
249-
mCountThread.setConnInfo( mUri.connectionInfo );
250-
mCountThread.setTableName( mTableName );
251-
mCountThread.setSqlWhereClause( sqlWhereClause );
252-
mCountThread.setGeometryColumn( geometryColumn );
253-
mCountThread.setCallback( this );
254-
QgsDebugMsg( "About to start mCountThread" );
255-
mCountThread.start();
256-
QgsDebugMsg( "Main thread just dispatched mCountThread" );
257-
#endif
258-
259237
//fill type names into sets
260238
mNativeTypes
261239
// integer types
@@ -292,25 +270,6 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
292270

293271
QgsPostgresProvider::~QgsPostgresProvider()
294272
{
295-
#ifdef POSTGRESQL_THREADS
296-
QgsDebugMsg( "About to wait for mExtentThread" );
297-
298-
mExtentThread.wait();
299-
300-
QgsDebugMsg( "Finished waiting for mExtentThread" );
301-
302-
QgsDebugMsg( "About to wait for mCountThread" );
303-
304-
mCountThread.wait();
305-
306-
QgsDebugMsg( "Finished waiting for mCountThread" );
307-
308-
// Make sure all events from threads have been processed
309-
// (otherwise they will get destroyed prematurely)
310-
QApplication::sendPostedEvents( this, QGis::ProviderExtentCalcEvent );
311-
QApplication::sendPostedEvents( this, QGis::ProviderCountCalcEvent );
312-
#endif
313-
314273
disconnectDb();
315274

316275
QgsDebugMsg( "deconstructing." );
@@ -2730,68 +2689,38 @@ long QgsPostgresProvider::getFeatureCount()
27302689
return featuresCounted;
27312690
}
27322691

2733-
// TODO: use the estimateExtents procedure of PostGIS and PostgreSQL 8
2734-
// This tip thanks to #qgis irc nick "creeping"
27352692
void QgsPostgresProvider::calculateExtents()
27362693
{
2737-
#ifdef POSTGRESQL_THREADS
2738-
// get the approximate extent by retrieving the bounding box
2739-
// of the first few items with a geometry
2740-
2741-
QString sql = QString( "select box3d(%1) from %2 where " )
2742-
.arg( quotedIdentifier( geometryColumn ) )
2743-
.arg( mSchemaTableName );
2744-
2745-
if ( sqlWhereClause.length() > 0 )
2746-
{
2747-
sql += QString( "(%1) and " ).arg( sqlWhereClause );
2748-
}
2749-
2750-
sql += QString( "not IsEmpty(%1) limit 5" ).arg( quotedIdentifier( geometryColumn ) );
2751-
2752-
QgsDebugMsg( "Getting approximate extent using: '" + sql + "'" );
2753-
2754-
Result result = connectionRO->PQexec( sql );
2755-
2756-
// TODO: Guard against the result having no rows
2757-
for ( int i = 0; i < PQntuples( result ); i++ )
2758-
{
2759-
QString box3d = PQgetvalue( result, i, 0 );
2760-
2761-
if ( 0 == i )
2762-
{
2763-
// create the initial extent
2764-
layerExtent = QgsPostGisBox3d( box3d );
2765-
}
2766-
else
2767-
{
2768-
// extend the initial extent
2769-
QgsPostGisBox3d b = QgsPostGisBox3d( box3d );
2770-
layerExtent.combineExtentWith( &b );
2771-
}
2772-
2773-
QgsDebugMsg( QString( "After row %1, extent is %2" ).arg( i ).arg( layerExtent.toString() ) );
2774-
}
2775-
2776-
#else // non-postgresql threads version
27772694
QString sql;
27782695
Result result;
27792696
QString ext;
27802697

27812698
// get the extents
2782-
if (( mUseEstimatedMetadata || sqlWhereClause.isEmpty() ) && !connectionRO->hasNoExtentEstimate() )
2699+
if ( mUseEstimatedMetadata || sqlWhereClause.isEmpty() )
27832700
{
2784-
result = connectionRO->PQexec( QString( "select estimated_extent(%1,%2,%3)" )
2785-
.arg( quotedValue( mSchemaName ) )
2786-
.arg( quotedValue( mTableName ) )
2787-
.arg( quotedValue( geometryColumn ) ) );
2788-
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
2701+
// do stats exists?
2702+
sql = QString( "SELECT COUNT(*) FROM pg_stats WHERE schemaname=%1 AND tablename=%2 AND attname=%3" )
2703+
.arg( quotedValue( mSchemaName ) )
2704+
.arg( quotedValue( mTableName ) )
2705+
.arg( quotedValue( geometryColumn ) );
2706+
result = connectionRO->PQexec( sql );
2707+
if ( PQresultStatus( result ) == PGRES_TUPLES_OK && PQntuples( result ) == 1 )
27892708
{
2790-
connectionRO->PQexecNR( "ROLLBACK" );
2791-
connectionRO->setNoExtentEstimate();
2709+
if ( QString::fromUtf8( PQgetvalue( result, 0, 0 ) ).toInt() > 0 )
2710+
{
2711+
sql = QString( "select estimated_extent(%1,%2,%3)" )
2712+
.arg( quotedValue( mSchemaName ) )
2713+
.arg( quotedValue( mTableName ) )
2714+
.arg( quotedValue( geometryColumn ) );
2715+
result = connectionRO->PQexec( sql );
2716+
if ( PQresultStatus( result ) == PGRES_TUPLES_OK && PQntuples( result ) == 1 )
2717+
ext = PQgetvalue( result, 0, 0 );
2718+
}
2719+
}
2720+
else
2721+
{
2722+
QgsDebugMsg( QString( "no column statistics for %1.%2.%3" ).arg( mSchemaName ).arg( mTableName ).arg( geometryColumn ) );
27922723
}
2793-
else if ( PQntuples( result ) == 1 )
2794-
ext = PQgetvalue( result, 0, 0 );
27952724
}
27962725

27972726
if ( ext.isEmpty() )
@@ -2810,7 +2739,7 @@ void QgsPostgresProvider::calculateExtents()
28102739
ext = PQgetvalue( result, 0, 0 );
28112740
}
28122741

2813-
QgsDebugMsg( "Getting extents using schema.table: " + sql );
2742+
QgsDebugMsg( "Got extents using: " + sql );
28142743

28152744
QRegExp rx( "\\((.+) (.+),(.+) (.+)\\)" );
28162745
if ( ext.contains( rx ) )
@@ -2826,7 +2755,6 @@ void QgsPostgresProvider::calculateExtents()
28262755
{
28272756
QgsDebugMsg( "extents query failed" );
28282757
}
2829-
#endif
28302758

28312759
QgsDebugMsg( "Set extents to: " + layerExtent.toString() );
28322760
}

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,11 @@ class QgsPostgresProvider : public QgsVectorDataProvider
584584
class Conn
585585
{
586586
public:
587-
Conn( PGconn *connection ) :
588-
ref( 1 ),
589-
openCursors( 0 ),
590-
conn( connection ),
591-
gotPostgisVersion( false ),
592-
mHasNoExtentEstimate( false )
587+
Conn( PGconn *connection )
588+
: ref( 1 )
589+
, openCursors( 0 )
590+
, conn( connection )
591+
, gotPostgisVersion( false )
593592
{
594593
}
595594

@@ -614,12 +613,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
614613
//! PostgreSQL version
615614
int pgVersion() { return postgresqlVersion; }
616615

617-
//! has PostGIS no extent estimate?
618-
bool hasNoExtentEstimate() { return mHasNoExtentEstimate; }
619-
620-
//! PostGIS does not have a extent estimate
621-
void setNoExtentEstimate( bool flag = true ) { mHasNoExtentEstimate = flag; }
622-
623616
//! run a query and free result buffer
624617
bool PQexecNR( QString query );
625618

@@ -678,9 +671,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
678671
//! encode wkb in hex
679672
bool mUseWkbHex;
680673

681-
//! PostGIS doesn't have extent estimates
682-
bool mHasNoExtentEstimate;
683-
684674
static QMap<QString, Conn *> connectionsRW;
685675
static QMap<QString, Conn *> connectionsRO;
686676
static QMap<QString, QString> passwordCache;

0 commit comments

Comments
 (0)
Please sign in to comment.