<H4><ANAME="4.16.2">4.16.2</A>) How do I get the back the generated SERIAL value after an insert?</H4><P>
Probably the simplest approach is to to retrieve the next SERIAL value from the sequence object with the <I>nextval()</I> function <I>before</I> inserting and then insert it explicitly. Using the example table in <AHREF="#4.16.1">4.16.1</A>, that might look like this:
One approach is to to retrieve the next SERIAL value from the sequence object with the <I>nextval()</I> function <I>before</I> inserting and then insert it explicitly. Using the example table in <AHREF="#4.16.1">4.16.1</A>, that might look like this:
<PRE>
$newSerialID = nextval('person_id_seq');
INSERT INTO person (id, name) VALUES ($newSerialID, 'Blaise Pascal');
</PRE>
You would then also have the new value stored in <CODE>$newSerialID</CODE> for use in other queries (e.g., as a foreign key to the <CODE>person</CODE> table). Note that the name of the automatically-created SEQUENCE object will be named <<I>table</I>>_<<I>serialcolumn</I>>_<I>seq</I>, where <I>table</I> and <I>serialcolumn</I> are the names of your table and your SERIAL column, respectively.
<P>
Similarly, you could retrieve the just-assigned SERIAL value with the <I>currval</I>() function <I>after</I> it was inserted by default, e.g.,
Alternatively, you could retrieve the just-assigned SERIAL value with the <I>currval</I>() function <I>after</I> it was inserted by default, e.g.,
<PRE>
INSERT INTO person (name) VALUES ('Blaise Pascal');
$newID = currval('person_id_seq');
...
...
@@ -1068,37 +1060,36 @@ Finally, you could use the <A HREF="#4.17">oid</A> returned from the
INSERT statement to lookup the default value, though this is probably
the least portable approach. In perl, using DBI with Edmund Mergl's
DBD::Pg module, the oid value is made available via
$sth->{pg_oid_status} after $sth->execute().
<I>$sth->{pg_oid_status} after $sth->execute().</I>
<H4><ANAME="4.16.3">4.16.3</A>) Don't currval() and nextval() lead to a race condition with other
concurrent backend processes?</H4><P>
No. That has been handled by the backends.
No. This is handled by the backends.
<H4><ANAME="4.17">4.17</A>) What is an oid? What is a tid?</H4><P>
Oids are PostgreSQL's answer to unique row ids. Every row that is
created in PostgreSQL gets a unique oid. All oids generated during
initdb are less than 16384 (from <I>backend/access/transam.h</I>). All
user-created oids are equal or greater that this. By default, all these
oids are unique not only within a table, or database, but unique within
<small>OID</small>s are PostgreSQL's answer to unique row ids. Every row that is
created in PostgreSQL gets a unique <small>oid</small>. All <small>oid</small>s generated during
<I>initdb</I> are less than 16384 (from <I>backend/access/transam.h</I>). All
user-created <small>oid</small>s are equal or greater that this. By default, all these
<small>oid</small>s are unique not only within a table, or database, but unique within
the entire PostgreSQL installation.<P>
PostgreSQL uses oids in its internal system tables to link rows between
tables. These oids can be used to identify specific user rows and used
in joins. It is recommended you use column type oid to store oid
values. See the <I>sql(l)</I> manual page to see the other internal columns.
You can create an index on the oid field for faster access.<P>
PostgreSQL uses <small>oid</small>s in its internal system tables to link rows between
tables. These <small>oid</small>s can be used to identify specific user rows and used
in joins. It is recommended you use column type <small>oid</small> to store <small>oid</small>
values. You can create an index on the <small>oid</small> field for faster access.<P>
Oids are assigned to all new rows from a central area that is used by
all databases. If you want to change the oid to something else, or if
you want to make a copy of the table, with the original oid's, there is
<small>Oid</small>s are assigned to all new rows from a central area that is used by
all databases. If you want to change the <small>oid</small> to something else, or if
you want to make a copy of the table, with the original <small>oid</small>'s, there is
no reason you can't do it:
<PRE>
CREATE TABLE new_table(old_oid oid, mycol int);
SELECT INTO new SELECT old_oid, mycol FROM old;
SELECT old_oid, mycol INTO new FROM old;
COPY new TO '/tmp/pgtable';
DELETE FROM new;
COPY new WITH OIDS FROM '/tmp/pgtable';
...
...
@@ -1162,13 +1153,11 @@ You need to put <CODE>BEGIN WORK</CODE> and <CODE>COMMIT
</CODE> around any use of a large object handle, that is,