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