<H4><ANAME="4.16.2">4.16.2</A>) How do I get the back the generated SERIAL value after an insert?</H4><P>
<H4><ANAME="4.16.2">4.16.2</A>) How do I get the value of a
<small>SERIAL</small> insert?</H4><P>
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:
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>
<PRE>
$newSerialID = nextval('person_id_seq');
$newSerialID = nextval('person_id_seq');
...
@@ -1056,35 +1060,37 @@ Alternatively, you could retrieve the just-assigned SERIAL value with the <I>cur
...
@@ -1056,35 +1060,37 @@ Alternatively, you could retrieve the just-assigned SERIAL value with the <I>cur
INSERT INTO person (name) VALUES ('Blaise Pascal');
INSERT INTO person (name) VALUES ('Blaise Pascal');
$newID = currval('person_id_seq');
$newID = currval('person_id_seq');
</PRE>
</PRE>
Finally, you could use the <AHREF="#4.17">oid</A> returned from the
Finally, you could use the <AHREF="#4.17"><small>OID</small></A> returned from the
INSERT statement to lookup the default value, though this is probably
INSERT statement to lookup the default value, though this is probably
the least portable approach. In perl, using DBI with Edmund Mergl's
the least portable approach. In perl, using DBI with Edmund Mergl's
DBD::Pg module, the oid value is made available via
DBD::Pg module, the oid value is made available via
<I>$sth->{pg_oid_status} after $sth->execute().</I>
<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
<H4><ANAME="4.16.3">4.16.3</A>) Don't <I>currval()</I> and <I>nextval()</I> lead to
concurrent backend processes?</H4><P>
a race condition with other users?</H4><P>
No. This is 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>
<H4><ANAME="4.17">4.17</A>) What is an <small>OID</small>? What is a
<small>TID</small>?</H4><P>
<small>OID</small>s are PostgreSQL's answer to unique row ids. Every row that is
<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
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
<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
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
<small>OID</small>s are unique not only within a table, or database, but unique within
the entire PostgreSQL installation.<P>
the entire PostgreSQL installation.<P>
PostgreSQL uses <small>oid</small>s in its internal system tables to link rows between
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
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>
in joins. It is recommended you use column type <small>OID</small> to
values. You can create an index on the <small>oid</small> field for faster access.<P>
store <small>OID</small>
values. You can create an index on the <small>OID</small> field for faster access.<P>
<small>Oid</small>s are assigned to all new rows from a central area that is used by
O<small>id</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
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
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:
no reason you can't do it:
<PRE>
<PRE>
...
@@ -1099,7 +1105,7 @@ no reason you can't do it:
...
@@ -1099,7 +1105,7 @@ no reason you can't do it:
-->
-->
</PRE><P>
</PRE><P>
Tids are used to identify specific physical rows with block and offset
T<small>ID</small>s are used to identify specific physical rows with block and offset
values. Tids change after rows are modified or reloaded. They are used
values. Tids change after rows are modified or reloaded. They are used
by index entries to point to physical rows.<P>
by index entries to point to physical rows.<P>
...
@@ -1117,17 +1123,17 @@ common usage. Here are some:
...
@@ -1117,17 +1123,17 @@ common usage. Here are some:
<LI> retrieve, select
<LI> retrieve, select
<LI> replace, update
<LI> replace, update
<LI> append, insert
<LI> append, insert
<LI>oid, serial value
<LI><small>OID</small>, serial value
<LI> portal, cursor
<LI> portal, cursor
<LI> range variable, table name, table alias
<LI> range variable, table name, table alias
</UL><P>
</UL><P>
<H4><ANAME="4.19">4.19</A>) Why do I get the error "FATAL: palloc
<H4><ANAME="4.19">4.19</A>) Why do I get the error <I>"FATAL: palloc
failure: memory exhausted?"<BR></H4><P>
failure: memory exhausted?"</I><BR></H4><P>
It is possible you have run out of virtual memory on your system, or
It is possible you have run out of virtual memory on your system, or
your kernel has a low limit for certain resources. Try this before
your kernel has a low limit for certain resources. Try this before
starting the postmaster:
starting the <i>postmaster:</i>
<PRE>
<PRE>
ulimit -d 65536
ulimit -d 65536
...
@@ -1169,12 +1175,11 @@ Use <i>now()</i>:
...
@@ -1169,12 +1175,11 @@ Use <i>now()</i>:
CREATE TABLE test (x int, modtime timestamp default now() );
CREATE TABLE test (x int, modtime timestamp default now() );
</PRE></CODE>
</PRE></CODE>
<P>
<P>
<H4><ANAME="4.23">4.23</A>) Why are my subqueries using<CODE>IN</CODE> so
<H4><ANAME="4.23">4.23</A>) Why are my subqueries using
slow?<BR></H4><P>
<CODE><small>IN</small></CODE> so slow?<BR></H4><P>
Currently, we join subqueries to outer queries by sequential scanning
Currently, we join subqueries to outer queries by sequential scanning
the result of the subquery for each row of the outer query. A workaround
the result of the subquery for each row of the outer query. A workaround
is to replace <CODE>IN</CODE> with <CODE>EXISTS</CODE>. For example,
is to replace <CODE>IN</CODE> with <CODE>EXISTS</CODE>:
change:
<CODE><PRE>
<CODE><PRE>
SELECT *
SELECT *
FROM tab
FROM tab
...
@@ -1215,8 +1220,8 @@ I run it in <I>psql,</I> why does it dump core?</H4><P>
...
@@ -1215,8 +1220,8 @@ I run it in <I>psql,</I> why does it dump core?</H4><P>
The problem could be a number of things. Try testing your user-defined
The problem could be a number of things. Try testing your user-defined
function in a stand alone test program first.
function in a stand alone test program first.
<H4><ANAME="5.2">5.2</A>) What does the message:
<H4><ANAME="5.2">5.2</A>) What does the message
<I>NOTICE:PortalHeapMemoryFree: 0x402251d0 not in alloc set!</I> mean?</H4><P>
<I>"NOTICE:PortalHeapMemoryFree: 0x402251d0 not in alloc set!"</I> mean?</H4><P>
You are <I>pfree'ing</I> something that was not <I>palloc'ed.</I>
You are <I>pfree'ing</I> something that was not <I>palloc'ed.</I>
Beware of mixing <I>malloc/free</I> and <I>palloc/pfree.</I>
Beware of mixing <I>malloc/free</I> and <I>palloc/pfree.</I>
...
@@ -1237,12 +1242,10 @@ This requires wizardry so extreme that the authors have never
...
@@ -1237,12 +1242,10 @@ This requires wizardry so extreme that the authors have never
tried it, though in principle it can be done.<P>
tried it, though in principle it can be done.<P>
<H4><ANAME="5.5">5.5</A>) I have changed a source file. Why does the
<H4><ANAME="5.5">5.5</A>) I have changed a source file. Why does the
recompile does not see the change?</H4><P>
recompile not see the change?</H4><P>
The Makefiles do not have the proper dependencies for include files. You
The <I>Makefiles</I> do not have the proper dependencies for include files. You
have to do a <I>make clean</I> and then another <I>make</I>.
have to do a <I>make clean</I> and then another <I>make</I>.<P>
You
have to do a <I>make clean</I> and then another <I>make.</I><P>