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