Index: ogr/ogrsf_frmts/pg/ogr_pg.h
===================================================================
--- ogr/ogrsf_frmts/pg/ogr_pg.h	(révision 20540)
+++ ogr/ogrsf_frmts/pg/ogr_pg.h	(copie de travail)
@@ -213,6 +213,9 @@
     int                 bHasWarnedIncompatibleGeom;
     void                CheckGeomTypeCompatibility(OGRGeometry* poGeom);
 
+    int                 bRetrieveFID;
+    int                 bHasWarnedAlreadySetFID;
+
 public:
                         OGRPGTableLayer( OGRPGDataSource *,
                                          CPLString& osCurrentSchema,
Index: ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp
===================================================================
--- ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp	(révision 20540)
+++ ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp	(copie de travail)
@@ -82,7 +82,11 @@
     pszSqlGeomParentTableName = NULL;
 
     bHasWarnedIncompatibleGeom = FALSE;
+    bHasWarnedAlreadySetFID = FALSE;
 
+    /* Just in provision for people yelling about broken backward compatibility ... */
+    bRetrieveFID = CSLTestBoolean(CPLGetConfigOption("OGR_PG_RETRIEVE_FID", "TRUE"));
+
 /* -------------------------------------------------------------------- */
 /*      Build the layer defn name.                                      */
 /* -------------------------------------------------------------------- */
@@ -1368,6 +1372,10 @@
         bNeedComma = TRUE;
     }
 
+    /* REMOVE ME ? */
+    /* Hum, this is weird if we create a feature with an already existings */
+    /* FID ! I see this code path is never taken in the coverage analysis of */
+    /* the autotest suite, so we could probably remove it ? */
     if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL )
     {
         if( bNeedComma )
@@ -1450,6 +1458,10 @@
             osCommand += "''";
     }
 
+    /* REMOVE ME ? */
+    /* Hum, this is weird if we create a feature with an already existings */
+    /* FID ! I see this code path is never taken in the coverage analysis of */
+    /* the autotest suite, so we could probably remove it ? */
     /* Set the FID */
     if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL )
     {
@@ -1475,16 +1487,47 @@
 
     osCommand += ")";
 
+    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 += pszFIDColumn;
+    }
+
 /* -------------------------------------------------------------------- */
 /*      Execute the insert.                                             */
 /* -------------------------------------------------------------------- */
     hResult = PQexec(hPGConn, osCommand);
-    if( PQresultStatus(hResult) != PGRES_COMMAND_OK )
+    if (bReturnRequested && PQresultStatus(hResult) == PGRES_TUPLES_OK &&
+        PQntuples(hResult) == 1 && PQnfields(hResult) == 1 )
     {
+        const char* pszFID = PQgetvalue(hResult, 0, 0 );
+        long nFID = atol(pszFID);
+        poFeature->SetFID(nFID);
+    }
+    else if( bReturnRequested || PQresultStatus(hResult) != PGRES_COMMAND_OK )
+    {
         CPLError( CE_Failure, CPLE_AppDefined,
                   "INSERT command for new feature failed.\n%s\nCommand: %s",
                   PQerrorMessage(hPGConn), osCommand.c_str() );
 
+        if( !bHasWarnedAlreadySetFID && poFeature->GetFID() != OGRNullFID &&
+            pszFIDColumn != NULL )
+        {
+            bHasWarnedAlreadySetFID = TRUE;
+            CPLError(CE_Warning, CPLE_AppDefined,
+                    "You've inserted feature with an already set FID and that's perhaps the reason for the failure. "
+                    "If so, this can happen if you reuse the same feature object for sequential insertions. "
+                    "Indeed, since GDAL 1.8.0, the FID of an inserted feature is got from the server, so it is not a good idea"
+                    "to reuse it afterwards... All in all, try unsetting the FID with SetFID(-1) before calling CreateFeature()");
+        }
+
         OGRPGClearResult( hResult );
 
         poDS->SoftRollback();
@@ -1492,13 +1535,6 @@
         return OGRERR_FAILURE;
     }
 
-#ifdef notdef
-    /* Should we use this oid to get back the FID and assign back to the
-       feature?  I think we are supposed to. */
-    Oid nNewOID = PQoidValue( hResult );
-    printf( "nNewOID = %d\n", (int) nNewOID );
-#endif
-
     OGRPGClearResult( hResult );
 
     return poDS->SoftCommit();
Index: ../autotest/ogr/ogr_pg.py
===================================================================
--- ../autotest/ogr/ogr_pg.py	(révision 20508)
+++ ../autotest/ogr/ogr_pg.py	(copie de travail)
@@ -255,6 +255,7 @@
     
         dst_feat.SetGeometryDirectly( geom )
         dst_feat.SetField( 'PRFEDEA', item )
+        dst_feat.SetFID(-1)
         gdaltest.pg_lyr.CreateFeature( dst_feat )
         
         ######################################################################
@@ -2434,6 +2435,7 @@
             dst_feat.SetField( 'SHORTNAME', option )
             if bHasSetFieldDoubleList:
                 dst_feat.SetFieldDoubleList( feature_def.GetFieldIndex('REALLIST'), [float(value), float(value)])
+            dst_feat.SetFID(-1)
             gdaltest.pg_lyr.CreateFeature( dst_feat )
 
     gdal.SetConfigOption( 'PG_USE_COPY', 'NO' )

