Skip to content

Commit c3611c6

Browse files
committedJul 19, 2023
create postgres RO connection in the thread where the provider live
1 parent 9719883 commit c3611c6

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed
 

‎python/core/auto_generated/providers/qgsdataprovider.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Abstract base class for spatial data provider implementations.
7979
SkipFullScan,
8080
ForceReadOnly,
8181
SkipCredentialsRequest,
82+
ParallelThreadLoading,
8283
};
8384
typedef QFlags<QgsDataProvider::ReadFlag> ReadFlags;
8485

‎src/core/project/qgsproject.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ void QgsProject::preloadProviders( const QVector<QDomNode> &parallelLayerNodes,
13681368

13691369
// Requesting credential from worker thread could lead to deadlocks because the main thread is waiting for worker thread to fininsh
13701370
layerToLoad.flags.setFlag( QgsDataProvider::SkipCredentialsRequest, true );
1371+
layerToLoad.flags.setFlag( QgsDataProvider::ParallelThreadLoading, true );
13711372

13721373
layersToLoad.insert( layerToLoad.layerId, layerToLoad );
13731374
}
@@ -1426,6 +1427,7 @@ void QgsProject::preloadProviders( const QVector<QDomNode> &parallelLayerNodes,
14261427
const LayerToLoad &lay = it.value();
14271428
QgsDataProvider::ReadFlags providerFlags = lay.flags;
14281429
providerFlags.setFlag( QgsDataProvider::SkipCredentialsRequest, false );
1430+
providerFlags.setFlag( QgsDataProvider::ParallelThreadLoading, false );
14291431
QgsScopedRuntimeProfile profile( "Create data providers/" + lay.layerId, QStringLiteral( "projectload" ) );
14301432
std::unique_ptr<QgsDataProvider> provider( QgsProviderRegistry::instance()->createProvider( lay.provider, lay.dataSource, lay.options, providerFlags ) );
14311433
i++;

‎src/core/providers/qgsdataprovider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class CORE_EXPORT QgsDataProvider : public QObject
128128
SkipFullScan = 1 << 4, //!< Skip expensive full scan on files (i.e. on delimited text) (since QGIS 3.24)
129129
ForceReadOnly = 1 << 5, //!< Open layer in a read-only mode (since QGIS 3.28)
130130
SkipCredentialsRequest = 1 << 6, //! Skip credentials if the provided one are not valid, let the provider be invalid, avoiding to block the thread creating the provider if it is not the main thread (since QGIS 3.32).
131+
ParallelThreadLoading = 1 << 7, //! Provider is created in a parallel thread than the one where it will live (since QGIS 3.32.1).
131132
};
132133
Q_DECLARE_FLAGS( ReadFlags, ReadFlag )
133134

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri, const ProviderOpti
303303

304304
mLayerMetadata.setType( QStringLiteral( "dataset" ) );
305305
mLayerMetadata.setCrs( crs() );
306+
307+
// Constructor is called in another thread than the thread where the provider will live,
308+
// so we disconnect the DB, connection will be done later in the provider thread when needed
309+
if ( flags.testFlag( QgsDataProvider::ParallelThreadLoading ) )
310+
disconnectDb();
306311
}
307312

308313
QgsPostgresProvider::~QgsPostgresProvider()
@@ -320,7 +325,13 @@ QgsAbstractFeatureSource *QgsPostgresProvider::featureSource() const
320325

321326
QgsPostgresConn *QgsPostgresProvider::connectionRO() const
322327
{
323-
return mTransaction ? mTransaction->connection() : mConnectionRO;
328+
if ( mTransaction )
329+
return mTransaction->connection();
330+
331+
if ( !mConnectionRO )
332+
mConnectionRO = QgsPostgresConn::connectDb( mUri, true, true, false, !mReadFlags.testFlag( QgsDataProvider::SkipCredentialsRequest ) );
333+
334+
return mConnectionRO;
324335
}
325336

326337
void QgsPostgresProvider::setListening( bool isListening )
@@ -3887,7 +3898,7 @@ QgsRectangle QgsPostgresProvider::extent() const
38873898
quotedValue( mSchemaName ),
38883899
quotedValue( mTableName ),
38893900
quotedValue( mGeometryColumn ) );
3890-
result = mConnectionRO->LoggedPQexec( "QgsPostgresProvider", sql );
3901+
result = connectionRO()->LoggedPQexec( "QgsPostgresProvider", sql );
38913902
if ( result.PQresultStatus() == PGRES_TUPLES_OK && result.PQntuples() == 1 && !result.PQgetisnull( 0, 0 ) )
38923903
{
38933904
ext = result.PQgetvalue( 0, 0 );

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ class QgsPostgresProvider final: public QgsVectorDataProvider
450450

451451
QString paramValue( const QString &fieldvalue, const QString &defaultValue ) const;
452452

453-
QgsPostgresConn *mConnectionRO = nullptr ; //!< Read-only database connection (initially)
453+
mutable QgsPostgresConn *mConnectionRO = nullptr ; //!< Read-only database connection (initially)
454454
QgsPostgresConn *mConnectionRW = nullptr ; //!< Read-write database connection (on update)
455455

456456
QgsPostgresConn *connectionRO() const;

0 commit comments

Comments
 (0)
Please sign in to comment.