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