<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi,</div><div><br></div><div>I posted this earlier in the QGis Issue Tracker (<a href="https://issues.qgis.org/issues/21839">https://issues.qgis.org/issues/21839</a> ) but it did not gain much traction.</div><div><br></div><div>I noticed that initial loading time of large PostgreSQL views (> 10 million records) seems to have increased considerably compared to QGis 2. After observing the db logs, seems the sql query for checking primary key unicity is one of the causes for this. From the API documentation I had the impression that this behaviour could be disabled by setting the property TrustLayerMetadata on the project to true (in the GUI under project properties or through pyqgis). This only makes a difference while opening a project, not when loading new PG layers in an open project.</div><div><br></div><div>So I tried setting the parameter checkPrimaryKeyUnicity to '0' in several ways (editing an exported QLR layer definition file and through pyqgis) but also without result. The parameter checkPrimaryKeyUnicity in the edited URI of the QLR file is ignored. For pyqgis, it seems, in case of a postgres provider, the parameter checkPrimaryKeyUnicity set on the QgsDatasourecUri instance is overriden depending on the provided layerOptions (more specific the readExtentFromXml option) specified on the QgsVectorLayer class:</div><div><br></div><div><div><i>bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProvider::ProviderOptions &options )</i></div><div><i>{</i></div><div><i>  mProviderKey = provider;     // XXX is this necessary?  Usually already set</i></div><div><i><br></i></div><div><i>  // primary key unicity is tested at construction time, so it has to be set</i></div><div><i>  // before initializing postgres provider</i></div><div><i>  QString checkUnicityKey = QStringLiteral( "checkPrimaryKeyUnicity" );</i></div><div><i>  QString dataSource = mDataSource;</i></div><div><i>  if ( provider.compare( QLatin1String( "postgres" ) ) == 0 )</i></div><div><i>  {</i></div><div><i>    QgsDataSourceUri uri( dataSource );</i></div><div><i><br></i></div><div><i>    if ( uri.hasParam( checkUnicityKey ) )</i></div><div><i>      uri.removeParam( checkUnicityKey );</i></div><div><i><br></i></div><div><i>    uri.setParam( checkUnicityKey, mReadExtentFromXml ? "0" : "1" );</i></div><div><i>    dataSource = uri.uri( false );</i></div><div><i>  }</i></div></div><div><i>...</i></div><div><br></div><div>So the only way to influence the parameter is by specifying the readExtentFromXml layeroption. Is there a reason for this construction?</div><div><br></div><div>I always was impressed with the loading of postgis layers in QGis 2 but this has considerably changed in QGis 3. Therefore I wonder if there's a way to specify the checkPrimaryKeyUnicity in the GUI or by changing the URI strig of a datasource?</div><div><br></div><div>best regards and keep up the good work,</div><div><br></div><div>Tom</div></div></div></div></div>