Skip to content

Commit f071030

Browse files
AchilleAshnyalldawson
authored andcommitted
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
1 parent 8c18a5c commit f071030

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -2040,19 +2040,22 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
20402040
if ( mPrimaryKeyAttrs.size() == 1 &&
20412041
defaultValueClause( mPrimaryKeyAttrs[0] ).startsWith( "nextval(" ) )
20422042
{
2043-
bool foundNonNullPK = false;
2043+
bool foundNonEmptyPK = false;
20442044
int idx = mPrimaryKeyAttrs[0];
2045+
QString defaultValue = defaultValueClause( idx );
20452046
for ( int i = 0; i < flist.size(); i++ )
20462047
{
20472048
QgsAttributes attrs2 = flist[i].attributes();
20482049
QVariant v2 = attrs2.value( idx, QVariant( QVariant::Int ) );
2049-
if ( !v2.isNull() )
2050+
// a PK field with a sequence val is auto populate by QGIS with this default
2051+
// we are only interested in non default values
2052+
if ( !v2.isNull() && v2.toString() != defaultValue )
20502053
{
2051-
foundNonNullPK = true;
2054+
foundNonEmptyPK = true;
20522055
break;
20532056
}
20542057
}
2055-
skipSinglePKField = !foundNonNullPK;
2058+
skipSinglePKField = !foundNonEmptyPK;
20562059
}
20572060

20582061
if ( !skipSinglePKField )

0 commit comments

Comments
 (0)