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