Opened 10 years ago

Closed 10 years ago

#5375 closed defect (fixed)

CreateFeature failure related to wrong parsing of "select version()" in ogrpgdatasource.cpp

Reported by: giorgiomugnaini Owned by: warmerdam
Priority: normal Milestone: 1.10.2
Component: default Version: 1.10.0
Severity: normal Keywords: OGRPGDataSource, PostgreSQL
Cc:

Description

CreateFeature() has to set FID of newly created features by means of following fragment : (line 1629, ogrpgtablelayer.cpp )

   if (bEmptyInsert)
        osCommand.Printf( "INSERT INTO %s DEFAULT VALUES", pszSqlTableName );

    int bReturnRequested = FALSE;
    /* RETURNING is only available since Postgres 8.2 */
    /* We only get the FID, but we also could add the unset fields to get */
    /* the default values */
    if (bRetrieveFID && pszFIDColumn != NULL && poFeature->GetFID() == OGRNullFID &&
        (poDS->sPostgreSQLVersion.nMajor >= 9 ||
         (poDS->sPostgreSQLVersion.nMajor == 8 && poDS->sPostgreSQLVersion.nMinor >= 2)))
    {
        bReturnRequested = TRUE;
        osCommand += " RETURNING ";
        osCommand += OGRPGEscapeColumnName(pszFIDColumn);
    }

IF sPostgreSQLVersion is not correctly evalueted, "RETURNING ..." is not performed after the insert, therefore FID will be not correctly set.

It is interesting to note that sPostgreSQLVersion is obtaine by parsing the response to a "select version()", assuming related response is like "Postgresql 9.2..." .

See line 586, file: ogrpgdatasource.cpp:

 hResult = OGRPG_PQexec(hPGConn, "SELECT version()" );
    if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK
        && PQntuples(hResult) > 0 )
    {
        char * pszVer = PQgetvalue(hResult,0,0);

        CPLDebug("PG","PostgreSQL version string : '%s'", pszVer);

        if (EQUALN(pszVer, "PostgreSQL ", 11))
        {
            OGRPGDecodeVersionString(&sPostgreSQLVersion, pszVer + 11);
            if (sPostgreSQLVersion.nMajor == 7 && sPostgreSQLVersion.nMinor < 4)
            {
                /* We don't support BINARY CURSOR for PostgreSQL < 7.4. */
                /* The binary protocol for arrays seems to be different from later versions */
                CPLDebug("PG","BINARY cursor will finally NOT be used because version < 7.4");
                bUseBinaryCursor = FALSE;
            }
        }
    }

The problem is that in presence of EnterpriseDb 9.3 the response HAS is like the following :

EnterpriseDB 9.3.2.5, compiled by Visual C++ build 1600, 64-bit"

Aboveseen trivial version string parsing fails in such case, therefore Createfeature() works uncorrectly.

Change History (2)

comment:1 by giorgiomugnaini, 10 years ago

Keywords: OGRPGDataSource PostgreSQL added

comment:2 by Even Rouault, 10 years ago

Milestone: 1.10.2
Resolution: fixed
Status: newclosed

Fixed in trunk (r26915) and branches/1.10 (r26916)

Note: See TracTickets for help on using tickets.