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
  • Loading branch information
AchilleAsh authored and nyalldawson committed Feb 20, 2019
1 parent 8c18a5c commit f071030
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2040,19 +2040,22 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
if ( mPrimaryKeyAttrs.size() == 1 && if ( mPrimaryKeyAttrs.size() == 1 &&
defaultValueClause( mPrimaryKeyAttrs[0] ).startsWith( "nextval(" ) ) defaultValueClause( mPrimaryKeyAttrs[0] ).startsWith( "nextval(" ) )
{ {
bool foundNonNullPK = false; bool foundNonEmptyPK = false;
int idx = mPrimaryKeyAttrs[0]; int idx = mPrimaryKeyAttrs[0];
QString defaultValue = defaultValueClause( idx );
for ( int i = 0; i < flist.size(); i++ ) for ( int i = 0; i < flist.size(); i++ )
{ {
QgsAttributes attrs2 = flist[i].attributes(); QgsAttributes attrs2 = flist[i].attributes();
QVariant v2 = attrs2.value( idx, QVariant( QVariant::Int ) ); 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; break;
} }
} }
skipSinglePKField = !foundNonNullPK; skipSinglePKField = !foundNonEmptyPK;
} }


if ( !skipSinglePKField ) if ( !skipSinglePKField )
Expand Down

0 comments on commit f071030

Please sign in to comment.