Saltar a contenido

OIDS Migration

If you have migrated from PostgresQL version 10 or lower to versions 12 or higher, there is no longer the use and ease of using oids as a means of obtaining the last record entered into a table.

To migrate the existing code, one of the following alternatives must be used.

All these examples are based on the use of the Pg_VB.pm library, but they can be used in a similar way directly on the DBI dbh libraries.

Execute

Indicate in the query of an insert that you want the unique serial key field that you have declared in your table to be returned. As an example in this case: id

$id = $rs->execute("insert into table (name) values ('value') returning id",'id');

This means you must indicate in your query that you want the inserted value of the primary key id to be returned and so that the library knows what value to return it should return.

Using standard DBD libraries

$sth->execute("insert ... returning id");
$nrs = $sth->fetchrow_hashref();
print "my new id = $$nrs{'id'}";

insert

If you use the insert method to insert hash data into the table, you must include the name of the table's serial field in the RETURN_LAST_ID field in the hash data.

$data{'RETURN_LAST_ID'} = 'id';
$oid = $rs->insert('rusers',\%data,1);

Do not migrate

If you do not explicitly migrate your code, consider that the Pg_VB library tries to return the value of the last key inserted anyway, trying to locate which is the primary key of the table.

Use of non-migrated key

Be careful to migrate the code after receiving the generated sequential number, since previous encodings followed the following logic

$oid = $rs->execute($qry);
$rs->excute("select id from table where oid=$oid")

This is already an error because the oid data does not exist or, if it exists, it no longer corresponds to the value returned by the library. You should still migrate the code to

$oid = $rs->execute($qry);
$rs->execute("select id from table where id=$oid")