1
1
2
2
Frequently Asked Questions (FAQ) for PostgreSQL
3
3
4
- Last updated: Sun Jan 9 14:44:04 EST 2005
4
+ Last updated: Sat Jan 15 00:18:38 EST 2005
5
5
6
6
Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
7
7
60
60
61
61
4.1) What is the difference between binary cursors and normal cursors?
62
62
4.2) How do I SELECT only the first few rows of a query? A random row?
63
- 4.3) How do I get a list of tables or other things I can see in psql?
63
+ 4.3) How do I find out what tables, indexes, databases, and users are
64
+ defined? How do I see the queries used by psql to display them?
64
65
4.4) How do you remove a column from a table, or change it's data
65
66
type?
66
67
4.5) What is the maximum size for a row, a table, and a database?
67
68
4.6) How much database disk space is required to store data from a
68
69
typical text file?
69
- 4.7) How do I find out what tables, indexes, databases, and users are
70
- defined?
71
- 4.8) My queries are slow or don't make use of the indexes. Why?
72
- 4.9) How do I see how the query optimizer is evaluating my query?
73
- 4.10) What is an R-tree index?
74
- 4.11) What is the Genetic Query Optimizer?
75
- 4.12) How do I perform regular expression searches and
70
+ 4.7) My queries are slow or don't make use of the indexes. Why?
71
+ 4.8) How do I see how the query optimizer is evaluating my query?
72
+ 4.9) What is an R-tree index?
73
+ 4.10) What is the Genetic Query Optimizer?
74
+ 4.11) How do I perform regular expression searches and
76
75
case-insensitive regular expression searches? How do I use an index
77
76
for case-insensitive searches?
78
- 4.13 ) In a query, how do I detect if a field is NULL?
79
- 4.14 ) What is the difference between the various character types?
80
- 4.15.1 ) How do I create a serial/auto-incrementing field?
81
- 4.15.2 ) How do I get the value of a SERIAL insert?
82
- 4.15.3 ) Doesn't currval() lead to a race condition with other users?
83
- 4.15.4 ) Why aren't my sequence numbers reused on transaction abort?
77
+ 4.12 ) In a query, how do I detect if a field is NULL?
78
+ 4.13 ) What is the difference between the various character types?
79
+ 4.14.0 ) How do I create a serial/auto-incrementing field?
80
+ 4.14.1 ) How do I get the value of a SERIAL insert?
81
+ 4.14.2 ) Doesn't currval() lead to a race condition with other users?
82
+ 4.14.3 ) Why aren't my sequence numbers reused on transaction abort?
84
83
Why are there gaps in the numbering of my sequence/SERIAL column?
85
- 4.16 ) What is an OID? What is a TID?
86
- 4.17 ) What is the meaning of some of the terms used in PostgreSQL?
87
- 4.18 ) Why do I get the error "ERROR: Memory exhausted in
84
+ 4.15 ) What is an OID? What is a TID?
85
+ 4.16 ) What is the meaning of some of the terms used in PostgreSQL?
86
+ 4.17 ) Why do I get the error "ERROR: Memory exhausted in
88
87
AllocSetAlloc()"?
89
- 4.19 ) How do I tell what PostgreSQL version I am running?
90
- 4.20 ) Why does my large-object operations get "invalid large obj
88
+ 4.18 ) How do I tell what PostgreSQL version I am running?
89
+ 4.19 ) Why does my large-object operations get "invalid large obj
91
90
descriptor"?
92
- 4.21 ) How do I create a column that will default to the current time?
93
- 4.22 ) Why are my subqueries using IN so slow?
94
- 4.23 ) How do I perform an outer join?
95
- 4.24 ) How do I perform queries using multiple databases?
96
- 4.25 ) How do I return multiple rows or columns from a function?
97
- 4.26 ) Why can't I reliably create/drop temporary tables in PL/PgSQL
91
+ 4.20 ) How do I create a column that will default to the current time?
92
+ 4.21 ) Why are my subqueries using IN so slow?
93
+ 4.22 ) How do I perform an outer join?
94
+ 4.23 ) How do I perform queries using multiple databases?
95
+ 4.24 ) How do I return multiple rows or columns from a function?
96
+ 4.25 ) Why can't I reliably create/drop temporary tables in PL/PgSQL
98
97
functions?
99
- 4.27 ) What encryption options are available?
98
+ 4.26 ) What encryption options are available?
100
99
101
100
Extending PostgreSQL
102
101
@@ -659,17 +658,25 @@ log_*
659
658
ORDER BY random()
660
659
LIMIT 1;
661
660
662
- 4.3) How do I get a list of tables or other things I can see in psql?
661
+ 4.3) How do I find out what tables, indexes, databases, and users are
662
+ defined? How do I see the queries used by psql to display them?
663
663
664
664
Use the \dt command to see tables in psql. For a complete list of
665
665
commands inside psql you can use \?. Alternatively you can read the
666
666
source code for psql in file pgsql/src/bin/psql/describe.c, it
667
667
contains SQL commands that generate the output for psql's backslash
668
668
commands. You can also start psql with the -E option so it will print
669
669
out the queries it uses to execute the commands you give. PostgreSQL
670
- also provides an SQLi compliant INFORMATION SCHEMA interface you can
670
+ also provides an SQL compliant INFORMATION SCHEMA interface you can
671
671
query to get information about the database.
672
672
673
+ There are also system tables beginning with pg_ that describe these
674
+ too. Use psql -l will list all databases.
675
+
676
+ Also try the file pgsql/src/tutorial/syscat.source. It illustrates
677
+ many of the SELECTs needed to get information from the database system
678
+ tables.
679
+
673
680
4.4) How do you remove a column from a table, or change its data type?
674
681
675
682
DROP COLUMN functionality was added in release 7.3 with ALTER TABLE
@@ -749,18 +756,7 @@ log_*
749
756
750
757
NULLs are stored as bitmaps, so they use very little space.
751
758
752
- 4.7) How do I find out what tables, indexes, databases, and users are
753
- defined?
754
-
755
- psql has a variety of backslash commands to show such information. Use
756
- \? to see them. There are also system tables beginning with pg_ that
757
- describe these too. Also, psql -l will list all databases.
758
-
759
- Also try the file pgsql/src/tutorial/syscat.source. It illustrates
760
- many of the SELECTs needed to get information from the database system
761
- tables.
762
-
763
- 4.8) My queries are slow or don't make use of the indexes. Why?
759
+ 4.7) My queries are slow or don't make use of the indexes. Why?
764
760
765
761
Indexes are not automatically used by every query. Indexes are only
766
762
used if the table is larger than a minimum size, and the query selects
@@ -802,7 +798,7 @@ log_*
802
798
[a-e].
803
799
* Case-insensitive searches such as ILIKE and ~* do not utilise
804
800
indexes. Instead, use functional indexes, which are described in
805
- section 4.12 .
801
+ section 4.11 .
806
802
* The default C locale must be used during initdb because it is not
807
803
possible to know the next-greater character in a non-C locale. You
808
804
can create a special
@@ -815,11 +811,11 @@ LIKE
815
811
types exactly match the index's column types. This is particularly
816
812
true of int2, int8, and numeric column indexes.
817
813
818
- 4.9 ) How do I see how the query optimizer is evaluating my query?
814
+ 4.8 ) How do I see how the query optimizer is evaluating my query?
819
815
820
816
See the EXPLAIN manual page.
821
817
822
- 4.10 ) What is an R-tree index?
818
+ 4.9 ) What is an R-tree index?
823
819
824
820
An R-tree index is used for indexing spatial data. A hash index can't
825
821
handle range searches. A B-tree index only handles range searches in a
@@ -842,13 +838,13 @@ LIKE
842
838
extending R-trees requires a bit of work and we don't currently have
843
839
any documentation on how to do it.
844
840
845
- 4.11 ) What is the Genetic Query Optimizer?
841
+ 4.10 ) What is the Genetic Query Optimizer?
846
842
847
843
The GEQO module speeds query optimization when joining many tables by
848
844
means of a Genetic Algorithm (GA). It allows the handling of large
849
845
join queries through nonexhaustive search.
850
846
851
- 4.12 ) How do I perform regular expression searches and case-insensitive
847
+ 4.11 ) How do I perform regular expression searches and case-insensitive
852
848
regular expression searches? How do I use an index for case-insensitive
853
849
searches?
854
850
@@ -865,11 +861,11 @@ LIKE
865
861
functional index, it will be used:
866
862
CREATE INDEX tabindex ON tab (lower(col));
867
863
868
- 4.13 ) In a query, how do I detect if a field is NULL?
864
+ 4.12 ) In a query, how do I detect if a field is NULL?
869
865
870
866
You test the column with IS NULL and IS NOT NULL.
871
867
872
- 4.14 ) What is the difference between the various character types?
868
+ 4.13 ) What is the difference between the various character types?
873
869
874
870
Type Internal Name Notes
875
871
--------------------------------------------------
@@ -897,7 +893,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
897
893
particularly values that include NULL bytes. All the types described
898
894
here have similar performance characteristics.
899
895
900
- 4.15 .1) How do I create a serial/auto-incrementing field?
896
+ 4.14 .1) How do I create a serial/auto-incrementing field?
901
897
902
898
PostgreSQL supports a SERIAL data type. It auto-creates a sequence.
903
899
For example, this:
@@ -918,11 +914,11 @@ BYTEA bytea variable-length byte array (null-byte safe)
918
914
However, if you need to dump and reload the database, you need to use
919
915
pg_dump's -o option or COPY WITH OIDS option to preserve the OIDs.
920
916
921
- 4.15 .2) How do I get the value of a SERIAL insert?
917
+ 4.14 .2) How do I get the value of a SERIAL insert?
922
918
923
919
One approach is to retrieve the next SERIAL value from the sequence
924
920
object with the nextval() function before inserting and then insert it
925
- explicitly. Using the example table in 4.15 .1, an example in a
921
+ explicitly. Using the example table in 4.14 .1, an example in a
926
922
pseudo-language would look like this:
927
923
new_id = execute("SELECT nextval('person_id_seq')");
928
924
execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
@@ -944,19 +940,19 @@ BYTEA bytea variable-length byte array (null-byte safe)
944
940
billion. In Perl, using DBI with the DBD::Pg module, the oid value is
945
941
made available via $sth->{pg_oid_status} after $sth->execute().
946
942
947
- 4.15 .3) Doesn't currval() lead to a race condition with other users?
943
+ 4.14 .3) Doesn't currval() lead to a race condition with other users?
948
944
949
945
No. currval() returns the current value assigned by your backend, not
950
946
by all users.
951
947
952
- 4.15 .4) Why aren't my sequence numbers reused on transaction abort? Why are
948
+ 4.14 .4) Why aren't my sequence numbers reused on transaction abort? Why are
953
949
there gaps in the numbering of my sequence/SERIAL column?
954
950
955
951
To improve concurrency, sequence values are given out to running
956
952
transactions as needed and are not locked until the transaction
957
953
completes. This causes gaps in numbering from aborted transactions.
958
954
959
- 4.16 ) What is an OID? What is a TID?
955
+ 4.15 ) What is an OID? What is a TID?
960
956
961
957
OIDs are PostgreSQL's answer to unique row ids. Every row that is
962
958
created in PostgreSQL gets a unique OID. All OIDs generated during
@@ -989,7 +985,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
989
985
values. TIDs change after rows are modified or reloaded. They are used
990
986
by index entries to point to physical rows.
991
987
992
- 4.17 ) What is the meaning of some of the terms used in PostgreSQL?
988
+ 4.16 ) What is the meaning of some of the terms used in PostgreSQL?
993
989
994
990
Some of the source code and older documentation use terms that have
995
991
more common usage. Here are some:
@@ -1007,7 +1003,7 @@ BYTEA bytea variable-length byte array (null-byte safe)
1007
1003
http://hea-www.harvard.edu/MST/simul/software/docs/pkgs/pgsql/glossary
1008
1004
/glossary.html
1009
1005
1010
- 4.18 ) Why do I get the error "ERROR: Memory exhausted in AllocSetAlloc()"?
1006
+ 4.17 ) Why do I get the error "ERROR: Memory exhausted in AllocSetAlloc()"?
1011
1007
1012
1008
You probably have run out of virtual memory on your system, or your
1013
1009
kernel has a low limit for certain resources. Try this before starting
@@ -1022,11 +1018,11 @@ BYTEA bytea variable-length byte array (null-byte safe)
1022
1018
problem with the SQL client because the backend is returning too much
1023
1019
data, try it before starting the client.
1024
1020
1025
- 4.19 ) How do I tell what PostgreSQL version I am running?
1021
+ 4.18 ) How do I tell what PostgreSQL version I am running?
1026
1022
1027
1023
From psql, type SELECT version();
1028
1024
1029
- 4.20 ) Why does my large-object operations get "invalid large obj
1025
+ 4.19 ) Why does my large-object operations get "invalid large obj
1030
1026
descriptor"?
1031
1027
1032
1028
You need to put BEGIN WORK and COMMIT around any use of a large object
@@ -1041,12 +1037,12 @@ BYTEA bytea variable-length byte array (null-byte safe)
1041
1037
If you are using a client interface like ODBC you may need to set
1042
1038
auto-commit off.
1043
1039
1044
- 4.21 ) How do I create a column that will default to the current time?
1040
+ 4.20 ) How do I create a column that will default to the current time?
1045
1041
1046
1042
Use CURRENT_TIMESTAMP:
1047
1043
CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1048
1044
1049
- 4.22 ) Why are my subqueries using IN so slow?
1045
+ 4.21 ) Why are my subqueries using IN so slow?
1050
1046
1051
1047
In versions prior to 7.4, subqueries were joined to outer queries by
1052
1048
sequentially scanning the result of the subquery for each row of the
@@ -1067,7 +1063,7 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1067
1063
In version 7.4 and later, IN actually uses the same sophisticated join
1068
1064
techniques as normal queries, and is prefered to using EXISTS.
1069
1065
1070
- 4.23 ) How do I perform an outer join?
1066
+ 4.22 ) How do I perform an outer join?
1071
1067
1072
1068
PostgreSQL supports outer joins using the SQL standard syntax. Here
1073
1069
are two examples:
@@ -1097,7 +1093,7 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1097
1093
WHERE tab1.col1 NOT IN (SELECT tab2.col1 FROM tab2)
1098
1094
ORDER BY col1
1099
1095
1100
- 4.24 ) How do I perform queries using multiple databases?
1096
+ 4.23 ) How do I perform queries using multiple databases?
1101
1097
1102
1098
There is no way to query a database other than the current one.
1103
1099
Because PostgreSQL loads database-specific system catalogs, it is
@@ -1107,12 +1103,12 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1107
1103
course, a client can make simultaneous connections to different
1108
1104
databases and merge the results on the client side.
1109
1105
1110
- 4.25 ) How do I return multiple rows or columns from a function?
1106
+ 4.24 ) How do I return multiple rows or columns from a function?
1111
1107
1112
1108
In 7.3, you can easily return multiple rows or columns from a
1113
1109
function, http://techdocs.postgresql.org/guides/SetReturningFunctions.
1114
1110
1115
- 4.26 ) Why can't I reliably create/drop temporary tables in PL/PgSQL
1111
+ 4.25 ) Why can't I reliably create/drop temporary tables in PL/PgSQL
1116
1112
functions?
1117
1113
1118
1114
PL/PgSQL caches function contents, and an unfortunate side effect is
@@ -1123,7 +1119,7 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
1123
1119
table access in PL/PgSQL. This will cause the query to be reparsed
1124
1120
every time.
1125
1121
1126
- 4.27 ) What encryption options are available?
1122
+ 4.26 ) What encryption options are available?
1127
1123
1128
1124
* contrib/pgcrypto contains many encryption functions for use in SQL
1129
1125
queries.
0 commit comments