11
22 Frequently Asked Questions (FAQ) for PostgreSQL
33
4- Last updated: Sat Jan 29 23:06:02 EST 2005
4+ Last updated: Sat Jan 29 23:15:42 EST 2005
55
66 Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
77
5858 typical text file?
5959 4.6) Why are my queries slow? Why don't they use my indexes?
6060 4.7) How do I see how the query optimizer is evaluating my query?
61- 4.8) What is an R-tree index?
62- 4.9) What is the Genetic Query Optimizer?
63- 4.10) How do I perform regular expression searches and
64- case-insensitive regular expression searches? How do I use an index
65- for case-insensitive searches?
66- 4.11) In a query, how do I detect if a field is NULL?
67- 4.12) What is the difference between the various character types?
68- 4.13.0) How do I create a serial/auto-incrementing field?
69- 4.13.1) How do I get the value of a SERIAL insert?
70- 4.13.2) Doesn't currval() lead to a race condition with other users?
71- 4.13.3) Why aren't my sequence numbers reused on transaction abort?
61+ 4.8) How do I perform regular expression searches and case-insensitive
62+ regular expression searches? How do I use an index for
63+ case-insensitive searches?
64+ 4.9) In a query, how do I detect if a field is NULL?
65+ 4.10) What is the difference between the various character types?
66+ 4.11.0) How do I create a serial/auto-incrementing field?
67+ 4.11.1) How do I get the value of a SERIAL insert?
68+ 4.11.2) Doesn't currval() lead to a race condition with other users?
69+ 4.11.3) Why aren't my sequence numbers reused on transaction abort?
7270 Why are there gaps in the numbering of my sequence/SERIAL column?
73- 4.14 ) What is an OID? What is a TID?
74- 4.15 ) What is the meaning of some of the terms used in PostgreSQL?
75- 4.16 ) Why do I get the error "ERROR: Memory exhausted in
71+ 4.12 ) What is an OID? What is a TID?
72+ 4.13 ) What is the meaning of some of the terms used in PostgreSQL?
73+ 4.14 ) Why do I get the error "ERROR: Memory exhausted in
7674 AllocSetAlloc()"?
77- 4.17 ) How do I tell what PostgreSQL version I am running?
78- 4.18 ) Why does my large-object operations get "invalid large obj
75+ 4.15 ) How do I tell what PostgreSQL version I am running?
76+ 4.16 ) Why does my large-object operations get "invalid large obj
7977 descriptor"?
80- 4.19 ) How do I create a column that will default to the current time?
81- 4.20 ) Why are my subqueries using IN so slow?
82- 4.21 ) How do I perform an outer join?
83- 4.22 ) How do I perform queries using multiple databases?
84- 4.23 ) How do I return multiple rows or columns from a function?
85- 4.24 ) Why can't I reliably create/drop temporary tables in PL/PgSQL
78+ 4.17 ) How do I create a column that will default to the current time?
79+ 4.18 ) Why are my subqueries using IN so slow?
80+ 4.19 ) How do I perform an outer join?
81+ 4.20 ) How do I perform queries using multiple databases?
82+ 4.21 ) How do I return multiple rows or columns from a function?
83+ 4.22 ) Why can't I reliably create/drop temporary tables in PL/PgSQL
8684 functions?
87- 4.25 ) What encryption options are available?
85+ 4.23 ) What encryption options are available?
8886
8987 Extending PostgreSQL
9088
@@ -742,36 +740,7 @@ LIKE
742740
743741 See the EXPLAIN manual page.
744742
745- 4.8) What is an R-tree index?
746-
747- An R-tree index is used for indexing spatial data. A hash index can't
748- handle range searches. A B-tree index only handles range searches in a
749- single dimension. R-trees can handle multi-dimensional data. For
750- example, if an R-tree index can be built on an attribute of type
751- point, the system can more efficiently answer queries such as "select
752- all points within a bounding rectangle."
753-
754- The canonical paper that describes the original R-tree design is:
755-
756- Guttman, A. "R-trees: A Dynamic Index Structure for Spatial
757- Searching." Proceedings of the 1984 ACM SIGMOD Int'l Conf on Mgmt of
758- Data, 45-57.
759-
760- You can also find this paper in Stonebraker's "Readings in Database
761- Systems".
762-
763- Built-in R-trees can handle polygons and boxes. In theory, R-trees can
764- be extended to handle higher number of dimensions. In practice,
765- extending R-trees requires a bit of work and we don't currently have
766- any documentation on how to do it.
767-
768- 4.9) What is the Genetic Query Optimizer?
769-
770- The GEQO module speeds query optimization when joining many tables by
771- means of a Genetic Algorithm (GA). It allows the handling of large
772- join queries through nonexhaustive search.
773-
774- 4.10) How do I perform regular expression searches and case-insensitive
743+ 4.8) How do I perform regular expression searches and case-insensitive
775744 regular expression searches? How do I use an index for case-insensitive
776745 searches?
777746
@@ -788,11 +757,11 @@ LIKE
788757 functional index, it will be used:
789758 CREATE INDEX tabindex ON tab (lower(col));
790759
791- 4.11 ) In a query, how do I detect if a field is NULL?
760+ 4.9 ) In a query, how do I detect if a field is NULL?
792761
793762 You test the column with IS NULL and IS NOT NULL.
794763
795- 4.12 ) What is the difference between the various character types?
764+ 4.10 ) What is the difference between the various character types?
796765
797766Type Internal Name Notes
798767--------------------------------------------------
@@ -820,7 +789,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
820789 particularly values that include NULL bytes. All the types described
821790 here have similar performance characteristics.
822791
823- 4.13 .1) How do I create a serial/auto-incrementing field?
792+ 4.11 .1) How do I create a serial/auto-incrementing field?
824793
825794 PostgreSQL supports a SERIAL data type. It auto-creates a sequence.
826795 For example, this:
@@ -841,11 +810,11 @@ BYTEA bytea variable-length byte array (null-byte safe)
841810 However, if you need to dump and reload the database, you need to use
842811 pg_dump's -o option or COPY WITH OIDS option to preserve the OIDs.
843812
844- 4.13 .2) How do I get the value of a SERIAL insert?
813+ 4.11 .2) How do I get the value of a SERIAL insert?
845814
846815 One approach is to retrieve the next SERIAL value from the sequence
847816 object with the nextval() function before inserting and then insert it
848- explicitly. Using the example table in 4.13 .1, an example in a
817+ explicitly. Using the example table in 4.11 .1, an example in a
849818 pseudo-language would look like this:
850819 new_id = execute("SELECT nextval('person_id_seq')");
851820 execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
@@ -867,19 +836,19 @@ BYTEA bytea variable-length byte array (null-byte safe)
867836 billion. In Perl, using DBI with the DBD::Pg module, the oid value is
868837 made available via $sth->{pg_oid_status} after $sth->execute().
869838
870- 4.13 .3) Doesn't currval() lead to a race condition with other users?
839+ 4.11 .3) Doesn't currval() lead to a race condition with other users?
871840
872841 No. currval() returns the current value assigned by your backend, not
873842 by all users.
874843
875- 4.13 .4) Why aren't my sequence numbers reused on transaction abort? Why are
844+ 4.11 .4) Why aren't my sequence numbers reused on transaction abort? Why are
876845 there gaps in the numbering of my sequence/SERIAL column?
877846
878847 To improve concurrency, sequence values are given out to running
879848 transactions as needed and are not locked until the transaction
880849 completes. This causes gaps in numbering from aborted transactions.
881850
882- 4.14 ) What is an OID? What is a TID?
851+ 4.12 ) What is an OID? What is a TID?
883852
884853 Every row that is created in PostgreSQL gets a unique OID unless
885854 created WITHOUT OIDS. OIDs are autotomatically assigned unique 4-byte
@@ -896,7 +865,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
896865 values. TIDs change after rows are modified or reloaded. They are used
897866 by index entries to point to physical rows.
898867
899- 4.15 ) What is the meaning of some of the terms used in PostgreSQL?
868+ 4.13 ) What is the meaning of some of the terms used in PostgreSQL?
900869
901870 Some of the source code and older documentation use terms that have
902871 more common usage. Here are some:
@@ -914,7 +883,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
914883 http://hea-www.harvard.edu/MST/simul/software/docs/pkgs/pgsql/glossary
915884 /glossary.html
916885
917- 4.16 ) Why do I get the error "ERROR: Memory exhausted in AllocSetAlloc()"?
886+ 4.14 ) Why do I get the error "ERROR: Memory exhausted in AllocSetAlloc()"?
918887
919888 You probably have run out of virtual memory on your system, or your
920889 kernel has a low limit for certain resources. Try this before starting
@@ -929,11 +898,11 @@ BYTEA bytea variable-length byte array (null-byte safe)
929898 problem with the SQL client because the backend is returning too much
930899 data, try it before starting the client.
931900
932- 4.17 ) How do I tell what PostgreSQL version I am running?
901+ 4.15 ) How do I tell what PostgreSQL version I am running?
933902
934903 From psql, type SELECT version();
935904
936- 4.18 ) Why does my large-object operations get "invalid large obj
905+ 4.16 ) Why does my large-object operations get "invalid large obj
937906 descriptor"?
938907
939908 You need to put BEGIN WORK and COMMIT around any use of a large object
@@ -948,12 +917,12 @@ BYTEA bytea variable-length byte array (null-byte safe)
948917 If you are using a client interface like ODBC you may need to set
949918 auto-commit off.
950919
951- 4.19 ) How do I create a column that will default to the current time?
920+ 4.17 ) How do I create a column that will default to the current time?
952921
953922 Use CURRENT_TIMESTAMP:
954923CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
955924
956- 4.20 ) Why are my subqueries using IN so slow?
925+ 4.18 ) Why are my subqueries using IN so slow?
957926
958927 In versions prior to 7.4, subqueries were joined to outer queries by
959928 sequentially scanning the result of the subquery for each row of the
@@ -974,7 +943,7 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
974943 In version 7.4 and later, IN actually uses the same sophisticated join
975944 techniques as normal queries, and is prefered to using EXISTS.
976945
977- 4.21 ) How do I perform an outer join?
946+ 4.19 ) How do I perform an outer join?
978947
979948 PostgreSQL supports outer joins using the SQL standard syntax. Here
980949 are two examples:
@@ -1004,7 +973,7 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1004973 WHERE tab1.col1 NOT IN (SELECT tab2.col1 FROM tab2)
1005974 ORDER BY col1
1006975
1007- 4.22 ) How do I perform queries using multiple databases?
976+ 4.20 ) How do I perform queries using multiple databases?
1008977
1009978 There is no way to query a database other than the current one.
1010979 Because PostgreSQL loads database-specific system catalogs, it is
@@ -1014,12 +983,12 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1014983 course, a client can make simultaneous connections to different
1015984 databases and merge the results on the client side.
1016985
1017- 4.23 ) How do I return multiple rows or columns from a function?
986+ 4.21 ) How do I return multiple rows or columns from a function?
1018987
1019988 In 7.3, you can easily return multiple rows or columns from a
1020989 function, http://techdocs.postgresql.org/guides/SetReturningFunctions.
1021990
1022- 4.24 ) Why can't I reliably create/drop temporary tables in PL/PgSQL
991+ 4.22 ) Why can't I reliably create/drop temporary tables in PL/PgSQL
1023992 functions?
1024993
1025994 PL/PgSQL caches function contents, and an unfortunate side effect is
@@ -1030,7 +999,7 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1030999 table access in PL/PgSQL. This will cause the query to be reparsed
10311000 every time.
10321001
1033- 4.25 ) What encryption options are available?
1002+ 4.23 ) What encryption options are available?
10341003
10351004 * contrib/pgcrypto contains many encryption functions for use in SQL
10361005 queries.