Skip to content

Commit

Permalink
Merge pull request #4209 from nyalldawson/postgres_queue_size2
Browse files Browse the repository at this point in the history
Dynamically adjust postgres feature queue size
  • Loading branch information
nyalldawson committed Mar 5, 2017
2 parents 9a747de + fbe4be8 commit 5a543e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/providers/postgres/qgspostgresfeatureiterator.cpp
Expand Up @@ -22,15 +22,12 @@
#include "qgsmessagelog.h"
#include "qgssettings.h"

#include <QElapsedTimer>
#include <QObject>


const int QgsPostgresFeatureIterator::FEATURE_QUEUE_SIZE = 2000;


QgsPostgresFeatureIterator::QgsPostgresFeatureIterator( QgsPostgresFeatureSource *source, bool ownSource, const QgsFeatureRequest &request )
: QgsAbstractFeatureIteratorFromSource<QgsPostgresFeatureSource>( source, ownSource, request )
, mFeatureQueueSize( FEATURE_QUEUE_SIZE )
, mFeatureQueueSize( 1 )
, mFetched( 0 )
, mFetchGeometry( false )
, mExpressionCompiled( false )
Expand Down Expand Up @@ -223,6 +220,9 @@ bool QgsPostgresFeatureIterator::fetchFeature( QgsFeature &feature )

if ( mFeatureQueue.empty() && !mLastFetch )
{
QElapsedTimer timer;
timer.start();

QString fetch = QStringLiteral( "FETCH FORWARD %1 FROM %2" ).arg( mFeatureQueueSize ).arg( mCursorName );
QgsDebugMsgLevel( QString( "fetching %1 features." ).arg( mFeatureQueueSize ), 4 );

Expand Down Expand Up @@ -258,6 +258,15 @@ bool QgsPostgresFeatureIterator::fetchFeature( QgsFeature &feature )
} // for each row in queue
}
unlock();

if ( timer.elapsed() > 500 && mFeatureQueueSize > 1 )
{
mFeatureQueueSize /= 2;
}
else if ( timer.elapsed() < 50 && mFeatureQueueSize < 10000 )
{
mFeatureQueueSize *= 2;
}
}

if ( mFeatureQueue.empty() )
Expand Down
2 changes: 0 additions & 2 deletions src/providers/postgres/qgspostgresfeatureiterator.h
Expand Up @@ -110,8 +110,6 @@ class QgsPostgresFeatureIterator : public QgsAbstractFeatureIteratorFromSource<Q

bool mIsTransactionConnection;

static const int FEATURE_QUEUE_SIZE;

private:
virtual bool providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const override;

Expand Down

0 comments on commit 5a543e4

Please sign in to comment.