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 Jul 11 23:58:24 EDT 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.10</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.27">4.27</A>) What replication options are available?<BR>
141 <A href="#4.28">4.28</A>) What encryption options are available?<BR>
144 <H2 align="center">Extending PostgreSQL</H2>
145 <A href="#5.1">5.1</A>) I wrote a user-defined function. When I run
146 it in <I>psql</I>, why does it dump core?<BR>
147 <A href="#5.2">5.2</A>) How can I contribute some nifty new types
148 and functions to PostgreSQL?<BR>
149 <A href="#5.3">5.3</A>) How do I write a C function to return a
151 <A href="#5.4">5.4</A>) I have changed a source file. Why does the
152 recompile not see the change?<BR>
156 <H2 align="center">General Questions</H2>
158 <H4><A name="1.1">1.1</A>) What is PostgreSQL? How is it pronounced?</H4>
160 <P>PostgreSQL is pronounced <I>Post-Gres-Q-L</I>. An audio file is
161 available at http://www.postgresql.org/postgresql.mp3 for those
162 would like to hear the pronunciation.
165 <P>PostgreSQL is an enhancement of the POSTGRES database management
166 system (and is still sometimes reffered to as simply "Postgres"),
167 a next-generation <SMALL>DBMS</SMALL> research prototype.
168 While PostgreSQL retains the powerful data model and rich data
169 types of POSTGRES, it replaces the PostQuel query language with an
170 extended subset of <SMALL>SQL</SMALL>. PostgreSQL is free and the
171 complete source is available.</P>
173 <P>PostgreSQL development is performed by a team of
174 developers who all subscribe to the PostgreSQL development mailing
175 list. The current coordinator is Marc G. Fournier (<A href=
176 "mailto:scrappy@PostgreSQL.org">scrappy@PostgreSQL.org</A>). (See
177 section <a href="#1.6">1.6</a> on how to join). This team is now
178 responsible for all development of PostgreSQL. It is a community
179 project and is not controlled by any company. To get involved, see
180 the developer's FAQ at <A href=
181 "http://www.PostgreSQL.org/docs/faqs/FAQ_DEV.html">http://www.PostgreSQL.org/docs/faqs/FAQ_DEV.html</A>
184 <P>The authors of PostgreSQL 1.01 were Andrew Yu and Jolly Chen.
185 Many others have contributed to the porting, testing, debugging,
186 and enhancement of the code. The original Postgres code, from which
187 PostgreSQL is derived, was the effort of many graduate students,
188 undergraduate students, and staff programmers working under the
189 direction of Professor Michael Stonebraker at the University of
190 California, Berkeley.</P>
192 <P>The original name of the software at Berkeley was Postgres. When
193 <SMALL>SQL</SMALL> functionality was added in 1995, its name was
194 changed to Postgres95. The name was changed at the end of 1996 to
197 <H4><A name="1.2">1.2</A>) What is the copyright on
200 <P>PostgreSQL is subject to the following COPYRIGHT:</P>
202 <P>PostgreSQL Data Base Management System</P>
204 <P>Portions copyright (c) 1996-2004, PostgreSQL Global Development
205 Group Portions Copyright (c) 1994-6 Regents of the University of
208 <P>Permission to use, copy, modify, and distribute this software
209 and its documentation for any purpose, without fee, and without a
210 written agreement is hereby granted, provided that the above
211 copyright notice and this paragraph and the following two
212 paragraphs appear in all copies.</P>
214 <P>IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
215 PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
216 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS
217 SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
218 CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</P>
220 <P>THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
221 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
222 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
223 SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE
224 UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
225 SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</P>
227 <P>The above is the BSD license, the classic open-source license.
228 It has no restrictions on how the source code may be used. We like
229 it and have no intention of changing it.</P>
231 <H4><A name="1.3">1.3</A>) What Unix platforms does PostgreSQL run
234 <P>In general, any modern Unix-compatible platform should be able to
235 run PostgreSQL. The platforms that had received explicit testing at
236 the time of release are listed in the installation
239 <H4><A name="1.4">1.4</A>) What non-Unix ports are available?</H4>
241 <P><STRONG>Client</STRONG></P>
243 <P>It is possible to compile the <I>libpq</I> C library, psql, and
244 other interfaces and client applications to run on MS Windows platforms.
245 In this case, the client is running on MS Windows, and communicates
246 via TCP/IP to a server running on one of our supported Unix
247 platforms. A file <I>win32.mak</I> is included in the distribution
248 for making a Win32 <I>libpq</I> library and <I>psql</I>. PostgreSQL
249 also communicates with <SMALL>ODBC</SMALL> clients.</P>
251 <P><STRONG>Server</STRONG></P>
253 <P>The database server can run on Windows NT and Win2k using
254 Cygwin, the Cygnus Unix/NT porting library. See
255 <I>pgsql/doc/FAQ_MSWIN</I> in the distribution or the MS Windows FAQ
256 at <A href="http://www.PostgreSQL.org/docs/faqs/text/FAQ_MSWIN">
257 http://www.PostgreSQL.org/docs/faqs/text/FAQ_MSWIN</A>.</P>
259 <p>A native port to MS Win NT/2000/XP is currently being worked
260 on. For more details on the current status of PostgreSQL on Windows see
261 <a href="http://techdocs.postgresql.org/guides/Windows">
262 http://techdocs.postgresql.org/guides/Windows</a> and
263 <a href="http://momjian.postgresql.org/main/writings/pgsql/win32.html">
264 http://momjian.postgresql.org/main/writings/pgsql/win32.html</a>.</p>
266 <p>There is also a Novell Netware 6 port at
267 <a href="http://forge.novell.com">http://forge.novell.com</a>.</p>
269 <H4><A name="1.5">1.5</A>) Where can I get PostgreSQL?</H4>
271 <P>The primary anonymous ftp site for PostgreSQL is <A href=
272 "ftp://ftp.PostgreSQL.org/pub">ftp://ftp.PostgreSQL.org/pub</A>.
273 For mirror sites, see our main web site.</P>
275 <H4><A name="1.6">1.6</A>) Where can I get support?</H4>
277 <P>The main mailing list is: <A href=
278 "mailto:pgsql-general@PostgreSQL.org">pgsql-general@PostgreSQL.org</A>.
279 It is available for discussion of matters pertaining to PostgreSQL.
280 To subscribe, send mail with the following lines in the body (not
281 the subject line):</P>
288 "mailto:pgsql-general-request@PostgreSQL.org">pgsql-general-request@PostgreSQL.org</A>.</P>
290 <P>There is also a digest list available. To subscribe to this
291 list, send email to: <A href=
292 "mailto:pgsql-general-digest-request@PostgreSQL.org">pgsql-general-digest-request@PostgreSQL.org</A>
299 Digests are sent out to members of this list whenever the main list
300 has received around 30k of messages.
302 <P>The bugs mailing list is available. To subscribe to this list,
303 send email to <A href=
304 "mailto:pgsql-bugs-request@PostgreSQL.org">pgsql-bugs-request@PostgreSQL.org</A>
311 There is also a developers discussion mailing list available. To
312 subscribe to this list, send email to <A href=
313 "mailto:pgsql-hackers-request@PostgreSQL.org">pgsql-hackers-request@PostgreSQL.org</A>
320 <P>Additional mailing lists and information about PostgreSQL can be
321 found via the PostgreSQL WWW home page at:</P>
324 <A href="http://www.PostgreSQL.org">http://www.PostgreSQL.org</A>
327 <P>There is also an IRC channel on Freenode and EFNet,
328 channel <I>#PostgreSQL</I>. You can use the Unix command <CODE>irc -c
329 '#PostgreSQL' "$USER" irc.phoenix.net.</CODE> or <CODE>irc -c
330 '#PostgreSQL' "$USER" irc.freenode.net.</CODE></P>
332 <P>A list of commercial support companies is available at <A href=
333 "http://techdocs.postgresql.org/companies.php">http://techdocs.postgresql.org/companies.php</A>.</P>
335 <H4><A name="1.7">1.7</A>) What is the latest release?</H4>
337 <P>The latest release of PostgreSQL is version 7.4.3.</P>
339 <P>We plan to have major releases every six to eight months.</P>
341 <H4><A name="1.8">1.8</A>) What documentation is available?</H4>
343 <P>Several manuals, manual pages, and some small test examples are
344 included in the distribution. See the <I>/doc</I> directory. You
345 can also browse the manuals online at <A href=
346 "http://www.PostgreSQL.org/docs">http://www.PostgreSQL.org/docs</A>.</P>
348 <P>There are two PostgreSQL books available online at <A href=
349 "http://www.PostgreSQL.org/docs/awbook.html">http://www.PostgreSQL.org/docs/awbook.html</A>
351 "http://www.commandprompt.com/ppbook/">http://www.commandprompt.com/ppbook/</A>.
352 There is a list of PostgreSQL books available for purchase at <A
354 "http://techdocs.postgresql.org/techdocs/bookreviews.php">http://techdocs.PostgreSQL.org/techdocs/bookreviews.php</A>.
355 There is also a collection of PostgreSQL technical articles at <A
357 "http://techdocs.PostgreSQL.org/">http://techdocs.PostgreSQL.org/</A>.</P>
359 <P><I>psql</I> has some nice \d commands to show information about
360 types, operators, functions, aggregates, etc.</P>
362 <P>Our web site contains even more documentation.</P>
364 <H4><A name="1.9">1.9</A>) How do I find out about known bugs or
365 missing features?</H4>
367 <P>PostgreSQL supports an extended subset of <SMALL>SQL</SMALL>-92.
368 See our <A href="http://developer.PostgreSQL.org/todo.php">TODO</A>
369 list for known bugs, missing features, and future plans.</P>
371 <H4><A name="1.10">1.10</A>) How can I learn
372 <SMALL>SQL</SMALL>?</H4>
374 <P>The PostgreSQL book at <A href=
375 "http://www.PostgreSQL.org/docs/awbook.html">http://www.PostgreSQL.org/docs/awbook.html</A>
376 teaches <SMALL>SQL</SMALL>. There is another PostgreSQL book at <A
378 "http://www.commandprompt.com/ppbook/">http://www.commandprompt.com/ppbook.</A>
379 There is a nice tutorial at <A href=
380 "http://www.intermedia.net/support/sql/sqltut.shtm">http://www.intermedia.net/support/sql/sqltut.shtm,</A>
382 "http://ourworld.compuserve.com/homepages/graeme_birchall/HTM_COOK.HTM">
383 http://ourworld.compuserve.com/homepages/graeme_birchall/HTM_COOK.HTM,</A>
385 "http://sqlcourse.com/">http://sqlcourse.com.</A></P>
387 <P>Another one is "Teach Yourself SQL in 21 Days, Second Edition"
389 "http://members.tripod.com/er4ebus/sql/index.htm">http://members.tripod.com/er4ebus/sql/index.htm</A></P>
391 <P>Many of our users like <I>The Practical SQL Handbook</I>,
392 Bowman, Judith S., et al., Addison-Wesley. Others like <I>The
393 Complete Reference SQL</I>, Groff et al., McGraw-Hill.</P>
395 <H4><A name="1.11">1.11</A>) Is PostgreSQL Y2K compliant?</H4>
397 <P>Yes, we easily handle dates past the year 2000 AD, and before
400 <H4><A name="1.12">1.12</A>) How do I join the development
403 <P>First, download the latest source and read the PostgreSQL
404 Developers documentation on our web site, or in the distribution.
405 Second, subscribe to the <I>pgsql-hackers</I> and
406 <I>pgsql-patches</I> mailing lists. Third, submit high quality
407 patches to pgsql-patches.</P>
409 <P>There are about a dozen people who have commit privileges to the
410 PostgreSQL <SMALL>CVS</SMALL> archive. They each have submitted so
411 many high-quality patches that it was impossible for the existing
412 committers to keep up, and we had confidence that patches they
413 committed were of high quality.</P>
415 <H4><A name="1.13">1.13</A>) How do I submit a bug report?</H4>
417 <P>Please visit the PostgreSQL BugTool page at <A href=
418 "http://www.PostgreSQL.org/bugs/bugs.php">http://www.PostgreSQL.org/bugs/bugs.php</A>,
419 which gives guidelines and directions on how to submit a
422 <P>Also check out our ftp site <A href=
423 "ftp://ftp.PostgreSQL.org/pub">ftp://ftp.PostgreSQL.org/pub</A> to
424 see if there is a more recent PostgreSQL version or patches.</P>
426 <H4><A name="1.14">1.14</A>) How does PostgreSQL compare to other
427 <SMALL>DBMS</SMALL>s?</H4>
429 <P>There are several ways of measuring software: features,
430 performance, reliability, support, and price.</P>
433 <DT><B>Features</B></DT>
435 <DD>PostgreSQL has most features present in large commercial
436 <SMALL>DBMS</SMALL>s, like transactions, subselects, triggers,
437 views, foreign key referential integrity, and sophisticated
438 locking. We have some features they do not have, like
439 user-defined types, inheritance, rules, and multi-version
440 concurrency control to reduce lock contention.<BR>
444 <DT><B>Performance</B></DT>
446 <DD>PostgreSQL has performance similar to other commercial and
447 open source databases. it is faster for some things, slower for
448 others. In comparison to MySQL or leaner database systems, we are
449 faster for multiple users, complex queries, and a read/write query
450 load. MySQL is faster for simple SELECT queries done by a few users.
451 Of course, MySQL does not have most of the features mentioned in the
452 <I>Features</I> section above. We are built for reliability and
453 features, and we continue to improve performance in every
454 release. There is an interesting Web page comparing PostgreSQL to
455 MySQL at <A href="http://openacs.org/philosophy/why-not-mysql.html">
456 http://openacs.org/philosophy/why-not-mysql.html</A> Also, MySQL is
457 is a company that distributes its products via open source, and requires
458 a commercial license for close-source software, not an
459 open source development community like PostgreSQL.<BR>
464 <DT><B>Reliability</B></DT>
466 <DD>We realize that a <SMALL>DBMS</SMALL> must be reliable, or it
467 is worthless. We strive to release well-tested, stable code that
468 has a minimum of bugs. Each release has at least one month of
469 beta testing, and our release history shows that we can provide
470 stable, solid releases that are ready for production use. We
471 believe we compare favorably to other database software in this
476 <DT><B>Support</B></DT>
478 <DD>Our mailing lists provide contact with a large group of developers
479 and users to help resolve any problems encountered. While we cannot
480 guarantee a fix, commercial <SMALL>DBMS</SMALL>s do not always
481 supply a fix either. Direct access to developers, the user
482 community, manuals, and the source code often make PostgreSQL
483 support superior to other <SMALL>DBMS</SMALL>s. There is
484 commercial per-incident support available for those who need it.
485 (See <A href="#1.6">FAQ section 1.6</A>.)<BR>
489 <DT><B>Price</B></DT>
491 <DD>We are free for all use, both commercial and non-commercial.
492 You can add our code to your product with no limitations, except
493 those outlined in our BSD-style license stated above.<BR>
498 <H4><A name="1.15">1.15</A>) How can I financially assist
501 <P>PostgreSQL has had a first-class infrastructure since we started
502 in 1996. This is all thanks to Marc Fournier, who has created
503 and managed this infrastructure over the years.</P>
505 <P>Quality infrastructure is very important to an open-source
506 project. It prevents disruptions that can greatly delay forward
507 movement of the project.</P>
509 <P>Of course, this infrastructure is not cheap. There are a variety
510 of monthly and one-time expenses that are required to keep it
511 going. If you or your company has money it can donate to help fund
512 this effort, please go to <A href="http://store.pgsql.com/shopping/">http://store.pgsql.com/shopping/</A>
513 and make a donation.</P>
515 <P>Although the web page mentions PostgreSQL, Inc, the
516 "contributions" item is solely to support the PostgreSQL project
517 and does not fund any specific company. If you prefer, you can also
518 send a check to the contact address.</P>
521 <P>Also, if you have a success story about PostgreSQL, please submit
522 it to our advocacy site at <a href="http://advocacy.postgresql.org">
523 http://advocacy.postgresql.org</a>.</P>
526 <H2 align="center">User Client Questions</H2>
528 <H4><A name="2.1">2.1</A>) Are there <SMALL>ODBC</SMALL> drivers
531 <P>There are two <SMALL>ODBC</SMALL> drivers available, PsqlODBC
532 and OpenLink <SMALL>ODBC</SMALL>.</P>
534 <P>You can download PsqlODBC from <A href=
535 "http://gborg.postgresql.org/project/psqlodbc/projdisplay.php">
536 http://gborg.postgresql.org/project/psqlodbc/projdisplay.php</A>.</P>
538 <P>OpenLink <SMALL>ODBC</SMALL> can be gotten from <A href=
539 "http://www.openlinksw.com/">http://www.openlinksw.com</A>. It
540 works with their standard <SMALL>ODBC</SMALL> client software so
541 you'll have PostgreSQL <SMALL>ODBC</SMALL> available on every
542 client platform they support (Win, Mac, Unix, VMS).</P>
544 <P>They will probably be selling this product to people who need
545 commercial-quality support, but a freeware version will always be
546 available. Please send questions to <A href=
547 "mailto:postgres95@openlink.co.uk">postgres95@openlink.co.uk</A>.</P>
549 <H4><A name="2.2">2.2</A>) What tools are available for using
550 PostgreSQL with Web pages?</H4>
552 <P>A nice introduction to Database-backed Web pages can be seen at:
553 <A href="http://www.webreview.com">http://www.webreview.com</A></P>
555 <P>For Web integration, PHP is an excellent interface. It is at <A
556 href="http://www.php.net">http://www.php.net</A>.</P>
558 <P>For complex cases, many use the Perl interface and CGI.pm or mod_perl.</P>
560 <H4><A name="2.3">2.3</A>) Does PostgreSQL have a graphical user
563 <P>Yes, there are several graphical interfaces to PostgreSQL available.
564 These include PgAccess <a href="http://www.pgaccess.org">
565 http://www.pgaccess.org</a>), PgAdmin III (<a
566 href="http://www.pgadmin.org">http://www.pgadmin.org</a>, RHDB Admin (<a
567 href="http://sources.redhat.com/rhdb/">http://sources.redhat.com/rhdb/
568 </a>) and Rekall (<a href="http://www.thekompany.com/products/rekall/">
569 http://www.thekompany.com/products/rekall/</a>, proprietary). There is
570 also PhpPgAdmin (<a href="http://phppgadmin.sourceforge.net/">
571 http://phppgadmin.sourceforge.net/ </a>), a web-based interface to
574 <P>See <a href="http://techdocs.postgresql.org/guides/GUITools">http://techdocs.postgresql.org/guides/GUITools</a> for a more detailed list.</P>
576 <H4><A name="2.4">2.4</A>) What languages are able to communicate with
579 <P>Most popular programming languages contain an interface to
580 PostgreSQL. Check your programming language's list of extension
583 <P>The following interfaces are included in the PostgreSQL
589 <LI>Embedded C (ecpg)</LI>
593 <LI>Python (PyGreSQL)</LI>
595 <LI>TCL (libpgtcl)</LI>
598 <P>Additional interfaces are available at
599 <a href="http://gborg.postgresql.org">http://gborg.postgresql.org</A>
600 in the <I>Drivers/Interfaces</I> section.
604 <H2 align="center">Administrative Questions</H2>
606 <H4><A name="3.1">3.1</A>) How do I install PostgreSQL somewhere
607 other than <I>/usr/local/pgsql</I>?</H4>
609 <P>Specify the <I>--prefix</I> option when running
610 <I>configure</I>.</P>
612 <H4><A name="3.2">3.2</A>) When I start <I>postmaster</I>, I get a
613 <I>Bad System Call</I> or core dumped message. Why?</H4>
615 <P>It could be a variety of problems, but first check to see that
616 you have System V extensions installed in your kernel. PostgreSQL
617 requires kernel support for shared memory and semaphores.</P>
619 <H4><A name="3.3">3.3</A>) When I try to start <I>postmaster</I>, I
620 get <I>IpcMemoryCreate</I> errors. Why?</H4>
622 <P>You either do not have shared memory configured properly in your
623 kernel or you need to enlarge the shared memory available in the
624 kernel. The exact amount you need depends on your architecture and
625 how many buffers and backend processes you configure for
626 <I>postmaster</I>. For most systems, with default numbers of
627 buffers and processes, you need a minimum of ~1 MB. See the <A
629 "http://www.PostgreSQL.org/docs/view.php?version=current&idoc=1&file=kernel-resources.html">PostgreSQL
630 Administrator's Guide</A> for more detailed information about
631 shared memory and semaphores.</P>
633 <H4><A name="3.4">3.4</A>) When I try to start <I>postmaster</I>, I
634 get <I>IpcSemaphoreCreate</I> errors. Why?</H4>
636 <P>If the error message is <I>IpcSemaphoreCreate: semget failed (No
637 space left on device)</I> then your kernel is not configured with
638 enough semaphores. Postgres needs one semaphore per potential
639 backend process. A temporary solution is to start <I>postmaster</I>
640 with a smaller limit on the number of backend processes. Use
641 <I>-N</I> with a parameter less than the default of 32. A more
642 permanent solution is to increase your kernel's
643 <SMALL>SEMMNS</SMALL> and <SMALL>SEMMNI</SMALL> parameters.</P>
645 <P>Inoperative semaphores can also cause crashes during heavy
648 <P>If the error message is something else, you might not have
649 semaphore support configured in your kernel at all. See the
650 PostgreSQL Administrator's Guide for more detailed information
651 about shared memory and semaphores.</P>
653 <H4><A name="3.5">3.5</A>) How do I control connections from other
656 <P>By default, PostgreSQL only allows connections from the local
657 machine using Unix domain sockets. Other machines will not be able
658 to connect unless you turn on tcpip_sockets in the postgresql.conf
659 <B>and</B> enable host-based authentication by modifying the file
660 <I>$PGDATA/pg_hba.conf</I> accordingly. This will allow TCP/IP
663 <H4><A name="3.6">3.6</A>) How do I tune the database engine for
664 better performance?</H4>
666 <P>Certainly, indexes can speed up queries. The
667 <SMALL>EXPLAIN ANALYZE</SMALL> command allows you to see how PostgreSQL is
668 interpreting your query, and which indexes are being used.</P>
670 <P>If you are doing many <SMALL>INSERTs</SMALL>, consider doing
671 them in a large batch using the <SMALL>COPY</SMALL> command. This
672 is much faster than individual <SMALL>INSERTS</SMALL>. Second,
673 statements not in a <SMALL>BEGIN WORK/COMMIT</SMALL> transaction
674 block are considered to be in their own transaction. Consider
675 performing several statements in a single transaction block. This
676 reduces the transaction overhead. Also, consider dropping and
677 recreating indexes when making large data changes.</P>
679 <P>There are several tuning options. You can disable <I>fsync()</I>
680 by starting <I>postmaster</I> with a <I>-o -F</I> option. This will
681 prevent <I>fsync()</I>s from flushing to disk after every
684 <P>You can also use the <I>postmaster</I> <I>-B</I> option to
685 increase the number of shared memory buffers used by the backend
686 processes. If you make this parameter too high, the
687 <I>postmaster</I> may not start because you have exceeded your
688 kernel's limit on shared memory space. Each buffer is 8K and the
689 default is 64 buffers.</P>
691 <P>You can also use the backend <I>-S</I> option to increase the
692 maximum amount of memory used by the backend process for temporary
693 sorts. The <I>-S</I> value is measured in kilobytes, and the
694 default is 512 (i.e. 512K).</P>
696 <P>You can also use the <SMALL>CLUSTER</SMALL> command to group
697 data in tables to match an index. See the <SMALL>CLUSTER</SMALL>
698 manual page for more details.</P>
700 <H4><A name="3.7">3.7</A>) What debugging features are
703 <P>PostgreSQL has several features that report status information
704 that can be valuable for debugging purposes.</P>
706 <P>First, by running <I>configure</I> with the --enable-cassert
707 option, many <I>assert()</I>s monitor the progress of the backend
708 and halt the program when something unexpected occurs.</P>
710 <P>Both <I>postmaster</I> and <I>postgres</I> have several debug
711 options available. First, whenever you start <I>postmaster</I>,
712 make sure you send the standard output and error to a log file,
716 ./bin/postmaster >server.log 2>&1 &
719 <P>This will put a server.log file in the top-level PostgreSQL
720 directory. This file contains useful information about problems or
721 errors encountered by the server. <I>Postmaster</I> has a <I>-d</I>
722 option that allows even more detailed information to be reported.
723 The <I>-d</I> option takes a number that specifies the debug level.
724 Be warned that high debug level values generate large log
727 <P>If <I>postmaster</I> is not running, you can actually run the
728 <I>postgres</I> backend from the command line, and type your
729 <SMALL>SQL</SMALL> statement directly. This is recommended
730 <B>only</B> for debugging purposes. Note that a newline terminates
731 the query, not a semicolon. If you have compiled with debugging
732 symbols, you can use a debugger to see what is happening. Because
733 the backend was not started from <I>postmaster</I>, it is not
734 running in an identical environment and locking/backend interaction
735 problems may not be duplicated.</P>
737 <P>If <I>postmaster</I> is running, start <I>psql</I> in one
738 window, then find the <SMALL>PID</SMALL> of the <I>postgres</I>
739 process used by <I>psql</I>. Use a debugger to attach to the
740 <I>postgres</I> <SMALL>PID</SMALL>. You can set breakpoints in the
741 debugger and issue queries from <I>psql</I>. If you are debugging
742 <I>postgres</I> startup, you can set PGOPTIONS="-W n", then start
743 <I>psql</I>. This will cause startup to delay for <I>n</I> seconds
744 so you can attach to the process with the debugger, set any
745 breakpoints, and continue through the startup sequence.</P>
747 <P>The <I>postgres</I> program has <I>-s, -A</I>, and <I>-t</I>
748 options that can be very useful for debugging and performance
751 <P>You can also compile with profiling to see what functions are
752 taking execution time. The backend profile files will be deposited
753 in the <I>pgsql/data/base/dbname</I> directory. The client profile
754 file will be put in the client's current directory. Linux requires
755 a compile with <I>-DLINUX_PROFILE</I> for proper profiling.</P>
757 <H4><A name="3.8">3.8</A>) Why do I get <I>"Sorry, too many
758 clients"</I> when trying to connect?</H4>
760 <P>You need to increase <I>postmaster</I>'s limit on how many
761 concurrent backend processes it can start.</P>
763 <P>The default limit is 32 processes. You can increase it by
764 restarting <I>postmaster</I> with a suitable <I>-N</I> value or
765 modifying <I>postgresql.conf</I>.</P>
767 <P>Note that if you make <I>-N</I> larger than 32, you must also
768 increase <I>-B</I> beyond its default of 64; <I>-B</I> must be at
769 least twice <I>-N</I>, and probably should be more than that for
770 best performance. For large numbers of backend processes, you are
771 also likely to find that you need to increase various Unix kernel
772 configuration parameters. Things to check include the maximum size
773 of shared memory blocks, <SMALL>SHMMAX;</SMALL> the maximum number
774 of semaphores, <SMALL>SEMMNS</SMALL> and <SMALL>SEMMNI;</SMALL> the
775 maximum number of processes, <SMALL>NPROC;</SMALL> the maximum
776 number of processes per user, <SMALL>MAXUPRC;</SMALL> and the
777 maximum number of open files, <SMALL>NFILE</SMALL> and
778 <SMALL>NINODE</SMALL>. The reason that PostgreSQL has a limit on
779 the number of allowed backend processes is so your system won't run
780 out of resources.</P>
782 <H4><A name="3.9">3.9</A>) What is in the <I>pgsql_tmp</I> directory?</H4>
784 <P>This directory contains temporary files generated by the query
785 executor. For example, if a sort needs to be done to satisfy an
786 <SMALL>ORDER BY</SMALL> and the sort requires more space than the
787 backend's <I>-S</I> parameter allows, then temporary files are created
788 here to hold the extra data.</P>
790 <P>The temporary files are usually deleted automatically, but might
791 remain if a backend crashes during a sort. A stop and restart of the
792 <I>postmaster</I> will remove files from those directories.</P>
794 <H4><A name="3.10">3.10</A>) Why do I need to do a dump and restore
795 to upgrade between major PostgreSQL releases?</H4>
797 <P>The PostgreSQL team makes only small changes between minor releases,
798 so upgrading from 7.2 to 7.2.1 does not require a dump and restore.
799 However, major releases (e.g. from 7.2 to 7.3) often change the internal
800 format of system tables and data files. These changes are often complex,
801 so we don't maintain backward compatability for data files. A dump outputs
802 data in a generic format that can then be loaded in using the new internal
805 <P>In releases where the on-disk format does not change, the
806 <I>pg_upgrade</I> script can be used to upgrade without a dump/restore.
807 The release notes mention whether <I>pg_upgrade</I> is available for the
810 <H4><A name="3.11">3.11</A>) What computer hardware should I use?</H4>
812 <P>Because PC hardware is mostly compatible, people tend to believe that
813 all PC hardware is of equal quality. It is not. ECC RAM, SCSI, and
814 quality motherboards are more reliable and have better performance than
815 less expensive hardware. PostgreSQL will run on almost any hardware,
816 but if reliability and performance are important it is wise to
817 research your hardware options thoroughly. Our email lists can be used
818 to discuss hardware options and tradeoffs.</P>
822 <H2 align="center">Operational Questions</H2>
824 <H4><A name="4.1">4.1</A>) What is the difference between binary
825 cursors and normal cursors?</H4>
827 <P>See the <SMALL>DECLARE</SMALL> manual page for a
830 <H4><A name="4.2">4.2</A>) How do I <SMALL>SELECT</SMALL> only the
831 first few rows of a query? A random row?</H4>
833 <P>See the <SMALL>FETCH</SMALL> manual page, or use
834 <SMALL>SELECT</SMALL> ... <SMALL>LIMIT</SMALL>....</P>
836 <P>The entire query may have to be evaluated, even if you only want
837 the first few rows. Consider using a query that has an <SMALL>ORDER
838 BY</SMALL>. If there is an index that matches the <SMALL>ORDER
839 BY</SMALL>, PostgreSQL may be able to evaluate only the first few
840 records requested, or the entire query may have to be evaluated
841 until the desired rows have been generated.</P>
843 <P>To <SMALL>SELECT</SMALL> a random row, use:
851 <H4><A name="4.3">4.3</A>) How do I get a list of tables or other
852 things I can see in <I>psql</I>?</H4>
854 <P>Use the \dt command to see tables in <I>psql</I>. For a complete list of
855 commands inside psql you can use \?. Alternatively you can read the source
856 code for <I>psql</I> in file <I>pgsql/src/bin/psql/describe.c</I>, it
857 contains <SMALL>SQL</SMALL> commands that generate the output for
858 <I>psql</I>'s backslash commands. You can also start <I>psql</I> with the
859 <I>-E</I> option so it will print out the queries it uses to execute the
860 commands you give. PostgreSQL also provides an <SMALL>SQLi</SMALL> compliant
861 INFORMATION SCHEMA interface you can query to get information about the
864 <H4><A name="4.4">4.4</A>) How do you remove a column from a
865 table, or change its data type?</H4>
867 <P><SMALL>DROP COLUMN</SMALL> functionality was added in release 7.3 with
868 <SMALL>ALTER TABLE DROP COLUMN</SMALL>. In earlier versions,
872 LOCK TABLE old_table;
873 SELECT ... -- select all columns but the one you want to remove
876 DROP TABLE old_table;
877 ALTER TABLE new_table RENAME TO old_table;
881 <P>To change the data type of a column, do this:</P>
884 ALTER TABLE tab ADD COLUMN new_col <i>new_data_type</i>;
885 UPDATE tab SET new_col = CAST(old_col AS <i>new_data_type</i>);
886 ALTER TABLE tab DROP COLUMN old_col;
889 <P>You might then want to do <I>VACUUM FULL tab</I> to reclaim the
890 disk space used by the expired rows.</P>
892 <H4><A name="4.5">4.5</A>) What is the maximum size for a row, a
893 table, and a database?</H4>
895 <P>These are the limits:</P>
897 Maximum size for a database? unlimited (32 TB databases exist)
898 Maximum size for a table? 32 TB
899 Maximum size for a row? 1.6TB
900 Maximum size for a field? 1 GB
901 Maximum number of rows in a table? unlimited
902 Maximum number of columns in a table? 250-1600 depending on column types
903 Maximum number of indexes on a table? unlimited
906 Of course, these are not actually unlimited, but limited to
907 available disk space and memory/swap space. Performance may suffer
908 when these values get unusually large.
910 <P>The maximum table size of 32 TB does not require large file
911 support from the operating system. Large tables are stored as
912 multiple 1 GB files so file system size limits are not
915 <P>The maximum table size and maximum number of columns can be
916 quadrupled by increasing the default block size to 32k.</P>
918 <H4><A name="4.6">4.6</A>) How much database disk space is required
919 to store data from a typical text file?</H4>
921 <P>A PostgreSQL database may require up to five times the disk
922 space to store data from a text file.</P>
924 <P>As an example, consider a file of 100,000 lines with an integer
925 and text description on each line. Suppose the text string
926 avergages twenty bytes in length. The flat file would be 2.8 MB.
927 The size of the PostgreSQL database file containing this data can
928 be estimated as 6.4 MB:</P>
930 36 bytes: each row header (approximate)
931 24 bytes: one int field and one text field
932 + 4 bytes: pointer on page to tuple
933 ----------------------------------------
936 The data page size in PostgreSQL is 8192 bytes (8 KB), so:
939 ------------------- = 128 rows per database page (rounded down)
943 -------------------- = 782 database pages (rounded up)
946 782 database pages * 8192 bytes per page = 6,406,144 bytes (6.4 MB)
949 <P>Indexes do not require as much overhead, but do contain the data
950 that is being indexed, so they can be large also.</P>
952 <P><SMALL>NULL</SMALL>s are stored as bitmaps, so they
953 use very little space.</P>
955 <H4><A name="4.7">4.7</A>) How do I find out what tables, indexes,
956 databases, and users are defined?</H4>
958 <P><I>psql</I> has a variety of backslash commands to show such
959 information. Use \? to see them. There are also system tables
960 beginning with <I>pg_</I> that describe these too. Also, <I>psql
961 -l</I> will list all databases.</P>
963 <P>Also try the file <I>pgsql/src/tutorial/syscat.source</I>. It
964 illustrates many of the <SMALL>SELECT</SMALL>s needed to get
965 information from the database system tables.</P>
967 <H4><A name="4.8">4.8</A>) My queries are slow or don't make use of
968 the indexes. Why?</H4>
969 Indexes are not automatically used by every query. Indexes are only
970 used if the table is larger than a minimum size, and the query
971 selects only a small percentage of the rows in the table. This is
972 because the random disk access caused by an index scan can be
973 slower than a straight read through the table, or sequential scan.
975 <P>To determine if an index should be used, PostgreSQL must have
976 statistics about the table. These statistics are collected using
977 <SMALL>VACUUM ANALYZE</SMALL>, or simply <SMALL>ANALYZE</SMALL>.
978 Using statistics, the optimizer knows how many rows are in the
979 table, and can better determine if indexes should be used.
980 Statistics are also valuable in determining optimal join order and
981 join methods. Statistics collection should be performed
982 periodically as the contents of the table change.</P>
984 <P>Indexes are normally not used for <SMALL>ORDER BY</SMALL> or to
985 perform joins. A sequential scan followed by an explicit sort is
986 usually faster than an index scan of a large table.</P>
987 However, <SMALL>LIMIT</SMALL> combined with <SMALL>ORDER BY</SMALL>
988 often will use an index because only a small portion of the table
989 is returned. In fact, though MAX() and MIN() don't use indexes,
990 it is possible to retrieve such values using an index with ORDER BY
995 ORDER BY col [ DESC ]
999 <P>If you believe the optimizer is incorrect in choosing a
1000 sequential scan, use <CODE>SET enable_seqscan TO 'off'</CODE> and
1001 run tests to see if an index scan is indeed faster.</P>
1003 <P>When using wild-card operators such as <SMALL>LIKE</SMALL> or
1004 <I>~</I>, indexes can only be used in certain circumstances:</P>
1006 <LI>The beginning of the search string must be anchored to the start
1009 <LI><SMALL>LIKE</SMALL> patterns must not start with <I>%</I>.</LI>
1010 <LI><I>~</I> (regular expression) patterns must start with
1013 <LI>The search string can not start with a character class,
1015 <LI>Case-insensitive searches such as <SMALL>ILIKE</SMALL> and
1016 <I>~*</I> do not utilise indexes. Instead, use functional
1017 indexes, which are described in section <a href="#4.12">4.12</a>.</LI>
1018 <LI>The default <I>C</I> locale must be used during
1023 <H4><A name="4.9">4.9</A>) How do I see how the query optimizer is
1024 evaluating my query?</H4>
1026 <P>See the <SMALL>EXPLAIN</SMALL> manual page.</P>
1028 <H4><A name="4.10">4.10</A>) What is an R-tree index?</H4>
1030 <P>An R-tree index is used for indexing spatial data. A hash index
1031 can't handle range searches. A B-tree index only handles range
1032 searches in a single dimension. R-trees can handle
1033 multi-dimensional data. For example, if an R-tree index can be
1034 built on an attribute of type <I>point</I>, the system can more
1035 efficiently answer queries such as "select all points within a
1036 bounding rectangle."</P>
1038 <P>The canonical paper that describes the original R-tree design
1041 <P>Guttman, A. "R-trees: A Dynamic Index Structure for Spatial
1042 Searching." Proceedings of the 1984 ACM SIGMOD Int'l Conf on Mgmt
1045 <P>You can also find this paper in Stonebraker's "Readings in
1046 Database Systems".</P>
1048 <P>Built-in R-trees can handle polygons and boxes. In theory,
1049 R-trees can be extended to handle higher number of dimensions. In
1050 practice, extending R-trees requires a bit of work and we don't
1051 currently have any documentation on how to do it.</P>
1053 <H4><A name="4.11">4.11</A>) What is the Genetic Query
1056 <P>The <SMALL>GEQO</SMALL> module speeds query optimization when
1057 joining many tables by means of a Genetic Algorithm (GA). It allows
1058 the handling of large join queries through nonexhaustive
1061 <H4><A name="4.12">4.12</A>) How do I perform regular expression
1062 searches and case-insensitive regular expression searches? How do I
1063 use an index for case-insensitive searches?</H4>
1065 <P>The <I>~</I> operator does regular expression matching, and
1066 <I>~*</I> does case-insensitive regular expression matching. The
1067 case-insensitive variant of <SMALL>LIKE</SMALL> is called
1068 <SMALL>ILIKE</SMALL>.</P>
1070 <P>Case-insensitive equality comparisons are normally expressed
1075 WHERE lower(col) = 'abc';
1078 This will not use an standard index. However, if you create a
1079 functional index, it will be used:
1081 CREATE INDEX tabindex ON tab (lower(col));
1084 <H4><A name="4.13">4.13</A>) In a query, how do I detect if a field
1085 is <SMALL>NULL</SMALL>?</H4>
1087 <P>You test the column with <SMALL>IS NULL</SMALL> and <SMALL>IS
1088 NOT NULL</SMALL>.</P>
1090 <H4><A name="4.14">4.14</A>) What is the difference between the
1091 various character types?</H4>
1093 Type Internal Name Notes
1094 --------------------------------------------------
1095 VARCHAR(n) varchar size specifies maximum length, no padding
1096 CHAR(n) bpchar blank padded to the specified fixed length
1097 TEXT text no specific upper limit on length
1098 BYTEA bytea variable-length byte array (null-byte safe)
1099 "char" char one character
1102 <P>You will see the internal name when examining system catalogs
1103 and in some error messages.</P>
1105 <P>The first four types above are "varlena" types (i.e., the first
1106 four bytes on disk are the length, followed by the data). Thus the
1107 actual space used is slightly greater than the declared size.
1108 However, these data types are also subject to compression or being
1109 stored out-of-line by <SMALL>TOAST</SMALL>, so the space on disk
1110 might also be less than expected.</P>
1112 <SMALL>VARCHAR(n)</SMALL> is best when storing variable-length
1113 strings and it limits how long a string can be. <SMALL>TEXT</SMALL>
1114 is for strings of unlimited length, with a maximum of one gigabyte.
1115 <P><SMALL>CHAR(n)</SMALL> is for storing strings that are all the
1116 same length. <SMALL>CHAR(n)</SMALL> pads with blanks to the specified
1117 length, while <SMALL>VARCHAR(n)</SMALL> only stores the characters
1118 supplied. <SMALL>BYTEA</SMALL> is for storing binary data,
1119 particularly values that include <SMALL>NULL</SMALL> bytes. All the
1120 types described here have similar performance characteristics.</P>
1122 <H4><A name="4.15.1">4.15.1</A>) How do I create a
1123 serial/auto-incrementing field?</H4>
1125 <P>PostgreSQL supports a <SMALL>SERIAL</SMALL> data type. It
1126 auto-creates a sequence. For example,
1129 CREATE TABLE person (
1135 is automatically translated into this:
1137 CREATE SEQUENCE person_id_seq;
1138 CREATE TABLE person (
1139 id INT4 NOT NULL DEFAULT nextval('person_id_seq'),
1144 See the <I>create_sequence</I> manual page for more information
1145 about sequences. You can also use each row's <I>OID</I> field as a
1146 unique value. However, if you need to dump and reload the database,
1147 you need to use <I>pg_dump</I>'s <I>-o</I> option or <SMALL>COPY
1148 WITH OIDS</SMALL> option to preserve the <SMALL>OID</SMALL>s.
1150 <H4><A name="4.15.2">4.15.2</A>) How do I get the value of a
1151 <SMALL>SERIAL</SMALL> insert?</H4>
1153 <P>One approach is to retrieve the next <SMALL>SERIAL</SMALL> value
1154 from the sequence object with the <I>nextval()</I> function
1155 <I>before</I> inserting and then insert it explicitly. Using the
1156 example table in <A href="#4.15.1">4.15.1</A>, an example in a
1157 pseudo-language would look like this:</P>
1159 new_id = execute("SELECT nextval('person_id_seq')");
1160 execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
1163 You would then also have the new value stored in
1164 <CODE>new_id</CODE> for use in other queries (e.g., as a foreign
1165 key to the <CODE>person</CODE> table). Note that the name of the
1166 automatically created <SMALL>SEQUENCE</SMALL> object will be named
1167 <<I>table</I>>_<<I>serialcolumn</I>>_<I>seq</I>, where
1168 <I>table</I> and <I>serialcolumn</I> are the names of your table
1169 and your <SMALL>SERIAL</SMALL> column, respectively.
1171 <P>Alternatively, you could retrieve the assigned
1172 <SMALL>SERIAL</SMALL> value with the <I>currval()</I> function
1173 <I>after</I> it was inserted by default, e.g.,</P>
1175 execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
1176 new_id = execute("SELECT currval('person_id_seq')");
1179 Finally, you could use the <A href="#4.16"><SMALL>OID</SMALL></A>
1180 returned from the <SMALL>INSERT</SMALL> statement to look up the
1181 default value, though this is probably the least portable approach,
1182 and the oid value will wrap around when it reaches 4 billion.
1183 In Perl, using DBI with Edmund Mergl's DBD::Pg module, the oid
1184 value is made available via <I>$sth->{pg_oid_status}</I> after
1185 <I>$sth->execute()</I>.
1187 <H4><A name="4.15.3">4.15.3</A>) Don't <I>currval()</I> and
1188 <I>nextval()</I> lead to a race condition with other users?</H4>
1190 <P>No. <I>currval()</I> returns the current value assigned by your
1191 backend, not by all users.</P>
1193 <H4><A name="4.15.4">4.15.4</A>) Why aren't my sequence numbers
1194 reused on transaction abort? Why are there gaps in the numbering of
1195 my sequence/SERIAL column?</H4>
1197 <P>To improve concurrency, sequence values are given out to running
1198 transactions as needed and are not locked until the transaction
1199 completes. This causes gaps in numbering from aborted
1202 <H4><A name="4.16">4.16</A>) What is an <SMALL>OID</SMALL>? What is
1203 a <SMALL>TID</SMALL>?</H4>
1205 <P><SMALL>OID</SMALL>s are PostgreSQL's answer to unique row ids.
1206 Every row that is created in PostgreSQL gets a unique
1207 <SMALL>OID</SMALL>. All <SMALL>OID</SMALL>s generated during
1208 <I>initdb</I> are less than 16384 (from
1209 <I>include/access/transam.h</I>). All user-created
1210 <SMALL>OID</SMALL>s are equal to or greater than this. By default,
1211 all these <SMALL>OID</SMALL>s are unique not only within a table or
1212 database, but unique within the entire PostgreSQL installation.</P>
1214 <P>PostgreSQL uses <SMALL>OID</SMALL>s in its internal system
1215 tables to link rows between tables. These <SMALL>OID</SMALL>s can
1216 be used to identify specific user rows and used in joins. It is
1217 recommended you use column type <SMALL>OID</SMALL> to store
1218 <SMALL>OID</SMALL> values. You can create an index on the
1219 <SMALL>OID</SMALL> field for faster access.</P>
1221 <P>O<SMALL>ID</SMALL>s are assigned to all new rows from a central
1222 area that is used by all databases. If you want to change the
1223 <SMALL>OID</SMALL> to something else, or if you want to make a copy
1224 of the table, with the original <SMALL>OID</SMALL>s, there is no
1225 reason you can't do it:</P>
1227 CREATE TABLE new_table(mycol int);
1228 SELECT oid AS old_oid, mycol INTO tmp_table FROM old_table;
1229 COPY tmp_table TO '/tmp/pgtable';
1230 COPY new_table WITH OIDS FROM '/tmp/pgtable';
1231 DROP TABLE tmp_table;
1233 <P>O<SMALL>ID</SMALL>s are stored as 4-byte integers, and will
1234 overflow at 4 billion. No one has reported this ever happening, and
1235 we plan to have the limit removed before anyone does.</P>
1237 <P>T<SMALL>ID</SMALL>s are used to identify specific physical rows
1238 with block and offset values. T<SMALL>ID</SMALL>s change after rows
1239 are modified or reloaded. They are used by index entries to point
1240 to physical rows.</P>
1242 <H4><A name="4.17">4.17</A>) What is the meaning of some of the
1243 terms used in PostgreSQL?</H4>
1245 <P>Some of the source code and older documentation use terms that
1246 have more common usage. Here are some:</P>
1249 <LI>table, relation, class</LI>
1251 <LI>row, record, tuple</LI>
1253 <LI>column, field, attribute</LI>
1255 <LI>retrieve, select</LI>
1257 <LI>replace, update</LI>
1259 <LI>append, insert</LI>
1261 <LI><SMALL>OID</SMALL>, serial value</LI>
1263 <LI>portal, cursor</LI>
1265 <LI>range variable, table name, table alias</LI>
1268 <P>A list of general database terms can be found at: <A href=
1269 "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>
1271 <H4><A name="4.18">4.18</A>) Why do I get the error <I>"ERROR:
1272 Memory exhausted in AllocSetAlloc()"</I>?</H4>
1274 <P>You probably have run out of virtual memory on your system,
1275 or your kernel has a low limit for certain resources. Try this
1276 before starting <I>postmaster</I>:</P>
1282 Depending on your shell, only one of these may succeed, but it will
1283 set your process data segment limit much higher and perhaps allow
1284 the query to complete. This command applies to the current process,
1285 and all subprocesses created after the command is run. If you are
1286 having a problem with the <SMALL>SQL</SMALL> client because the
1287 backend is returning too much data, try it before starting the
1290 <H4><A name="4.19">4.19</A>) How do I tell what PostgreSQL version
1293 <P>From <I>psql</I>, type <CODE>SELECT version();</CODE></P>
1295 <H4><A name="4.20">4.20</A>) Why does my large-object operations
1296 get <I>"invalid large obj descriptor"</I>?</H4>
1298 <P>You need to put <CODE>BEGIN WORK</CODE> and <CODE>COMMIT</CODE>
1299 around any use of a large object handle, that is, surrounding
1300 <CODE>lo_open</CODE> ... <CODE>lo_close.</CODE></P>
1302 <P>Currently PostgreSQL enforces the rule by closing large object
1303 handles at transaction commit. So the first attempt to do anything
1304 with the handle will draw <I>invalid large obj descriptor</I>. So
1305 code that used to work (at least most of the time) will now
1306 generate that error message if you fail to use a transaction.</P>
1308 <P>If you are using a client interface like <SMALL>ODBC</SMALL> you
1309 may need to set <CODE>auto-commit off.</CODE></P>
1311 <H4><A name="4.21">4.21</A>) How do I create a column that will
1312 default to the current time?</H4>
1314 <P>Use <I>CURRENT_TIMESTAMP</I>:</P>
1316 <CODE>CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1320 <H4><A name="4.22">4.22</A>) Why are my subqueries using
1321 <CODE><SMALL>IN</SMALL></CODE> so slow?</H4>
1323 <P>In versions prior to 7.4, subqueries were joined to outer queries
1324 by sequentially scanning the result of the subquery for each row of
1325 the outer query. If the subquery returns only a few rows and the outer
1326 query returns many rows, <CODE><SMALL>IN</SMALL></CODE> is fastest. To
1327 speed up other queries, replace <CODE>IN</CODE> with
1328 <CODE>EXISTS</CODE>:</P>
1331 WHERE col IN (SELECT subcol FROM subtab);
1336 WHERE EXISTS (SELECT subcol FROM subtab WHERE subcol = col);
1339 For this to be fast, <CODE>subcol</CODE> should be an indexed column.
1340 <P>In version 7.4 and later, <CODE>IN</CODE> actually uses the same
1341 sophisticated join techniques as normal queries, and is prefered
1342 to using <CODE>EXISTS</CODE>.
1344 <H4><A name="4.23">4.23</A>) How do I perform an outer join?</H4>
1346 <P>PostgreSQL supports outer joins using the SQL standard syntax.
1347 Here are two examples:</P>
1350 FROM t1 LEFT OUTER JOIN t2 ON (t1.col = t2.col);
1355 FROM t1 LEFT OUTER JOIN t2 USING (col);
1358 <P>These identical queries join t1.col to t2.col, and also return
1359 any unjoined rows in t1 (those with no match in t2). A
1360 <SMALL>RIGHT</SMALL> join would add unjoined rows of t2. A
1361 <SMALL>FULL</SMALL> join would return the matched rows plus all
1362 unjoined rows from t1 and t2. The word <SMALL>OUTER</SMALL> is
1363 optional and is assumed in <SMALL>LEFT</SMALL>,
1364 <SMALL>RIGHT</SMALL>, and <SMALL>FULL</SMALL> joins. Ordinary joins
1365 are called <SMALL>INNER</SMALL> joins.</P>
1367 <P>In previous releases, outer joins can be simulated using
1368 <SMALL>UNION</SMALL> and <SMALL>NOT IN</SMALL>. For example, when
1369 joining <I>tab1</I> and <I>tab2</I>, the following query does an
1370 <I>outer</I> join of the two tables:<BR>
1374 SELECT tab1.col1, tab2.col2
1376 WHERE tab1.col1 = tab2.col1
1378 SELECT tab1.col1, NULL
1380 WHERE tab1.col1 NOT IN (SELECT tab2.col1 FROM tab2)
1384 <H4><A name="4.24">4.24</A>) How do I perform queries using
1385 multiple databases?</H4>
1387 <P>There is no way to query a database other than the current one.
1388 Because PostgreSQL loads database-specific system catalogs, it is
1389 uncertain how a cross-database query should even behave.</P>
1391 <P><I>contrib/dblink</I> allows cross-database queries using
1392 function calls. Of course, a client can make simultaneous
1393 connections to different databases and merge the results on the
1396 <H4><A name="4.25">4.25</A>) How do I return multiple rows or
1397 columns from a function?</H4>
1399 <P>In 7.3, you can easily return multiple rows or columns from a
1401 <a href="http://techdocs.postgresql.org/guides/SetReturningFunctions">
1402 http://techdocs.postgresql.org/guides/SetReturningFunctions</a>.
1404 <H4><A name="4.26">4.26</A>) Why can't I reliably create/drop
1405 temporary tables in PL/PgSQL functions?</H4>
1406 <P>PL/PgSQL caches function contents, and an unfortunate side effect
1407 is that if a PL/PgSQL function accesses a temporary table, and that
1408 table is later dropped and recreated, and the function called
1409 again, the function will fail because the cached function contents
1410 still point to the old temporary table. The solution is to use
1411 <SMALL>EXECUTE</SMALL> for temporary table access in PL/PgSQL. This
1412 will cause the query to be reparsed every time.</P>
1414 <H4><A name="4.27">4.27</A>) What replication options are available?
1416 <P>There are several master/slave replication options available.
1417 These allow only the master to make database changes and the slave
1418 can only do database reads. The bottom of <a
1419 href="http://gborg.PostgreSQL.org/genpage?replication_research">
1420 http://gborg.PostgreSQL.org/genpage?replication_research</a> lists
1421 them. A multi-master replication solution is being worked on at <a
1422 href="http://gborg.PostgreSQL.org/project/pgreplication/projdisplay.php">http://gborg.PostgreSQL.org/project/pgreplication/projdisplay.php</a>.</P>
1424 <H4><A name="4.28">4.28</A>) What encryption options are available?
1427 <LI><I>contrib/pgcrypto</I> contains many encryption functions for
1428 use in <SMALL>SQL</SMALL> queries.</LI>
1429 <LI>To encrypt transmission from the client to the server, the server
1430 must have the <I>ssl</I> option set to <I>true</I> in <I>postgresql.conf,
1431 </I> and an applicable <I>host</I> or <I>hostssl</I> record must exist in
1432 <I>pg_hba.conf</I>, and the client <I>sslmode</I> must not be
1433 <I>disable.</I> (Note that it is also possible to use a third-party
1434 encrypted transport, such as stunnel or ssh, rather than PostgreSQL's
1435 native SSL connections.)
1436 <LI>Database user passwords are automatically encrypted when stored in
1437 version 7.3. In previous versions, you must enable the option
1438 <I>PASSWORD_ENCRYPTION</I> in <I>postgresql.conf</I>.</LI>
1439 <LI>The server can run using an encrypted file system.</LI>
1444 <H2 align="center">Extending PostgreSQL</H2>
1446 <H4><A name="5.1">5.1</A>) I wrote a user-defined function. When I
1447 run it in <I>psql</I>, why does it dump core?</H4>
1449 <P>The problem could be a number of things. Try testing your
1450 user-defined function in a stand-alone test program first.</P>
1452 <H4><A name="5.2">5.2</A>) How can I contribute some nifty new
1453 types and functions to PostgreSQL?</H4>
1455 <P>Send your extensions to the <I>pgsql-hackers</I> mailing list,
1456 and they will eventually end up in the <I>contrib/</I>
1459 <H4><A name="5.3">5.3</A>) How do I write a C function to return a
1462 <P>In versions of PostgreSQL beginning with 7.3, table-returning
1463 functions are fully supported in C, PL/PgSQL, and SQL. See the
1464 Programmer's Guide for more information. An example of a
1465 table-returning function defined in C can be found in
1466 <I>contrib/tablefunc</I>.</P>
1468 <H4><A name="5.4">5.4</A>) I have changed a source file. Why does
1469 the recompile not see the change?</H4>
1471 <P>The <I>Makefiles</I> do not have the proper dependencies for
1472 include files. You have to do a <I>make clean</I> and then another
1473 <I>make</I>. If you are using <SMALL>GCC</SMALL> you can use the
1474 <I>--enable-depend</I> option of <I>configure</I> to have the
1475 compiler compute the dependencies automatically.</P>