]> granicus.if.org Git - postgresql/blob - doc/src/FAQ/FAQ.html
Update MySQL mention.
[postgresql] / doc / src / FAQ / FAQ.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML>
3   <HEAD>
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>
7   </HEAD>
8
9   <BODY bgcolor="#ffffff" text="#000000" link="#ff0000" vlink="#a00000"
10   alink="#0000ff">
11     <H1>Frequently Asked Questions (FAQ) for PostgreSQL</H1>
12
13     <P>Last updated: Fri Oct  3 21:39:47 EDT 2003</P>
14
15     <P>Current maintainer: Bruce Momjian (<A href=
16     "mailto:pgman@candle.pha.pa.us">pgman@candle.pha.pa.us</A>)<BR>
17     </P>
18
19     <P>The most recent version of this document can be viewed at <A
20     href=
21     "http://www.PostgreSQL.org/docs/faqs/FAQ.html">http://www.PostgreSQL.org/docs/faqs/FAQ.html</A>.</P>
22
23     <P>Platform-specific questions are answered at <A href=
24     "http://www.PostgreSQL.org/docs/index.html">http://www.PostgreSQL.org/docs/index.html</A>.</P>
25     <HR>
26
27     <H2 align="center">General Questions</H2>
28     <A href="#1.1">1.1</A>) What is PostgreSQL? How is it pronounced?<BR>
29      <A href="#1.2">1.2</A>) What is the copyright on PostgreSQL?<BR>
30      <A href="#1.3">1.3</A>) What Unix platforms does PostgreSQL run
31     on?<BR>
32      <A href="#1.4">1.4</A>) What non-Unix ports are available?<BR>
33      <A href="#1.5">1.5</A>) Where can I get PostgreSQL?<BR>
34      <A href="#1.6">1.6</A>) Where can I get support?<BR>
35      <A href="#1.7">1.7</A>) What is the latest release?<BR>
36      <A href="#1.8">1.8</A>) What documentation is available?<BR>
37      <A href="#1.9">1.9</A>) How do I find out about known bugs or
38     missing features?<BR>
39      <A href="#1.10">1.10</A>) How can I learn <SMALL>SQL</SMALL>?<BR>
40      <A href="#1.11">1.11</A>) Is PostgreSQL Y2K compliant?<BR>
41      <A href="#1.12">1.12</A>) How do I join the development team?<BR>
42      <A href="#1.13">1.13</A>) How do I submit a bug report?<BR>
43      <A href="#1.14">1.14</A>) How does PostgreSQL compare to other
44     <SMALL>DBMS</SMALL>s?<BR>
45      <A href="#1.15">1.15</A>) How can I financially assist
46     PostgreSQL?<BR>
47      
48
49     <H2 align="center">User Client Questions</H2>
50     <A href="#2.1">2.1</A>) Are there <SMALL>ODBC</SMALL> drivers for
51     PostgreSQL?<BR>
52      <A href="#2.2">2.2</A>) What tools are available for using
53     PostgreSQL with Web pages?<BR>
54      <A href="#2.3">2.3</A>) Does PostgreSQL have a graphical user
55     interface?<BR>
56      <A href="#2.4">2.4</A>) What languages are available to
57     communicate with PostgreSQL?<BR>
58      
59
60     <H2 align="center">Administrative Questions</H2>
61     <A href="#3.1">3.1</A>) How do I install PostgreSQL somewhere other
62     than <I>/usr/local/pgsql</I>?<BR>
63      <A href="#3.2">3.2</A>) When I start <I>postmaster</I>, I get a
64     <I>Bad System Call</I> or core dumped message. Why?<BR>
65      <A href="#3.3">3.3</A>) When I try to start <I>postmaster</I>, I
66     get <I>IpcMemoryCreate</I> errors. Why?<BR>
67      <A href="#3.4">3.4</A>) When I try to start <I>postmaster</I>, I
68     get <I>IpcSemaphoreCreate</I> errors. Why?<BR>
69      <A href="#3.5">3.5</A>) How do I control connections from other
70     hosts?<BR>
71      <A href="#3.6">3.6</A>) How do I tune the database engine for
72     better performance?<BR>
73      <A href="#3.7">3.7</A>) What debugging features are available?<BR>
74      <A href="#3.8">3.8</A>) Why do I get <I>"Sorry, too many
75     clients"</I> when trying to connect?<BR>
76      <A href="#3.9">3.9</A>) What is in the  <I>pgsql_tmp</I>
77     directory?<BR>
78      <A href="#3.10">3.10</A>) Why do I need to do a dump and restore
79     to upgrade PostgreSQL releases?<BR>
80
81
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
98     the indexes. Why?<BR>
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
126     am running?<BR>
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
135     databases?<BR>
136      <A href="#4.25">4.25</A>) How do I return multiple rows or columns
137     from a function?<BR>
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>
142      
143
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
150     tuple?<BR>
151      <A href="#5.4">5.4</A>) I have changed a source file. Why does the
152     recompile not see the change?<BR>
153      
154     <HR>
155
156     <H2 align="center">General Questions</H2>
157
158     <H4><A name="1.1">1.1</A>) What is PostgreSQL? How is it pronounced?</H4>
159
160     <P>PostgreSQL is pronounced <I>Post-Gres-Q-L</I>.</P>
161
162     <P>PostgreSQL is an enhancement of the POSTGRES database management
163     system, a next-generation <SMALL>DBMS</SMALL> research prototype.
164     While PostgreSQL retains the powerful data model and rich data
165     types of POSTGRES, it replaces the PostQuel query language with an
166     extended subset of <SMALL>SQL</SMALL>. PostgreSQL is free and the
167     complete source is available.</P>
168
169     <P>PostgreSQL development is performed by a team of
170     developers who all subscribe to the PostgreSQL development mailing
171     list. The current coordinator is Marc G. Fournier (<A href=
172     "mailto:scrappy@PostgreSQL.org">scrappy@PostgreSQL.org</A>). (See
173     section <a href="#1.6">1.6</a> on how to join). This team is now 
174     responsible for all development of PostgreSQL.</P>
175
176     <P>The authors of PostgreSQL 1.01 were Andrew Yu and Jolly Chen.
177     Many others have contributed to the porting, testing, debugging,
178     and enhancement of the code. The original Postgres code, from which
179     PostgreSQL is derived, was the effort of many graduate students,
180     undergraduate students, and staff programmers working under the
181     direction of Professor Michael Stonebraker at the University of
182     California, Berkeley.</P>
183
184     <P>The original name of the software at Berkeley was Postgres. When
185     <SMALL>SQL</SMALL> functionality was added in 1995, its name was
186     changed to Postgres95. The name was changed at the end of 1996 to
187     PostgreSQL.</P>
188
189     <H4><A name="1.2">1.2</A>) What is the copyright on
190     PostgreSQL?</H4>
191
192     <P>PostgreSQL is subject to the following COPYRIGHT:</P>
193
194     <P>PostgreSQL Data Base Management System</P>
195
196     <P>Portions copyright (c) 1996-2002, PostgreSQL Global Development
197     Group Portions Copyright (c) 1994-6 Regents of the University of
198     California</P>
199
200     <P>Permission to use, copy, modify, and distribute this software
201     and its documentation for any purpose, without fee, and without a
202     written agreement is hereby granted, provided that the above
203     copyright notice and this paragraph and the following two
204     paragraphs appear in all copies.</P>
205
206     <P>IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
207     PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
208     DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS
209     SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
210     CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</P>
211
212     <P>THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
213     WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
214     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
215     SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE
216     UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
217     SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</P>
218
219     <P>The above is the BSD license, the classic open-source license.
220     It has no restrictions on how the source code may be used. We like
221     it and have no intention of changing it.</P>
222
223     <H4><A name="1.3">1.3</A>) What Unix platforms does PostgreSQL run
224     on?</H4>
225
226     <P>In general, a modern Unix-compatible platform should be able to
227     run PostgreSQL. The platforms that had received explicit testing at
228     the time of release are listed in the installation
229     instructions.</P>
230
231     <H4><A name="1.4">1.4</A>) What non-Unix ports are available?</H4>
232
233     <P><STRONG>Client</STRONG></P>
234
235     <P>It is possible to compile the <I>libpq</I> C library, psql, and
236     other interfaces and client applications to run on MS Windows platforms.
237     In this case, the client is running on MS Windows, and communicates
238     via TCP/IP to a server running on one of our supported Unix
239     platforms. A file <I>win32.mak</I> is included in the distribution
240     for making a Win32 <I>libpq</I> library and <I>psql</I>. PostgreSQL
241     also communicates with <SMALL>ODBC</SMALL> clients.</P>
242
243     <P><STRONG>Server</STRONG></P>
244
245     <P>The database server can run on Windows NT and Win2k using
246     Cygwin, the Cygnus Unix/NT porting library. See
247     <I>pgsql/doc/FAQ_MSWIN</I> in the distribution or the MS Windows FAQ
248     at <A href="http://www.PostgreSQL.org/docs/faqs/text/FAQ_MSWIN">
249     http://www.PostgreSQL.org/docs/faqs/text/FAQ_MSWIN</A>.</P>
250
251     <p>A native port to MS Win NT/2000/XP is currently being worked
252     on. For more details on the current status of PostgreSQL on Windows see
253     <a href="http://techdocs.postgresql.org/guides/Windows">
254     http://techdocs.postgresql.org/guides/Windows</a> and
255     <a href="http://momjian.postgresql.org/main/writings/pgsql/win32.html">
256     http://momjian.postgresql.org/main/writings/pgsql/win32.html</a>.</p>
257
258     <p>There is also a Novell Netware 6 port at
259     <a href="http://forge.novell.com">http://forge.novell.com</a>.</p>
260
261     <H4><A name="1.5">1.5</A>) Where can I get PostgreSQL?</H4>
262
263     <P>The primary anonymous ftp site for PostgreSQL is <A href=
264     "ftp://ftp.PostgreSQL.org/pub">ftp://ftp.PostgreSQL.org/pub</A>.
265     For mirror sites, see our main web site.</P>
266
267     <H4><A name="1.6">1.6</A>) Where can I get support?</H4>
268
269     <P>The main mailing list is: <A href=
270     "mailto:pgsql-general@PostgreSQL.org">pgsql-general@PostgreSQL.org</A>.
271     It is available for discussion of matters pertaining to PostgreSQL.
272     To subscribe, send mail with the following lines in the body (not
273     the subject line):</P>
274 <PRE>
275     subscribe
276     end
277 </PRE>
278
279     <P>to <A href=
280     "mailto:pgsql-general-request@PostgreSQL.org">pgsql-general-request@PostgreSQL.org</A>.</P>
281
282     <P>There is also a digest list available. To subscribe to this
283     list, send email to: <A href=
284     "mailto:pgsql-general-digest-request@PostgreSQL.org">pgsql-general-digest-request@PostgreSQL.org</A>
285     with a body of:</P>
286 <PRE>
287     subscribe
288     end
289 </PRE>
290
291     Digests are sent out to members of this list whenever the main list
292     has received around 30k of messages. 
293
294     <P>The bugs mailing list is available. To subscribe to this list,
295     send email to <A href=
296     "mailto:pgsql-bugs-request@PostgreSQL.org">pgsql-bugs-request@PostgreSQL.org</A>
297     with a body of:</P>
298 <PRE>
299     subscribe
300     end
301 </PRE>
302
303     There is also a developers discussion mailing list available. To
304     subscribe to this list, send email to <A href=
305     "mailto:pgsql-hackers-request@PostgreSQL.org">pgsql-hackers-request@PostgreSQL.org</A>
306     with a body of: 
307 <PRE>
308     subscribe
309     end
310 </PRE>
311
312     <P>Additional mailing lists and information about PostgreSQL can be
313     found via the PostgreSQL WWW home page at:</P>
314
315     <BLOCKQUOTE>
316       <A href="http://www.PostgreSQL.org">http://www.PostgreSQL.org</A>
317     </BLOCKQUOTE>
318
319     <P>There is also an IRC channel on EFNet and OpenProjects,
320     channel <I>#PostgreSQL</I>. I use the Unix command <CODE>irc -c
321     '#PostgreSQL' "$USER" irc.phoenix.net.</CODE></P>
322
323     <P>A list of commercial support companies is available at <A href=
324     "http://techdocs.postgresql.org/companies.php">http://techdocs.postgresql.org/companies.php</A>.</P>
325
326     <H4><A name="1.7">1.7</A>) What is the latest release?</H4>
327
328     <P>The latest release of PostgreSQL is version 7.3.4.</P>
329
330     <P>We plan to have major releases every six to eight months.</P>
331
332     <H4><A name="1.8">1.8</A>) What documentation is available?</H4>
333
334     <P>Several manuals, manual pages, and some small test examples are
335     included in the distribution. See the <I>/doc</I> directory. You
336     can also browse the manuals online at <A href=
337     "http://www.PostgreSQL.org/docs">http://www.PostgreSQL.org/docs</A>.</P>
338
339     <P>There are two PostgreSQL books available online at <A href=
340     "http://www.PostgreSQL.org/docs/awbook.html">http://www.PostgreSQL.org/docs/awbook.html</A>
341     and <A href=
342     "http://www.commandprompt.com/ppbook/">http://www.commandprompt.com/ppbook/</A>.
343     There is a list of PostgreSQL books available for purchase at <A
344     href=
345     "http://techdocs.postgresql.org/techdocs/bookreviews.php">http://techdocs.PostgreSQL.org/techdocs/bookreviews.php</A>.
346     There is also a collection of PostgreSQL technical articles at <A
347     href=
348     "http://techdocs.PostgreSQL.org/">http://techdocs.PostgreSQL.org/</A>.</P>
349
350     <P><I>psql</I> has some nice \d commands to show information about
351     types, operators, functions, aggregates, etc.</P>
352
353     <P>Our web site contains even more documentation.</P>
354
355     <H4><A name="1.9">1.9</A>) How do I find out about known bugs or
356     missing features?</H4>
357
358     <P>PostgreSQL supports an extended subset of <SMALL>SQL</SMALL>-92.
359     See our <A href="http://developer.PostgreSQL.org/todo.php">TODO</A>
360     list for known bugs, missing features, and future plans.</P>
361
362     <H4><A name="1.10">1.10</A>) How can I learn
363     <SMALL>SQL</SMALL>?</H4>
364
365     <P>The PostgreSQL book at <A href=
366     "http://www.PostgreSQL.org/docs/awbook.html">http://www.PostgreSQL.org/docs/awbook.html</A>
367     teaches <SMALL>SQL</SMALL>. There is another PostgreSQL book at <A
368     href=
369     "http://www.commandprompt.com/ppbook/">http://www.commandprompt.com/ppbook.</A>
370     There is a nice tutorial at <A href=
371     "http://www.intermedia.net/support/sql/sqltut.shtm">http://www.intermedia.net/support/sql/sqltut.shtm,</A>
372     at <A href=
373     "http://ourworld.compuserve.com/homepages/graeme_birchall/HTM_COOK.HTM">
374     http://ourworld.compuserve.com/homepages/graeme_birchall/HTM_COOK.HTM,</A>
375     and at <A href=
376     "http://sqlcourse.com/">http://sqlcourse.com.</A></P>
377
378     <P>Another one is "Teach Yourself SQL in 21 Days, Second Edition"
379     at <A href=
380     "http://members.tripod.com/er4ebus/sql/index.htm">http://members.tripod.com/er4ebus/sql/index.htm</A></P>
381
382     <P>Many of our users like <I>The Practical SQL Handbook</I>,
383     Bowman, Judith S., et al., Addison-Wesley. Others like <I>The
384     Complete Reference SQL</I>, Groff et al., McGraw-Hill.</P>
385
386     <H4><A name="1.11">1.11</A>) Is PostgreSQL Y2K compliant?</H4>
387
388     <P>Yes, we easily handle dates past the year 2000 AD, and before
389     2000 BC.</P>
390
391     <H4><A name="1.12">1.12</A>) How do I join the development
392     team?</H4>
393
394     <P>First, download the latest source and read the PostgreSQL
395     Developers documentation on our web site, or in the distribution.
396     Second, subscribe to the <I>pgsql-hackers</I> and
397     <I>pgsql-patches</I> mailing lists. Third, submit high quality
398     patches to pgsql-patches.</P>
399
400     <P>There are about a dozen people who have commit privileges to the
401     PostgreSQL <SMALL>CVS</SMALL> archive. They each have submitted so
402     many high-quality patches that it was impossible for the existing
403     committers to keep up, and we had confidence that patches they
404     committed were of high quality.</P>
405
406     <H4><A name="1.13">1.13</A>) How do I submit a bug report?</H4>
407
408     <P>Please visit the PostgreSQL BugTool page at <A href=
409     "http://www.PostgreSQL.org/bugs/bugs.php">http://www.PostgreSQL.org/bugs/bugs.php</A>,
410     which gives guidelines and directions on how to submit a
411     bug report.</P>
412
413     <P>Also check out our ftp site <A href=
414     "ftp://ftp.PostgreSQL.org/pub">ftp://ftp.PostgreSQL.org/pub</A> to
415     see if there is a more recent PostgreSQL version or patches.</P>
416
417     <H4><A name="1.14">1.14</A>) How does PostgreSQL compare to other
418     <SMALL>DBMS</SMALL>s?</H4>
419
420     <P>There are several ways of measuring software: features,
421     performance, reliability, support, and price.</P>
422
423     <DL>
424       <DT><B>Features</B></DT>
425
426       <DD>PostgreSQL has most features present in large commercial
427       <SMALL>DBMS</SMALL>s, like transactions, subselects, triggers,
428       views, foreign key referential integrity, and sophisticated
429       locking. We have some features they do not have, like
430       user-defined types, inheritance, rules, and multi-version
431       concurrency control to reduce lock contention.<BR>
432       <BR>
433       </DD>
434
435       <DT><B>Performance</B></DT>
436
437       <DD>PostgreSQL has performance similar to other commercial and
438       open source databases. it is faster for some things, slower for
439       others. In comparison to MySQL or leaner database systems, we are
440       faster for multiple users, complex queries, and a read/write query
441       load.  MySQL is faster for SELECT queries done by a few users.  Of
442       course, MySQL does not have any of the features mentioned in the
443       <I>Features</I> section above. We are built for reliability and
444       features, though we continue to improve performance in every
445       release. There is an interesting Web page comparing PostgreSQL to
446       MySQL at <A href="http://openacs.org/philosophy/why-not-mysql.html">
447       http://openacs.org/philosophy/why-not-mysql.html</A><BR>
448
449       <BR>
450       </DD>
451
452       <DT><B>Reliability</B></DT>
453
454       <DD>We realize that a <SMALL>DBMS</SMALL> must be reliable, or it
455       is worthless. We strive to release well-tested, stable code that
456       has a minimum of bugs. Each release has at least one month of
457       beta testing, and our release history shows that we can provide
458       stable, solid releases that are ready for production use. We
459       believe we compare favorably to other database software in this
460       area.<BR>
461       <BR>
462       </DD>
463
464       <DT><B>Support</B></DT>
465
466       <DD>Our mailing lists provide contact with a large group of developers
467       and users to help resolve any problems encountered. While we cannot
468       guarantee a fix, commercial <SMALL>DBMS</SMALL>s do not always
469       supply a fix either. Direct access to developers, the user
470       community, manuals, and the source code often make PostgreSQL
471       support superior to other <SMALL>DBMS</SMALL>s. There is
472       commercial per-incident support available for those who need it.
473       (See <A href="#1.6">FAQ section 1.6</A>.)<BR>
474       <BR>
475       </DD>
476
477       <DT><B>Price</B></DT>
478
479       <DD>We are free for all use, both commercial and non-commercial.
480       You can add our code to your product with no limitations, except
481       those outlined in our BSD-style license stated above.<BR>
482       <BR>
483       </DD>
484     </DL>
485
486     <H4><A name="1.15">1.15</A>) How can I financially assist
487     PostgreSQL?</H4>
488
489     <P>PostgreSQL has had a first-class infrastructure since we started
490     in 1996. This is all thanks to Marc Fournier, who has created
491     and managed this infrastructure over the years.</P>
492
493     <P>Quality infrastructure is very important to an open-source
494     project. It prevents disruptions that can greatly delay forward
495     movement of the project.</P>
496
497     <P>Of course, this infrastructure is not cheap. There are a variety
498     of monthly and one-time expenses that are required to keep it
499     going. If you or your company has money it can donate to help fund
500     this effort, please go to <A href="http://store.pgsql.com/shopping/">http://store.pgsql.com/shopping/</A>
501     and make a donation.</P>
502
503     <P>Although the web page mentions PostgreSQL, Inc, the
504     "contributions" item is solely to support the PostgreSQL project
505     and does not fund any specific company. If you prefer, you can also
506     send a check to the contact address.</P>
507     <HR>
508
509     <P>Also, if you have a success story about PostgreSQL, please submit
510     it to our advocacy site at <a href="http://advocacy.postgresql.org">
511     http://advocacy.postgresql.org</a>.</P>
512
513
514     <H2 align="center">User Client Questions</H2>
515
516     <H4><A name="2.1">2.1</A>) Are there <SMALL>ODBC</SMALL> drivers
517     for PostgreSQL?</H4>
518
519     <P>There are two <SMALL>ODBC</SMALL> drivers available, PsqlODBC
520     and OpenLink <SMALL>ODBC</SMALL>.</P>
521
522     <P>You can download PsqlODBC from <A href=
523     "http://gborg.postgresql.org/project/psqlodbc/projdisplay.php">
524     http://gborg.postgresql.org/project/psqlodbc/projdisplay.php</A>.</P>
525
526     <P>OpenLink <SMALL>ODBC</SMALL> can be gotten from <A href=
527     "http://www.openlinksw.com/">http://www.openlinksw.com</A>. It
528     works with their standard <SMALL>ODBC</SMALL> client software so
529     you'll have PostgreSQL <SMALL>ODBC</SMALL> available on every
530     client platform they support (Win, Mac, Unix, VMS).</P>
531
532     <P>They will probably be selling this product to people who need
533     commercial-quality support, but a freeware version will always be
534     available. Please send questions to <A href=
535     "mailto:postgres95@openlink.co.uk">postgres95@openlink.co.uk</A>.</P>
536
537     <H4><A name="2.2">2.2</A>) What tools are available for using
538     PostgreSQL with Web pages?</H4>
539
540     <P>A nice introduction to Database-backed Web pages can be seen at:
541     <A href="http://www.webreview.com">http://www.webreview.com</A></P>
542
543     <P>For Web integration, PHP is an excellent interface. It is at <A
544     href="http://www.php.net">http://www.php.net</A>.</P>
545
546     <P>For complex cases, many use the Perl interface and CGI.pm or mod_perl.</P>
547
548     <H4><A name="2.3">2.3</A>) Does PostgreSQL have a graphical user
549     interface?</H4>
550
551     <P>Yes, there are several graphical interfaces to PostgreSQL available.
552     These include PgAccess <a href="http://www.pgaccess.org">
553     http://www.pgaccess.org</a>), PgAdmin II (<a
554     href="http://www.pgadmin.org">http://www.pgadmin.org</a>,
555     Win32-only), RHDB Admin (<a
556     href="http://sources.redhat.com/rhdb/">http://sources.redhat.com/rhdb/
557     </a>) and Rekall (<a href="http://www.thekompany.com/products/rekall/">
558     http://www.thekompany.com/products/rekall/</a>, proprietary). There is 
559     also PHPPgAdmin (<a href="http://phppgadmin.sourceforge.net/">
560     http://phppgadmin.sourceforge.net/ </a>), a web-based interface to 
561     PostgreSQL.</P>
562
563     <P>See <a href="http://techdocs.postgresql.org/guides/GUITools">http://techdocs.postgresql.org/guides/GUITools</a> for a more detailed list.</P>
564
565     <H4><A name="2.4">2.4</A>) What languages are able to communicate with 
566     PostgreSQL?</H4>
567
568     <P>Most popular programming languages contain an interface to
569     PostgreSQL. Check your programming language's list of extension
570     modules.</P>
571
572     <P>The following interfaces are included in the PostgreSQL
573     distribution:</P>
574
575     <UL>
576       <LI>C (libpq)</LI>
577
578       <LI>Embedded C (ecpg)</LI>
579
580       <LI>Java (jdbc)</LI>
581
582       <LI>Python (PyGreSQL)</LI>
583
584       <LI>TCL (libpgtcl)</LI>
585
586     </UL>
587     <P>Additional interfaces are available at 
588     <a href="http://gborg.postgresql.org">http://gborg.postgresql.org</A>
589     in the <I>Drivers/Interfaces</I> section.
590     </P>
591     <HR>
592
593     <H2 align="center">Administrative Questions</H2>
594
595     <H4><A name="3.1">3.1</A>) How do I install PostgreSQL somewhere
596     other than <I>/usr/local/pgsql</I>?</H4>
597
598     <P>Specify the <I>--prefix</I> option when running
599     <I>configure</I>.</P>
600
601     <H4><A name="3.2">3.2</A>) When I start <I>postmaster</I>, I get a
602     <I>Bad System Call</I> or core dumped message. Why?</H4>
603
604     <P>It could be a variety of problems, but first check to see that
605     you have System V extensions installed in your kernel. PostgreSQL
606     requires kernel support for shared memory and semaphores.</P>
607
608     <H4><A name="3.3">3.3</A>) When I try to start <I>postmaster</I>, I
609     get <I>IpcMemoryCreate</I> errors. Why?</H4>
610
611     <P>You either do not have shared memory configured properly in your
612     kernel or you need to enlarge the shared memory available in the
613     kernel. The exact amount you need depends on your architecture and
614     how many buffers and backend processes you configure for
615     <I>postmaster</I>. For most systems, with default numbers of
616     buffers and processes, you need a minimum of ~1 MB. See the <A
617     href=
618     "http://www.PostgreSQL.org/docs/view.php?version=current&amp;idoc=1&amp;file=kernel-resources.html">PostgreSQL
619     Administrator's Guide</A> for more detailed information about
620     shared memory and semaphores.</P>
621
622     <H4><A name="3.4">3.4</A>) When I try to start <I>postmaster</I>, I
623     get <I>IpcSemaphoreCreate</I> errors. Why?</H4>
624
625     <P>If the error message is <I>IpcSemaphoreCreate: semget failed (No
626     space left on device)</I> then your kernel is not configured with
627     enough semaphores. Postgres needs one semaphore per potential
628     backend process. A temporary solution is to start <I>postmaster</I>
629     with a smaller limit on the number of backend processes. Use
630     <I>-N</I> with a parameter less than the default of 32. A more
631     permanent solution is to increase your kernel's
632     <SMALL>SEMMNS</SMALL> and <SMALL>SEMMNI</SMALL> parameters.</P>
633
634     <P>Inoperative semaphores can also cause crashes during heavy
635     database access.</P>
636
637     <P>If the error message is something else, you might not have
638     semaphore support configured in your kernel at all. See the
639     PostgreSQL Administrator's Guide for more detailed information
640     about shared memory and semaphores.</P>
641
642     <H4><A name="3.5">3.5</A>) How do I control connections from other
643     hosts?</H4>
644
645     <P>By default, PostgreSQL only allows connections from the local
646     machine using Unix domain sockets. Other machines will not be able
647     to connect unless you add the <I>-i</I> flag to <I>postmaster</I>,
648     <B>and</B> enable host-based authentication by modifying the file
649     <I>$PGDATA/pg_hba.conf</I> accordingly. This will allow TCP/IP
650     connections.</P>
651
652     <H4><A name="3.6">3.6</A>) How do I tune the database engine for
653     better performance?</H4>
654
655     <P>Certainly, indexes can speed up queries. The
656     <SMALL>EXPLAIN</SMALL> command allows you to see how PostgreSQL is
657     interpreting your query, and which indexes are being used.</P>
658
659     <P>If you are doing many <SMALL>INSERTs</SMALL>, consider doing
660     them in a large batch using the <SMALL>COPY</SMALL> command. This
661     is much faster than individual <SMALL>INSERTS</SMALL>. Second,
662     statements not in a <SMALL>BEGIN WORK/COMMIT</SMALL> transaction
663     block are considered to be in their own transaction. Consider
664     performing several statements in a single transaction block. This
665     reduces the transaction overhead. Also, consider dropping and
666     recreating indexes when making large data changes.</P>
667
668     <P>There are several tuning options. You can disable <I>fsync()</I>
669     by starting <I>postmaster</I> with a <I>-o -F</I> option. This will
670     prevent <I>fsync()</I>s from flushing to disk after every
671     transaction.</P>
672
673     <P>You can also use the <I>postmaster</I> <I>-B</I> option to
674     increase the number of shared memory buffers used by the backend
675     processes. If you make this parameter too high, the
676     <I>postmaster</I> may not start because you have exceeded your
677     kernel's limit on shared memory space. Each buffer is 8K and the
678     default is 64 buffers.</P>
679
680     <P>You can also use the backend <I>-S</I> option to increase the
681     maximum amount of memory used by the backend process for temporary
682     sorts. The <I>-S</I> value is measured in kilobytes, and the
683     default is 512 (i.e. 512K).</P>
684
685     <P>You can also use the <SMALL>CLUSTER</SMALL> command to group
686     data in tables to match an index. See the <SMALL>CLUSTER</SMALL>
687     manual page for more details.</P>
688
689     <H4><A name="3.7">3.7</A>) What debugging features are
690     available?</H4>
691
692     <P>PostgreSQL has several features that report status information
693     that can be valuable for debugging purposes.</P>
694
695     <P>First, by running <I>configure</I> with the --enable-cassert
696     option, many <I>assert()</I>s monitor the progress of the backend
697     and halt the program when something unexpected occurs.</P>
698
699     <P>Both <I>postmaster</I> and <I>postgres</I> have several debug
700     options available. First, whenever you start <I>postmaster</I>,
701     make sure you send the standard output and error to a log file,
702     like:</P>
703 <PRE>
704     cd /usr/local/pgsql
705     ./bin/postmaster &gt;server.log 2&gt;&amp;1 &amp;
706 </PRE>
707
708     <P>This will put a server.log file in the top-level PostgreSQL
709     directory. This file contains useful information about problems or
710     errors encountered by the server. <I>Postmaster</I> has a <I>-d</I>
711     option that allows even more detailed information to be reported.
712     The <I>-d</I> option takes a number that specifies the debug level.
713     Be warned that high debug level values generate large log
714     files.</P>
715
716     <P>If <I>postmaster</I> is not running, you can actually run the
717     <I>postgres</I> backend from the command line, and type your
718     <SMALL>SQL</SMALL> statement directly. This is recommended
719     <B>only</B> for debugging purposes. Note that a newline terminates
720     the query, not a semicolon. If you have compiled with debugging
721     symbols, you can use a debugger to see what is happening. Because
722     the backend was not started from <I>postmaster</I>, it is not
723     running in an identical environment and locking/backend interaction
724     problems may not be duplicated.</P>
725
726     <P>If <I>postmaster</I> is running, start <I>psql</I> in one
727     window, then find the <SMALL>PID</SMALL> of the <I>postgres</I>
728     process used by <I>psql</I>. Use a debugger to attach to the
729     <I>postgres</I> <SMALL>PID</SMALL>. You can set breakpoints in the
730     debugger and issue queries from <I>psql</I>. If you are debugging
731     <I>postgres</I> startup, you can set PGOPTIONS="-W n", then start
732     <I>psql</I>. This will cause startup to delay for <I>n</I> seconds
733     so you can attach to the process with the debugger, set any
734     breakpoints, and continue through the startup sequence.</P>
735
736     <P>The <I>postgres</I> program has <I>-s, -A</I>, and <I>-t</I>
737     options that can be very useful for debugging and performance
738     measurements.</P>
739
740     <P>You can also compile with profiling to see what functions are
741     taking execution time. The backend profile files will be deposited
742     in the <I>pgsql/data/base/dbname</I> directory. The client profile
743     file will be put in the client's current directory. Linux requires
744     a compile with <I>-DLINUX_PROFILE</I> for proper profiling.</P>
745
746     <H4><A name="3.8">3.8</A>) Why do I get <I>"Sorry, too many
747     clients"</I> when trying to connect?</H4>
748
749     <P>You need to increase <I>postmaster</I>'s limit on how many
750     concurrent backend processes it can start.</P>
751
752     <P>The default limit is 32 processes. You can increase it by
753     restarting <I>postmaster</I> with a suitable <I>-N</I> value or
754     modifying <I>postgresql.conf</I>.</P>
755
756     <P>Note that if you make <I>-N</I> larger than 32, you must also
757     increase <I>-B</I> beyond its default of 64; <I>-B</I> must be at
758     least twice <I>-N</I>, and probably should be more than that for
759     best performance. For large numbers of backend processes, you are
760     also likely to find that you need to increase various Unix kernel
761     configuration parameters. Things to check include the maximum size
762     of shared memory blocks, <SMALL>SHMMAX;</SMALL> the maximum number
763     of semaphores, <SMALL>SEMMNS</SMALL> and <SMALL>SEMMNI;</SMALL> the
764     maximum number of processes, <SMALL>NPROC;</SMALL> the maximum
765     number of processes per user, <SMALL>MAXUPRC;</SMALL> and the
766     maximum number of open files, <SMALL>NFILE</SMALL> and
767     <SMALL>NINODE</SMALL>. The reason that PostgreSQL has a limit on
768     the number of allowed backend processes is so your system won't run
769     out of resources.</P>
770
771     <H4><A name="3.9">3.9</A>) What is in the <I>pgsql_tmp</I> directory?</H4>
772
773     <P>This directory contains temporary files generated by the query 
774     executor. For example, if a sort needs to be done to satisfy an 
775     <SMALL>ORDER BY</SMALL> and the sort requires more space than the
776     backend's <I>-S</I> parameter allows, then temporary files are created
777     here to hold the extra data.</P>
778
779     <P>The temporary files are usually deleted automatically, but might
780     remain if a backend crashes during a sort. A stop and restart of the
781     <I>postmaster</I> will remove files from those directories.</P>
782
783     <H4><A name="3.10">3.10</A>) Why do I need to do a dump and restore
784     to upgrade between major PostgreSQL releases?</H4>
785
786     <P>The PostgreSQL team makes only small changes between minor releases,
787     so upgrading from 7.2 to 7.2.1 does not require a dump and restore.
788     However, major releases (e.g. from 7.2 to 7.3) often change the internal
789     format of system tables and data files. These changes are often complex,
790     so we don't maintain backward compatability for data files. A dump outputs
791     data in a generic format that can then be loaded in using the new internal
792     format.</P>
793
794     <P>In releases where the on-disk format does not change, the
795     <I>pg_upgrade</I> script can be used to upgrade without a dump/restore.
796     The release notes mention whether <I>pg_upgrade</I> is available for the
797     release.</P>
798
799     <HR>
800
801     <H2 align="center">Operational Questions</H2>
802
803     <H4><A name="4.1">4.1</A>) What is the difference between binary
804     cursors and normal cursors?</H4>
805
806     <P>See the <SMALL>DECLARE</SMALL> manual page for a
807     description.</P>
808
809     <H4><A name="4.2">4.2</A>) How do I <SMALL>SELECT</SMALL> only the
810     first few rows of a query?  A random row?</H4>
811
812     <P>See the <SMALL>FETCH</SMALL> manual page, or use
813     <SMALL>SELECT</SMALL> ... <SMALL>LIMIT</SMALL>....</P>
814
815     <P>The entire query may have to be evaluated, even if you only want
816     the first few rows. Consider using a query that has an <SMALL>ORDER
817     BY</SMALL>. If there is an index that matches the <SMALL>ORDER
818     BY</SMALL>, PostgreSQL may be able to evaluate only the first few
819     records requested, or the entire query may have to be evaluated
820     until the desired rows have been generated.</P>
821
822     <P>To <SMALL>SELECT</SMALL> a random row, use:
823 <PRE>
824     SELECT col
825     FROM tab
826     ORDER BY random()
827     LIMIT 1;
828 </PRE>
829
830     <H4><A name="4.3">4.3</A>) How do I get a list of tables or other
831     things I can see in <I>psql</I>?</H4>
832
833     <P>You can read the source code for <I>psql</I> in file
834     <I>pgsql/src/bin/psql/describe.c</I>. It contains
835     <SMALL>SQL</SMALL> commands that generate the output for psql's
836     backslash commands. You can also start <I>psql</I> with the
837     <I>-E</I> option so it will print out the queries it uses to
838     execute the commands you give.</P>
839
840     <H4><A name="4.4">4.4</A>) How do you remove a column from a
841     table, or change its data type?</H4>
842
843     <P><SMALL>DROP COLUMN</SMALL> functionality was added in release 7.3 with
844     <SMALL>ALTER TABLE DROP COLUMN</SMALL>.  In earlier versions,
845     you can do this:</P>
846 <PRE>
847     BEGIN;
848     LOCK TABLE old_table;
849     SELECT ...  -- select all columns but the one you want to remove
850     INTO TABLE new_table
851     FROM old_table;
852     DROP TABLE old_table;
853     ALTER TABLE new_table RENAME TO old_table;
854     COMMIT;
855 </PRE>
856
857     <P>To change the data type of a column, do this:</P>
858 <PRE>
859     BEGIN;
860     ALTER TABLE tab ADD COLUMN new_col <i>new_data_type</i>;
861     UPDATE tab SET new_col = CAST(old_col AS <i>new_data_type</i>);
862     ALTER TABLE tab DROP COLUMN old_col;
863     COMMIT;
864 </PRE>
865     <P>You might then want to do <I>VACUUM FULL tab</I> to reclaim the
866     disk space used by the expired rows.</P>
867     
868     <H4><A name="4.5">4.5</A>) What is the maximum size for a row, a
869     table, and a database?</H4>
870
871     <P>These are the limits:</P>
872 <PRE>
873     Maximum size for a database?             unlimited (4 TB databases exist)
874     Maximum size for a table?                32 TB
875     Maximum size for a row?                  1.6TB
876     Maximum size for a field?                1 GB
877     Maximum number of rows in a table?       unlimited
878     Maximum number of columns in a table?    250-1600 depending on column types
879     Maximum number of indexes on a table?    unlimited
880 </PRE>
881
882     Of course, these are not actually unlimited, but limited to
883     available disk space and memory/swap space. Performance may suffer
884     when these values get unusually large. 
885
886     <P>The maximum table size of 32 TB does not require large file
887     support from the operating system. Large tables are stored as
888     multiple 1 GB files so file system size limits are not
889     important.</P>
890
891     <P>The maximum table size and maximum number of columns can be
892     quadrupled by increasing the default block size to 32k.</P>
893
894     <H4><A name="4.6">4.6</A>) How much database disk space is required
895     to store data from a typical text file?</H4>
896
897     <P>A PostgreSQL database may require up to five times the disk
898     space to store data from a text file.</P>
899
900     <P>As an example, consider a file of 100,000 lines with an integer
901     and text description on each line. Suppose the text string
902     avergages twenty bytes in length. The flat file would be 2.8 MB.
903     The size of the PostgreSQL database file containing this data can
904     be estimated as 6.4 MB:</P>
905 <PRE>
906     36 bytes: each row header (approximate)
907     24 bytes: one int field and one text field
908    + 4 bytes: pointer on page to tuple
909    ----------------------------------------
910     64 bytes per row
911
912    The data page size in PostgreSQL is 8192 bytes (8 KB), so:
913
914    8192 bytes per page
915    -------------------   =  128 rows per database page (rounded down)
916      64 bytes per row
917
918    100000 data rows
919    --------------------  =  782 database pages (rounded up)
920       128 rows per page
921
922 782 database pages * 8192 bytes per page  =  6,406,144 bytes (6.4 MB)
923 </PRE>
924
925     <P>Indexes do not require as much overhead, but do contain the data
926     that is being indexed, so they can be large also.</P>
927
928     <P><SMALL>NULL</SMALL>s are stored in bitmaps, so they
929     use very little space.</P>
930     
931     <H4><A name="4.7">4.7</A>) How do I find out what tables, indexes,
932     databases, and users are defined?</H4>
933
934     <P><I>psql</I> has a variety of backslash commands to show such
935     information. Use \? to see them. There are also system tables
936     beginning with <I>pg_</I> that describe these too. Also, <I>psql
937     -l</I> will list all databases.</P>
938
939     <P>Also try the file <I>pgsql/src/tutorial/syscat.source</I>. It
940     illustrates many of the <SMALL>SELECT</SMALL>s needed to get
941     information from the database system tables.</P>
942
943     <H4><A name="4.8">4.8</A>) My queries are slow or don't make use of
944     the indexes. Why?</H4>
945     Indexes are not automatically used by every query. Indexes are only
946     used if the table is larger than a minimum size, and the query
947     selects only a small percentage of the rows in the table. This is
948     because the random disk access caused by an index scan can be
949     slower than a straight read through the table, or sequential scan. 
950
951     <P>To determine if an index should be used, PostgreSQL must have
952     statistics about the table. These statistics are collected using
953     <SMALL>VACUUM ANALYZE</SMALL>, or simply <SMALL>ANALYZE</SMALL>.
954     Using statistics, the optimizer knows how many rows are in the
955     table, and can better determine if indexes should be used.
956     Statistics are also valuable in determining optimal join order and
957     join methods. Statistics collection should be performed
958     periodically as the contents of the table change.</P>
959
960     <P>Indexes are normally not used for <SMALL>ORDER BY</SMALL> or to
961     perform joins. A sequential scan followed by an explicit sort is
962     usually faster than an index scan of a large table.</P>
963     However, <SMALL>LIMIT</SMALL> combined with <SMALL>ORDER BY</SMALL>
964     often will use an index because only a small portion of the table
965     is returned.  In fact, though MAX() and MIN() don't use indexes,
966     it is possible to retrieve such values using an index with ORDER BY
967     and LIMIT:
968 <PRE>
969     SELECT col
970     FROM tab
971     ORDER BY col [ DESC ]
972     LIMIT 1;
973 </PRE>
974
975     <P>If you believe the optimizer is incorrect in choosing a
976     sequential scan, use <CODE>SET enable_seqscan TO 'off'</CODE> and
977     run tests to see if an index scan is indeed faster.</P>
978
979     <P>When using wild-card operators such as <SMALL>LIKE</SMALL> or
980     <I>~</I>, indexes can only be used in certain circumstances:</P>
981     <UL>
982     <LI>The beginning of the search string must be anchored to the start
983     of the string, i.e.
984     <UL>
985     <LI><SMALL>LIKE</SMALL> patterns must not start with <I>%</I>.</LI>
986     <LI><I>~</I> (regular expression) patterns must start with
987     <I>^</I>.</LI>
988     </UL></LI>
989     <LI>The search string can not start with a character class,
990     e.g. [a-e].</LI>
991     <LI>Case-insensitive searches such as <SMALL>ILIKE</SMALL> and
992     <I>~*</I> do not utilise indexes. Instead, use functional
993     indexes, which are described in section <a href="#4.12">4.12</a>.</LI>
994     <LI>The default <I>C</I> locale must be used during
995     <i>initdb</i>.</LI>
996     </UL>
997     <P>
998
999     <H4><A name="4.9">4.9</A>) How do I see how the query optimizer is
1000     evaluating my query?</H4>
1001
1002     <P>See the <SMALL>EXPLAIN</SMALL> manual page.</P>
1003
1004     <H4><A name="4.10">4.10</A>) What is an R-tree index?</H4>
1005
1006     <P>An R-tree index is used for indexing spatial data. A hash index
1007     can't handle range searches. A B-tree index only handles range
1008     searches in a single dimension. R-trees can handle
1009     multi-dimensional data. For example, if an R-tree index can be
1010     built on an attribute of type <I>point</I>, the system can more
1011     efficiently answer queries such as "select all points within a
1012     bounding rectangle."</P>
1013
1014     <P>The canonical paper that describes the original R-tree design
1015     is:</P>
1016
1017     <P>Guttman, A. "R-trees: A Dynamic Index Structure for Spatial
1018     Searching." Proceedings of the 1984 ACM SIGMOD Int'l Conf on Mgmt
1019     of Data, 45-57.</P>
1020
1021     <P>You can also find this paper in Stonebraker's "Readings in
1022     Database Systems".</P>
1023
1024     <P>Built-in R-trees can handle polygons and boxes. In theory,
1025     R-trees can be extended to handle higher number of dimensions. In
1026     practice, extending R-trees requires a bit of work and we don't
1027     currently have any documentation on how to do it.</P>
1028
1029     <H4><A name="4.11">4.11</A>) What is the Genetic Query
1030     Optimizer?</H4>
1031
1032     <P>The <SMALL>GEQO</SMALL> module speeds query optimization when
1033     joining many tables by means of a Genetic Algorithm (GA). It allows
1034     the handling of large join queries through nonexhaustive
1035     search.</P>
1036
1037     <H4><A name="4.12">4.12</A>) How do I perform regular expression
1038     searches and case-insensitive regular expression searches? How do I
1039     use an index for case-insensitive searches?</H4>
1040
1041     <P>The <I>~</I> operator does regular expression matching, and
1042     <I>~*</I> does case-insensitive regular expression matching. The
1043     case-insensitive variant of <SMALL>LIKE</SMALL> is called
1044     <SMALL>ILIKE</SMALL>.</P>
1045
1046     <P>Case-insensitive equality comparisons are normally expressed
1047     as:</P>
1048 <PRE>
1049     SELECT *
1050     FROM tab
1051     WHERE lower(col) = 'abc';
1052 </PRE>
1053
1054     This will not use an standard index. However, if you create a
1055     functional index, it will be used: 
1056 <PRE>
1057     CREATE INDEX tabindex ON tab (lower(col));
1058 </PRE>
1059
1060     <H4><A name="4.13">4.13</A>) In a query, how do I detect if a field
1061     is <SMALL>NULL</SMALL>?</H4>
1062
1063     <P>You test the column with <SMALL>IS NULL</SMALL> and <SMALL>IS
1064     NOT NULL</SMALL>.</P>
1065
1066     <H4><A name="4.14">4.14</A>) What is the difference between the
1067     various character types?</H4>
1068 <PRE>
1069 Type            Internal Name   Notes
1070 --------------------------------------------------
1071 VARCHAR(n)      varchar         size specifies maximum length, no padding
1072 CHAR(n)         bpchar          blank padded to the specified fixed length
1073 TEXT            text            no specific upper limit on length
1074 BYTEA           bytea           variable-length byte array (null-byte safe)
1075 "char"          char            one character
1076 </PRE>
1077
1078     <P>You will see the internal name when examining system catalogs
1079     and in some error messages.</P>
1080
1081     <P>The first four types above are "varlena" types (i.e., the first
1082     four bytes on disk are the length, followed by the data). Thus the
1083     actual space used is slightly greater than the declared size.
1084     However, these data types are also subject to compression or being
1085     stored out-of-line by <SMALL>TOAST</SMALL>, so the space on disk
1086     might also be less than expected.</P>
1087
1088     <SMALL>VARCHAR(n)</SMALL> is best when storing variable-length
1089     strings and it limits how long a string can be. <SMALL>TEXT</SMALL>
1090     is for strings of unlimited length, with a maximum of one gigabyte.
1091     <P><SMALL>CHAR(n)</SMALL> is for storing strings that are all the
1092     same length. <SMALL>CHAR(n)</SMALL> pads with blanks to the specified
1093     length, while <SMALL>VARCHAR(n)</SMALL> only stores the characters
1094     supplied.  <SMALL>BYTEA</SMALL> is for storing binary data,
1095     particularly values that include <SMALL>NULL</SMALL> bytes. All the
1096     types described here have similar performance characteristics.</P>
1097
1098     <H4><A name="4.15.1">4.15.1</A>) How do I create a
1099     serial/auto-incrementing field?</H4>
1100
1101     <P>PostgreSQL supports a <SMALL>SERIAL</SMALL> data type. It
1102     auto-creates a sequence and index on the column. For example,
1103     this:</P>
1104 <PRE>
1105     CREATE TABLE person ( 
1106         id   SERIAL, 
1107         name TEXT 
1108     );
1109 </PRE>
1110
1111     is automatically translated into this: 
1112 <PRE>
1113     CREATE SEQUENCE person_id_seq;
1114     CREATE TABLE person ( 
1115         id   INT4 NOT NULL DEFAULT nextval('person_id_seq'),
1116         name TEXT 
1117     );
1118     CREATE UNIQUE INDEX person_id_key ON person ( id );
1119 </PRE>
1120
1121     See the <I>create_sequence</I> manual page for more information
1122     about sequences. You can also use each row's <I>OID</I> field as a
1123     unique value. However, if you need to dump and reload the database,
1124     you need to use <I>pg_dump</I>'s <I>-o</I> option or <SMALL>COPY
1125     WITH OIDS</SMALL> option to preserve the <SMALL>OID</SMALL>s. 
1126
1127     <H4><A name="4.15.2">4.15.2</A>) How do I get the value of a
1128     <SMALL>SERIAL</SMALL> insert?</H4>
1129
1130     <P>One approach is to retrieve the next <SMALL>SERIAL</SMALL> value
1131     from the sequence object with the <I>nextval()</I> function
1132     <I>before</I> inserting and then insert it explicitly. Using the
1133     example table in <A href="#4.15.1">4.15.1</A>, an example in a
1134     pseudo-language would look like this:</P>
1135 <PRE>
1136     new_id = execute("SELECT nextval('person_id_seq')");
1137     execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
1138 </PRE>
1139
1140     You would then also have the new value stored in
1141     <CODE>new_id</CODE> for use in other queries (e.g., as a foreign
1142     key to the <CODE>person</CODE> table). Note that the name of the
1143     automatically created <SMALL>SEQUENCE</SMALL> object will be named
1144     &lt;<I>table</I>&gt;_&lt;<I>serialcolumn</I>&gt;_<I>seq</I>, where
1145     <I>table</I> and <I>serialcolumn</I> are the names of your table
1146     and your <SMALL>SERIAL</SMALL> column, respectively.
1147
1148     <P>Alternatively, you could retrieve the assigned
1149     <SMALL>SERIAL</SMALL> value with the <I>currval()</I> function
1150     <I>after</I> it was inserted by default, e.g.,</P>
1151 <PRE>
1152     execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
1153     new_id = execute("SELECT currval('person_id_seq')");
1154 </PRE>
1155
1156     Finally, you could use the <A href="#4.16"><SMALL>OID</SMALL></A>
1157     returned from the <SMALL>INSERT</SMALL> statement to look up the
1158     default value, though this is probably the least portable approach,
1159     and the oid value will wrap around when it reaches 4 billion.
1160     In Perl, using DBI with Edmund Mergl's DBD::Pg module, the oid
1161     value is made available via <I>$sth-&gt;{pg_oid_status}</I> after
1162     <I>$sth-&gt;execute()</I>.
1163
1164     <H4><A name="4.15.3">4.15.3</A>) Don't <I>currval()</I> and
1165     <I>nextval()</I> lead to a race condition with other users?</H4>
1166
1167     <P>No. <I>currval()</I> returns the current value assigned by your
1168     backend, not by all users.</P>
1169
1170     <H4><A name="4.15.4">4.15.4</A>) Why aren't my sequence numbers
1171     reused on transaction abort? Why are there gaps in the numbering of
1172     my sequence/SERIAL column?</H4>
1173
1174     <P>To improve concurrency, sequence values are given out to running
1175     transactions as needed and are not locked until the transaction
1176     completes. This causes gaps in numbering from aborted
1177     transactions.</P>
1178
1179     <H4><A name="4.16">4.16</A>) What is an <SMALL>OID</SMALL>? What is
1180     a <SMALL>TID</SMALL>?</H4>
1181
1182     <P><SMALL>OID</SMALL>s are PostgreSQL's answer to unique row ids.
1183     Every row that is created in PostgreSQL gets a unique
1184     <SMALL>OID</SMALL>. All <SMALL>OID</SMALL>s generated during
1185     <I>initdb</I> are less than 16384 (from
1186     <I>include/access/transam.h</I>). All user-created
1187     <SMALL>OID</SMALL>s are equal to or greater than this. By default,
1188     all these <SMALL>OID</SMALL>s are unique not only within a table or
1189     database, but unique within the entire PostgreSQL installation.</P>
1190
1191     <P>PostgreSQL uses <SMALL>OID</SMALL>s in its internal system
1192     tables to link rows between tables. These <SMALL>OID</SMALL>s can
1193     be used to identify specific user rows and used in joins. It is
1194     recommended you use column type <SMALL>OID</SMALL> to store
1195     <SMALL>OID</SMALL> values. You can create an index on the
1196     <SMALL>OID</SMALL> field for faster access.</P>
1197
1198     <P>O<SMALL>ID</SMALL>s are assigned to all new rows from a central
1199     area that is used by all databases. If you want to change the
1200     <SMALL>OID</SMALL> to something else, or if you want to make a copy
1201     of the table, with the original <SMALL>OID</SMALL>s, there is no
1202     reason you can't do it:</P>
1203 <PRE>
1204         CREATE TABLE new_table(old_oid oid, mycol int);
1205         SELECT old_oid, mycol INTO new FROM old;
1206         COPY new TO '/tmp/pgtable';
1207         DELETE FROM new;
1208         COPY new WITH OIDS FROM '/tmp/pgtable';
1209 </PRE>
1210 <!--
1211     CREATE TABLE new_table (mycol int);
1212     INSERT INTO new_table (oid, mycol) SELECT oid, mycol FROM old_table;
1213 -->
1214     <P>O<SMALL>ID</SMALL>s are stored as 4-byte integers, and will
1215     overflow at 4 billion. No one has reported this ever happening, and
1216     we plan to have the limit removed before anyone does.</P>
1217
1218     <P>T<SMALL>ID</SMALL>s are used to identify specific physical rows
1219     with block and offset values. T<SMALL>ID</SMALL>s change after rows
1220     are modified or reloaded. They are used by index entries to point
1221     to physical rows.</P>
1222
1223     <H4><A name="4.17">4.17</A>) What is the meaning of some of the
1224     terms used in PostgreSQL?</H4>
1225
1226     <P>Some of the source code and older documentation use terms that
1227     have more common usage. Here are some:</P>
1228
1229     <UL>
1230       <LI>table, relation, class</LI>
1231
1232       <LI>row, record, tuple</LI>
1233
1234       <LI>column, field, attribute</LI>
1235
1236       <LI>retrieve, select</LI>
1237
1238       <LI>replace, update</LI>
1239
1240       <LI>append, insert</LI>
1241
1242       <LI><SMALL>OID</SMALL>, serial value</LI>
1243
1244       <LI>portal, cursor</LI>
1245
1246       <LI>range variable, table name, table alias</LI>
1247     </UL>
1248
1249     <P>A list of general database terms can be found at: <A href=
1250     "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>
1251
1252     <H4><A name="4.18">4.18</A>) Why do I get the error <I>"ERROR:
1253     Memory exhausted in AllocSetAlloc()"</I>?</H4>
1254
1255     <P>You probably have run out of virtual memory on your system,
1256     or your kernel has a low limit for certain resources. Try this
1257     before starting <I>postmaster</I>:</P>
1258 <PRE>
1259     ulimit -d 262144
1260     limit datasize 256m
1261 </PRE>
1262
1263     Depending on your shell, only one of these may succeed, but it will
1264     set your process data segment limit much higher and perhaps allow
1265     the query to complete. This command applies to the current process,
1266     and all subprocesses created after the command is run. If you are
1267     having a problem with the <SMALL>SQL</SMALL> client because the
1268     backend is returning too much data, try it before starting the
1269     client.
1270
1271     <H4><A name="4.19">4.19</A>) How do I tell what PostgreSQL version
1272     I am running?</H4>
1273
1274     <P>From <I>psql</I>, type <CODE>SELECT version();</CODE></P>
1275
1276     <H4><A name="4.20">4.20</A>) Why does my large-object operations
1277     get <I>"invalid large obj descriptor"</I>?</H4>
1278
1279     <P>You need to put <CODE>BEGIN WORK</CODE> and <CODE>COMMIT</CODE>
1280     around any use of a large object handle, that is, surrounding
1281     <CODE>lo_open</CODE> ... <CODE>lo_close.</CODE></P>
1282
1283     <P>Currently PostgreSQL enforces the rule by closing large object
1284     handles at transaction commit. So the first attempt to do anything
1285     with the handle will draw <I>invalid large obj descriptor</I>. So
1286     code that used to work (at least most of the time) will now
1287     generate that error message if you fail to use a transaction.</P>
1288
1289     <P>If you are using a client interface like <SMALL>ODBC</SMALL> you
1290     may need to set <CODE>auto-commit off.</CODE></P>
1291
1292     <H4><A name="4.21">4.21</A>) How do I create a column that will
1293     default to the current time?</H4>
1294
1295     <P>Use <I>CURRENT_TIMESTAMP</I>:</P>
1296 <PRE>
1297 <CODE>CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1298 </CODE>
1299 </PRE>
1300
1301     <H4><A name="4.22">4.22</A>) Why are my subqueries using
1302     <CODE><SMALL>IN</SMALL></CODE> so slow?</H4>
1303
1304     <P>Currently, we join subqueries to outer queries by sequentially
1305     scanning the result of the subquery for each row of the outer
1306     query. If the subquery returns only a few rows and the outer query
1307     returns many rows, <CODE><SMALL>IN</SMALL></CODE> is fastest.  To
1308     speed up other queries, replace <CODE>IN</CODE> with
1309     <CODE>EXISTS</CODE>:</P>
1310 <PRE>    SELECT *
1311     FROM tab
1312     WHERE col IN (SELECT subcol FROM subtab);
1313 </PRE>
1314     to:
1315 <PRE>    SELECT *
1316     FROM tab
1317     WHERE EXISTS (SELECT subcol FROM subtab WHERE subcol = col);
1318 </PRE>
1319
1320     For this to be fast, <CODE>subcol</CODE> should be an indexed column.
1321     This preformance problem will be fixed in 7.4.
1322
1323     <H4><A name="4.23">4.23</A>) How do I perform an outer join?</H4>
1324
1325     <P>PostgreSQL supports outer joins using the SQL standard syntax.
1326     Here are two examples:</P>
1327 <PRE>
1328     SELECT *
1329     FROM t1 LEFT OUTER JOIN t2 ON (t1.col = t2.col);
1330 </PRE>
1331     or 
1332 <PRE>
1333     SELECT *
1334     FROM t1 LEFT OUTER JOIN t2 USING (col);
1335 </PRE>
1336
1337     <P>These identical queries join t1.col to t2.col, and also return
1338     any unjoined rows in t1 (those with no match in t2). A
1339     <SMALL>RIGHT</SMALL> join would add unjoined rows of t2. A
1340     <SMALL>FULL</SMALL> join would return the matched rows plus all
1341     unjoined rows from t1 and t2. The word <SMALL>OUTER</SMALL> is
1342     optional and is assumed in <SMALL>LEFT</SMALL>,
1343     <SMALL>RIGHT</SMALL>, and <SMALL>FULL</SMALL> joins. Ordinary joins
1344     are called <SMALL>INNER</SMALL> joins.</P>
1345
1346     <P>In previous releases, outer joins can be simulated using
1347     <SMALL>UNION</SMALL> and <SMALL>NOT IN</SMALL>. For example, when
1348     joining <I>tab1</I> and <I>tab2</I>, the following query does an
1349     <I>outer</I> join of the two tables:<BR>
1350     <BR>
1351     </P>
1352 <PRE>
1353     SELECT tab1.col1, tab2.col2
1354     FROM tab1, tab2
1355     WHERE tab1.col1 = tab2.col1
1356     UNION ALL
1357     SELECT tab1.col1, NULL
1358     FROM tab1
1359     WHERE tab1.col1 NOT IN (SELECT tab2.col1 FROM tab2)
1360     ORDER BY col1
1361 </PRE>
1362
1363     <H4><A name="4.24">4.24</A>) How do I perform queries using
1364     multiple databases?</H4>
1365
1366     <P>There is no way to query a database other than the current one.
1367     Because PostgreSQL loads database-specific system catalogs, it is
1368     uncertain how a cross-database query should even behave.</P>
1369
1370     <P><I>contrib/dblink</I> allows cross-database queries using
1371     function calls. Of course, a client can make simultaneous
1372     connections to different databases and merge the results on the
1373     client side.</P>
1374
1375     <H4><A name="4.25">4.25</A>) How do I return multiple rows or
1376     columns from a function?</H4>
1377
1378     <P>In 7.3, you can easily return multiple rows or columns from a
1379     function,
1380     <a href="http://techdocs.postgresql.org/guides/SetReturningFunctions">
1381     http://techdocs.postgresql.org/guides/SetReturningFunctions</a>.
1382
1383     <H4><A name="4.26">4.26</A>) Why can't I reliably create/drop
1384     temporary tables in PL/PgSQL functions?</H4>
1385     <P>PL/PgSQL caches function contents, and an unfortunate side effect
1386     is that if a PL/PgSQL function accesses a temporary table, and that
1387     table is later dropped and recreated, and the function called
1388     again, the function will fail because the cached function contents
1389     still point to the old temporary table. The solution is to use
1390     <SMALL>EXECUTE</SMALL> for temporary table access in PL/PgSQL. This
1391     will cause the query to be reparsed every time.</P>
1392
1393     <H4><A name="4.27">4.27</A>) What replication options are available?
1394     </H4>
1395     <P>There are several master/slave replication options available.
1396     These allow only the master to make database changes and the slave
1397     can only do database reads. The bottom of <a
1398     href="http://gborg.PostgreSQL.org/genpage?replication_research">
1399     http://gborg.PostgreSQL.org/genpage?replication_research</a> lists
1400     them. A multi-master replication solution is being worked on at <a
1401     href="http://gborg.PostgreSQL.org/project/pgreplication/projdisplay.php">http://gborg.PostgreSQL.org/project/pgreplication/projdisplay.php</a>.</P>
1402
1403     <H4><A name="4.28">4.28</A>) What encryption options are available?
1404     </H4>
1405     <UL>
1406     <LI><I>contrib/pgcrypto</I> contains many encryption functions for
1407     use in <SMALL>SQL</SMALL> queries.</LI>
1408     <LI>To encrypt transmission from the client to the server, the server
1409     must have the <I>ssl</I> option set to <I>true</I> in <I>postgresql.conf,
1410     </I> and an applicable <I>host</I> or <I>hostssl</I> record must exist in
1411     <I>pg_hba.conf</I>, and the client <I>sslmode</I> must not be
1412     <I>disable.</I> (Note that it is also possible to use a third-party
1413     encrypted transport, such as stunnel or ssh, rather than PostgreSQL's
1414     native SSL connections.)
1415     <LI>Database user passwords are automatically encrypted when stored in
1416     version 7.3. In previous versions, you must enable the option
1417     <I>PASSWORD_ENCRYPTION</I> in <I>postgresql.conf</I>.</LI>
1418     <LI>The server can run using an encrypted file system.</LI>
1419     </UL>
1420
1421     <HR>
1422
1423     <H2 align="center">Extending PostgreSQL</H2>
1424
1425     <H4><A name="5.1">5.1</A>) I wrote a user-defined function. When I
1426     run it in <I>psql</I>, why does it dump core?</H4>
1427
1428     <P>The problem could be a number of things. Try testing your
1429     user-defined function in a stand-alone test program first.</P>
1430
1431     <H4><A name="5.2">5.2</A>) How can I contribute some nifty new
1432     types and functions to PostgreSQL?</H4>
1433
1434     <P>Send your extensions to the <I>pgsql-hackers</I> mailing list,
1435     and they will eventually end up in the <I>contrib/</I>
1436     subdirectory.</P>
1437
1438     <H4><A name="5.3">5.3</A>) How do I write a C function to return a
1439     tuple?</H4>
1440
1441     <P>In versions of PostgreSQL beginning with 7.3, table-returning
1442     functions are fully supported in C, PL/PgSQL, and SQL. See the
1443     Programmer's Guide for more information. An example of a
1444     table-returning function defined in C can be found in
1445     <I>contrib/tablefunc</I>.</P>
1446
1447     <H4><A name="5.4">5.4</A>) I have changed a source file. Why does
1448     the recompile not see the change?</H4>
1449
1450     <P>The <I>Makefiles</I> do not have the proper dependencies for
1451     include files. You have to do a <I>make clean</I> and then another
1452     <I>make</I>. If you are using <SMALL>GCC</SMALL> you can use the
1453     <I>--enable-depend</I> option of <I>configure</I> to have the
1454     compiler compute the dependencies automatically.</P>
1455   </BODY>
1456 </HTML>
1457