@@ -547,6 +547,11 @@ QString QgsPostgresProvider::whereClause( QgsFeatureId featureId ) const
547
547
return QgsPostgresUtils::whereClause ( featureId, mAttributeFields , connectionRO (), mPrimaryKeyType , mPrimaryKeyAttrs , mShared );
548
548
}
549
549
550
+ QString QgsPostgresProvider::whereClause ( QgsFeatureIds featureIds ) const
551
+ {
552
+ return QgsPostgresUtils::whereClause ( featureIds, mAttributeFields , connectionRO (), mPrimaryKeyType , mPrimaryKeyAttrs , mShared );
553
+ }
554
+
550
555
551
556
QString QgsPostgresUtils::whereClause ( QgsFeatureId featureId, const QgsFields &fields, QgsPostgresConn *conn, QgsPostgresPrimaryKeyType pkType, const QList<int > &pkAttrs, const std::shared_ptr<QgsPostgresSharedData> &sharedData )
552
557
{
@@ -2537,8 +2542,11 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
2537
2542
return returnvalue;
2538
2543
}
2539
2544
2540
- bool QgsPostgresProvider::deleteFeatures ( const QgsFeatureIds &id )
2545
+ bool QgsPostgresProvider::deleteFeatures ( const QgsFeatureIds &ids )
2541
2546
{
2547
+ if ( ids.isEmpty () )
2548
+ return true ;
2549
+
2542
2550
bool returnvalue = true ;
2543
2551
2544
2552
if ( mIsQuery )
@@ -2558,18 +2566,29 @@ bool QgsPostgresProvider::deleteFeatures( const QgsFeatureIds &id )
2558
2566
{
2559
2567
conn->begin ();
2560
2568
2561
- for ( QgsFeatureIds::const_iterator it = id.begin (); it != id.end (); ++it )
2569
+ QgsFeatureIds chunkIds;
2570
+ const QgsFeatureIds::const_iterator lastId = --ids.end ();
2571
+ for ( QgsFeatureIds::const_iterator it = ids.begin (); it != ids.end (); ++it )
2562
2572
{
2563
- QString sql = QStringLiteral ( " DELETE FROM %1 WHERE %2" )
2564
- .arg ( mQuery , whereClause ( *it ) );
2573
+ // create chunks of fids to delete, the last chunk may be smaller
2574
+ chunkIds.insert ( *it );
2575
+ if ( chunkIds.size () < 5000 && it != lastId )
2576
+ continue ;
2577
+
2578
+ const QString sql = QStringLiteral ( " DELETE FROM %1 WHERE %2" )
2579
+ .arg ( mQuery , whereClause ( chunkIds ) );
2565
2580
QgsDebugMsgLevel ( " delete sql: " + sql, 2 );
2566
2581
2567
2582
// send DELETE statement and do error handling
2568
2583
QgsPostgresResult result ( conn->PQexec ( sql ) );
2569
2584
if ( result.PQresultStatus () != PGRES_COMMAND_OK && result.PQresultStatus () != PGRES_TUPLES_OK )
2570
2585
throw PGException ( result );
2571
2586
2572
- mShared ->removeFid ( *it );
2587
+ for ( QgsFeatureIds::const_iterator chunkIt = chunkIds.begin (); chunkIt != chunkIds.end (); ++chunkIt )
2588
+ {
2589
+ mShared ->removeFid ( *chunkIt );
2590
+ }
2591
+ chunkIds.clear ();
2573
2592
}
2574
2593
2575
2594
returnvalue &= conn->commit ();
@@ -2587,7 +2606,7 @@ bool QgsPostgresProvider::deleteFeatures( const QgsFeatureIds &id )
2587
2606
dropOrphanedTopoGeoms ();
2588
2607
}
2589
2608
2590
- mShared ->addFeaturesCounted ( -id .size () );
2609
+ mShared ->addFeaturesCounted ( -ids .size () );
2591
2610
}
2592
2611
catch ( PGException &e )
2593
2612
{
0 commit comments