From f071030019ea6c7b3fa448ec17607663f329f5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9=20Perez?= Date: Wed, 6 Feb 2019 23:22:47 +0100 Subject: [PATCH] Fix condition to enable pgsql insert optimization 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 --- src/providers/postgres/qgspostgresprovider.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/providers/postgres/qgspostgresprovider.cpp b/src/providers/postgres/qgspostgresprovider.cpp index 6eae624002fc..7bb0979b2f07 100644 --- a/src/providers/postgres/qgspostgresprovider.cpp +++ b/src/providers/postgres/qgspostgresprovider.cpp @@ -2040,19 +2040,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 )