Skip to content

Commit

Permalink
[postgres] Avoid unnecessary column type retrieval when sql
Browse files Browse the repository at this point in the history
results are unused

This avoids a lot of spurious "SELECT oid, typname FROM pg_type"
queries being evaluated when populating postgres browser items
  • Loading branch information
nyalldawson committed Apr 9, 2023
1 parent 8192fee commit bac0de2
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/providers/postgres/qgspostgresproviderconnection.cpp
Expand Up @@ -497,9 +497,9 @@ long long QgsPostgresProviderResultIterator::rowCountPrivate() const
void QgsPostgresProviderConnection::vacuum( const QString &schema, const QString &name ) const
{
checkCapability( Capability::Vacuum );
executeSql( QStringLiteral( "VACUUM FULL ANALYZE %1.%2" )
.arg( QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( name ) ) );
executeSqlPrivate( QStringLiteral( "VACUUM FULL ANALYZE %1.%2" )
.arg( QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( name ) ), false );
}

void QgsPostgresProviderConnection::createSpatialIndex( const QString &schema, const QString &name, const QgsAbstractDatabaseProviderConnection::SpatialIndexOptions &options ) const
Expand Down Expand Up @@ -527,11 +527,11 @@ void QgsPostgresProviderConnection::createSpatialIndex( const QString &schema, c
}

const QString indexName = QStringLiteral( "sidx_%1_%2" ).arg( name, geometryColumnName );
executeSql( QStringLiteral( "CREATE INDEX %1 ON %2.%3 USING GIST (%4);" )
.arg( QgsPostgresConn::quotedIdentifier( indexName ),
QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( name ),
QgsPostgresConn::quotedIdentifier( geometryColumnName ) ) );
executeSqlPrivate( QStringLiteral( "CREATE INDEX %1 ON %2.%3 USING GIST (%4);" )
.arg( QgsPostgresConn::quotedIdentifier( indexName ),
QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( name ),
QgsPostgresConn::quotedIdentifier( geometryColumnName ) ), false );
}

bool QgsPostgresProviderConnection::spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const
Expand Down Expand Up @@ -582,8 +582,8 @@ void QgsPostgresProviderConnection::deleteSpatialIndex( const QString &schema, c

const QString indexName = res.at( 0 ).at( 0 ).toString();

executeSql( QStringLiteral( "DROP INDEX %1.%2" ).arg( QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( indexName ) ) );
executeSqlPrivate( QStringLiteral( "DROP INDEX %1.%2" ).arg( QgsPostgresConn::quotedIdentifier( schema ),
QgsPostgresConn::quotedIdentifier( indexName ) ), false );
}

QList<QgsPostgresProviderConnection::TableProperty> QgsPostgresProviderConnection::tables( const QString &schema, const TableFlags &flags ) const
Expand Down Expand Up @@ -676,16 +676,16 @@ QList<QgsPostgresProviderConnection::TableProperty> QgsPostgresProviderConnectio
{
try
{
const auto pks = executeSql( QStringLiteral( R"(
const QList<QVariantList> pks = executeSqlPrivate( QStringLiteral( R"(
WITH pkrelid AS (
SELECT indexrelid AS idxri FROM pg_index WHERE indrelid='%1.%2'::regclass AND (indisprimary OR indisunique)
ORDER BY CASE WHEN indisprimary THEN 1 ELSE 2 END LIMIT 1)
SELECT attname FROM pg_index,pg_attribute, pkrelid
WHERE indexrelid=pkrelid.idxri AND indrelid=attrelid AND pg_attribute.attnum=any(pg_index.indkey);
)" ).arg( QgsPostgresConn::quotedIdentifier( pr.schemaName ),
QgsPostgresConn::quotedIdentifier( pr.tableName ) ) );
QgsPostgresConn::quotedIdentifier( pr.tableName ) ), false );
QStringList pkNames;
for ( const auto &pk : std::as_const( pks ) )
for ( const QVariantList &pk : std::as_const( pks ) )
{
pkNames.push_back( pk.first().toString() );
}
Expand Down

0 comments on commit bac0de2

Please sign in to comment.