1
1
2
2
Frequently Asked Questions (FAQ) for PostgreSQL
3
3
4
- Last updated: Sat Jan 29 23:06:02 EST 2005
4
+ Last updated: Sat Jan 29 23:15:42 EST 2005
5
5
6
6
Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
7
7
58
58
typical text file?
59
59
4.6) Why are my queries slow? Why don't they use my indexes?
60
60
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?
72
70
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
76
74
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
79
77
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
86
84
functions?
87
- 4.25 ) What encryption options are available?
85
+ 4.23 ) What encryption options are available?
88
86
89
87
Extending PostgreSQL
90
88
@@ -742,36 +740,7 @@ LIKE
742
740
743
741
See the EXPLAIN manual page.
744
742
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
775
744
regular expression searches? How do I use an index for case-insensitive
776
745
searches?
777
746
@@ -788,11 +757,11 @@ LIKE
788
757
functional index, it will be used:
789
758
CREATE INDEX tabindex ON tab (lower(col));
790
759
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?
792
761
793
762
You test the column with IS NULL and IS NOT NULL.
794
763
795
- 4.12 ) What is the difference between the various character types?
764
+ 4.10 ) What is the difference between the various character types?
796
765
797
766
Type Internal Name Notes
798
767
--------------------------------------------------
@@ -820,7 +789,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
820
789
particularly values that include NULL bytes. All the types described
821
790
here have similar performance characteristics.
822
791
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?
824
793
825
794
PostgreSQL supports a SERIAL data type. It auto-creates a sequence.
826
795
For example, this:
@@ -841,11 +810,11 @@ BYTEA bytea variable-length byte array (null-byte safe)
841
810
However, if you need to dump and reload the database, you need to use
842
811
pg_dump's -o option or COPY WITH OIDS option to preserve the OIDs.
843
812
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?
845
814
846
815
One approach is to retrieve the next SERIAL value from the sequence
847
816
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
849
818
pseudo-language would look like this:
850
819
new_id = execute("SELECT nextval('person_id_seq')");
851
820
execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
@@ -867,19 +836,19 @@ BYTEA bytea variable-length byte array (null-byte safe)
867
836
billion. In Perl, using DBI with the DBD::Pg module, the oid value is
868
837
made available via $sth->{pg_oid_status} after $sth->execute().
869
838
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?
871
840
872
841
No. currval() returns the current value assigned by your backend, not
873
842
by all users.
874
843
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
876
845
there gaps in the numbering of my sequence/SERIAL column?
877
846
878
847
To improve concurrency, sequence values are given out to running
879
848
transactions as needed and are not locked until the transaction
880
849
completes. This causes gaps in numbering from aborted transactions.
881
850
882
- 4.14 ) What is an OID? What is a TID?
851
+ 4.12 ) What is an OID? What is a TID?
883
852
884
853
Every row that is created in PostgreSQL gets a unique OID unless
885
854
created WITHOUT OIDS. OIDs are autotomatically assigned unique 4-byte
@@ -896,7 +865,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
896
865
values. TIDs change after rows are modified or reloaded. They are used
897
866
by index entries to point to physical rows.
898
867
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?
900
869
901
870
Some of the source code and older documentation use terms that have
902
871
more common usage. Here are some:
@@ -914,7 +883,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
914
883
http://hea-www.harvard.edu/MST/simul/software/docs/pkgs/pgsql/glossary
915
884
/glossary.html
916
885
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()"?
918
887
919
888
You probably have run out of virtual memory on your system, or your
920
889
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)
929
898
problem with the SQL client because the backend is returning too much
930
899
data, try it before starting the client.
931
900
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?
933
902
934
903
From psql, type SELECT version();
935
904
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
937
906
descriptor"?
938
907
939
908
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)
948
917
If you are using a client interface like ODBC you may need to set
949
918
auto-commit off.
950
919
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?
952
921
953
922
Use CURRENT_TIMESTAMP:
954
923
CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
955
924
956
- 4.20 ) Why are my subqueries using IN so slow?
925
+ 4.18 ) Why are my subqueries using IN so slow?
957
926
958
927
In versions prior to 7.4, subqueries were joined to outer queries by
959
928
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 );
974
943
In version 7.4 and later, IN actually uses the same sophisticated join
975
944
techniques as normal queries, and is prefered to using EXISTS.
976
945
977
- 4.21 ) How do I perform an outer join?
946
+ 4.19 ) How do I perform an outer join?
978
947
979
948
PostgreSQL supports outer joins using the SQL standard syntax. Here
980
949
are two examples:
@@ -1004,7 +973,7 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1004
973
WHERE tab1.col1 NOT IN (SELECT tab2.col1 FROM tab2)
1005
974
ORDER BY col1
1006
975
1007
- 4.22 ) How do I perform queries using multiple databases?
976
+ 4.20 ) How do I perform queries using multiple databases?
1008
977
1009
978
There is no way to query a database other than the current one.
1010
979
Because PostgreSQL loads database-specific system catalogs, it is
@@ -1014,12 +983,12 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1014
983
course, a client can make simultaneous connections to different
1015
984
databases and merge the results on the client side.
1016
985
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?
1018
987
1019
988
In 7.3, you can easily return multiple rows or columns from a
1020
989
function, http://techdocs.postgresql.org/guides/SetReturningFunctions.
1021
990
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
1023
992
functions?
1024
993
1025
994
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 );
1030
999
table access in PL/PgSQL. This will cause the query to be reparsed
1031
1000
every time.
1032
1001
1033
- 4.25 ) What encryption options are available?
1002
+ 4.23 ) What encryption options are available?
1034
1003
1035
1004
* contrib/pgcrypto contains many encryption functions for use in SQL
1036
1005
queries.
0 commit comments