1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <META name="generator" content="HTML Tidy, see www.w3.org">
5 <META http-equiv="Content-Type" content="text/html; charset=US-ASCII">
6 <TITLE>PostgreSQL FAQ</TITLE>
9 <BODY bgcolor="#ffffff" text="#000000" link="#ff0000" vlink="#a00000"
11 <H1>Frequently Asked Questions (FAQ) for PostgreSQL</H1>
13 <P>Last updated: Sun Nov 14 16:32:47 EST 2004</P>
15 <P>Current maintainer: Bruce Momjian (<A href=
16 "mailto:pgman@candle.pha.pa.us">pgman@candle.pha.pa.us</A>)<BR>
19 <P>The most recent version of this document can be viewed at <A href=
20 "http://www.PostgreSQL.org/docs/faqs/FAQ.html">http://www.PostgreSQL.org/docs/faqs/FAQ.html</A>.</P>
22 <P>Platform-specific questions are answered at <A href=
23 "http://www.PostgreSQL.org/docs/index.html">http://www.PostgreSQL.org/docs/index.html</A>.</P>
26 <H2 align="center">General Questions</H2>
27 <A href="#1.1">1.1</A>) What is PostgreSQL? How is it pronounced?<BR>
28 <A href="#1.2">1.2</A>) What is the copyright on PostgreSQL?<BR>
29 <A href="#1.3">1.3</A>) What Unix platforms does PostgreSQL run
31 <A href="#1.4">1.4</A>) What non-Unix ports are available?<BR>
32 <A href="#1.5">1.5</A>) Where can I get PostgreSQL?<BR>
33 <A href="#1.6">1.6</A>) Where can I get support?<BR>
34 <A href="#1.7">1.7</A>) What is the latest release?<BR>
35 <A href="#1.8">1.8</A>) What documentation is available?<BR>
36 <A href="#1.9">1.9</A>) How do I find out about known bugs or
38 <A href="#1.10">1.10</A>) How can I learn <SMALL>SQL</SMALL>?<BR>
39 <A href="#1.11">1.11</A>) Is PostgreSQL Y2K compliant?<BR>
40 <A href="#1.12">1.12</A>) How do I join the development team?<BR>
41 <A href="#1.13">1.13</A>) How do I submit a bug report?<BR>
42 <A href="#1.14">1.14</A>) How does PostgreSQL compare to other
43 <SMALL>DBMS</SMALL>s?<BR>
44 <A href="#1.15">1.15</A>) How can I financially assist
48 <H2 align="center">User Client Questions</H2>
49 <A href="#2.1">2.1</A>) Are there <SMALL>ODBC</SMALL> drivers for
51 <A href="#2.2">2.2</A>) What tools are available for using
52 PostgreSQL with Web pages?<BR>
53 <A href="#2.3">2.3</A>) Does PostgreSQL have a graphical user
55 <A href="#2.4">2.4</A>) What languages are available to
56 communicate with PostgreSQL?<BR>
59 <H2 align="center">Administrative Questions</H2>
60 <A href="#3.1">3.1</A>) How do I install PostgreSQL somewhere other
61 than <I>/usr/local/pgsql</I>?<BR>
62 <A href="#3.2">3.2</A>) When I start <I>postmaster</I>, I get a
63 <I>Bad System Call</I> or core dumped message. Why?<BR>
64 <A href="#3.3">3.3</A>) When I try to start <I>postmaster</I>, I
65 get <I>IpcMemoryCreate</I> errors. Why?<BR>
66 <A href="#3.4">3.4</A>) When I try to start <I>postmaster</I>, I
67 get <I>IpcSemaphoreCreate</I> errors. Why?<BR>
68 <A href="#3.5">3.5</A>) How do I control connections from other
70 <A href="#3.6">3.6</A>) How do I tune the database engine for
71 better performance?<BR>
72 <A href="#3.7">3.7</A>) What debugging features are available?<BR>
73 <A href="#3.8">3.8</A>) Why do I get <I>"Sorry, too many
74 clients"</I> when trying to connect?<BR>
75 <A href="#3.9">3.9</A>) What is in the <I>pgsql_tmp</I>
77 <A href="#3.10">3.10</A>) Why do I need to do a dump and restore
78 to upgrade PostgreSQL releases?<BR>
79 <A href="#3.11">3.11</A>) What computer hardware should I use?<BR>
82 <H2 align="center">Operational Questions</H2>
83 <A href="#4.1">4.1</A>) What is the difference between binary
84 cursors and normal cursors?<BR>
85 <A href="#4.2">4.2</A>) How do I <SMALL>SELECT</SMALL> only the
86 first few rows of a query? A random row?<BR>
87 <A href="#4.3">4.3</A>) How do I get a list of tables or other
88 things I can see in <I>psql</I>?<BR>
89 <A href="#4.4">4.4</A>) How do you remove a column from a
90 table, or change it's data type?<BR>
91 <A href="#4.5">4.5</A>) What is the maximum size for a row, a
92 table, and a database?<BR>
93 <A href="#4.6">4.6</A>) How much database disk space is required
94 to store data from a typical text file?<BR>
95 <A href="#4.7">4.7</A>) How do I find out what tables, indexes,
96 databases, and users are defined?<BR>
97 <A href="#4.8">4.8</A>) My queries are slow or don't make use of
99 <A href="#4.9">4.9</A>) How do I see how the query optimizer is
100 evaluating my query?<BR>
101 <A href="#4.10">4.10</A>) What is an R-tree index?<BR>
102 <A href="#4.11">4.11</A>) What is the Genetic Query Optimizer?<BR>
103 <A href="#4.12">4.12</A>) How do I perform regular expression
104 searches and case-insensitive regular expression searches? How do I
105 use an index for case-insensitive searches?<BR>
106 <A href="#4.13">4.13</A>) In a query, how do I detect if a field
107 is <SMALL>NULL</SMALL>?<BR>
108 <A href="#4.14">4.14</A>) What is the difference between the
109 various character types?<BR>
110 <A href="#4.15.1">4.15.1</A>) How do I create a
111 serial/auto-incrementing field?<BR>
112 <A href="#4.15.2">4.15.2</A>) How do I get the value of a
113 <SMALL>SERIAL</SMALL> insert?<BR>
114 <A href="#4.15.3">4.15.3</A>) Don't <I>currval()</I> and
115 <I>nextval()</I> lead to a race condition with other users?<BR>
116 <A href="#4.15.4">4.15.4</A>) Why aren't my sequence numbers
117 reused on transaction abort? Why are there gaps in the numbering of
118 my sequence/SERIAL column?<BR>
119 <A href="#4.16">4.16</A>) What is an <SMALL>OID</SMALL>? What is a
120 <SMALL>TID</SMALL>?<BR>
121 <A href="#4.17">4.17</A>) What is the meaning of some of the terms
122 used in PostgreSQL?<BR>
123 <A href="#4.18">4.18</A>) Why do I get the error <I>"ERROR: Memory
124 exhausted in AllocSetAlloc()"</I>?<BR>
125 <A href="#4.19">4.19</A>) How do I tell what PostgreSQL version I
127 <A href="#4.20">4.20</A>) Why does my large-object operations get
128 <I>"invalid large obj descriptor"</I>?<BR>
129 <A href="#4.21">4.21</A>) How do I create a column that will
130 default to the current time?<BR>
131 <A href="#4.22">4.22</A>) Why are my subqueries using
132 <CODE><SMALL>IN</SMALL></CODE> so slow?<BR>
133 <A href="#4.23">4.23</A>) How do I perform an outer join?<BR>
134 <A href="#4.24">4.24</A>) How do I perform queries using multiple
136 <A href="#4.25">4.25</A>) How do I return multiple rows or columns
138 <A href="#4.26">4.26</A>) Why can't I reliably create/drop
139 temporary tables in PL/PgSQL functions?<BR>
140 <A href="#4.28">4.27</A>) What encryption options are available?<BR>
143 <H2 align="center">Extending PostgreSQL</H2>
144 <A href="#5.1">5.1</A>) I wrote a user-defined function. When I run
145 it in <I>psql</I>, why does it dump core?<BR>
146 <A href="#5.2">5.2</A>) How can I contribute some nifty new types
147 and functions to PostgreSQL?<BR>
148 <A href="#5.3">5.3</A>) How do I write a C function to return a
150 <A href="#5.4">5.4</A>) I have changed a source file. Why does the
151 recompile not see the change?<BR>
155 <H2 align="center">General Questions</H2>
157 <H4><A name="1.1">1.1</A>) What is PostgreSQL? How is it pronounced?</H4>
159 <P>PostgreSQL is pronounced <I>Post-Gres-Q-L</I>. An audio file is
160 available at http://www.postgresql.org/postgresql.mp3 for those
161 would like to hear the pronunciation.
164 <P>PostgreSQL is an enhancement of the POSTGRES database management
165 system (and is still sometimes reffered to as simply "Postgres"),
166 a next-generation <SMALL>DBMS</SMALL> research prototype.
167 While PostgreSQL retains the powerful data model and rich data
168 types of POSTGRES, it replaces the PostQuel query language with an
169 extended subset of <SMALL>SQL</SMALL>. PostgreSQL is free and the
170 complete source is available.</P>
172 <P>PostgreSQL development is performed by a team of
173 developers who all subscribe to the PostgreSQL development mailing
174 list. The current coordinator is Marc G. Fournier (<A href=
175 "mailto:scrappy@PostgreSQL.org">scrappy@PostgreSQL.org</A>). (See
176 section <a href="#1.6">1.6</a> on how to join). This team is now
177 responsible for all development of PostgreSQL. It is a community
178 project and is not controlled by any company. To get involved, see
179 the developer's FAQ at <A href=
180 "http://www.PostgreSQL.org/docs/faqs/FAQ_DEV.html">http://www.PostgreSQL.org/docs/faqs/FAQ_DEV.html</A>
183 <P>The authors of PostgreSQL 1.01 were Andrew Yu and Jolly Chen.
184 Many others have contributed to the porting, testing, debugging,
185 and enhancement of the code. The original Postgres code, from which
186 PostgreSQL is derived, was the effort of many graduate students,
187 undergraduate students, and staff programmers working under the
188 direction of Professor Michael Stonebraker at the University of
189 California, Berkeley.</P>
191 <P>The original name of the software at Berkeley was Postgres. When
192 <SMALL>SQL</SMALL> functionality was added in 1995, its name was
193 changed to Postgres95. The name was changed at the end of 1996 to
196 <H4><A name="1.2">1.2</A>) What is the copyright on
199 <P>PostgreSQL is subject to the following COPYRIGHT:</P>
201 <P>PostgreSQL Data Base Management System</P>
203 <P>Portions copyright (c) 1996-2004, PostgreSQL Global Development
204 Group Portions Copyright (c) 1994-6 Regents of the University of
207 <P>Permission to use, copy, modify, and distribute this software
208 and its documentation for any purpose, without fee, and without a
209 written agreement is hereby granted, provided that the above
210 copyright notice and this paragraph and the following two
211 paragraphs appear in all copies.</P>
213 <P>IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
214 PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
215 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS
216 SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
217 CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</P>
219 <P>THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
220 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
221 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
222 SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE
223 UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
224 SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</P>
226 <P>The above is the BSD license, the classic open-source license.
227 It has no restrictions on how the source code may be used. We like
228 it and have no intention of changing it.</P>
230 <H4><A name="1.3">1.3</A>) What Unix platforms does PostgreSQL run
233 <P>In general, any modern Unix-compatible platform should be able to
234 run PostgreSQL. The platforms that had received explicit testing at
235 the time of release are listed in the installation
238 <H4><A name="1.4">1.4</A>) What non-Unix ports are available?</H4>
240 <P>Starting with version 8.0, PostgreSQL now runs natively on
241 Microsoft Windows NT-based operating systems like Win2000, WinXP,
242 and Win2003. A prepackaged installer is available at <a href=
243 "http://pgfoundry.org/projects/pginstaller">
244 http://pgfoundry.org/projects/pginstaller</a>.
246 <p>There is also a Novell Netware 6 port at
247 <a href="http://forge.novell.com">http://forge.novell.com</a>.</p>
249 <H4><A name="1.5">1.5</A>) Where can I get PostgreSQL?</H4>
251 <P>The primary anonymous ftp site for PostgreSQL is <A href=
252 "ftp://ftp.PostgreSQL.org/pub">ftp://ftp.PostgreSQL.org/pub</A>.
253 For mirror sites, see our main web site.</P>
255 <H4><A name="1.6">1.6</A>) Where can I get support?</H4>
257 <P>The main mailing list is: <A href=
258 "mailto:pgsql-general@PostgreSQL.org">pgsql-general@PostgreSQL.org</A>.
259 It is available for discussion of matters pertaining to PostgreSQL.
260 To subscribe, send mail with the following lines in the body (not
261 the subject line):</P>
268 "mailto:pgsql-general-request@PostgreSQL.org">pgsql-general-request@PostgreSQL.org</A>.</P>
270 <P>There is also a digest list available. To subscribe to this
271 list, send email to: <A href=
272 "mailto:pgsql-general-digest-request@PostgreSQL.org">pgsql-general-digest-request@PostgreSQL.org</A>
279 Digests are sent out to members of this list whenever the main list
280 has received around 30k of messages.
282 <P>The bugs mailing list is available. To subscribe to this list,
283 send email to <A href=
284 "mailto:pgsql-bugs-request@PostgreSQL.org">pgsql-bugs-request@PostgreSQL.org</A>
291 There is also a developers discussion mailing list available. To
292 subscribe to this list, send email to <A href=
293 "mailto:pgsql-hackers-request@PostgreSQL.org">pgsql-hackers-request@PostgreSQL.org</A>
300 <P>Additional mailing lists and information about PostgreSQL can be
301 found via the PostgreSQL WWW home page at:</P>
304 <A href="http://www.PostgreSQL.org">http://www.PostgreSQL.org</A>
307 <P>There is also an IRC channel on Freenode and EFNet,
308 channel <I>#PostgreSQL</I>. You can use the Unix command <CODE>irc -c
309 '#PostgreSQL' "$USER" irc.phoenix.net.</CODE> or <CODE>irc -c
310 '#PostgreSQL' "$USER" irc.freenode.net.</CODE></P>
312 <P>A list of commercial support companies is available at <A href=
313 "http://techdocs.postgresql.org/companies.php">http://techdocs.postgresql.org/companies.php</A>.</P>
315 <H4><A name="1.7">1.7</A>) What is the latest release?</H4>
317 <P>The latest release of PostgreSQL is version 7.4.5.</P>
319 <P>We plan to have major releases every six to eight months.</P>
321 <H4><A name="1.8">1.8</A>) What documentation is available?</H4>
323 <P>Several manuals, manual pages, and some small test examples are
324 included in the distribution. See the <I>/doc</I> directory. You
325 can also browse the manuals online at <A href=
326 "http://www.PostgreSQL.org/docs">http://www.PostgreSQL.org/docs</A>.</P>
328 <P>There are two PostgreSQL books available online at <A href=
329 "http://www.PostgreSQL.org/docs/awbook.html">http://www.PostgreSQL.org/docs/awbook.html</A>
331 "http://www.commandprompt.com/ppbook/">http://www.commandprompt.com/ppbook/</A>.
332 There is a list of PostgreSQL books available for purchase at <A
334 "http://techdocs.postgresql.org/techdocs/bookreviews.php">http://techdocs.PostgreSQL.org/techdocs/bookreviews.php</A>.
335 There is also a collection of PostgreSQL technical articles at <A
337 "http://techdocs.PostgreSQL.org/">http://techdocs.PostgreSQL.org/</A>.</P>
339 <P><I>psql</I> has some nice \d commands to show information about
340 types, operators, functions, aggregates, etc.</P>
342 <P>Our web site contains even more documentation.</P>
344 <H4><A name="1.9">1.9</A>) How do I find out about known bugs or
345 missing features?</H4>
347 <P>PostgreSQL supports an extended subset of <SMALL>SQL</SMALL>-92.
348 See our <A href="http://developer.PostgreSQL.org/todo.php">TODO</A>
349 list for known bugs, missing features, and future plans.</P>
351 <H4><A name="1.10">1.10</A>) How can I learn
352 <SMALL>SQL</SMALL>?</H4>
354 <P>The PostgreSQL book at <A href=
355 "http://www.PostgreSQL.org/docs/awbook.html">http://www.PostgreSQL.org/docs/awbook.html</A>
356 teaches <SMALL>SQL</SMALL>. There is another PostgreSQL book at <A
358 "http://www.commandprompt.com/ppbook/">http://www.commandprompt.com/ppbook.</A>
359 There is a nice tutorial at <A href=
360 "http://www.intermedia.net/support/sql/sqltut.shtm">http://www.intermedia.net/support/sql/sqltut.shtm,</A>
362 "http://ourworld.compuserve.com/homepages/graeme_birchall/HTM_COOK.HTM">
363 http://ourworld.compuserve.com/homepages/graeme_birchall/HTM_COOK.HTM,</A>
365 "http://sqlcourse.com/">http://sqlcourse.com.</A></P>
367 <P>Another one is "Teach Yourself SQL in 21 Days, Second Edition"
369 "http://members.tripod.com/er4ebus/sql/index.htm">http://members.tripod.com/er4ebus/sql/index.htm</A></P>
371 <P>Many of our users like <I>The Practical SQL Handbook</I>,
372 Bowman, Judith S., et al., Addison-Wesley. Others like <I>The
373 Complete Reference SQL</I>, Groff et al., McGraw-Hill.</P>
375 <H4><A name="1.11">1.11</A>) Is PostgreSQL Y2K compliant?</H4>
377 <P>Yes, we easily handle dates past the year 2000 AD, and before
380 <H4><A name="1.12">1.12</A>) How do I join the development
383 <P>First, download the latest source and read the PostgreSQL
384 Developers documentation on our web site, or in the distribution.
385 Second, subscribe to the <I>pgsql-hackers</I> and
386 <I>pgsql-patches</I> mailing lists. Third, submit high quality
387 patches to pgsql-patches.</P>
389 <P>There are about a dozen people who have commit privileges to the
390 PostgreSQL <SMALL>CVS</SMALL> archive. They each have submitted so
391 many high-quality patches that it was impossible for the existing
392 committers to keep up, and we had confidence that patches they
393 committed were of high quality.</P>
395 <H4><A name="1.13">1.13</A>) How do I submit a bug report?</H4>
397 <P>Please visit the PostgreSQL BugTool page at <A href=
398 "http://www.PostgreSQL.org/bugs/bugs.php">http://www.PostgreSQL.org/bugs/bugs.php</A>,
399 which gives guidelines and directions on how to submit a
402 <P>Also check out our ftp site <A href=
403 "ftp://ftp.PostgreSQL.org/pub">ftp://ftp.PostgreSQL.org/pub</A> to
404 see if there is a more recent PostgreSQL version or patches.</P>
406 <H4><A name="1.14">1.14</A>) How does PostgreSQL compare to other
407 <SMALL>DBMS</SMALL>s?</H4>
409 <P>There are several ways of measuring software: features,
410 performance, reliability, support, and price.</P>
413 <DT><B>Features</B></DT>
415 <DD>PostgreSQL has most features present in large commercial
416 <SMALL>DBMS</SMALL>s, like transactions, subselects, triggers,
417 views, foreign key referential integrity, and sophisticated
418 locking. We have some features they do not have, like
419 user-defined types, inheritance, rules, and multi-version
420 concurrency control to reduce lock contention.<BR>
424 <DT><B>Performance</B></DT>
426 <DD>PostgreSQL has performance similar to other commercial and
427 open source databases. it is faster for some things, slower for
428 others. In comparison to MySQL or leaner database systems, we are
429 faster for multiple users, complex queries, and a read/write query
430 load. MySQL is faster for simple SELECT queries done by a few users.
431 Of course, MySQL does not have most of the features mentioned in the
432 <I>Features</I> section above. We are built for reliability and
433 features, and we continue to improve performance in every
434 release. There is an interesting Web page comparing PostgreSQL to
435 MySQL at <A href="http://openacs.org/philosophy/why-not-mysql.html">
436 http://openacs.org/philosophy/why-not-mysql.html</A> Also, MySQL is
437 is a company that distributes its products via open source, and requires
438 a commercial license for close-source software, not an
439 open source development community like PostgreSQL.<BR>
444 <DT><B>Reliability</B></DT>
446 <DD>We realize that a <SMALL>DBMS</SMALL> must be reliable, or it
447 is worthless. We strive to release well-tested, stable code that
448 has a minimum of bugs. Each release has at least one month of
449 beta testing, and our release history shows that we can provide
450 stable, solid releases that are ready for production use. We
451 believe we compare favorably to other database software in this
456 <DT><B>Support</B></DT>
458 <DD>Our mailing lists provide contact with a large group of developers
459 and users to help resolve any problems encountered. While we cannot
460 guarantee a fix, commercial <SMALL>DBMS</SMALL>s do not always
461 supply a fix either. Direct access to developers, the user
462 community, manuals, and the source code often make PostgreSQL
463 support superior to other <SMALL>DBMS</SMALL>s. There is
464 commercial per-incident support available for those who need it.
465 (See <A href="#1.6">FAQ section 1.6</A>.)<BR>
469 <DT><B>Price</B></DT>
471 <DD>We are free for all use, both commercial and non-commercial.
472 You can add our code to your product with no limitations, except
473 those outlined in our BSD-style license stated above.<BR>
478 <H4><A name="1.15">1.15</A>) How can I financially assist
481 <P>PostgreSQL has had a first-class infrastructure since we started
482 in 1996. This is all thanks to Marc Fournier, who has created
483 and managed this infrastructure over the years.</P>
485 <P>Quality infrastructure is very important to an open-source
486 project. It prevents disruptions that can greatly delay forward
487 movement of the project.</P>
489 <P>Of course, this infrastructure is not cheap. There are a variety
490 of monthly and one-time expenses that are required to keep it
491 going. If you or your company has money it can donate to help fund
492 this effort, please go to <A href="http://store.pgsql.com/shopping/">http://store.pgsql.com/shopping/</A>
493 and make a donation.</P>
495 <P>Although the web page mentions PostgreSQL, Inc, the
496 "contributions" item is solely to support the PostgreSQL project
497 and does not fund any specific company. If you prefer, you can also
498 send a check to the contact address.</P>
501 <P>Also, if you have a success story about PostgreSQL, please submit
502 it to our advocacy site at <a href="http://advocacy.postgresql.org">
503 http://advocacy.postgresql.org</a>.</P>
506 <H2 align="center">User Client Questions</H2>
508 <H4><A name="2.1">2.1</A>) Are there <SMALL>ODBC</SMALL> drivers
511 <P>There are two <SMALL>ODBC</SMALL> drivers available, PsqlODBC
512 and OpenLink <SMALL>ODBC</SMALL>.</P>
514 <P>You can download PsqlODBC from <A href=
515 "http://gborg.postgresql.org/project/psqlodbc/projdisplay.php">
516 http://gborg.postgresql.org/project/psqlodbc/projdisplay.php</A>.</P>
518 <P>OpenLink <SMALL>ODBC</SMALL> can be gotten from <A href=
519 "http://www.openlinksw.com/">http://www.openlinksw.com</A>. It
520 works with their standard <SMALL>ODBC</SMALL> client software so
521 you'll have PostgreSQL <SMALL>ODBC</SMALL> available on every
522 client platform they support (Win, Mac, Unix, VMS).</P>
524 <P>They will probably be selling this product to people who need
525 commercial-quality support, but a freeware version will always be
526 available. Please send questions to <A href=
527 "mailto:postgres95@openlink.co.uk">postgres95@openlink.co.uk</A>.</P>
529 <H4><A name="2.2">2.2</A>) What tools are available for using
530 PostgreSQL with Web pages?</H4>
532 <P>A nice introduction to Database-backed Web pages can be seen at:
533 <A href="http://www.webreview.com">http://www.webreview.com</A></P>
535 <P>For Web integration, PHP is an excellent interface. It is at <A
536 href="http://www.php.net">http://www.php.net</A>.</P>
538 <P>For complex cases, many use the Perl interface and CGI.pm or mod_perl.</P>
540 <H4><A name="2.3">2.3</A>) Does PostgreSQL have a graphical user
543 <P>Yes, there are several graphical interfaces to PostgreSQL available.
544 These include PgAccess <a href="http://www.pgaccess.org">
545 http://www.pgaccess.org</a>), PgAdmin III (<a
546 href="http://www.pgadmin.org">http://www.pgadmin.org</a>, RHDB Admin (<a
547 href="http://sources.redhat.com/rhdb/">http://sources.redhat.com/rhdb/
548 </a>) and Rekall (<a href="http://www.thekompany.com/products/rekall/">
549 http://www.thekompany.com/products/rekall/</a>, proprietary). There is
550 also PhpPgAdmin (<a href="http://phppgadmin.sourceforge.net/">
551 http://phppgadmin.sourceforge.net/ </a>), a web-based interface to
554 <P>See <a href="http://techdocs.postgresql.org/guides/GUITools">http://techdocs.postgresql.org/guides/GUITools</a> for a more detailed list.</P>
556 <H4><A name="2.4">2.4</A>) What languages are able to communicate with
559 <P>Most popular programming languages contain an interface to
560 PostgreSQL. Check your programming language's list of extension
563 <P>The following interfaces are included in the PostgreSQL
569 <LI>Embedded C (ecpg)</LI>
573 <LI>Python (PyGreSQL)</LI>
575 <LI>TCL (libpgtcl)</LI>
578 <P>Additional interfaces are available at
579 <a href="http://gborg.postgresql.org">http://gborg.postgresql.org</A>
580 in the <I>Drivers/Interfaces</I> section.
584 <H2 align="center">Administrative Questions</H2>
586 <H4><A name="3.1">3.1</A>) How do I install PostgreSQL somewhere
587 other than <I>/usr/local/pgsql</I>?</H4>
589 <P>Specify the <I>--prefix</I> option when running
590 <I>configure</I>.</P>
592 <H4><A name="3.2">3.2</A>) When I start <I>postmaster</I>, I get a
593 <I>Bad System Call</I> or core dumped message. Why?</H4>
595 <P>It could be a variety of problems, but first check to see that
596 you have System V extensions installed in your kernel. PostgreSQL
597 requires kernel support for shared memory and semaphores.</P>
599 <H4><A name="3.3">3.3</A>) When I try to start <I>postmaster</I>, I
600 get <I>IpcMemoryCreate</I> errors. Why?</H4>
602 <P>You either do not have shared memory configured properly in your
603 kernel or you need to enlarge the shared memory available in the
604 kernel. The exact amount you need depends on your architecture and
605 how many buffers and backend processes you configure for
606 <I>postmaster</I>. For most systems, with default numbers of
607 buffers and processes, you need a minimum of ~1 MB. See the <A
609 "http://www.PostgreSQL.org/docs/view.php?version=current&idoc=1&file=kernel-resources.html">PostgreSQL
610 Administrator's Guide</A> for more detailed information about
611 shared memory and semaphores.</P>
613 <H4><A name="3.4">3.4</A>) When I try to start <I>postmaster</I>, I
614 get <I>IpcSemaphoreCreate</I> errors. Why?</H4>
616 <P>If the error message is <I>IpcSemaphoreCreate: semget failed (No
617 space left on device)</I> then your kernel is not configured with
618 enough semaphores. Postgres needs one semaphore per potential
619 backend process. A temporary solution is to start <I>postmaster</I>
620 with a smaller limit on the number of backend processes. Use
621 <I>-N</I> with a parameter less than the default of 32. A more
622 permanent solution is to increase your kernel's
623 <SMALL>SEMMNS</SMALL> and <SMALL>SEMMNI</SMALL> parameters.</P>
625 <P>Inoperative semaphores can also cause crashes during heavy
628 <P>If the error message is something else, you might not have
629 semaphore support configured in your kernel at all. See the
630 PostgreSQL Administrator's Guide for more detailed information
631 about shared memory and semaphores.</P>
633 <H4><A name="3.5">3.5</A>) How do I control connections from other
636 <P>By default, PostgreSQL only allows connections from the local
637 machine using Unix domain sockets or TCP/IP connections. Other
638 machines will not be able to connect unless you modify
639 listen_addresses in the postgresql.conf <B>and</B> enable
640 host-based authentication by modifying the file
641 <I>$PGDATA/pg_hba.conf</I> accordingly.</P>
643 <H4><A name="3.6">3.6</A>) How do I tune the database engine for
644 better performance?</H4>
646 <P>Certainly, indexes can speed up queries. The
647 <SMALL>EXPLAIN ANALYZE</SMALL> command allows you to see how PostgreSQL is
648 interpreting your query, and which indexes are being used.</P>
650 <P>If you are doing many <SMALL>INSERTs</SMALL>, consider doing
651 them in a large batch using the <SMALL>COPY</SMALL> command. This
652 is much faster than individual <SMALL>INSERTS</SMALL>. Second,
653 statements not in a <SMALL>BEGIN WORK/COMMIT</SMALL> transaction
654 block are considered to be in their own transaction. Consider
655 performing several statements in a single transaction block. This
656 reduces the transaction overhead. Also, consider dropping and
657 recreating indexes when making large data changes.</P>
659 <P>There are several tuning options. You can disable <I>fsync()</I>
660 by starting <I>postmaster</I> with a <I>-o -F</I> option. This will
661 prevent <I>fsync()</I>s from flushing to disk after every
664 <P>You can also use the <I>postmaster</I> <I>-B</I> option to
665 increase the number of shared memory buffers used by the backend
666 processes. If you make this parameter too high, the
667 <I>postmaster</I> may not start because you have exceeded your
668 kernel's limit on shared memory space. Each buffer is 8K and the
669 default is 64 buffers.</P>
671 <P>You can also use the backend <I>-S</I> option to increase the
672 maximum amount of memory used by the backend process for temporary
673 sorts. The <I>-S</I> value is measured in kilobytes, and the
674 default is 512 (i.e. 512K).</P>
676 <P>You can also use the <SMALL>CLUSTER</SMALL> command to group
677 data in tables to match an index. See the <SMALL>CLUSTER</SMALL>
678 manual page for more details.</P>
680 <H4><A name="3.7">3.7</A>) What debugging features are
683 <P>PostgreSQL has several features that report status information
684 that can be valuable for debugging purposes.</P>
686 <P>First, by running <I>configure</I> with the --enable-cassert
687 option, many <I>assert()</I>s monitor the progress of the backend
688 and halt the program when something unexpected occurs.</P>
690 <P>Both <I>postmaster</I> and <I>postgres</I> have several debug
691 options available. First, whenever you start <I>postmaster</I>,
692 make sure you send the standard output and error to a log file,
696 ./bin/postmaster >server.log 2>&1 &
699 <P>This will put a server.log file in the top-level PostgreSQL
700 directory. This file contains useful information about problems or
701 errors encountered by the server. <I>Postmaster</I> has a <I>-d</I>
702 option that allows even more detailed information to be reported.
703 The <I>-d</I> option takes a number that specifies the debug level.
704 Be warned that high debug level values generate large log
707 <P>If <I>postmaster</I> is not running, you can actually run the
708 <I>postgres</I> backend from the command line, and type your
709 <SMALL>SQL</SMALL> statement directly. This is recommended
710 <B>only</B> for debugging purposes. Note that a newline terminates
711 the query, not a semicolon. If you have compiled with debugging
712 symbols, you can use a debugger to see what is happening. Because
713 the backend was not started from <I>postmaster</I>, it is not
714 running in an identical environment and locking/backend interaction
715 problems may not be duplicated.</P>
717 <P>If <I>postmaster</I> is running, start <I>psql</I> in one
718 window, then find the <SMALL>PID</SMALL> of the <I>postgres</I>
719 process used by <I>psql</I>. Use a debugger to attach to the
720 <I>postgres</I> <SMALL>PID</SMALL>. You can set breakpoints in the
721 debugger and issue queries from <I>psql</I>. If you are debugging
722 <I>postgres</I> startup, you can set PGOPTIONS="-W n", then start
723 <I>psql</I>. This will cause startup to delay for <I>n</I> seconds
724 so you can attach to the process with the debugger, set any
725 breakpoints, and continue through the startup sequence.</P>
727 <P>The <I>postgres</I> program has <I>-s, -A</I>, and <I>-t</I>
728 options that can be very useful for debugging and performance
731 <P>You can also compile with profiling to see what functions are
732 taking execution time. The backend profile files will be deposited
733 in the <I>pgsql/data/base/dbname</I> directory. The client profile
734 file will be put in the client's current directory. Linux requires
735 a compile with <I>-DLINUX_PROFILE</I> for proper profiling.</P>
737 <H4><A name="3.8">3.8</A>) Why do I get <I>"Sorry, too many
738 clients"</I> when trying to connect?</H4>
740 <P>You need to increase <I>postmaster</I>'s limit on how many
741 concurrent backend processes it can start.</P>
743 <P>The default limit is 32 processes. You can increase it by
744 restarting <I>postmaster</I> with a suitable <I>-N</I> value or
745 modifying <I>postgresql.conf</I>.</P>
747 <P>Note that if you make <I>-N</I> larger than 32, you must also
748 increase <I>-B</I> beyond its default of 64; <I>-B</I> must be at
749 least twice <I>-N</I>, and probably should be more than that for
750 best performance. For large numbers of backend processes, you are
751 also likely to find that you need to increase various Unix kernel
752 configuration parameters. Things to check include the maximum size
753 of shared memory blocks, <SMALL>SHMMAX;</SMALL> the maximum number
754 of semaphores, <SMALL>SEMMNS</SMALL> and <SMALL>SEMMNI;</SMALL> the
755 maximum number of processes, <SMALL>NPROC;</SMALL> the maximum
756 number of processes per user, <SMALL>MAXUPRC;</SMALL> and the
757 maximum number of open files, <SMALL>NFILE</SMALL> and
758 <SMALL>NINODE</SMALL>. The reason that PostgreSQL has a limit on
759 the number of allowed backend processes is so your system won't run
760 out of resources.</P>
762 <H4><A name="3.9">3.9</A>) What is in the <I>pgsql_tmp</I> directory?</H4>
764 <P>This directory contains temporary files generated by the query
765 executor. For example, if a sort needs to be done to satisfy an
766 <SMALL>ORDER BY</SMALL> and the sort requires more space than the
767 backend's <I>-S</I> parameter allows, then temporary files are created
768 here to hold the extra data.</P>
770 <P>The temporary files are usually deleted automatically, but might
771 remain if a backend crashes during a sort. A stop and restart of the
772 <I>postmaster</I> will remove files from those directories.</P>
774 <H4><A name="3.10">3.10</A>) Why do I need to do a dump and restore
775 to upgrade between major PostgreSQL releases?</H4>
777 <P>The PostgreSQL team makes only small changes between minor releases,
778 so upgrading from 7.2 to 7.2.1 does not require a dump and restore.
779 However, major releases (e.g. from 7.2 to 7.3) often change the internal
780 format of system tables and data files. These changes are often complex,
781 so we don't maintain backward compatability for data files. A dump outputs
782 data in a generic format that can then be loaded in using the new internal
785 <P>In releases where the on-disk format does not change, the
786 <I>pg_upgrade</I> script can be used to upgrade without a dump/restore.
787 The release notes mention whether <I>pg_upgrade</I> is available for the
790 <H4><A name="3.11">3.11</A>) What computer hardware should I use?</H4>
792 <P>Because PC hardware is mostly compatible, people tend to believe that
793 all PC hardware is of equal quality. It is not. ECC RAM, SCSI, and
794 quality motherboards are more reliable and have better performance than
795 less expensive hardware. PostgreSQL will run on almost any hardware,
796 but if reliability and performance are important it is wise to
797 research your hardware options thoroughly. Our email lists can be used
798 to discuss hardware options and tradeoffs.</P>
802 <H2 align="center">Operational Questions</H2>
804 <H4><A name="4.1">4.1</A>) What is the difference between binary
805 cursors and normal cursors?</H4>
807 <P>See the <SMALL>DECLARE</SMALL> manual page for a
810 <H4><A name="4.2">4.2</A>) How do I <SMALL>SELECT</SMALL> only the
811 first few rows of a query? A random row?</H4>
813 <P>See the <SMALL>FETCH</SMALL> manual page, or use
814 <SMALL>SELECT</SMALL> ... <SMALL>LIMIT</SMALL>....</P>
816 <P>The entire query may have to be evaluated, even if you only want
817 the first few rows. Consider using a query that has an <SMALL>ORDER
818 BY</SMALL>. If there is an index that matches the <SMALL>ORDER
819 BY</SMALL>, PostgreSQL may be able to evaluate only the first few
820 records requested, or the entire query may have to be evaluated
821 until the desired rows have been generated.</P>
823 <P>To <SMALL>SELECT</SMALL> a random row, use:
831 <H4><A name="4.3">4.3</A>) How do I get a list of tables or other
832 things I can see in <I>psql</I>?</H4>
834 <P>Use the \dt command to see tables in <I>psql</I>. For a complete list of
835 commands inside psql you can use \?. Alternatively you can read the source
836 code for <I>psql</I> in file <I>pgsql/src/bin/psql/describe.c</I>, it
837 contains <SMALL>SQL</SMALL> commands that generate the output for
838 <I>psql</I>'s backslash commands. You can also start <I>psql</I> with the
839 <I>-E</I> option so it will print out the queries it uses to execute the
840 commands you give. PostgreSQL also provides an <SMALL>SQLi</SMALL> compliant
841 INFORMATION SCHEMA interface you can query to get information about the
844 <H4><A name="4.4">4.4</A>) How do you remove a column from a
845 table, or change its data type?</H4>
847 <P><SMALL>DROP COLUMN</SMALL> functionality was added in release 7.3 with
848 <SMALL>ALTER TABLE DROP COLUMN</SMALL>. In earlier versions,
852 LOCK TABLE old_table;
853 SELECT ... -- select all columns but the one you want to remove
856 DROP TABLE old_table;
857 ALTER TABLE new_table RENAME TO old_table;
861 <P>To change the data type of a column, do this:</P>
864 ALTER TABLE tab ADD COLUMN new_col <i>new_data_type</i>;
865 UPDATE tab SET new_col = CAST(old_col AS <i>new_data_type</i>);
866 ALTER TABLE tab DROP COLUMN old_col;
869 <P>You might then want to do <I>VACUUM FULL tab</I> to reclaim the
870 disk space used by the expired rows.</P>
872 <H4><A name="4.5">4.5</A>) What is the maximum size for a row, a
873 table, and a database?</H4>
875 <P>These are the limits:</P>
877 Maximum size for a database? unlimited (32 TB databases exist)
878 Maximum size for a table? 32 TB
879 Maximum size for a row? 1.6TB
880 Maximum size for a field? 1 GB
881 Maximum number of rows in a table? unlimited
882 Maximum number of columns in a table? 250-1600 depending on column types
883 Maximum number of indexes on a table? unlimited
886 Of course, these are not actually unlimited, but limited to
887 available disk space and memory/swap space. Performance may suffer
888 when these values get unusually large.
890 <P>The maximum table size of 32 TB does not require large file
891 support from the operating system. Large tables are stored as
892 multiple 1 GB files so file system size limits are not
895 <P>The maximum table size and maximum number of columns can be
896 quadrupled by increasing the default block size to 32k.</P>
898 <H4><A name="4.6">4.6</A>) How much database disk space is required
899 to store data from a typical text file?</H4>
901 <P>A PostgreSQL database may require up to five times the disk
902 space to store data from a text file.</P>
904 <P>As an example, consider a file of 100,000 lines with an integer
905 and text description on each line. Suppose the text string
906 avergages twenty bytes in length. The flat file would be 2.8 MB.
907 The size of the PostgreSQL database file containing this data can
908 be estimated as 6.4 MB:</P>
910 32 bytes: each row header (approximate)
911 24 bytes: one int field and one text field
912 + 4 bytes: pointer on page to tuple
913 ----------------------------------------
916 The data page size in PostgreSQL is 8192 bytes (8 KB), so:
919 ------------------- = 136 rows per database page (rounded down)
923 -------------------- = 735 database pages (rounded up)
926 735 database pages * 8192 bytes per page = 6,021,120 bytes (6 MB)
929 <P>Indexes do not require as much overhead, but do contain the data
930 that is being indexed, so they can be large also.</P>
932 <P><SMALL>NULL</SMALL>s are stored as bitmaps, so they
933 use very little space.</P>
935 <H4><A name="4.7">4.7</A>) How do I find out what tables, indexes,
936 databases, and users are defined?</H4>
938 <P><I>psql</I> has a variety of backslash commands to show such
939 information. Use \? to see them. There are also system tables
940 beginning with <I>pg_</I> that describe these too. Also, <I>psql
941 -l</I> will list all databases.</P>
943 <P>Also try the file <I>pgsql/src/tutorial/syscat.source</I>. It
944 illustrates many of the <SMALL>SELECT</SMALL>s needed to get
945 information from the database system tables.</P>
947 <H4><A name="4.8">4.8</A>) My queries are slow or don't make use of
948 the indexes. Why?</H4>
949 Indexes are not automatically used by every query. Indexes are only
950 used if the table is larger than a minimum size, and the query
951 selects only a small percentage of the rows in the table. This is
952 because the random disk access caused by an index scan can be
953 slower than a straight read through the table, or sequential scan.
955 <P>To determine if an index should be used, PostgreSQL must have
956 statistics about the table. These statistics are collected using
957 <SMALL>VACUUM ANALYZE</SMALL>, or simply <SMALL>ANALYZE</SMALL>.
958 Using statistics, the optimizer knows how many rows are in the
959 table, and can better determine if indexes should be used.
960 Statistics are also valuable in determining optimal join order and
961 join methods. Statistics collection should be performed
962 periodically as the contents of the table change.</P>
964 <P>Indexes are normally not used for <SMALL>ORDER BY</SMALL> or to
965 perform joins. A sequential scan followed by an explicit sort is
966 usually faster than an index scan of a large table.</P>
967 However, <SMALL>LIMIT</SMALL> combined with <SMALL>ORDER BY</SMALL>
968 often will use an index because only a small portion of the table
969 is returned. In fact, though MAX() and MIN() don't use indexes,
970 it is possible to retrieve such values using an index with ORDER BY
975 ORDER BY col [ DESC ]
979 <P>If you believe the optimizer is incorrect in choosing a
980 sequential scan, use <CODE>SET enable_seqscan TO 'off'</CODE> and
981 run tests to see if an index scan is indeed faster.</P>
983 <P>When using wild-card operators such as <SMALL>LIKE</SMALL> or
984 <I>~</I>, indexes can only be used in certain circumstances:</P>
986 <LI>The beginning of the search string must be anchored to the start
989 <LI><SMALL>LIKE</SMALL> patterns must not start with <I>%</I>.</LI>
990 <LI><I>~</I> (regular expression) patterns must start with
993 <LI>The search string can not start with a character class,
995 <LI>Case-insensitive searches such as <SMALL>ILIKE</SMALL> and
996 <I>~*</I> do not utilise indexes. Instead, use functional
997 indexes, which are described in section <a href="#4.12">4.12</a>.</LI>
998 <LI>The default <I>C</I> locale must be used during
1003 <P>In pre-8.0 releases, indexes often can not be used unless the data
1004 types exactly match the index's column types. This is particularly
1005 true of int2, int8, and numeric column indexes.</P>
1007 <H4><A name="4.9">4.9</A>) How do I see how the query optimizer is
1008 evaluating my query?</H4>
1010 <P>See the <SMALL>EXPLAIN</SMALL> manual page.</P>
1012 <H4><A name="4.10">4.10</A>) What is an R-tree index?</H4>
1014 <P>An R-tree index is used for indexing spatial data. A hash index
1015 can't handle range searches. A B-tree index only handles range
1016 searches in a single dimension. R-trees can handle
1017 multi-dimensional data. For example, if an R-tree index can be
1018 built on an attribute of type <I>point</I>, the system can more
1019 efficiently answer queries such as "select all points within a
1020 bounding rectangle."</P>
1022 <P>The canonical paper that describes the original R-tree design
1025 <P>Guttman, A. "R-trees: A Dynamic Index Structure for Spatial
1026 Searching." Proceedings of the 1984 ACM SIGMOD Int'l Conf on Mgmt
1029 <P>You can also find this paper in Stonebraker's "Readings in
1030 Database Systems".</P>
1032 <P>Built-in R-trees can handle polygons and boxes. In theory,
1033 R-trees can be extended to handle higher number of dimensions. In
1034 practice, extending R-trees requires a bit of work and we don't
1035 currently have any documentation on how to do it.</P>
1037 <H4><A name="4.11">4.11</A>) What is the Genetic Query
1040 <P>The <SMALL>GEQO</SMALL> module speeds query optimization when
1041 joining many tables by means of a Genetic Algorithm (GA). It allows
1042 the handling of large join queries through nonexhaustive
1045 <H4><A name="4.12">4.12</A>) How do I perform regular expression
1046 searches and case-insensitive regular expression searches? How do I
1047 use an index for case-insensitive searches?</H4>
1049 <P>The <I>~</I> operator does regular expression matching, and
1050 <I>~*</I> does case-insensitive regular expression matching. The
1051 case-insensitive variant of <SMALL>LIKE</SMALL> is called
1052 <SMALL>ILIKE</SMALL>.</P>
1054 <P>Case-insensitive equality comparisons are normally expressed
1059 WHERE lower(col) = 'abc';
1062 This will not use an standard index. However, if you create a
1063 functional index, it will be used:
1065 CREATE INDEX tabindex ON tab (lower(col));
1068 <H4><A name="4.13">4.13</A>) In a query, how do I detect if a field
1069 is <SMALL>NULL</SMALL>?</H4>
1071 <P>You test the column with <SMALL>IS NULL</SMALL> and <SMALL>IS
1072 NOT NULL</SMALL>.</P>
1074 <H4><A name="4.14">4.14</A>) What is the difference between the
1075 various character types?</H4>
1077 Type Internal Name Notes
1078 --------------------------------------------------
1079 VARCHAR(n) varchar size specifies maximum length, no padding
1080 CHAR(n) bpchar blank padded to the specified fixed length
1081 TEXT text no specific upper limit on length
1082 BYTEA bytea variable-length byte array (null-byte safe)
1083 "char" char one character
1086 <P>You will see the internal name when examining system catalogs
1087 and in some error messages.</P>
1089 <P>The first four types above are "varlena" types (i.e., the first
1090 four bytes on disk are the length, followed by the data). Thus the
1091 actual space used is slightly greater than the declared size.
1092 However, these data types are also subject to compression or being
1093 stored out-of-line by <SMALL>TOAST</SMALL>, so the space on disk
1094 might also be less than expected.</P>
1096 <SMALL>VARCHAR(n)</SMALL> is best when storing variable-length
1097 strings and it limits how long a string can be. <SMALL>TEXT</SMALL>
1098 is for strings of unlimited length, with a maximum of one gigabyte.
1099 <P><SMALL>CHAR(n)</SMALL> is for storing strings that are all the
1100 same length. <SMALL>CHAR(n)</SMALL> pads with blanks to the specified
1101 length, while <SMALL>VARCHAR(n)</SMALL> only stores the characters
1102 supplied. <SMALL>BYTEA</SMALL> is for storing binary data,
1103 particularly values that include <SMALL>NULL</SMALL> bytes. All the
1104 types described here have similar performance characteristics.</P>
1106 <H4><A name="4.15.1">4.15.1</A>) How do I create a
1107 serial/auto-incrementing field?</H4>
1109 <P>PostgreSQL supports a <SMALL>SERIAL</SMALL> data type. It
1110 auto-creates a sequence. For example,
1113 CREATE TABLE person (
1119 is automatically translated into this:
1121 CREATE SEQUENCE person_id_seq;
1122 CREATE TABLE person (
1123 id INT4 NOT NULL DEFAULT nextval('person_id_seq'),
1128 See the <I>create_sequence</I> manual page for more information
1129 about sequences. You can also use each row's <I>OID</I> field as a
1130 unique value. However, if you need to dump and reload the database,
1131 you need to use <I>pg_dump</I>'s <I>-o</I> option or <SMALL>COPY
1132 WITH OIDS</SMALL> option to preserve the <SMALL>OID</SMALL>s.
1134 <H4><A name="4.15.2">4.15.2</A>) How do I get the value of a
1135 <SMALL>SERIAL</SMALL> insert?</H4>
1137 <P>One approach is to retrieve the next <SMALL>SERIAL</SMALL> value
1138 from the sequence object with the <I>nextval()</I> function
1139 <I>before</I> inserting and then insert it explicitly. Using the
1140 example table in <A href="#4.15.1">4.15.1</A>, an example in a
1141 pseudo-language would look like this:</P>
1143 new_id = execute("SELECT nextval('person_id_seq')");
1144 execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
1147 You would then also have the new value stored in
1148 <CODE>new_id</CODE> for use in other queries (e.g., as a foreign
1149 key to the <CODE>person</CODE> table). Note that the name of the
1150 automatically created <SMALL>SEQUENCE</SMALL> object will be named
1151 <<I>table</I>>_<<I>serialcolumn</I>>_<I>seq</I>, where
1152 <I>table</I> and <I>serialcolumn</I> are the names of your table
1153 and your <SMALL>SERIAL</SMALL> column, respectively.
1155 <P>Alternatively, you could retrieve the assigned
1156 <SMALL>SERIAL</SMALL> value with the <I>currval()</I> function
1157 <I>after</I> it was inserted by default, e.g.,</P>
1159 execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
1160 new_id = execute("SELECT currval('person_id_seq')");
1163 Finally, you could use the <A href="#4.16"><SMALL>OID</SMALL></A>
1164 returned from the <SMALL>INSERT</SMALL> statement to look up the
1165 default value, though this is probably the least portable approach,
1166 and the oid value will wrap around when it reaches 4 billion.
1167 In Perl, using DBI with Edmund Mergl's DBD::Pg module, the oid
1168 value is made available via <I>$sth->{pg_oid_status}</I> after
1169 <I>$sth->execute()</I>.
1171 <H4><A name="4.15.3">4.15.3</A>) Don't <I>currval()</I> and
1172 <I>nextval()</I> lead to a race condition with other users?</H4>
1174 <P>No. <I>currval()</I> returns the current value assigned by your
1175 backend, not by all users.</P>
1177 <H4><A name="4.15.4">4.15.4</A>) Why aren't my sequence numbers
1178 reused on transaction abort? Why are there gaps in the numbering of
1179 my sequence/SERIAL column?</H4>
1181 <P>To improve concurrency, sequence values are given out to running
1182 transactions as needed and are not locked until the transaction
1183 completes. This causes gaps in numbering from aborted
1186 <H4><A name="4.16">4.16</A>) What is an <SMALL>OID</SMALL>? What is
1187 a <SMALL>TID</SMALL>?</H4>
1189 <P><SMALL>OID</SMALL>s are PostgreSQL's answer to unique row ids.
1190 Every row that is created in PostgreSQL gets a unique
1191 <SMALL>OID</SMALL>. All <SMALL>OID</SMALL>s generated during
1192 <I>initdb</I> are less than 16384 (from
1193 <I>include/access/transam.h</I>). All user-created
1194 <SMALL>OID</SMALL>s are equal to or greater than this. By default,
1195 all these <SMALL>OID</SMALL>s are unique not only within a table or
1196 database, but unique within the entire PostgreSQL installation.</P>
1198 <P>PostgreSQL uses <SMALL>OID</SMALL>s in its internal system
1199 tables to link rows between tables. These <SMALL>OID</SMALL>s can
1200 be used to identify specific user rows and used in joins. It is
1201 recommended you use column type <SMALL>OID</SMALL> to store
1202 <SMALL>OID</SMALL> values. You can create an index on the
1203 <SMALL>OID</SMALL> field for faster access.</P>
1205 <P>O<SMALL>ID</SMALL>s are assigned to all new rows from a central
1206 area that is used by all databases. If you want to change the
1207 <SMALL>OID</SMALL> to something else, or if you want to make a copy
1208 of the table, with the original <SMALL>OID</SMALL>s, there is no
1209 reason you can't do it:</P>
1211 CREATE TABLE new_table(mycol int);
1212 SELECT oid AS old_oid, mycol INTO tmp_table FROM old_table;
1213 COPY tmp_table TO '/tmp/pgtable';
1214 COPY new_table WITH OIDS FROM '/tmp/pgtable';
1215 DROP TABLE tmp_table;
1217 <P>O<SMALL>ID</SMALL>s are stored as 4-byte integers, and will
1218 overflow at 4 billion. No one has reported this ever happening, and
1219 we plan to have the limit removed before anyone does.</P>
1221 <P>T<SMALL>ID</SMALL>s are used to identify specific physical rows
1222 with block and offset values. T<SMALL>ID</SMALL>s change after rows
1223 are modified or reloaded. They are used by index entries to point
1224 to physical rows.</P>
1226 <H4><A name="4.17">4.17</A>) What is the meaning of some of the
1227 terms used in PostgreSQL?</H4>
1229 <P>Some of the source code and older documentation use terms that
1230 have more common usage. Here are some:</P>
1233 <LI>table, relation, class</LI>
1235 <LI>row, record, tuple</LI>
1237 <LI>column, field, attribute</LI>
1239 <LI>retrieve, select</LI>
1241 <LI>replace, update</LI>
1243 <LI>append, insert</LI>
1245 <LI><SMALL>OID</SMALL>, serial value</LI>
1247 <LI>portal, cursor</LI>
1249 <LI>range variable, table name, table alias</LI>
1252 <P>A list of general database terms can be found at: <A href=
1253 "http://hea-www.harvard.edu/MST/simul/software/docs/pkgs/pgsql/glossary/glossary.html">http://hea-www.harvard.edu/MST/simul/software/docs/pkgs/pgsql/glossary/glossary.html</A></P>
1255 <H4><A name="4.18">4.18</A>) Why do I get the error <I>"ERROR:
1256 Memory exhausted in AllocSetAlloc()"</I>?</H4>
1258 <P>You probably have run out of virtual memory on your system,
1259 or your kernel has a low limit for certain resources. Try this
1260 before starting <I>postmaster</I>:</P>
1266 Depending on your shell, only one of these may succeed, but it will
1267 set your process data segment limit much higher and perhaps allow
1268 the query to complete. This command applies to the current process,
1269 and all subprocesses created after the command is run. If you are
1270 having a problem with the <SMALL>SQL</SMALL> client because the
1271 backend is returning too much data, try it before starting the
1274 <H4><A name="4.19">4.19</A>) How do I tell what PostgreSQL version
1277 <P>From <I>psql</I>, type <CODE>SELECT version();</CODE></P>
1279 <H4><A name="4.20">4.20</A>) Why does my large-object operations
1280 get <I>"invalid large obj descriptor"</I>?</H4>
1282 <P>You need to put <CODE>BEGIN WORK</CODE> and <CODE>COMMIT</CODE>
1283 around any use of a large object handle, that is, surrounding
1284 <CODE>lo_open</CODE> ... <CODE>lo_close.</CODE></P>
1286 <P>Currently PostgreSQL enforces the rule by closing large object
1287 handles at transaction commit. So the first attempt to do anything
1288 with the handle will draw <I>invalid large obj descriptor</I>. So
1289 code that used to work (at least most of the time) will now
1290 generate that error message if you fail to use a transaction.</P>
1292 <P>If you are using a client interface like <SMALL>ODBC</SMALL> you
1293 may need to set <CODE>auto-commit off.</CODE></P>
1295 <H4><A name="4.21">4.21</A>) How do I create a column that will
1296 default to the current time?</H4>
1298 <P>Use <I>CURRENT_TIMESTAMP</I>:</P>
1300 <CODE>CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1304 <H4><A name="4.22">4.22</A>) Why are my subqueries using
1305 <CODE><SMALL>IN</SMALL></CODE> so slow?</H4>
1307 <P>In versions prior to 7.4, subqueries were joined to outer queries
1308 by sequentially scanning the result of the subquery for each row of
1309 the outer query. If the subquery returns only a few rows and the outer
1310 query returns many rows, <CODE><SMALL>IN</SMALL></CODE> is fastest. To
1311 speed up other queries, replace <CODE>IN</CODE> with
1312 <CODE>EXISTS</CODE>:</P>
1315 WHERE col IN (SELECT subcol FROM subtab);
1320 WHERE EXISTS (SELECT subcol FROM subtab WHERE subcol = col);
1323 For this to be fast, <CODE>subcol</CODE> should be an indexed column.
1324 <P>In version 7.4 and later, <CODE>IN</CODE> actually uses the same
1325 sophisticated join techniques as normal queries, and is prefered
1326 to using <CODE>EXISTS</CODE>.
1328 <H4><A name="4.23">4.23</A>) How do I perform an outer join?</H4>
1330 <P>PostgreSQL supports outer joins using the SQL standard syntax.
1331 Here are two examples:</P>
1334 FROM t1 LEFT OUTER JOIN t2 ON (t1.col = t2.col);
1339 FROM t1 LEFT OUTER JOIN t2 USING (col);
1342 <P>These identical queries join t1.col to t2.col, and also return
1343 any unjoined rows in t1 (those with no match in t2). A
1344 <SMALL>RIGHT</SMALL> join would add unjoined rows of t2. A
1345 <SMALL>FULL</SMALL> join would return the matched rows plus all
1346 unjoined rows from t1 and t2. The word <SMALL>OUTER</SMALL> is
1347 optional and is assumed in <SMALL>LEFT</SMALL>,
1348 <SMALL>RIGHT</SMALL>, and <SMALL>FULL</SMALL> joins. Ordinary joins
1349 are called <SMALL>INNER</SMALL> joins.</P>
1351 <P>In previous releases, outer joins can be simulated using
1352 <SMALL>UNION</SMALL> and <SMALL>NOT IN</SMALL>. For example, when
1353 joining <I>tab1</I> and <I>tab2</I>, the following query does an
1354 <I>outer</I> join of the two tables:<BR>
1358 SELECT tab1.col1, tab2.col2
1360 WHERE tab1.col1 = tab2.col1
1362 SELECT tab1.col1, NULL
1364 WHERE tab1.col1 NOT IN (SELECT tab2.col1 FROM tab2)
1368 <H4><A name="4.24">4.24</A>) How do I perform queries using
1369 multiple databases?</H4>
1371 <P>There is no way to query a database other than the current one.
1372 Because PostgreSQL loads database-specific system catalogs, it is
1373 uncertain how a cross-database query should even behave.</P>
1375 <P><I>contrib/dblink</I> allows cross-database queries using
1376 function calls. Of course, a client can make simultaneous
1377 connections to different databases and merge the results on the
1380 <H4><A name="4.25">4.25</A>) How do I return multiple rows or
1381 columns from a function?</H4>
1383 <P>In 7.3, you can easily return multiple rows or columns from a
1385 <a href="http://techdocs.postgresql.org/guides/SetReturningFunctions">
1386 http://techdocs.postgresql.org/guides/SetReturningFunctions</a>.
1388 <H4><A name="4.26">4.26</A>) Why can't I reliably create/drop
1389 temporary tables in PL/PgSQL functions?</H4>
1390 <P>PL/PgSQL caches function contents, and an unfortunate side effect
1391 is that if a PL/PgSQL function accesses a temporary table, and that
1392 table is later dropped and recreated, and the function called
1393 again, the function will fail because the cached function contents
1394 still point to the old temporary table. The solution is to use
1395 <SMALL>EXECUTE</SMALL> for temporary table access in PL/PgSQL. This
1396 will cause the query to be reparsed every time.</P>
1398 <H4><A name="4.27">4.27</A>) What encryption options are available?
1401 <LI><I>contrib/pgcrypto</I> contains many encryption functions for
1402 use in <SMALL>SQL</SMALL> queries.</LI>
1403 <LI>To encrypt transmission from the client to the server, the server
1404 must have the <I>ssl</I> option set to <I>true</I> in <I>postgresql.conf,
1405 </I> and an applicable <I>host</I> or <I>hostssl</I> record must exist in
1406 <I>pg_hba.conf</I>, and the client <I>sslmode</I> must not be
1407 <I>disable.</I> (Note that it is also possible to use a third-party
1408 encrypted transport, such as stunnel or ssh, rather than PostgreSQL's
1409 native SSL connections.)
1410 <LI>Database user passwords are automatically encrypted when stored in
1411 version 7.3. In previous versions, you must enable the option
1412 <I>PASSWORD_ENCRYPTION</I> in <I>postgresql.conf</I>.</LI>
1413 <LI>The server can run using an encrypted file system.</LI>
1418 <H2 align="center">Extending PostgreSQL</H2>
1420 <H4><A name="5.1">5.1</A>) I wrote a user-defined function. When I
1421 run it in <I>psql</I>, why does it dump core?</H4>
1423 <P>The problem could be a number of things. Try testing your
1424 user-defined function in a stand-alone test program first.</P>
1426 <H4><A name="5.2">5.2</A>) How can I contribute some nifty new
1427 types and functions to PostgreSQL?</H4>
1429 <P>Send your extensions to the <I>pgsql-hackers</I> mailing list,
1430 and they will eventually end up in the <I>contrib/</I>
1433 <H4><A name="5.3">5.3</A>) How do I write a C function to return a
1436 <P>In versions of PostgreSQL beginning with 7.3, table-returning
1437 functions are fully supported in C, PL/PgSQL, and SQL. See the
1438 Programmer's Guide for more information. An example of a
1439 table-returning function defined in C can be found in
1440 <I>contrib/tablefunc</I>.</P>
1442 <H4><A name="5.4">5.4</A>) I have changed a source file. Why does
1443 the recompile not see the change?</H4>
1445 <P>The <I>Makefiles</I> do not have the proper dependencies for
1446 include files. You have to do a <I>make clean</I> and then another
1447 <I>make</I>. If you are using <SMALL>GCC</SMALL> you can use the
1448 <I>--enable-depend</I> option of <I>configure</I> to have the
1449 compiler compute the dependencies automatically.</P>