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: Mon Mar 29 00:07:11 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>
81 <H2 align="center">Operational Questions</H2>
82 <A href="#4.1">4.1</A>) What is the difference between binary
83 cursors and normal cursors?<BR>
84 <A href="#4.2">4.2</A>) How do I <SMALL>SELECT</SMALL> only the
85 first few rows of a query? A random row?<BR>
86 <A href="#4.3">4.3</A>) How do I get a list of tables or other
87 things I can see in <I>psql</I>?<BR>
88 <A href="#4.4">4.4</A>) How do you remove a column from a
89 table, or change it's data type?<BR>
90 <A href="#4.5">4.5</A>) What is the maximum size for a row, a
91 table, and a database?<BR>
92 <A href="#4.6">4.6</A>) How much database disk space is required
93 to store data from a typical text file?<BR>
94 <A href="#4.7">4.7</A>) How do I find out what tables, indexes,
95 databases, and users are defined?<BR>
96 <A href="#4.8">4.8</A>) My queries are slow or don't make use of
98 <A href="#4.9">4.9</A>) How do I see how the query optimizer is
99 evaluating my query?<BR>
100 <A href="#4.10">4.10</A>) What is an R-tree index?<BR>
101 <A href="#4.11">4.11</A>) What is the Genetic Query Optimizer?<BR>
102 <A href="#4.12">4.12</A>) How do I perform regular expression
103 searches and case-insensitive regular expression searches? How do I
104 use an index for case-insensitive searches?<BR>
105 <A href="#4.13">4.13</A>) In a query, how do I detect if a field
106 is <SMALL>NULL</SMALL>?<BR>
107 <A href="#4.14">4.14</A>) What is the difference between the
108 various character types?<BR>
109 <A href="#4.15.1">4.15.1</A>) How do I create a
110 serial/auto-incrementing field?<BR>
111 <A href="#4.15.2">4.15.2</A>) How do I get the value of a
112 <SMALL>SERIAL</SMALL> insert?<BR>
113 <A href="#4.15.3">4.15.3</A>) Don't <I>currval()</I> and
114 <I>nextval()</I> lead to a race condition with other users?<BR>
115 <A href="#4.15.4">4.15.4</A>) Why aren't my sequence numbers
116 reused on transaction abort? Why are there gaps in the numbering of
117 my sequence/SERIAL column?<BR>
118 <A href="#4.16">4.16</A>) What is an <SMALL>OID</SMALL>? What is a
119 <SMALL>TID</SMALL>?<BR>
120 <A href="#4.17">4.17</A>) What is the meaning of some of the terms
121 used in PostgreSQL?<BR>
122 <A href="#4.18">4.18</A>) Why do I get the error <I>"ERROR: Memory
123 exhausted in AllocSetAlloc()"</I>?<BR>
124 <A href="#4.19">4.19</A>) How do I tell what PostgreSQL version I
126 <A href="#4.20">4.20</A>) Why does my large-object operations get
127 <I>"invalid large obj descriptor"</I>?<BR>
128 <A href="#4.21">4.21</A>) How do I create a column that will
129 default to the current time?<BR>
130 <A href="#4.22">4.22</A>) Why are my subqueries using
131 <CODE><SMALL>IN</SMALL></CODE> so slow?<BR>
132 <A href="#4.23">4.23</A>) How do I perform an outer join?<BR>
133 <A href="#4.24">4.24</A>) How do I perform queries using multiple
135 <A href="#4.25">4.25</A>) How do I return multiple rows or columns
137 <A href="#4.26">4.26</A>) Why can't I reliably create/drop
138 temporary tables in PL/PgSQL functions?<BR>
139 <A href="#4.27">4.27</A>) What replication options are available?<BR>
140 <A href="#4.28">4.28</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><STRONG>Client</STRONG></P>
242 <P>It is possible to compile the <I>libpq</I> C library, psql, and
243 other interfaces and client applications to run on MS Windows platforms.
244 In this case, the client is running on MS Windows, and communicates
245 via TCP/IP to a server running on one of our supported Unix
246 platforms. A file <I>win32.mak</I> is included in the distribution
247 for making a Win32 <I>libpq</I> library and <I>psql</I>. PostgreSQL
248 also communicates with <SMALL>ODBC</SMALL> clients.</P>
250 <P><STRONG>Server</STRONG></P>
252 <P>The database server can run on Windows NT and Win2k using
253 Cygwin, the Cygnus Unix/NT porting library. See
254 <I>pgsql/doc/FAQ_MSWIN</I> in the distribution or the MS Windows FAQ
255 at <A href="http://www.PostgreSQL.org/docs/faqs/text/FAQ_MSWIN">
256 http://www.PostgreSQL.org/docs/faqs/text/FAQ_MSWIN</A>.</P>
258 <p>A native port to MS Win NT/2000/XP is currently being worked
259 on. For more details on the current status of PostgreSQL on Windows see
260 <a href="http://techdocs.postgresql.org/guides/Windows">
261 http://techdocs.postgresql.org/guides/Windows</a> and
262 <a href="http://momjian.postgresql.org/main/writings/pgsql/win32.html">
263 http://momjian.postgresql.org/main/writings/pgsql/win32.html</a>.</p>
265 <p>There is also a Novell Netware 6 port at
266 <a href="http://forge.novell.com">http://forge.novell.com</a>.</p>
268 <H4><A name="1.5">1.5</A>) Where can I get PostgreSQL?</H4>
270 <P>The primary anonymous ftp site for PostgreSQL is <A href=
271 "ftp://ftp.PostgreSQL.org/pub">ftp://ftp.PostgreSQL.org/pub</A>.
272 For mirror sites, see our main web site.</P>
274 <H4><A name="1.6">1.6</A>) Where can I get support?</H4>
276 <P>The main mailing list is: <A href=
277 "mailto:pgsql-general@PostgreSQL.org">pgsql-general@PostgreSQL.org</A>.
278 It is available for discussion of matters pertaining to PostgreSQL.
279 To subscribe, send mail with the following lines in the body (not
280 the subject line):</P>
287 "mailto:pgsql-general-request@PostgreSQL.org">pgsql-general-request@PostgreSQL.org</A>.</P>
289 <P>There is also a digest list available. To subscribe to this
290 list, send email to: <A href=
291 "mailto:pgsql-general-digest-request@PostgreSQL.org">pgsql-general-digest-request@PostgreSQL.org</A>
298 Digests are sent out to members of this list whenever the main list
299 has received around 30k of messages.
301 <P>The bugs mailing list is available. To subscribe to this list,
302 send email to <A href=
303 "mailto:pgsql-bugs-request@PostgreSQL.org">pgsql-bugs-request@PostgreSQL.org</A>
310 There is also a developers discussion mailing list available. To
311 subscribe to this list, send email to <A href=
312 "mailto:pgsql-hackers-request@PostgreSQL.org">pgsql-hackers-request@PostgreSQL.org</A>
319 <P>Additional mailing lists and information about PostgreSQL can be
320 found via the PostgreSQL WWW home page at:</P>
323 <A href="http://www.PostgreSQL.org">http://www.PostgreSQL.org</A>
326 <P>There is also an IRC channel on Freenode and EFNet,
327 channel <I>#PostgreSQL</I>. You can use the Unix command <CODE>irc -c
328 '#PostgreSQL' "$USER" irc.phoenix.net.</CODE> or <CODE>irc -c
329 '#PostgreSQL' "$USER" irc.freenode.net.</CODE></P>
331 <P>A list of commercial support companies is available at <A href=
332 "http://techdocs.postgresql.org/companies.php">http://techdocs.postgresql.org/companies.php</A>.</P>
334 <H4><A name="1.7">1.7</A>) What is the latest release?</H4>
336 <P>The latest release of PostgreSQL is version 7.4.2.</P>
338 <P>We plan to have major releases every six to eight months.</P>
340 <H4><A name="1.8">1.8</A>) What documentation is available?</H4>
342 <P>Several manuals, manual pages, and some small test examples are
343 included in the distribution. See the <I>/doc</I> directory. You
344 can also browse the manuals online at <A href=
345 "http://www.PostgreSQL.org/docs">http://www.PostgreSQL.org/docs</A>.</P>
347 <P>There are two PostgreSQL books available online at <A href=
348 "http://www.PostgreSQL.org/docs/awbook.html">http://www.PostgreSQL.org/docs/awbook.html</A>
350 "http://www.commandprompt.com/ppbook/">http://www.commandprompt.com/ppbook/</A>.
351 There is a list of PostgreSQL books available for purchase at <A
353 "http://techdocs.postgresql.org/techdocs/bookreviews.php">http://techdocs.PostgreSQL.org/techdocs/bookreviews.php</A>.
354 There is also a collection of PostgreSQL technical articles at <A
356 "http://techdocs.PostgreSQL.org/">http://techdocs.PostgreSQL.org/</A>.</P>
358 <P><I>psql</I> has some nice \d commands to show information about
359 types, operators, functions, aggregates, etc.</P>
361 <P>Our web site contains even more documentation.</P>
363 <H4><A name="1.9">1.9</A>) How do I find out about known bugs or
364 missing features?</H4>
366 <P>PostgreSQL supports an extended subset of <SMALL>SQL</SMALL>-92.
367 See our <A href="http://developer.PostgreSQL.org/todo.php">TODO</A>
368 list for known bugs, missing features, and future plans.</P>
370 <H4><A name="1.10">1.10</A>) How can I learn
371 <SMALL>SQL</SMALL>?</H4>
373 <P>The PostgreSQL book at <A href=
374 "http://www.PostgreSQL.org/docs/awbook.html">http://www.PostgreSQL.org/docs/awbook.html</A>
375 teaches <SMALL>SQL</SMALL>. There is another PostgreSQL book at <A
377 "http://www.commandprompt.com/ppbook/">http://www.commandprompt.com/ppbook.</A>
378 There is a nice tutorial at <A href=
379 "http://www.intermedia.net/support/sql/sqltut.shtm">http://www.intermedia.net/support/sql/sqltut.shtm,</A>
381 "http://ourworld.compuserve.com/homepages/graeme_birchall/HTM_COOK.HTM">
382 http://ourworld.compuserve.com/homepages/graeme_birchall/HTM_COOK.HTM,</A>
384 "http://sqlcourse.com/">http://sqlcourse.com.</A></P>
386 <P>Another one is "Teach Yourself SQL in 21 Days, Second Edition"
388 "http://members.tripod.com/er4ebus/sql/index.htm">http://members.tripod.com/er4ebus/sql/index.htm</A></P>
390 <P>Many of our users like <I>The Practical SQL Handbook</I>,
391 Bowman, Judith S., et al., Addison-Wesley. Others like <I>The
392 Complete Reference SQL</I>, Groff et al., McGraw-Hill.</P>
394 <H4><A name="1.11">1.11</A>) Is PostgreSQL Y2K compliant?</H4>
396 <P>Yes, we easily handle dates past the year 2000 AD, and before
399 <H4><A name="1.12">1.12</A>) How do I join the development
402 <P>First, download the latest source and read the PostgreSQL
403 Developers documentation on our web site, or in the distribution.
404 Second, subscribe to the <I>pgsql-hackers</I> and
405 <I>pgsql-patches</I> mailing lists. Third, submit high quality
406 patches to pgsql-patches.</P>
408 <P>There are about a dozen people who have commit privileges to the
409 PostgreSQL <SMALL>CVS</SMALL> archive. They each have submitted so
410 many high-quality patches that it was impossible for the existing
411 committers to keep up, and we had confidence that patches they
412 committed were of high quality.</P>
414 <H4><A name="1.13">1.13</A>) How do I submit a bug report?</H4>
416 <P>Please visit the PostgreSQL BugTool page at <A href=
417 "http://www.PostgreSQL.org/bugs/bugs.php">http://www.PostgreSQL.org/bugs/bugs.php</A>,
418 which gives guidelines and directions on how to submit a
421 <P>Also check out our ftp site <A href=
422 "ftp://ftp.PostgreSQL.org/pub">ftp://ftp.PostgreSQL.org/pub</A> to
423 see if there is a more recent PostgreSQL version or patches.</P>
425 <H4><A name="1.14">1.14</A>) How does PostgreSQL compare to other
426 <SMALL>DBMS</SMALL>s?</H4>
428 <P>There are several ways of measuring software: features,
429 performance, reliability, support, and price.</P>
432 <DT><B>Features</B></DT>
434 <DD>PostgreSQL has most features present in large commercial
435 <SMALL>DBMS</SMALL>s, like transactions, subselects, triggers,
436 views, foreign key referential integrity, and sophisticated
437 locking. We have some features they do not have, like
438 user-defined types, inheritance, rules, and multi-version
439 concurrency control to reduce lock contention.<BR>
443 <DT><B>Performance</B></DT>
445 <DD>PostgreSQL has performance similar to other commercial and
446 open source databases. it is faster for some things, slower for
447 others. In comparison to MySQL or leaner database systems, we are
448 faster for multiple users, complex queries, and a read/write query
449 load. MySQL is faster for simple SELECT queries done by a few users.
450 Of course, MySQL does not have most of the features mentioned in the
451 <I>Features</I> section above. We are built for reliability and
452 features, and we continue to improve performance in every
453 release. There is an interesting Web page comparing PostgreSQL to
454 MySQL at <A href="http://openacs.org/philosophy/why-not-mysql.html">
455 http://openacs.org/philosophy/why-not-mysql.html</A> Also, MySQL is
456 is a company that distributes its products via open source, and requires
457 a commercial license for close-source software, not an
458 open source development community like PostgreSQL.<BR>
463 <DT><B>Reliability</B></DT>
465 <DD>We realize that a <SMALL>DBMS</SMALL> must be reliable, or it
466 is worthless. We strive to release well-tested, stable code that
467 has a minimum of bugs. Each release has at least one month of
468 beta testing, and our release history shows that we can provide
469 stable, solid releases that are ready for production use. We
470 believe we compare favorably to other database software in this
475 <DT><B>Support</B></DT>
477 <DD>Our mailing lists provide contact with a large group of developers
478 and users to help resolve any problems encountered. While we cannot
479 guarantee a fix, commercial <SMALL>DBMS</SMALL>s do not always
480 supply a fix either. Direct access to developers, the user
481 community, manuals, and the source code often make PostgreSQL
482 support superior to other <SMALL>DBMS</SMALL>s. There is
483 commercial per-incident support available for those who need it.
484 (See <A href="#1.6">FAQ section 1.6</A>.)<BR>
488 <DT><B>Price</B></DT>
490 <DD>We are free for all use, both commercial and non-commercial.
491 You can add our code to your product with no limitations, except
492 those outlined in our BSD-style license stated above.<BR>
497 <H4><A name="1.15">1.15</A>) How can I financially assist
500 <P>PostgreSQL has had a first-class infrastructure since we started
501 in 1996. This is all thanks to Marc Fournier, who has created
502 and managed this infrastructure over the years.</P>
504 <P>Quality infrastructure is very important to an open-source
505 project. It prevents disruptions that can greatly delay forward
506 movement of the project.</P>
508 <P>Of course, this infrastructure is not cheap. There are a variety
509 of monthly and one-time expenses that are required to keep it
510 going. If you or your company has money it can donate to help fund
511 this effort, please go to <A href="http://store.pgsql.com/shopping/">http://store.pgsql.com/shopping/</A>
512 and make a donation.</P>
514 <P>Although the web page mentions PostgreSQL, Inc, the
515 "contributions" item is solely to support the PostgreSQL project
516 and does not fund any specific company. If you prefer, you can also
517 send a check to the contact address.</P>
520 <P>Also, if you have a success story about PostgreSQL, please submit
521 it to our advocacy site at <a href="http://advocacy.postgresql.org">
522 http://advocacy.postgresql.org</a>.</P>
525 <H2 align="center">User Client Questions</H2>
527 <H4><A name="2.1">2.1</A>) Are there <SMALL>ODBC</SMALL> drivers
530 <P>There are two <SMALL>ODBC</SMALL> drivers available, PsqlODBC
531 and OpenLink <SMALL>ODBC</SMALL>.</P>
533 <P>You can download PsqlODBC from <A href=
534 "http://gborg.postgresql.org/project/psqlodbc/projdisplay.php">
535 http://gborg.postgresql.org/project/psqlodbc/projdisplay.php</A>.</P>
537 <P>OpenLink <SMALL>ODBC</SMALL> can be gotten from <A href=
538 "http://www.openlinksw.com/">http://www.openlinksw.com</A>. It
539 works with their standard <SMALL>ODBC</SMALL> client software so
540 you'll have PostgreSQL <SMALL>ODBC</SMALL> available on every
541 client platform they support (Win, Mac, Unix, VMS).</P>
543 <P>They will probably be selling this product to people who need
544 commercial-quality support, but a freeware version will always be
545 available. Please send questions to <A href=
546 "mailto:postgres95@openlink.co.uk">postgres95@openlink.co.uk</A>.</P>
548 <H4><A name="2.2">2.2</A>) What tools are available for using
549 PostgreSQL with Web pages?</H4>
551 <P>A nice introduction to Database-backed Web pages can be seen at:
552 <A href="http://www.webreview.com">http://www.webreview.com</A></P>
554 <P>For Web integration, PHP is an excellent interface. It is at <A
555 href="http://www.php.net">http://www.php.net</A>.</P>
557 <P>For complex cases, many use the Perl interface and CGI.pm or mod_perl.</P>
559 <H4><A name="2.3">2.3</A>) Does PostgreSQL have a graphical user
562 <P>Yes, there are several graphical interfaces to PostgreSQL available.
563 These include PgAccess <a href="http://www.pgaccess.org">
564 http://www.pgaccess.org</a>), PgAdmin III (<a
565 href="http://www.pgadmin.org">http://www.pgadmin.org</a>, RHDB Admin (<a
566 href="http://sources.redhat.com/rhdb/">http://sources.redhat.com/rhdb/
567 </a>) and Rekall (<a href="http://www.thekompany.com/products/rekall/">
568 http://www.thekompany.com/products/rekall/</a>, proprietary). There is
569 also PhpPgAdmin (<a href="http://phppgadmin.sourceforge.net/">
570 http://phppgadmin.sourceforge.net/ </a>), a web-based interface to
573 <P>See <a href="http://techdocs.postgresql.org/guides/GUITools">http://techdocs.postgresql.org/guides/GUITools</a> for a more detailed list.</P>
575 <H4><A name="2.4">2.4</A>) What languages are able to communicate with
578 <P>Most popular programming languages contain an interface to
579 PostgreSQL. Check your programming language's list of extension
582 <P>The following interfaces are included in the PostgreSQL
588 <LI>Embedded C (ecpg)</LI>
592 <LI>Python (PyGreSQL)</LI>
594 <LI>TCL (libpgtcl)</LI>
597 <P>Additional interfaces are available at
598 <a href="http://gborg.postgresql.org">http://gborg.postgresql.org</A>
599 in the <I>Drivers/Interfaces</I> section.
603 <H2 align="center">Administrative Questions</H2>
605 <H4><A name="3.1">3.1</A>) How do I install PostgreSQL somewhere
606 other than <I>/usr/local/pgsql</I>?</H4>
608 <P>Specify the <I>--prefix</I> option when running
609 <I>configure</I>.</P>
611 <H4><A name="3.2">3.2</A>) When I start <I>postmaster</I>, I get a
612 <I>Bad System Call</I> or core dumped message. Why?</H4>
614 <P>It could be a variety of problems, but first check to see that
615 you have System V extensions installed in your kernel. PostgreSQL
616 requires kernel support for shared memory and semaphores.</P>
618 <H4><A name="3.3">3.3</A>) When I try to start <I>postmaster</I>, I
619 get <I>IpcMemoryCreate</I> errors. Why?</H4>
621 <P>You either do not have shared memory configured properly in your
622 kernel or you need to enlarge the shared memory available in the
623 kernel. The exact amount you need depends on your architecture and
624 how many buffers and backend processes you configure for
625 <I>postmaster</I>. For most systems, with default numbers of
626 buffers and processes, you need a minimum of ~1 MB. See the <A
628 "http://www.PostgreSQL.org/docs/view.php?version=current&idoc=1&file=kernel-resources.html">PostgreSQL
629 Administrator's Guide</A> for more detailed information about
630 shared memory and semaphores.</P>
632 <H4><A name="3.4">3.4</A>) When I try to start <I>postmaster</I>, I
633 get <I>IpcSemaphoreCreate</I> errors. Why?</H4>
635 <P>If the error message is <I>IpcSemaphoreCreate: semget failed (No
636 space left on device)</I> then your kernel is not configured with
637 enough semaphores. Postgres needs one semaphore per potential
638 backend process. A temporary solution is to start <I>postmaster</I>
639 with a smaller limit on the number of backend processes. Use
640 <I>-N</I> with a parameter less than the default of 32. A more
641 permanent solution is to increase your kernel's
642 <SMALL>SEMMNS</SMALL> and <SMALL>SEMMNI</SMALL> parameters.</P>
644 <P>Inoperative semaphores can also cause crashes during heavy
647 <P>If the error message is something else, you might not have
648 semaphore support configured in your kernel at all. See the
649 PostgreSQL Administrator's Guide for more detailed information
650 about shared memory and semaphores.</P>
652 <H4><A name="3.5">3.5</A>) How do I control connections from other
655 <P>By default, PostgreSQL only allows connections from the local
656 machine using Unix domain sockets. Other machines will not be able
657 to connect unless you turn on tcpip_sockets in the postgresql.conf
658 <B>and</B> enable host-based authentication by modifying the file
659 <I>$PGDATA/pg_hba.conf</I> accordingly. This will allow TCP/IP
662 <H4><A name="3.6">3.6</A>) How do I tune the database engine for
663 better performance?</H4>
665 <P>Certainly, indexes can speed up queries. The
666 <SMALL>EXPLAIN ANALYZE</SMALL> command allows you to see how PostgreSQL is
667 interpreting your query, and which indexes are being used.</P>
669 <P>If you are doing many <SMALL>INSERTs</SMALL>, consider doing
670 them in a large batch using the <SMALL>COPY</SMALL> command. This
671 is much faster than individual <SMALL>INSERTS</SMALL>. Second,
672 statements not in a <SMALL>BEGIN WORK/COMMIT</SMALL> transaction
673 block are considered to be in their own transaction. Consider
674 performing several statements in a single transaction block. This
675 reduces the transaction overhead. Also, consider dropping and
676 recreating indexes when making large data changes.</P>
678 <P>There are several tuning options. You can disable <I>fsync()</I>
679 by starting <I>postmaster</I> with a <I>-o -F</I> option. This will
680 prevent <I>fsync()</I>s from flushing to disk after every
683 <P>You can also use the <I>postmaster</I> <I>-B</I> option to
684 increase the number of shared memory buffers used by the backend
685 processes. If you make this parameter too high, the
686 <I>postmaster</I> may not start because you have exceeded your
687 kernel's limit on shared memory space. Each buffer is 8K and the
688 default is 64 buffers.</P>
690 <P>You can also use the backend <I>-S</I> option to increase the
691 maximum amount of memory used by the backend process for temporary
692 sorts. The <I>-S</I> value is measured in kilobytes, and the
693 default is 512 (i.e. 512K).</P>
695 <P>You can also use the <SMALL>CLUSTER</SMALL> command to group
696 data in tables to match an index. See the <SMALL>CLUSTER</SMALL>
697 manual page for more details.</P>
699 <H4><A name="3.7">3.7</A>) What debugging features are
702 <P>PostgreSQL has several features that report status information
703 that can be valuable for debugging purposes.</P>
705 <P>First, by running <I>configure</I> with the --enable-cassert
706 option, many <I>assert()</I>s monitor the progress of the backend
707 and halt the program when something unexpected occurs.</P>
709 <P>Both <I>postmaster</I> and <I>postgres</I> have several debug
710 options available. First, whenever you start <I>postmaster</I>,
711 make sure you send the standard output and error to a log file,
715 ./bin/postmaster >server.log 2>&1 &
718 <P>This will put a server.log file in the top-level PostgreSQL
719 directory. This file contains useful information about problems or
720 errors encountered by the server. <I>Postmaster</I> has a <I>-d</I>
721 option that allows even more detailed information to be reported.
722 The <I>-d</I> option takes a number that specifies the debug level.
723 Be warned that high debug level values generate large log
726 <P>If <I>postmaster</I> is not running, you can actually run the
727 <I>postgres</I> backend from the command line, and type your
728 <SMALL>SQL</SMALL> statement directly. This is recommended
729 <B>only</B> for debugging purposes. Note that a newline terminates
730 the query, not a semicolon. If you have compiled with debugging
731 symbols, you can use a debugger to see what is happening. Because
732 the backend was not started from <I>postmaster</I>, it is not
733 running in an identical environment and locking/backend interaction
734 problems may not be duplicated.</P>
736 <P>If <I>postmaster</I> is running, start <I>psql</I> in one
737 window, then find the <SMALL>PID</SMALL> of the <I>postgres</I>
738 process used by <I>psql</I>. Use a debugger to attach to the
739 <I>postgres</I> <SMALL>PID</SMALL>. You can set breakpoints in the
740 debugger and issue queries from <I>psql</I>. If you are debugging
741 <I>postgres</I> startup, you can set PGOPTIONS="-W n", then start
742 <I>psql</I>. This will cause startup to delay for <I>n</I> seconds
743 so you can attach to the process with the debugger, set any
744 breakpoints, and continue through the startup sequence.</P>
746 <P>The <I>postgres</I> program has <I>-s, -A</I>, and <I>-t</I>
747 options that can be very useful for debugging and performance
750 <P>You can also compile with profiling to see what functions are
751 taking execution time. The backend profile files will be deposited
752 in the <I>pgsql/data/base/dbname</I> directory. The client profile
753 file will be put in the client's current directory. Linux requires
754 a compile with <I>-DLINUX_PROFILE</I> for proper profiling.</P>
756 <H4><A name="3.8">3.8</A>) Why do I get <I>"Sorry, too many
757 clients"</I> when trying to connect?</H4>
759 <P>You need to increase <I>postmaster</I>'s limit on how many
760 concurrent backend processes it can start.</P>
762 <P>The default limit is 32 processes. You can increase it by
763 restarting <I>postmaster</I> with a suitable <I>-N</I> value or
764 modifying <I>postgresql.conf</I>.</P>
766 <P>Note that if you make <I>-N</I> larger than 32, you must also
767 increase <I>-B</I> beyond its default of 64; <I>-B</I> must be at
768 least twice <I>-N</I>, and probably should be more than that for
769 best performance. For large numbers of backend processes, you are
770 also likely to find that you need to increase various Unix kernel
771 configuration parameters. Things to check include the maximum size
772 of shared memory blocks, <SMALL>SHMMAX;</SMALL> the maximum number
773 of semaphores, <SMALL>SEMMNS</SMALL> and <SMALL>SEMMNI;</SMALL> the
774 maximum number of processes, <SMALL>NPROC;</SMALL> the maximum
775 number of processes per user, <SMALL>MAXUPRC;</SMALL> and the
776 maximum number of open files, <SMALL>NFILE</SMALL> and
777 <SMALL>NINODE</SMALL>. The reason that PostgreSQL has a limit on
778 the number of allowed backend processes is so your system won't run
779 out of resources.</P>
781 <H4><A name="3.9">3.9</A>) What is in the <I>pgsql_tmp</I> directory?</H4>
783 <P>This directory contains temporary files generated by the query
784 executor. For example, if a sort needs to be done to satisfy an
785 <SMALL>ORDER BY</SMALL> and the sort requires more space than the
786 backend's <I>-S</I> parameter allows, then temporary files are created
787 here to hold the extra data.</P>
789 <P>The temporary files are usually deleted automatically, but might
790 remain if a backend crashes during a sort. A stop and restart of the
791 <I>postmaster</I> will remove files from those directories.</P>
793 <H4><A name="3.10">3.10</A>) Why do I need to do a dump and restore
794 to upgrade between major PostgreSQL releases?</H4>
796 <P>The PostgreSQL team makes only small changes between minor releases,
797 so upgrading from 7.2 to 7.2.1 does not require a dump and restore.
798 However, major releases (e.g. from 7.2 to 7.3) often change the internal
799 format of system tables and data files. These changes are often complex,
800 so we don't maintain backward compatability for data files. A dump outputs
801 data in a generic format that can then be loaded in using the new internal
804 <P>In releases where the on-disk format does not change, the
805 <I>pg_upgrade</I> script can be used to upgrade without a dump/restore.
806 The release notes mention whether <I>pg_upgrade</I> is available for the
811 <H2 align="center">Operational Questions</H2>
813 <H4><A name="4.1">4.1</A>) What is the difference between binary
814 cursors and normal cursors?</H4>
816 <P>See the <SMALL>DECLARE</SMALL> manual page for a
819 <H4><A name="4.2">4.2</A>) How do I <SMALL>SELECT</SMALL> only the
820 first few rows of a query? A random row?</H4>
822 <P>See the <SMALL>FETCH</SMALL> manual page, or use
823 <SMALL>SELECT</SMALL> ... <SMALL>LIMIT</SMALL>....</P>
825 <P>The entire query may have to be evaluated, even if you only want
826 the first few rows. Consider using a query that has an <SMALL>ORDER
827 BY</SMALL>. If there is an index that matches the <SMALL>ORDER
828 BY</SMALL>, PostgreSQL may be able to evaluate only the first few
829 records requested, or the entire query may have to be evaluated
830 until the desired rows have been generated.</P>
832 <P>To <SMALL>SELECT</SMALL> a random row, use:
840 <H4><A name="4.3">4.3</A>) How do I get a list of tables or other
841 things I can see in <I>psql</I>?</H4>
843 <P>Use the \dt command to see tables in <I>psql</I>. For a complete list of
844 commands inside psql you can use \?. Alternatively you can read the source
845 code for <I>psql</I> in file <I>pgsql/src/bin/psql/describe.c</I>, it
846 contains <SMALL>SQL</SMALL> commands that generate the output for
847 <I>psql</I>'s backslash commands. You can also start <I>psql</I> with the
848 <I>-E</I> option so it will print out the queries it uses to execute the
849 commands you give. PostgreSQL also provides an <SMALL>SQLi</SMALL> compliant
850 INFORMATION SCHEMA interface you can query to get information about the
853 <H4><A name="4.4">4.4</A>) How do you remove a column from a
854 table, or change its data type?</H4>
856 <P><SMALL>DROP COLUMN</SMALL> functionality was added in release 7.3 with
857 <SMALL>ALTER TABLE DROP COLUMN</SMALL>. In earlier versions,
861 LOCK TABLE old_table;
862 SELECT ... -- select all columns but the one you want to remove
865 DROP TABLE old_table;
866 ALTER TABLE new_table RENAME TO old_table;
870 <P>To change the data type of a column, do this:</P>
873 ALTER TABLE tab ADD COLUMN new_col <i>new_data_type</i>;
874 UPDATE tab SET new_col = CAST(old_col AS <i>new_data_type</i>);
875 ALTER TABLE tab DROP COLUMN old_col;
878 <P>You might then want to do <I>VACUUM FULL tab</I> to reclaim the
879 disk space used by the expired rows.</P>
881 <H4><A name="4.5">4.5</A>) What is the maximum size for a row, a
882 table, and a database?</H4>
884 <P>These are the limits:</P>
886 Maximum size for a database? unlimited (32 TB databases exist)
887 Maximum size for a table? 32 TB
888 Maximum size for a row? 1.6TB
889 Maximum size for a field? 1 GB
890 Maximum number of rows in a table? unlimited
891 Maximum number of columns in a table? 250-1600 depending on column types
892 Maximum number of indexes on a table? unlimited
895 Of course, these are not actually unlimited, but limited to
896 available disk space and memory/swap space. Performance may suffer
897 when these values get unusually large.
899 <P>The maximum table size of 32 TB does not require large file
900 support from the operating system. Large tables are stored as
901 multiple 1 GB files so file system size limits are not
904 <P>The maximum table size and maximum number of columns can be
905 quadrupled by increasing the default block size to 32k.</P>
907 <H4><A name="4.6">4.6</A>) How much database disk space is required
908 to store data from a typical text file?</H4>
910 <P>A PostgreSQL database may require up to five times the disk
911 space to store data from a text file.</P>
913 <P>As an example, consider a file of 100,000 lines with an integer
914 and text description on each line. Suppose the text string
915 avergages twenty bytes in length. The flat file would be 2.8 MB.
916 The size of the PostgreSQL database file containing this data can
917 be estimated as 6.4 MB:</P>
919 36 bytes: each row header (approximate)
920 24 bytes: one int field and one text field
921 + 4 bytes: pointer on page to tuple
922 ----------------------------------------
925 The data page size in PostgreSQL is 8192 bytes (8 KB), so:
928 ------------------- = 128 rows per database page (rounded down)
932 -------------------- = 782 database pages (rounded up)
935 782 database pages * 8192 bytes per page = 6,406,144 bytes (6.4 MB)
938 <P>Indexes do not require as much overhead, but do contain the data
939 that is being indexed, so they can be large also.</P>
941 <P><SMALL>NULL</SMALL>s are stored as bitmaps, so they
942 use very little space.</P>
944 <H4><A name="4.7">4.7</A>) How do I find out what tables, indexes,
945 databases, and users are defined?</H4>
947 <P><I>psql</I> has a variety of backslash commands to show such
948 information. Use \? to see them. There are also system tables
949 beginning with <I>pg_</I> that describe these too. Also, <I>psql
950 -l</I> will list all databases.</P>
952 <P>Also try the file <I>pgsql/src/tutorial/syscat.source</I>. It
953 illustrates many of the <SMALL>SELECT</SMALL>s needed to get
954 information from the database system tables.</P>
956 <H4><A name="4.8">4.8</A>) My queries are slow or don't make use of
957 the indexes. Why?</H4>
958 Indexes are not automatically used by every query. Indexes are only
959 used if the table is larger than a minimum size, and the query
960 selects only a small percentage of the rows in the table. This is
961 because the random disk access caused by an index scan can be
962 slower than a straight read through the table, or sequential scan.
964 <P>To determine if an index should be used, PostgreSQL must have
965 statistics about the table. These statistics are collected using
966 <SMALL>VACUUM ANALYZE</SMALL>, or simply <SMALL>ANALYZE</SMALL>.
967 Using statistics, the optimizer knows how many rows are in the
968 table, and can better determine if indexes should be used.
969 Statistics are also valuable in determining optimal join order and
970 join methods. Statistics collection should be performed
971 periodically as the contents of the table change.</P>
973 <P>Indexes are normally not used for <SMALL>ORDER BY</SMALL> or to
974 perform joins. A sequential scan followed by an explicit sort is
975 usually faster than an index scan of a large table.</P>
976 However, <SMALL>LIMIT</SMALL> combined with <SMALL>ORDER BY</SMALL>
977 often will use an index because only a small portion of the table
978 is returned. In fact, though MAX() and MIN() don't use indexes,
979 it is possible to retrieve such values using an index with ORDER BY
984 ORDER BY col [ DESC ]
988 <P>If you believe the optimizer is incorrect in choosing a
989 sequential scan, use <CODE>SET enable_seqscan TO 'off'</CODE> and
990 run tests to see if an index scan is indeed faster.</P>
992 <P>When using wild-card operators such as <SMALL>LIKE</SMALL> or
993 <I>~</I>, indexes can only be used in certain circumstances:</P>
995 <LI>The beginning of the search string must be anchored to the start
998 <LI><SMALL>LIKE</SMALL> patterns must not start with <I>%</I>.</LI>
999 <LI><I>~</I> (regular expression) patterns must start with
1002 <LI>The search string can not start with a character class,
1004 <LI>Case-insensitive searches such as <SMALL>ILIKE</SMALL> and
1005 <I>~*</I> do not utilise indexes. Instead, use functional
1006 indexes, which are described in section <a href="#4.12">4.12</a>.</LI>
1007 <LI>The default <I>C</I> locale must be used during
1012 <H4><A name="4.9">4.9</A>) How do I see how the query optimizer is
1013 evaluating my query?</H4>
1015 <P>See the <SMALL>EXPLAIN</SMALL> manual page.</P>
1017 <H4><A name="4.10">4.10</A>) What is an R-tree index?</H4>
1019 <P>An R-tree index is used for indexing spatial data. A hash index
1020 can't handle range searches. A B-tree index only handles range
1021 searches in a single dimension. R-trees can handle
1022 multi-dimensional data. For example, if an R-tree index can be
1023 built on an attribute of type <I>point</I>, the system can more
1024 efficiently answer queries such as "select all points within a
1025 bounding rectangle."</P>
1027 <P>The canonical paper that describes the original R-tree design
1030 <P>Guttman, A. "R-trees: A Dynamic Index Structure for Spatial
1031 Searching." Proceedings of the 1984 ACM SIGMOD Int'l Conf on Mgmt
1034 <P>You can also find this paper in Stonebraker's "Readings in
1035 Database Systems".</P>
1037 <P>Built-in R-trees can handle polygons and boxes. In theory,
1038 R-trees can be extended to handle higher number of dimensions. In
1039 practice, extending R-trees requires a bit of work and we don't
1040 currently have any documentation on how to do it.</P>
1042 <H4><A name="4.11">4.11</A>) What is the Genetic Query
1045 <P>The <SMALL>GEQO</SMALL> module speeds query optimization when
1046 joining many tables by means of a Genetic Algorithm (GA). It allows
1047 the handling of large join queries through nonexhaustive
1050 <H4><A name="4.12">4.12</A>) How do I perform regular expression
1051 searches and case-insensitive regular expression searches? How do I
1052 use an index for case-insensitive searches?</H4>
1054 <P>The <I>~</I> operator does regular expression matching, and
1055 <I>~*</I> does case-insensitive regular expression matching. The
1056 case-insensitive variant of <SMALL>LIKE</SMALL> is called
1057 <SMALL>ILIKE</SMALL>.</P>
1059 <P>Case-insensitive equality comparisons are normally expressed
1064 WHERE lower(col) = 'abc';
1067 This will not use an standard index. However, if you create a
1068 functional index, it will be used:
1070 CREATE INDEX tabindex ON tab (lower(col));
1073 <H4><A name="4.13">4.13</A>) In a query, how do I detect if a field
1074 is <SMALL>NULL</SMALL>?</H4>
1076 <P>You test the column with <SMALL>IS NULL</SMALL> and <SMALL>IS
1077 NOT NULL</SMALL>.</P>
1079 <H4><A name="4.14">4.14</A>) What is the difference between the
1080 various character types?</H4>
1082 Type Internal Name Notes
1083 --------------------------------------------------
1084 VARCHAR(n) varchar size specifies maximum length, no padding
1085 CHAR(n) bpchar blank padded to the specified fixed length
1086 TEXT text no specific upper limit on length
1087 BYTEA bytea variable-length byte array (null-byte safe)
1088 "char" char one character
1091 <P>You will see the internal name when examining system catalogs
1092 and in some error messages.</P>
1094 <P>The first four types above are "varlena" types (i.e., the first
1095 four bytes on disk are the length, followed by the data). Thus the
1096 actual space used is slightly greater than the declared size.
1097 However, these data types are also subject to compression or being
1098 stored out-of-line by <SMALL>TOAST</SMALL>, so the space on disk
1099 might also be less than expected.</P>
1101 <SMALL>VARCHAR(n)</SMALL> is best when storing variable-length
1102 strings and it limits how long a string can be. <SMALL>TEXT</SMALL>
1103 is for strings of unlimited length, with a maximum of one gigabyte.
1104 <P><SMALL>CHAR(n)</SMALL> is for storing strings that are all the
1105 same length. <SMALL>CHAR(n)</SMALL> pads with blanks to the specified
1106 length, while <SMALL>VARCHAR(n)</SMALL> only stores the characters
1107 supplied. <SMALL>BYTEA</SMALL> is for storing binary data,
1108 particularly values that include <SMALL>NULL</SMALL> bytes. All the
1109 types described here have similar performance characteristics.</P>
1111 <H4><A name="4.15.1">4.15.1</A>) How do I create a
1112 serial/auto-incrementing field?</H4>
1114 <P>PostgreSQL supports a <SMALL>SERIAL</SMALL> data type. It
1115 auto-creates a sequence. For example,
1118 CREATE TABLE person (
1124 is automatically translated into this:
1126 CREATE SEQUENCE person_id_seq;
1127 CREATE TABLE person (
1128 id INT4 NOT NULL DEFAULT nextval('person_id_seq'),
1133 See the <I>create_sequence</I> manual page for more information
1134 about sequences. You can also use each row's <I>OID</I> field as a
1135 unique value. However, if you need to dump and reload the database,
1136 you need to use <I>pg_dump</I>'s <I>-o</I> option or <SMALL>COPY
1137 WITH OIDS</SMALL> option to preserve the <SMALL>OID</SMALL>s.
1139 <H4><A name="4.15.2">4.15.2</A>) How do I get the value of a
1140 <SMALL>SERIAL</SMALL> insert?</H4>
1142 <P>One approach is to retrieve the next <SMALL>SERIAL</SMALL> value
1143 from the sequence object with the <I>nextval()</I> function
1144 <I>before</I> inserting and then insert it explicitly. Using the
1145 example table in <A href="#4.15.1">4.15.1</A>, an example in a
1146 pseudo-language would look like this:</P>
1148 new_id = execute("SELECT nextval('person_id_seq')");
1149 execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
1152 You would then also have the new value stored in
1153 <CODE>new_id</CODE> for use in other queries (e.g., as a foreign
1154 key to the <CODE>person</CODE> table). Note that the name of the
1155 automatically created <SMALL>SEQUENCE</SMALL> object will be named
1156 <<I>table</I>>_<<I>serialcolumn</I>>_<I>seq</I>, where
1157 <I>table</I> and <I>serialcolumn</I> are the names of your table
1158 and your <SMALL>SERIAL</SMALL> column, respectively.
1160 <P>Alternatively, you could retrieve the assigned
1161 <SMALL>SERIAL</SMALL> value with the <I>currval()</I> function
1162 <I>after</I> it was inserted by default, e.g.,</P>
1164 execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
1165 new_id = execute("SELECT currval('person_id_seq')");
1168 Finally, you could use the <A href="#4.16"><SMALL>OID</SMALL></A>
1169 returned from the <SMALL>INSERT</SMALL> statement to look up the
1170 default value, though this is probably the least portable approach,
1171 and the oid value will wrap around when it reaches 4 billion.
1172 In Perl, using DBI with Edmund Mergl's DBD::Pg module, the oid
1173 value is made available via <I>$sth->{pg_oid_status}</I> after
1174 <I>$sth->execute()</I>.
1176 <H4><A name="4.15.3">4.15.3</A>) Don't <I>currval()</I> and
1177 <I>nextval()</I> lead to a race condition with other users?</H4>
1179 <P>No. <I>currval()</I> returns the current value assigned by your
1180 backend, not by all users.</P>
1182 <H4><A name="4.15.4">4.15.4</A>) Why aren't my sequence numbers
1183 reused on transaction abort? Why are there gaps in the numbering of
1184 my sequence/SERIAL column?</H4>
1186 <P>To improve concurrency, sequence values are given out to running
1187 transactions as needed and are not locked until the transaction
1188 completes. This causes gaps in numbering from aborted
1191 <H4><A name="4.16">4.16</A>) What is an <SMALL>OID</SMALL>? What is
1192 a <SMALL>TID</SMALL>?</H4>
1194 <P><SMALL>OID</SMALL>s are PostgreSQL's answer to unique row ids.
1195 Every row that is created in PostgreSQL gets a unique
1196 <SMALL>OID</SMALL>. All <SMALL>OID</SMALL>s generated during
1197 <I>initdb</I> are less than 16384 (from
1198 <I>include/access/transam.h</I>). All user-created
1199 <SMALL>OID</SMALL>s are equal to or greater than this. By default,
1200 all these <SMALL>OID</SMALL>s are unique not only within a table or
1201 database, but unique within the entire PostgreSQL installation.</P>
1203 <P>PostgreSQL uses <SMALL>OID</SMALL>s in its internal system
1204 tables to link rows between tables. These <SMALL>OID</SMALL>s can
1205 be used to identify specific user rows and used in joins. It is
1206 recommended you use column type <SMALL>OID</SMALL> to store
1207 <SMALL>OID</SMALL> values. You can create an index on the
1208 <SMALL>OID</SMALL> field for faster access.</P>
1210 <P>O<SMALL>ID</SMALL>s are assigned to all new rows from a central
1211 area that is used by all databases. If you want to change the
1212 <SMALL>OID</SMALL> to something else, or if you want to make a copy
1213 of the table, with the original <SMALL>OID</SMALL>s, there is no
1214 reason you can't do it:</P>
1216 CREATE TABLE new_table(old_oid oid, mycol int);
1217 SELECT old_oid, mycol INTO new FROM old;
1218 COPY new TO '/tmp/pgtable';
1220 COPY new WITH OIDS FROM '/tmp/pgtable';
1223 CREATE TABLE new_table (mycol int);
1224 INSERT INTO new_table (oid, mycol) SELECT oid, mycol FROM old_table;
1226 <P>O<SMALL>ID</SMALL>s are stored as 4-byte integers, and will
1227 overflow at 4 billion. No one has reported this ever happening, and
1228 we plan to have the limit removed before anyone does.</P>
1230 <P>T<SMALL>ID</SMALL>s are used to identify specific physical rows
1231 with block and offset values. T<SMALL>ID</SMALL>s change after rows
1232 are modified or reloaded. They are used by index entries to point
1233 to physical rows.</P>
1235 <H4><A name="4.17">4.17</A>) What is the meaning of some of the
1236 terms used in PostgreSQL?</H4>
1238 <P>Some of the source code and older documentation use terms that
1239 have more common usage. Here are some:</P>
1242 <LI>table, relation, class</LI>
1244 <LI>row, record, tuple</LI>
1246 <LI>column, field, attribute</LI>
1248 <LI>retrieve, select</LI>
1250 <LI>replace, update</LI>
1252 <LI>append, insert</LI>
1254 <LI><SMALL>OID</SMALL>, serial value</LI>
1256 <LI>portal, cursor</LI>
1258 <LI>range variable, table name, table alias</LI>
1261 <P>A list of general database terms can be found at: <A href=
1262 "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>
1264 <H4><A name="4.18">4.18</A>) Why do I get the error <I>"ERROR:
1265 Memory exhausted in AllocSetAlloc()"</I>?</H4>
1267 <P>You probably have run out of virtual memory on your system,
1268 or your kernel has a low limit for certain resources. Try this
1269 before starting <I>postmaster</I>:</P>
1275 Depending on your shell, only one of these may succeed, but it will
1276 set your process data segment limit much higher and perhaps allow
1277 the query to complete. This command applies to the current process,
1278 and all subprocesses created after the command is run. If you are
1279 having a problem with the <SMALL>SQL</SMALL> client because the
1280 backend is returning too much data, try it before starting the
1283 <H4><A name="4.19">4.19</A>) How do I tell what PostgreSQL version
1286 <P>From <I>psql</I>, type <CODE>SELECT version();</CODE></P>
1288 <H4><A name="4.20">4.20</A>) Why does my large-object operations
1289 get <I>"invalid large obj descriptor"</I>?</H4>
1291 <P>You need to put <CODE>BEGIN WORK</CODE> and <CODE>COMMIT</CODE>
1292 around any use of a large object handle, that is, surrounding
1293 <CODE>lo_open</CODE> ... <CODE>lo_close.</CODE></P>
1295 <P>Currently PostgreSQL enforces the rule by closing large object
1296 handles at transaction commit. So the first attempt to do anything
1297 with the handle will draw <I>invalid large obj descriptor</I>. So
1298 code that used to work (at least most of the time) will now
1299 generate that error message if you fail to use a transaction.</P>
1301 <P>If you are using a client interface like <SMALL>ODBC</SMALL> you
1302 may need to set <CODE>auto-commit off.</CODE></P>
1304 <H4><A name="4.21">4.21</A>) How do I create a column that will
1305 default to the current time?</H4>
1307 <P>Use <I>CURRENT_TIMESTAMP</I>:</P>
1309 <CODE>CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1313 <H4><A name="4.22">4.22</A>) Why are my subqueries using
1314 <CODE><SMALL>IN</SMALL></CODE> so slow?</H4>
1316 <P>In versions prior to 7.4, subqueries were joined to outer queries
1317 by sequentially scanning the result of the subquery for each row of
1318 the outer query. If the subquery returns only a few rows and the outer
1319 query returns many rows, <CODE><SMALL>IN</SMALL></CODE> is fastest. To
1320 speed up other queries, replace <CODE>IN</CODE> with
1321 <CODE>EXISTS</CODE>:</P>
1324 WHERE col IN (SELECT subcol FROM subtab);
1329 WHERE EXISTS (SELECT subcol FROM subtab WHERE subcol = col);
1332 For this to be fast, <CODE>subcol</CODE> should be an indexed column.
1333 <P>In version 7.4 and later, <CODE>IN</CODE> actually uses the same
1334 sophisticated join techniques as normal queries, and is prefered
1335 to using <CODE>EXISTS</CODE>.
1337 <H4><A name="4.23">4.23</A>) How do I perform an outer join?</H4>
1339 <P>PostgreSQL supports outer joins using the SQL standard syntax.
1340 Here are two examples:</P>
1343 FROM t1 LEFT OUTER JOIN t2 ON (t1.col = t2.col);
1348 FROM t1 LEFT OUTER JOIN t2 USING (col);
1351 <P>These identical queries join t1.col to t2.col, and also return
1352 any unjoined rows in t1 (those with no match in t2). A
1353 <SMALL>RIGHT</SMALL> join would add unjoined rows of t2. A
1354 <SMALL>FULL</SMALL> join would return the matched rows plus all
1355 unjoined rows from t1 and t2. The word <SMALL>OUTER</SMALL> is
1356 optional and is assumed in <SMALL>LEFT</SMALL>,
1357 <SMALL>RIGHT</SMALL>, and <SMALL>FULL</SMALL> joins. Ordinary joins
1358 are called <SMALL>INNER</SMALL> joins.</P>
1360 <P>In previous releases, outer joins can be simulated using
1361 <SMALL>UNION</SMALL> and <SMALL>NOT IN</SMALL>. For example, when
1362 joining <I>tab1</I> and <I>tab2</I>, the following query does an
1363 <I>outer</I> join of the two tables:<BR>
1367 SELECT tab1.col1, tab2.col2
1369 WHERE tab1.col1 = tab2.col1
1371 SELECT tab1.col1, NULL
1373 WHERE tab1.col1 NOT IN (SELECT tab2.col1 FROM tab2)
1377 <H4><A name="4.24">4.24</A>) How do I perform queries using
1378 multiple databases?</H4>
1380 <P>There is no way to query a database other than the current one.
1381 Because PostgreSQL loads database-specific system catalogs, it is
1382 uncertain how a cross-database query should even behave.</P>
1384 <P><I>contrib/dblink</I> allows cross-database queries using
1385 function calls. Of course, a client can make simultaneous
1386 connections to different databases and merge the results on the
1389 <H4><A name="4.25">4.25</A>) How do I return multiple rows or
1390 columns from a function?</H4>
1392 <P>In 7.3, you can easily return multiple rows or columns from a
1394 <a href="http://techdocs.postgresql.org/guides/SetReturningFunctions">
1395 http://techdocs.postgresql.org/guides/SetReturningFunctions</a>.
1397 <H4><A name="4.26">4.26</A>) Why can't I reliably create/drop
1398 temporary tables in PL/PgSQL functions?</H4>
1399 <P>PL/PgSQL caches function contents, and an unfortunate side effect
1400 is that if a PL/PgSQL function accesses a temporary table, and that
1401 table is later dropped and recreated, and the function called
1402 again, the function will fail because the cached function contents
1403 still point to the old temporary table. The solution is to use
1404 <SMALL>EXECUTE</SMALL> for temporary table access in PL/PgSQL. This
1405 will cause the query to be reparsed every time.</P>
1407 <H4><A name="4.27">4.27</A>) What replication options are available?
1409 <P>There are several master/slave replication options available.
1410 These allow only the master to make database changes and the slave
1411 can only do database reads. The bottom of <a
1412 href="http://gborg.PostgreSQL.org/genpage?replication_research">
1413 http://gborg.PostgreSQL.org/genpage?replication_research</a> lists
1414 them. A multi-master replication solution is being worked on at <a
1415 href="http://gborg.PostgreSQL.org/project/pgreplication/projdisplay.php">http://gborg.PostgreSQL.org/project/pgreplication/projdisplay.php</a>.</P>
1417 <H4><A name="4.28">4.28</A>) What encryption options are available?
1420 <LI><I>contrib/pgcrypto</I> contains many encryption functions for
1421 use in <SMALL>SQL</SMALL> queries.</LI>
1422 <LI>To encrypt transmission from the client to the server, the server
1423 must have the <I>ssl</I> option set to <I>true</I> in <I>postgresql.conf,
1424 </I> and an applicable <I>host</I> or <I>hostssl</I> record must exist in
1425 <I>pg_hba.conf</I>, and the client <I>sslmode</I> must not be
1426 <I>disable.</I> (Note that it is also possible to use a third-party
1427 encrypted transport, such as stunnel or ssh, rather than PostgreSQL's
1428 native SSL connections.)
1429 <LI>Database user passwords are automatically encrypted when stored in
1430 version 7.3. In previous versions, you must enable the option
1431 <I>PASSWORD_ENCRYPTION</I> in <I>postgresql.conf</I>.</LI>
1432 <LI>The server can run using an encrypted file system.</LI>
1437 <H2 align="center">Extending PostgreSQL</H2>
1439 <H4><A name="5.1">5.1</A>) I wrote a user-defined function. When I
1440 run it in <I>psql</I>, why does it dump core?</H4>
1442 <P>The problem could be a number of things. Try testing your
1443 user-defined function in a stand-alone test program first.</P>
1445 <H4><A name="5.2">5.2</A>) How can I contribute some nifty new
1446 types and functions to PostgreSQL?</H4>
1448 <P>Send your extensions to the <I>pgsql-hackers</I> mailing list,
1449 and they will eventually end up in the <I>contrib/</I>
1452 <H4><A name="5.3">5.3</A>) How do I write a C function to return a
1455 <P>In versions of PostgreSQL beginning with 7.3, table-returning
1456 functions are fully supported in C, PL/PgSQL, and SQL. See the
1457 Programmer's Guide for more information. An example of a
1458 table-returning function defined in C can be found in
1459 <I>contrib/tablefunc</I>.</P>
1461 <H4><A name="5.4">5.4</A>) I have changed a source file. Why does
1462 the recompile not see the change?</H4>
1464 <P>The <I>Makefiles</I> do not have the proper dependencies for
1465 include files. You have to do a <I>make clean</I> and then another
1466 <I>make</I>. If you are using <SMALL>GCC</SMALL> you can use the
1467 <I>--enable-depend</I> option of <I>configure</I> to have the
1468 compiler compute the dependencies automatically.</P>