mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Update FAQ by eliminating non-frequent items like large objects and
extending questions. Update wording of various entries.
This commit is contained in:
111
doc/FAQ
111
doc/FAQ
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
Frequently Asked Questions (FAQ) for PostgreSQL
|
Frequently Asked Questions (FAQ) for PostgreSQL
|
||||||
|
|
||||||
Last updated: Sat Apr 23 14:59:01 EDT 2005
|
Last updated: Sat Apr 23 16:49:43 EDT 2005
|
||||||
|
|
||||||
Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
|
Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
|
||||||
|
|
||||||
@ -67,29 +67,17 @@
|
|||||||
4.11.3) Doesn't currval() lead to a race condition with other users?
|
4.11.3) Doesn't currval() lead to a race condition with other users?
|
||||||
4.11.4) Why aren't my sequence numbers reused on transaction abort?
|
4.11.4) Why aren't my sequence numbers reused on transaction abort?
|
||||||
Why are there gaps in the numbering of my sequence/SERIAL column?
|
Why are there gaps in the numbering of my sequence/SERIAL column?
|
||||||
4.12) What is an OID? What is a TID?
|
4.12) What is an OID? What is a CTID?
|
||||||
4.13) Why do I get the error "ERROR: Memory exhausted in
|
4.13) Why do I get the error "ERROR: Memory exhausted in
|
||||||
AllocSetAlloc()"?
|
AllocSetAlloc()"?
|
||||||
4.14) How do I tell what PostgreSQL version I am running?
|
4.14) How do I tell what PostgreSQL version I am running?
|
||||||
4.15) Why does my large-object operations get "invalid large obj
|
4.15) How do I create a column that will default to the current time?
|
||||||
descriptor"?
|
4.16) How do I perform an outer join?
|
||||||
4.16) How do I create a column that will default to the current time?
|
4.17) How do I perform queries using multiple databases?
|
||||||
4.17) How do I perform an outer join?
|
4.18) How do I return multiple rows or columns from a function?
|
||||||
4.18) How do I perform queries using multiple databases?
|
4.19) Why do I get "relation with OID ##### does not exist" errors
|
||||||
4.19) How do I return multiple rows or columns from a function?
|
|
||||||
4.20) Why do I get "relation with OID ##### does not exist" errors
|
|
||||||
when accessing temporary tables in PL/PgSQL functions?
|
when accessing temporary tables in PL/PgSQL functions?
|
||||||
4.21) What encryption options are available?
|
4.20) What encryption options are available?
|
||||||
|
|
||||||
Extending PostgreSQL
|
|
||||||
|
|
||||||
5.1) I wrote a user-defined function. When I run it in psql, why does
|
|
||||||
it dump core?
|
|
||||||
5.2) How can I contribute some nifty new types and functions to
|
|
||||||
PostgreSQL?
|
|
||||||
5.3) How do I write a C function to return a tuple?
|
|
||||||
5.4) I have changed a source file. Why does the recompile not see the
|
|
||||||
change?
|
|
||||||
_________________________________________________________________
|
_________________________________________________________________
|
||||||
|
|
||||||
General Questions
|
General Questions
|
||||||
@ -529,8 +517,8 @@
|
|||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
If you believe the optimizer is incorrect in choosing a sequential
|
If you believe the optimizer is incorrect in choosing a sequential
|
||||||
scan, use SET enable_seqscan TO 'off' and run tests to see if an index
|
scan, use SET enable_seqscan TO 'off' and run query again to see if an
|
||||||
scan is indeed faster.
|
index scan is indeed faster.
|
||||||
|
|
||||||
When using wild-card operators such as LIKE or ~, indexes can only be
|
When using wild-card operators such as LIKE or ~, indexes can only be
|
||||||
used in certain circumstances:
|
used in certain circumstances:
|
||||||
@ -576,13 +564,13 @@
|
|||||||
4.9) In a query, how do I detect if a field is NULL? How can I sort on
|
4.9) In a query, how do I detect if a field is NULL? How can I sort on
|
||||||
whether a field is NULL or not?
|
whether a field is NULL or not?
|
||||||
|
|
||||||
You test the column with IS NULL and IS NOT NULL.
|
You test the column with IS NULL and IS NOT NULL, like this:
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM tab
|
FROM tab
|
||||||
WHERE col IS NULL;
|
WHERE col IS NULL;
|
||||||
|
|
||||||
To sort by the NULL status, use the IS NULL and IS NOT NULL modifiers
|
To sort by the NULL status, use the IS NULL and IS NOT NULL modifiers
|
||||||
in your WHERE clause. Things that are true will sort higher than
|
in your ORDER BY clause. Things that are true will sort higher than
|
||||||
things that are false, so the following will put NULL entries at the
|
things that are false, so the following will put NULL entries at the
|
||||||
top of the resulting list:
|
top of the resulting list:
|
||||||
SELECT *
|
SELECT *
|
||||||
@ -603,9 +591,9 @@
|
|||||||
|
|
||||||
The first four types above are "varlena" types (i.e., the first four
|
The first four types above are "varlena" types (i.e., the first four
|
||||||
bytes on disk are the length, followed by the data). Thus the actual
|
bytes on disk are the length, followed by the data). Thus the actual
|
||||||
space used is slightly greater than the declared size. However, these
|
space used is slightly greater than the declared size. However, long
|
||||||
data types are also subject to compression or being stored out-of-line
|
values are also subject to compression, so the space on disk might
|
||||||
by TOAST, so the space on disk might also be less than expected.
|
also be less than expected.
|
||||||
VARCHAR(n) is best when storing variable-length strings and it limits
|
VARCHAR(n) is best when storing variable-length strings and it limits
|
||||||
how long a string can be. TEXT is for strings of unlimited length,
|
how long a string can be. TEXT is for strings of unlimited length,
|
||||||
with a maximum of one gigabyte.
|
with a maximum of one gigabyte.
|
||||||
@ -647,8 +635,8 @@
|
|||||||
You would then also have the new value stored in new_id for use in
|
You would then also have the new value stored in new_id for use in
|
||||||
other queries (e.g., as a foreign key to the person table). Note that
|
other queries (e.g., as a foreign key to the person table). Note that
|
||||||
the name of the automatically created SEQUENCE object will be named
|
the name of the automatically created SEQUENCE object will be named
|
||||||
<table>_<serialcolumn>_seq, where table and serialcolumn are the names
|
<table>_< serialcolumn>_seq, where table and serialcolumn are the
|
||||||
of your table and your SERIAL column, respectively.
|
names of your table and your SERIAL column, respectively.
|
||||||
|
|
||||||
Alternatively, you could retrieve the assigned SERIAL value with the
|
Alternatively, you could retrieve the assigned SERIAL value with the
|
||||||
currval() function after it was inserted by default, e.g.,
|
currval() function after it was inserted by default, e.g.,
|
||||||
@ -667,7 +655,7 @@
|
|||||||
transactions as needed and are not locked until the transaction
|
transactions as needed and are not locked until the transaction
|
||||||
completes. This causes gaps in numbering from aborted transactions.
|
completes. This causes gaps in numbering from aborted transactions.
|
||||||
|
|
||||||
4.12) What is an OID? What is a TID?
|
4.12) What is an OID? What is a CTID?
|
||||||
|
|
||||||
Every row that is created in PostgreSQL gets a unique OID unless
|
Every row that is created in PostgreSQL gets a unique OID unless
|
||||||
created WITHOUT OIDS. OIDs are autotomatically assigned unique 4-byte
|
created WITHOUT OIDS. OIDs are autotomatically assigned unique 4-byte
|
||||||
@ -680,9 +668,9 @@
|
|||||||
single table. and are therefore less likely to overflow. SERIAL8 is
|
single table. and are therefore less likely to overflow. SERIAL8 is
|
||||||
available for storing eight-byte sequence values.
|
available for storing eight-byte sequence values.
|
||||||
|
|
||||||
TIDs are used to identify specific physical rows with block and offset
|
CTIDs are used to identify specific physical rows with block and
|
||||||
values. TIDs change after rows are modified or reloaded. They are used
|
offset values. CTIDs change after rows are modified or reloaded. They
|
||||||
by index entries to point to physical rows.
|
are used by index entries to point to physical rows.
|
||||||
|
|
||||||
4.13) Why do I get the error "ERROR: Memory exhausted in AllocSetAlloc()"?
|
4.13) Why do I get the error "ERROR: Memory exhausted in AllocSetAlloc()"?
|
||||||
|
|
||||||
@ -703,26 +691,12 @@
|
|||||||
|
|
||||||
From psql, type SELECT version();
|
From psql, type SELECT version();
|
||||||
|
|
||||||
4.15) Why does my large-object operations get "invalid large obj descriptor"?
|
4.15) How do I create a column that will default to the current time?
|
||||||
|
|
||||||
You need to put BEGIN WORK and COMMIT around any use of a large object
|
|
||||||
handle, that is, surrounding lo_open ... lo_close.
|
|
||||||
|
|
||||||
Currently PostgreSQL enforces the rule by closing large object handles
|
|
||||||
at transaction commit. So the first attempt to do anything with the
|
|
||||||
handle will draw invalid large obj descriptor. So code that used to
|
|
||||||
work (at least most of the time) will now generate that error message
|
|
||||||
if you fail to use a transaction.
|
|
||||||
|
|
||||||
If you are using a client interface like ODBC you may need to set
|
|
||||||
auto-commit off.
|
|
||||||
|
|
||||||
4.16) How do I create a column that will default to the current time?
|
|
||||||
|
|
||||||
Use CURRENT_TIMESTAMP:
|
Use CURRENT_TIMESTAMP:
|
||||||
CREATE TABLE test (x int, modtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
|
CREATE TABLE test (x int, modtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
|
||||||
|
|
||||||
4.17) How do I perform an outer join?
|
4.16) How do I perform an outer join?
|
||||||
|
|
||||||
PostgreSQL supports outer joins using the SQL standard syntax. Here
|
PostgreSQL supports outer joins using the SQL standard syntax. Here
|
||||||
are two examples:
|
are two examples:
|
||||||
@ -740,7 +714,7 @@
|
|||||||
is assumed in LEFT, RIGHT, and FULL joins. Ordinary joins are called
|
is assumed in LEFT, RIGHT, and FULL joins. Ordinary joins are called
|
||||||
INNER joins.
|
INNER joins.
|
||||||
|
|
||||||
4.18) How do I perform queries using multiple databases?
|
4.17) How do I perform queries using multiple databases?
|
||||||
|
|
||||||
There is no way to query a database other than the current one.
|
There is no way to query a database other than the current one.
|
||||||
Because PostgreSQL loads database-specific system catalogs, it is
|
Because PostgreSQL loads database-specific system catalogs, it is
|
||||||
@ -750,13 +724,13 @@
|
|||||||
course, a client can also make simultaneous connections to different
|
course, a client can also make simultaneous connections to different
|
||||||
databases and merge the results on the client side.
|
databases and merge the results on the client side.
|
||||||
|
|
||||||
4.19) How do I return multiple rows or columns from a function?
|
4.18) How do I return multiple rows or columns from a function?
|
||||||
|
|
||||||
It is easy using set-returning functions,
|
It is easy using set-returning functions,
|
||||||
http://techdocs.postgresql.org/guides/SetReturningFunctions
|
http://techdocs.postgresql.org/guides/SetReturningFunctions
|
||||||
.
|
.
|
||||||
|
|
||||||
4.20) Why do I get "relation with OID ##### does not exist" errors when
|
4.19) Why do I get "relation with OID ##### does not exist" errors when
|
||||||
accessing temporary tables in PL/PgSQL functions?
|
accessing temporary tables in PL/PgSQL functions?
|
||||||
|
|
||||||
PL/PgSQL caches function scripts, and an unfortunate side effect is
|
PL/PgSQL caches function scripts, and an unfortunate side effect is
|
||||||
@ -767,7 +741,7 @@
|
|||||||
table access in PL/PgSQL. This will cause the query to be reparsed
|
table access in PL/PgSQL. This will cause the query to be reparsed
|
||||||
every time.
|
every time.
|
||||||
|
|
||||||
4.21) What encryption options are available?
|
4.20) What encryption options are available?
|
||||||
|
|
||||||
* contrib/pgcrypto contains many encryption functions for use in SQL
|
* contrib/pgcrypto contains many encryption functions for use in SQL
|
||||||
queries.
|
queries.
|
||||||
@ -779,33 +753,4 @@
|
|||||||
or ssh, rather than PostgreSQL's native SSL connections.)
|
or ssh, rather than PostgreSQL's native SSL connections.)
|
||||||
* Database user passwords are automatically encrypted when stored in
|
* Database user passwords are automatically encrypted when stored in
|
||||||
the system tables.
|
the system tables.
|
||||||
* The server can run using an encrypted file system.
|
* The server can also run using an encrypted file system.
|
||||||
_________________________________________________________________
|
|
||||||
|
|
||||||
Extending PostgreSQL
|
|
||||||
|
|
||||||
5.1) I wrote a user-defined function. When I run it in psql, why does it dump
|
|
||||||
core?
|
|
||||||
|
|
||||||
The problem could be a number of things. Try testing your user-defined
|
|
||||||
function in a stand-alone test program first.
|
|
||||||
|
|
||||||
5.2) How can I contribute some nifty new types and functions to PostgreSQL?
|
|
||||||
|
|
||||||
Send your extensions to the pgsql-hackers mailing list, and they will
|
|
||||||
eventually end up in the contrib/ subdirectory.
|
|
||||||
|
|
||||||
5.3) How do I write a C function to return a tuple?
|
|
||||||
|
|
||||||
In versions of PostgreSQL beginning with 7.3, table-returning
|
|
||||||
functions are fully supported in C, PL/PgSQL, and SQL. See the
|
|
||||||
Programmer's Guide for more information. An example of a
|
|
||||||
table-returning function defined in C can be found in
|
|
||||||
contrib/tablefunc.
|
|
||||||
|
|
||||||
5.4) I have changed a source file. Why does the recompile not see the change?
|
|
||||||
|
|
||||||
The Makefiles do not have the proper dependencies for include files.
|
|
||||||
You have to do a make clean and then another make. If you are using
|
|
||||||
GCC you can use the --enable-depend option of configure to have the
|
|
||||||
compiler compute the dependencies automatically.
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
alink="#0000ff">
|
alink="#0000ff">
|
||||||
<H1>Frequently Asked Questions (FAQ) for PostgreSQL</H1>
|
<H1>Frequently Asked Questions (FAQ) for PostgreSQL</H1>
|
||||||
|
|
||||||
<P>Last updated: Sat Apr 23 14:59:01 EDT 2005</P>
|
<P>Last updated: Sat Apr 23 16:49:43 EDT 2005</P>
|
||||||
|
|
||||||
<P>Current maintainer: Bruce Momjian (<A href=
|
<P>Current maintainer: Bruce Momjian (<A href=
|
||||||
"mailto:pgman@candle.pha.pa.us">pgman@candle.pha.pa.us</A>)
|
"mailto:pgman@candle.pha.pa.us">pgman@candle.pha.pa.us</A>)
|
||||||
@ -99,36 +99,24 @@
|
|||||||
reused on transaction abort? Why are there gaps in the numbering of
|
reused on transaction abort? Why are there gaps in the numbering of
|
||||||
my sequence/SERIAL column?<BR>
|
my sequence/SERIAL column?<BR>
|
||||||
<A href="#4.12">4.12</A>) What is an <SMALL>OID</SMALL>? What is a
|
<A href="#4.12">4.12</A>) What is an <SMALL>OID</SMALL>? What is a
|
||||||
<SMALL>TID</SMALL>?<BR>
|
<SMALL>CTID</SMALL>?<BR>
|
||||||
<A href="#4.12">4.13</A>) Why do I get the error <I>"ERROR: Memory
|
<A href="#4.13">4.13</A>) Why do I get the error <I>"ERROR: Memory
|
||||||
exhausted in AllocSetAlloc()"</I>?<BR>
|
exhausted in AllocSetAlloc()"</I>?<BR>
|
||||||
<A href="#4.14">4.14</A>) How do I tell what PostgreSQL version I
|
<A href="#4.14">4.14</A>) How do I tell what PostgreSQL version I
|
||||||
am running?<BR>
|
am running?<BR>
|
||||||
<A href="#4.15">4.15</A>) Why does my large-object operations get
|
<A href="#4.15">4.15</A>) How do I create a column that will
|
||||||
<I>"invalid large obj descriptor"</I>?<BR>
|
|
||||||
<A href="#4.16">4.16</A>) How do I create a column that will
|
|
||||||
default to the current time?<BR>
|
default to the current time?<BR>
|
||||||
<A href="#4.17">4.17</A>) How do I perform an outer join?<BR>
|
<A href="#4.16">4.16</A>) How do I perform an outer join?<BR>
|
||||||
<A href="#4.18">4.18</A>) How do I perform queries using multiple
|
<A href="#4.17">4.17</A>) How do I perform queries using multiple
|
||||||
databases?<BR>
|
databases?<BR>
|
||||||
<A href="#4.19">4.19</A>) How do I return multiple rows or columns
|
<A href="#4.18">4.18</A>) How do I return multiple rows or columns
|
||||||
from a function?<BR>
|
from a function?<BR>
|
||||||
<A href="#4.20">4.20</A>) Why do I get "relation with OID #####
|
<A href="#4.19">4.19</A>) Why do I get "relation with OID #####
|
||||||
does not exist" errors when accessing temporary tables in PL/PgSQL
|
does not exist" errors when accessing temporary tables in PL/PgSQL
|
||||||
functions?<BR>
|
functions?<BR>
|
||||||
<A href="#4.21">4.21</A>) What encryption options are available?<BR>
|
<A href="#4.20">4.20</A>) What encryption options are available?<BR>
|
||||||
|
|
||||||
|
|
||||||
<H2 align="center">Extending PostgreSQL</H2>
|
|
||||||
<A href="#5.1">5.1</A>) I wrote a user-defined function. When I run
|
|
||||||
it in <I>psql</I>, why does it dump core?<BR>
|
|
||||||
<A href="#5.2">5.2</A>) How can I contribute some nifty new types
|
|
||||||
and functions to PostgreSQL?<BR>
|
|
||||||
<A href="#5.3">5.3</A>) How do I write a C function to return a
|
|
||||||
tuple?<BR>
|
|
||||||
<A href="#5.4">5.4</A>) I have changed a source file. Why does the
|
|
||||||
recompile not see the change?<BR>
|
|
||||||
|
|
||||||
<HR>
|
<HR>
|
||||||
|
|
||||||
<H2 align="center">General Questions</H2>
|
<H2 align="center">General Questions</H2>
|
||||||
@ -683,7 +671,7 @@ table?</TD><TD>unlimited</TD></TR>
|
|||||||
|
|
||||||
<P>If you believe the optimizer is incorrect in choosing a
|
<P>If you believe the optimizer is incorrect in choosing a
|
||||||
sequential scan, use <CODE>SET enable_seqscan TO 'off'</CODE> and
|
sequential scan, use <CODE>SET enable_seqscan TO 'off'</CODE> and
|
||||||
run tests to see if an index scan is indeed faster.</P>
|
run query again to see if an index scan is indeed faster.</P>
|
||||||
|
|
||||||
<P>When using wild-card operators such as <SMALL>LIKE</SMALL> or
|
<P>When using wild-card operators such as <SMALL>LIKE</SMALL> or
|
||||||
<I>~</I>, indexes can only be used in certain circumstances:</P>
|
<I>~</I>, indexes can only be used in certain circumstances:</P>
|
||||||
@ -733,7 +721,6 @@ table?</TD><TD>unlimited</TD></TR>
|
|||||||
FROM tab
|
FROM tab
|
||||||
WHERE lower(col) = 'abc';
|
WHERE lower(col) = 'abc';
|
||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
This will not use an standard index. However, if you create a
|
This will not use an standard index. However, if you create a
|
||||||
expresssion index, it will be used:
|
expresssion index, it will be used:
|
||||||
<PRE>
|
<PRE>
|
||||||
@ -745,7 +732,7 @@ table?</TD><TD>unlimited</TD></TR>
|
|||||||
NULL</SMALL> or not?</H3>
|
NULL</SMALL> or not?</H3>
|
||||||
|
|
||||||
<P>You test the column with <SMALL>IS NULL</SMALL> and <SMALL>IS
|
<P>You test the column with <SMALL>IS NULL</SMALL> and <SMALL>IS
|
||||||
NOT NULL</SMALL>.</P>
|
NOT NULL</SMALL>, like this:</P>
|
||||||
|
|
||||||
<PRE>
|
<PRE>
|
||||||
SELECT *
|
SELECT *
|
||||||
@ -754,7 +741,7 @@ table?</TD><TD>unlimited</TD></TR>
|
|||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
<P>To sort by the <SMALL>NULL</SMALL> status, use the <SMALL>IS NULL</SMALL>
|
<P>To sort by the <SMALL>NULL</SMALL> status, use the <SMALL>IS NULL</SMALL>
|
||||||
and <SMALL>IS NOT NULL</SMALL> modifiers in your <SMALL>WHERE</SMALL> clause.
|
and <SMALL>IS NOT NULL</SMALL> modifiers in your <SMALL>ORDER BY</SMALL> clause.
|
||||||
Things that are <I>true</I> will sort higher than things that are <I>false</I>,
|
Things that are <I>true</I> will sort higher than things that are <I>false</I>,
|
||||||
so the following will put NULL entries at the top of the resulting list:</P>
|
so the following will put NULL entries at the top of the resulting list:</P>
|
||||||
|
|
||||||
@ -787,9 +774,8 @@ length</TD></TR>
|
|||||||
<P>The first four types above are "varlena" types (i.e., the first
|
<P>The first four types above are "varlena" types (i.e., the first
|
||||||
four bytes on disk are the length, followed by the data). Thus the
|
four bytes on disk are the length, followed by the data). Thus the
|
||||||
actual space used is slightly greater than the declared size.
|
actual space used is slightly greater than the declared size.
|
||||||
However, these data types are also subject to compression or being
|
However, long values are also subject to compression, so the space
|
||||||
stored out-of-line by <SMALL>TOAST</SMALL>, so the space on disk
|
on disk might also be less than expected.</P>
|
||||||
might also be less than expected.</P>
|
|
||||||
|
|
||||||
<SMALL>VARCHAR(n)</SMALL> is best when storing variable-length
|
<SMALL>VARCHAR(n)</SMALL> is best when storing variable-length
|
||||||
strings and it limits how long a string can be. <SMALL>TEXT</SMALL>
|
strings and it limits how long a string can be. <SMALL>TEXT</SMALL>
|
||||||
@ -805,8 +791,7 @@ length</TD></TR>
|
|||||||
serial/auto-incrementing field?</H3>
|
serial/auto-incrementing field?</H3>
|
||||||
|
|
||||||
<P>PostgreSQL supports a <SMALL>SERIAL</SMALL> data type. It
|
<P>PostgreSQL supports a <SMALL>SERIAL</SMALL> data type. It
|
||||||
auto-creates a sequence. For example,
|
auto-creates a sequence. For example, this:</P>
|
||||||
this:</P>
|
|
||||||
<PRE>
|
<PRE>
|
||||||
CREATE TABLE person (
|
CREATE TABLE person (
|
||||||
id SERIAL,
|
id SERIAL,
|
||||||
@ -815,11 +800,12 @@ length</TD></TR>
|
|||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
is automatically translated into this:
|
is automatically translated into this:
|
||||||
|
|
||||||
<PRE>
|
<PRE>
|
||||||
CREATE SEQUENCE person_id_seq;
|
CREATE SEQUENCE person_id_seq;
|
||||||
CREATE TABLE person (
|
CREATE TABLE person (
|
||||||
id INT4 NOT NULL DEFAULT nextval('person_id_seq'),
|
id INT4 NOT NULL DEFAULT nextval('person_id_seq'),
|
||||||
name TEXT
|
name TEXT
|
||||||
);
|
);
|
||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
@ -839,17 +825,17 @@ length</TD></TR>
|
|||||||
execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
|
execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
|
||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
You would then also have the new value stored in
|
You would then also have the new value stored in <CODE>new_id</CODE>
|
||||||
<CODE>new_id</CODE> for use in other queries (e.g., as a foreign
|
for use in other queries (e.g., as a foreign key to the <CODE>person
|
||||||
key to the <CODE>person</CODE> table). Note that the name of the
|
</CODE> table). Note that the name of the automatically created
|
||||||
automatically created <SMALL>SEQUENCE</SMALL> object will be named
|
<SMALL>SEQUENCE</SMALL> object will be named <<I>table</I>>_<<I>
|
||||||
<<I>table</I>>_<<I>serialcolumn</I>>_<I>seq</I>, where
|
serialcolumn</I>>_<I>seq</I>, where <I>table</I> and <I>serialcolumn</I>
|
||||||
<I>table</I> and <I>serialcolumn</I> are the names of your table
|
are the names of your table and your <SMALL>SERIAL</SMALL> column,
|
||||||
and your <SMALL>SERIAL</SMALL> column, respectively.
|
respectively.
|
||||||
|
|
||||||
<P>Alternatively, you could retrieve the assigned
|
<P>Alternatively, you could retrieve the assigned <SMALL>SERIAL</SMALL>
|
||||||
<SMALL>SERIAL</SMALL> value with the <I>currval()</I> function
|
value with the <I>currval()</I> function <I>after</I> it was inserted by
|
||||||
<I>after</I> it was inserted by default, e.g.,</P>
|
default, e.g.,</P>
|
||||||
<PRE>
|
<PRE>
|
||||||
execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
|
execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
|
||||||
new_id = execute("SELECT currval('person_id_seq')");
|
new_id = execute("SELECT currval('person_id_seq')");
|
||||||
@ -871,7 +857,7 @@ length</TD></TR>
|
|||||||
transactions.</P>
|
transactions.</P>
|
||||||
|
|
||||||
<H3><A name="4.12">4.12</A>) What is an <SMALL>OID</SMALL>? What is
|
<H3><A name="4.12">4.12</A>) What is an <SMALL>OID</SMALL>? What is
|
||||||
a <SMALL>TID</SMALL>?</H3>
|
a <SMALL>CTID</SMALL>?</H3>
|
||||||
|
|
||||||
<P>Every row that is created in PostgreSQL gets a unique
|
<P>Every row that is created in PostgreSQL gets a unique
|
||||||
<SMALL>OID</SMALL> unless created <SMALL>WITHOUT OIDS</SMALL>.
|
<SMALL>OID</SMALL> unless created <SMALL>WITHOUT OIDS</SMALL>.
|
||||||
@ -888,8 +874,8 @@ length</TD></TR>
|
|||||||
<SMALL>SERIAL8</SMALL> is available for storing eight-byte sequence
|
<SMALL>SERIAL8</SMALL> is available for storing eight-byte sequence
|
||||||
values.</P>
|
values.</P>
|
||||||
|
|
||||||
<P>T<SMALL>ID</SMALL>s are used to identify specific physical rows
|
<P>C<SMALL>TID</SMALL>s are used to identify specific physical rows
|
||||||
with block and offset values. T<SMALL>ID</SMALL>s change after rows
|
with block and offset values. C<SMALL>TID</SMALL>s change after rows
|
||||||
are modified or reloaded. They are used by index entries to point
|
are modified or reloaded. They are used by index entries to point
|
||||||
to physical rows.</P>
|
to physical rows.</P>
|
||||||
|
|
||||||
@ -917,23 +903,7 @@ length</TD></TR>
|
|||||||
|
|
||||||
<P>From <I>psql</I>, type <CODE>SELECT version();</CODE></P>
|
<P>From <I>psql</I>, type <CODE>SELECT version();</CODE></P>
|
||||||
|
|
||||||
<H3><A name="4.15">4.15</A>) Why does my large-object operations
|
<H3><A name="4.15">4.15</A>) How do I create a column that will
|
||||||
get <I>"invalid large obj descriptor"</I>?</H3>
|
|
||||||
|
|
||||||
<P>You need to put <CODE>BEGIN WORK</CODE> and <CODE>COMMIT</CODE>
|
|
||||||
around any use of a large object handle, that is, surrounding
|
|
||||||
<CODE>lo_open</CODE> ... <CODE>lo_close.</CODE></P>
|
|
||||||
|
|
||||||
<P>Currently PostgreSQL enforces the rule by closing large object
|
|
||||||
handles at transaction commit. So the first attempt to do anything
|
|
||||||
with the handle will draw <I>invalid large obj descriptor</I>. So
|
|
||||||
code that used to work (at least most of the time) will now
|
|
||||||
generate that error message if you fail to use a transaction.</P>
|
|
||||||
|
|
||||||
<P>If you are using a client interface like <SMALL>ODBC</SMALL> you
|
|
||||||
may need to set <CODE>auto-commit off.</CODE></P>
|
|
||||||
|
|
||||||
<H3><A name="4.16">4.16</A>) How do I create a column that will
|
|
||||||
default to the current time?</H3>
|
default to the current time?</H3>
|
||||||
|
|
||||||
<P>Use <I>CURRENT_TIMESTAMP</I>:</P>
|
<P>Use <I>CURRENT_TIMESTAMP</I>:</P>
|
||||||
@ -941,7 +911,7 @@ length</TD></TR>
|
|||||||
CREATE TABLE test (x int, modtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
|
CREATE TABLE test (x int, modtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
|
||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
<H3><A name="4.17">4.17</A>) How do I perform an outer join?</H3>
|
<H3><A name="4.16">4.16</A>) How do I perform an outer join?</H3>
|
||||||
|
|
||||||
<P>PostgreSQL supports outer joins using the SQL standard syntax.
|
<P>PostgreSQL supports outer joins using the SQL standard syntax.
|
||||||
Here are two examples:</P>
|
Here are two examples:</P>
|
||||||
@ -964,7 +934,7 @@ length</TD></TR>
|
|||||||
<SMALL>RIGHT</SMALL>, and <SMALL>FULL</SMALL> joins. Ordinary joins
|
<SMALL>RIGHT</SMALL>, and <SMALL>FULL</SMALL> joins. Ordinary joins
|
||||||
are called <SMALL>INNER</SMALL> joins.</P>
|
are called <SMALL>INNER</SMALL> joins.</P>
|
||||||
|
|
||||||
<H3><A name="4.18">4.18</A>) How do I perform queries using
|
<H3><A name="4.17">4.17</A>) How do I perform queries using
|
||||||
multiple databases?</H3>
|
multiple databases?</H3>
|
||||||
|
|
||||||
<P>There is no way to query a database other than the current one.
|
<P>There is no way to query a database other than the current one.
|
||||||
@ -976,14 +946,14 @@ length</TD></TR>
|
|||||||
connections to different databases and merge the results on the
|
connections to different databases and merge the results on the
|
||||||
client side.</P>
|
client side.</P>
|
||||||
|
|
||||||
<H3><A name="4.19">4.19</A>) How do I return multiple rows or
|
<H3><A name="4.18">4.18</A>) How do I return multiple rows or
|
||||||
columns from a function?</H3>
|
columns from a function?</H3>
|
||||||
|
|
||||||
<P>It is easy using set-returning functions,
|
<P>It is easy using set-returning functions,
|
||||||
<a href="http://techdocs.postgresql.org/guides/SetReturningFunctions">
|
<a href="http://techdocs.postgresql.org/guides/SetReturningFunctions">
|
||||||
http://techdocs.postgresql.org/guides/SetReturningFunctions</a></P>.
|
http://techdocs.postgresql.org/guides/SetReturningFunctions</a></P>.
|
||||||
|
|
||||||
<H3><A name="4.20">4.20</A>) Why do I get "relation with OID #####
|
<H3><A name="4.19">4.19</A>) Why do I get "relation with OID #####
|
||||||
does not exist" errors when accessing temporary tables in PL/PgSQL
|
does not exist" errors when accessing temporary tables in PL/PgSQL
|
||||||
functions?</H3>
|
functions?</H3>
|
||||||
|
|
||||||
@ -995,7 +965,7 @@ length</TD></TR>
|
|||||||
<SMALL>EXECUTE</SMALL> for temporary table access in PL/PgSQL. This
|
<SMALL>EXECUTE</SMALL> for temporary table access in PL/PgSQL. This
|
||||||
will cause the query to be reparsed every time.</P>
|
will cause the query to be reparsed every time.</P>
|
||||||
|
|
||||||
<H3><A name="4.21">4.21</A>) What encryption options are available?
|
<H3><A name="4.20">4.20</A>) What encryption options are available?
|
||||||
</H3>
|
</H3>
|
||||||
<UL>
|
<UL>
|
||||||
<LI><I>contrib/pgcrypto</I> contains many encryption functions for
|
<LI><I>contrib/pgcrypto</I> contains many encryption functions for
|
||||||
@ -1009,42 +979,8 @@ length</TD></TR>
|
|||||||
native SSL connections.)</LI>
|
native SSL connections.)</LI>
|
||||||
<LI>Database user passwords are automatically encrypted when stored in
|
<LI>Database user passwords are automatically encrypted when stored in
|
||||||
the system tables.</LI>
|
the system tables.</LI>
|
||||||
<LI>The server can run using an encrypted file system.</LI>
|
<LI>The server can also run using an encrypted file system.</LI>
|
||||||
</UL>
|
</UL>
|
||||||
|
|
||||||
<HR>
|
|
||||||
|
|
||||||
<H2 align="center">Extending PostgreSQL</H2>
|
|
||||||
|
|
||||||
<H3><A name="5.1">5.1</A>) I wrote a user-defined function. When I
|
|
||||||
run it in <I>psql</I>, why does it dump core?</H3>
|
|
||||||
|
|
||||||
<P>The problem could be a number of things. Try testing your
|
|
||||||
user-defined function in a stand-alone test program first.</P>
|
|
||||||
|
|
||||||
<H3><A name="5.2">5.2</A>) How can I contribute some nifty new
|
|
||||||
types and functions to PostgreSQL?</H3>
|
|
||||||
|
|
||||||
<P>Send your extensions to the <I>pgsql-hackers</I> mailing list,
|
|
||||||
and they will eventually end up in the <I>contrib/</I>
|
|
||||||
subdirectory.</P>
|
|
||||||
|
|
||||||
<H3><A name="5.3">5.3</A>) How do I write a C function to return a
|
|
||||||
tuple?</H3>
|
|
||||||
|
|
||||||
<P>In versions of PostgreSQL beginning with 7.3, table-returning
|
|
||||||
functions are fully supported in C, PL/PgSQL, and SQL. See the
|
|
||||||
Programmer's Guide for more information. An example of a
|
|
||||||
table-returning function defined in C can be found in
|
|
||||||
<I>contrib/tablefunc</I>.</P>
|
|
||||||
|
|
||||||
<H3><A name="5.4">5.4</A>) I have changed a source file. Why does
|
|
||||||
the recompile not see the change?</H3>
|
|
||||||
|
|
||||||
<P>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>. If you are using <SMALL>GCC</SMALL> you can use the
|
|
||||||
<I>--enable-depend</I> option of <I>configure</I> to have the
|
|
||||||
compiler compute the dependencies automatically.</P>
|
|
||||||
</BODY>
|
</BODY>
|
||||||
</HTML>
|
</HTML>
|
||||||
|
Reference in New Issue
Block a user