Skip to content

Commit e42c6a3

Browse files
authored
Merge pull request #9219 from elpaso/bugfix-21303-postgis-slow-table-open
Postgis: cache information about enum fields
2 parents 17280c3 + 7627fb2 commit e42c6a3

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,10 @@ struct PGTypeInfo
711711

712712
bool QgsPostgresProvider::loadFields()
713713
{
714+
715+
// Clear cached information about enum values support
716+
mShared->clearSupportsEnumValuesCache();
717+
714718
if ( !mIsQuery )
715719
{
716720
QgsDebugMsg( QStringLiteral( "Loading fields for table %1" ).arg( mTableName ) );
@@ -1683,42 +1687,50 @@ QStringList QgsPostgresProvider::uniqueStringsMatching( int index, const QString
16831687

16841688
void QgsPostgresProvider::enumValues( int index, QStringList &enumList ) const
16851689
{
1686-
enumList.clear();
16871690

16881691
if ( index < 0 || index >= mAttributeFields.count() )
16891692
return;
16901693

1694+
if ( ! mShared->fieldSupportsEnumValuesIsSet( index ) )
1695+
{
1696+
mShared->setFieldSupportsEnumValues( index, true );
1697+
}
1698+
else if ( ! mShared->fieldSupportsEnumValues( index ) )
1699+
{
1700+
return;
1701+
}
1702+
16911703
//find out type of index
1692-
QString fieldName = mAttributeFields.at( index ).name();
1704+
const QString fieldName = mAttributeFields.at( index ).name();
16931705
QString typeName = mAttributeFields.at( index ).typeName();
16941706

16951707
// Remove schema extension from typeName
16961708
typeName.remove( QRegularExpression( "^([^.]+\\.)+" ) );
16971709

16981710
//is type an enum?
1699-
QString typeSql = QStringLiteral( "SELECT typtype FROM pg_type WHERE typname=%1" ).arg( quotedValue( typeName ) );
1711+
const QString typeSql = QStringLiteral( "SELECT typtype FROM pg_type WHERE typname=%1" ).arg( quotedValue( typeName ) );
17001712
QgsPostgresResult typeRes( connectionRO()->PQexec( typeSql ) );
17011713
if ( typeRes.PQresultStatus() != PGRES_TUPLES_OK || typeRes.PQntuples() < 1 )
17021714
{
1715+
mShared->setFieldSupportsEnumValues( index, false );
17031716
return;
17041717
}
17051718

1706-
1707-
QString typtype = typeRes.PQgetvalue( 0, 0 );
1719+
const QString typtype = typeRes.PQgetvalue( 0, 0 );
17081720
if ( typtype.compare( QLatin1String( "e" ), Qt::CaseInsensitive ) == 0 )
17091721
{
17101722
//try to read enum_range of attribute
17111723
if ( !parseEnumRange( enumList, fieldName ) )
17121724
{
1713-
enumList.clear();
1725+
mShared->setFieldSupportsEnumValues( index, false );
17141726
}
17151727
}
17161728
else
17171729
{
17181730
//is there a domain check constraint for the attribute?
17191731
if ( !parseDomainCheckConstraint( enumList, fieldName ) )
17201732
{
1721-
enumList.clear();
1733+
mShared->setFieldSupportsEnumValues( index, false );
17221734
}
17231735
}
17241736
}
@@ -5169,3 +5181,28 @@ void QgsPostgresSharedData::clear()
51695181
mFeaturesCounted = -1;
51705182
mFidCounter = 0;
51715183
}
5184+
5185+
void QgsPostgresSharedData::clearSupportsEnumValuesCache()
5186+
{
5187+
QMutexLocker locker( &mMutex );
5188+
mFieldSupportsEnumValues.clear();
5189+
}
5190+
5191+
bool QgsPostgresSharedData::fieldSupportsEnumValuesIsSet( int index )
5192+
{
5193+
QMutexLocker locker( &mMutex );
5194+
return mFieldSupportsEnumValues.contains( index );
5195+
}
5196+
5197+
bool QgsPostgresSharedData::fieldSupportsEnumValues( int index )
5198+
{
5199+
QMutexLocker locker( &mMutex );
5200+
return mFieldSupportsEnumValues.contains( index ) && mFieldSupportsEnumValues[ index ];
5201+
}
5202+
5203+
void QgsPostgresSharedData::setFieldSupportsEnumValues( int index, bool isSupported )
5204+
{
5205+
QMutexLocker locker( &mMutex );
5206+
mFieldSupportsEnumValues[ index ] = isSupported;
5207+
}
5208+

src/providers/postgres/qgspostgresprovider.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,11 @@ class QgsPostgresSharedData
522522
QVariantList lookupKey( QgsFeatureId featureId );
523523
void clear();
524524

525+
void clearSupportsEnumValuesCache( );
526+
bool fieldSupportsEnumValuesIsSet( int index );
527+
bool fieldSupportsEnumValues( int index );
528+
void setFieldSupportsEnumValues( int index, bool isSupported );
529+
525530
protected:
526531
QMutex mMutex; //!< Access to all data members is guarded by the mutex
527532

@@ -530,6 +535,7 @@ class QgsPostgresSharedData
530535
QgsFeatureId mFidCounter = 0; // next feature id if map is used
531536
QMap<QVariantList, QgsFeatureId> mKeyToFid; // map key values to feature id
532537
QMap<QgsFeatureId, QVariantList> mFidToKey; // map feature id back to key values
538+
QMap<int, bool> mFieldSupportsEnumValues; // map field index to bool flag supports enum values
533539
};
534540

535541
// clazy:excludeall=qstring-allocations

0 commit comments

Comments
 (0)