Skip to content

Commit

Permalink
Fix condition to enable pgsql insert optimization
Browse files Browse the repository at this point in the history
Fixes a condition to enable Postgis provider insert optimization
(by skipping the PK column if its default is a sequence)
The check must ensure that each row value is also not the SQL
default of nextval('seq'::regclass), otherwise the condition will
not be met

(cherry picked from commit f071030)
  • Loading branch information
AchilleAsh authored and nyalldawson committed Feb 20, 2019
1 parent d3eaa9b commit d0b053e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2051,19 +2051,22 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
if ( mPrimaryKeyAttrs.size() == 1 &&
defaultValueClause( mPrimaryKeyAttrs[0] ).startsWith( "nextval(" ) )
{
bool foundNonNullPK = false;
bool foundNonEmptyPK = false;
int idx = mPrimaryKeyAttrs[0];
QString defaultValue = defaultValueClause( idx );
for ( int i = 0; i < flist.size(); i++ )
{
QgsAttributes attrs2 = flist[i].attributes();
QVariant v2 = attrs2.value( idx, QVariant( QVariant::Int ) );
if ( !v2.isNull() )
// a PK field with a sequence val is auto populate by QGIS with this default
// we are only interested in non default values
if ( !v2.isNull() && v2.toString() != defaultValue )
{
foundNonNullPK = true;
foundNonEmptyPK = true;
break;
}
}
skipSinglePKField = !foundNonNullPK;
skipSinglePKField = !foundNonEmptyPK;
}

if ( !skipSinglePKField )
Expand Down

0 comments on commit d0b053e

Please sign in to comment.