Skip to content
Permalink
Browse files

split delete statements to chunks

  • Loading branch information
uclaros committed May 6, 2020
1 parent 52c3a7b commit 193a2e9657c86412e57b599c56851171a6c78db8
Showing with 21 additions and 10 deletions.
  1. +21 −10 src/providers/postgres/qgspostgresprovider.cpp
@@ -2558,18 +2558,29 @@ bool QgsPostgresProvider::deleteFeatures( const QgsFeatureIds &ids )
{
conn->begin();

const QString sql = QStringLiteral( "DELETE FROM %1 WHERE %2" )
.arg( mQuery, whereClause( ids ) );
QgsDebugMsgLevel( "delete sql: " + sql, 2 );

//send DELETE statement and do error handling
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK && result.PQresultStatus() != PGRES_TUPLES_OK )
throw PGException( result );

QgsFeatureIds chunkIds;
const QgsFeatureIds::const_iterator lastId = --ids.end();
for ( QgsFeatureIds::const_iterator it = ids.begin(); it != ids.end(); ++it )
{
mShared->removeFid( *it );
// create chunks of fids to delete, the last chunk may be smaller
chunkIds.insert( *it );
if ( chunkIds.size() < 5000 && it != lastId )
continue;

const QString sql = QStringLiteral( "DELETE FROM %1 WHERE %2" )
.arg( mQuery, whereClause( chunkIds ) );
QgsDebugMsgLevel( "delete sql: " + sql, 2 );

//send DELETE statement and do error handling
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK && result.PQresultStatus() != PGRES_TUPLES_OK )
throw PGException( result );

for ( QgsFeatureIds::const_iterator chunkIt = chunkIds.begin(); chunkIt != chunkIds.end(); ++chunkIt )
{
mShared->removeFid( *chunkIt );
}
chunkIds.clear();
}

returnvalue &= conn->commit();

0 comments on commit 193a2e9

Please sign in to comment.
You can’t perform that action at this time.