1
1
2
2
Frequently Asked Questions (FAQ) for PostgreSQL
3
3
4
- Last updated:Wed Dec 5 00:41:12 EST2001
4
+ Last updated:Fri Jan 4 00:44:42 EST2002
5
5
6
6
Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
7
7
58
58
59
59
Operational Questions
60
60
61
- 4.1) Why is the system confused about commas, decimal points, and date
62
- formats.
63
- 4.2) What is the exact difference between binary cursors and normal
64
- cursors?
65
- 4.3) How do I SELECT only the first few rows of a query?
66
- 4.4) How do I get a list of tables or other things I can see in psql?
67
- 4.5) How do you remove a column from a table?
68
- 4.6) What is the maximum size for a row, table, database?
69
- 4.7) How much database disk space is required to store data from a
61
+ 4.1) What is the difference between binary cursors and normal cursors?
62
+ 4.2) How do I SELECT only the first few rows of a query?
63
+ 4.3) How do I get a list of tables or other things I can see in psql?
64
+ 4.4) How do you remove a column from a table?
65
+ 4.5) What is the maximum size for a row, table, database?
66
+ 4.6) How much database disk space is required to store data from a
70
67
typical text file?
71
- 4.8 ) How do I find out what tables or indexes are defined in the
68
+ 4.7 ) How do I find out what tables or indexes are defined in the
72
69
database?
73
- 4.9 ) My queries are slow or don't make use of the indexes. Why?
74
- 4.10 ) How do I see how the query optimizer is evaluating my query?
75
- 4.11 ) What is an R-tree index?
76
- 4.12 ) What is the Genetic Query Optimizer?
77
- 4.13 ) How do I perform regular expression searches and
70
+ 4.8 ) My queries are slow or don't make use of the indexes. Why?
71
+ 4.9 ) How do I see how the query optimizer is evaluating my query?
72
+ 4.10 ) What is an R-tree index?
73
+ 4.11 ) What is the Genetic Query Optimizer?
74
+ 4.12 ) How do I perform regular expression searches and
78
75
case-insensitive regular expression searches? How do I use an index
79
76
for case-insensitive searches?
80
- 4.14 ) In a query, how do I detect if a field is NULL?
81
- 4.15 ) What is the difference between the various character types?
82
- 4.16 .1) How do I create a serial/auto-incrementing field?
83
- 4.16 .2) How do I get the value of a SERIAL insert?
84
- 4.16 .3) Don't currval() and nextval() lead to a race condition with
77
+ 4.13 ) In a query, how do I detect if a field is NULL?
78
+ 4.14 ) What is the difference between the various character types?
79
+ 4.15 .1) How do I create a serial/auto-incrementing field?
80
+ 4.15 .2) How do I get the value of a SERIAL insert?
81
+ 4.15 .3) Don't currval() and nextval() lead to a race condition with
85
82
other users?
86
- 4.17 ) What is an OID? What is a TID?
87
- 4.18 ) What is the meaning of some of the terms used in PostgreSQL?
88
- 4.19 ) Why do I get the error "ERROR: Memory exhausted in
83
+ 4.16 ) What is an OID? What is a TID?
84
+ 4.17 ) What is the meaning of some of the terms used in PostgreSQL?
85
+ 4.18 ) Why do I get the error "ERROR: Memory exhausted in
89
86
AllocSetAlloc()?"
90
- 4.20 ) How do I tell what PostgreSQL version I am running?
91
- 4.21 ) My large-object operations get invalid large obj descriptor.
87
+ 4.19 ) How do I tell what PostgreSQL version I am running?
88
+ 4.20 ) My large-object operations get invalid large obj descriptor.
92
89
Why?
93
- 4.22 ) How do I create a column that will default to the current time?
94
- 4.23 ) Why are my subqueries using IN so slow?
95
- 4.24 ) How do I perform an outer join?
96
- 4.25 ) How do I perform queries using multiple databases?
90
+ 4.21 ) How do I create a column that will default to the current time?
91
+ 4.22 ) Why are my subqueries using IN so slow?
92
+ 4.23 ) How do I perform an outer join?
93
+ 4.24 ) How do I perform queries using multiple databases?
97
94
98
95
Extending PostgreSQL
99
96
611
608
612
609
Operational Questions
613
610
614
- 4.1) Why is system confused about commas, decimal points, and date formats.
615
-
616
- Check your locale configuration. PostgreSQL uses the locale setting of
617
- the user that ran the postmaster process. There are postgres and psql
618
- SET commands to control the date format. Set those accordingly for
619
- your operating environment.
620
-
621
- 4.2) What is the exact difference between binary cursors and normal
622
- cursors?
611
+ 4.1) What is the difference between binary cursors and normal cursors?
623
612
624
613
See the DECLARE manual page for a description.
625
614
626
- 4.3 ) How do I SELECT only the first few rows of a query?
615
+ 4.2 ) How do I SELECT only the first few rows of a query?
627
616
628
617
See the FETCH manual page, or use SELECT ... LIMIT....
629
618
633
622
only the first few records requested, or the entire query may have to
634
623
be evaluated until the desired rows have been generated.
635
624
636
- 4.4 ) How do I get a list of tables or other things I can see in psql?
625
+ 4.3 ) How do I get a list of tables or other things I can see in psql?
637
626
638
627
You can read the source code for psql in file
639
628
pgsql/src/bin/psql/describe.c. It contains SQL commands that generate
640
629
the output for psql's backslash commands. You can also start psql with
641
630
the -E option so it will print out the queries it uses to execute the
642
631
commands you give.
643
632
644
- 4.5 ) How do you remove a column from a table?
633
+ 4.4 ) How do you remove a column from a table?
645
634
646
635
We do not support ALTER TABLE DROP COLUMN, but do this:
647
636
SELECT ... -- select all columns but the one you want to remove
650
639
DROP TABLE old_table;
651
640
ALTER TABLE new_table RENAME TO old_table;
652
641
653
- 4.6 ) What is the maximum size for a row, table, database?
642
+ 4.5 ) What is the maximum size for a row, table, database?
654
643
655
644
These are the limits:
656
645
Maximum size for a database? unlimited (60GB databases exist)
672
661
The maximum table size and maximum number of columns can be increased
673
662
if the default block size is increased to 32k.
674
663
675
- 4.7 ) How much database disk space is required to store data from a typical
664
+ 4.6 ) How much database disk space is required to store data from a typical
676
665
text file?
677
666
678
667
A PostgreSQL database may need six-and-a-half times the disk space
702
691
Indexes do not require as much overhead, but do contain the data that
703
692
is being indexed, so they can be large also.
704
693
705
- 4.8 ) How do I find out what tables or indexes are defined in the database?
694
+ 4.7 ) How do I find out what tables or indexes are defined in the database?
706
695
707
696
psql has a variety of backslash commands to show such information. Use
708
697
\? to see them.
711
700
many of the SELECTs needed to get information from the database system
712
701
tables.
713
702
714
- 4.9 ) My queries are slow or don't make use of the indexes. Why?
703
+ 4.8 ) My queries are slow or don't make use of the indexes. Why?
715
704
716
705
PostgreSQL does not automatically maintain statistics. VACUUM must be
717
706
run to update the statistics. After statistics are updated, the
736
725
string. So, to use indices, LIKE searches should not begin with %, and
737
726
~(regular expression searches) should start with ^.
738
727
739
- 4.10 ) How do I see how the query optimizer is evaluating my query?
728
+ 4.9 ) How do I see how the query optimizer is evaluating my query?
740
729
741
730
See the EXPLAIN manual page.
742
731
743
- 4.11 ) What is an R-tree index?
732
+ 4.10 ) What is an R-tree index?
744
733
745
734
An R-tree index is used for indexing spatial data. A hash index can't
746
735
handle range searches. A B-tree index only handles range searches in a
763
752
extending R-trees requires a bit of work and we don't currently have
764
753
any documentation on how to do it.
765
754
766
- 4.12 ) What is the Genetic Query Optimizer?
755
+ 4.11 ) What is the Genetic Query Optimizer?
767
756
768
757
The GEQO module speeds query optimization when joining many tables by
769
758
means of a Genetic Algorithm (GA). It allows the handling of large
770
759
join queries through nonexhaustive search.
771
760
772
- 4.13 ) How do I perform regular expression searches and case-insensitive
761
+ 4.12 ) How do I perform regular expression searches and case-insensitive
773
762
regular expression searches? How do I use an index for case-insensitive
774
763
searches?
775
764
788
777
CREATE INDEX tabindex on tab (lower(col));
789
778
790
779
791
- 4.14 ) In a query, how do I detect if a field is NULL?
780
+ 4.13 ) In a query, how do I detect if a field is NULL?
792
781
793
782
You test the column with IS NULLIS NOT NULL.
794
783
795
- 4.15 ) What is the difference between the various character types?
784
+ 4.14 ) What is the difference between the various character types?
796
785
797
786
Type Internal Name Notes
798
787
--------------------------------------------------
@@ -817,7 +806,7 @@ BYTEA bytea variable-length byte array (null-safe)
817
806
maximum 1 gigabyte. BYTEA is for storing binary data, particularly
818
807
values that include NULL bytes.
819
808
820
- 4.16 .1) How do I create a serial/auto-incrementing field?
809
+ 4.15 .1) How do I create a serial/auto-incrementing field?
821
810
822
811
PostgreSQL supports a SERIAL data type. It auto-creates a sequence and
823
812
index on the column. For example, this:
@@ -841,11 +830,11 @@ BYTEA bytea variable-length byte array (null-safe)
841
830
842
831
Numbering Rows.
843
832
844
- 4.16 .2) How do I get the value of a SERIAL insert?
833
+ 4.15 .2) How do I get the value of a SERIAL insert?
845
834
846
835
One approach is to to retrieve the next SERIAL value from the sequence
847
836
object with the nextval() function before inserting and then insert it
848
- explicitly. Using the example table in 4.16 .1, that might look like
837
+ explicitly. Using the example table in 4.15 .1, that might look like
849
838
this in Perl:
850
839
new_id = output of "SELECT nextval('person_id_seq')"
851
840
INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal');
@@ -867,13 +856,13 @@ BYTEA bytea variable-length byte array (null-safe)
867
856
oid value is made available via $sth->{pg_oid_status} after
868
857
$sth->execute().
869
858
870
- 4.16 .3) Don't currval() and nextval() lead to a race condition with other
859
+ 4.15 .3) Don't currval() and nextval() lead to a race condition with other
871
860
users?
872
861
873
862
No. Currval() returns the current value assigned by your backend, not
874
863
by all users.
875
864
876
- 4.17 ) What is an OID? What is a TID?
865
+ 4.16 ) What is an OID? What is a TID?
877
866
878
867
OIDs are PostgreSQL's answer to unique row ids. Every row that is
879
868
created in PostgreSQL gets a unique OID. All OIDs generated during
@@ -906,7 +895,7 @@ BYTEA bytea variable-length byte array (null-safe)
906
895
values. Tids change after rows are modified or reloaded. They are used
907
896
by index entries to point to physical rows.
908
897
909
- 4.18 ) What is the meaning of some of the terms used in PostgreSQL?
898
+ 4.17 ) What is the meaning of some of the terms used in PostgreSQL?
910
899
911
900
Some of the source code and older documentation use terms that have
912
901
more common usage. Here are some:
@@ -923,7 +912,7 @@ BYTEA bytea variable-length byte array (null-safe)
923
912
A list of general database terms can be found at:
924
913
http://www.comptechnews.com/~reaster/dbdesign.html
925
914
926
- 4.19 ) Why do I get the error "ERROR: Memory exhausted in AllocSetAlloc()?"
915
+ 4.18 ) Why do I get the error "ERROR: Memory exhausted in AllocSetAlloc()?"
927
916
928
917
If you are running a version older than 7.1, an upgrade may fix the
929
918
problem. Also it is possible you have run out of virtual memory on
@@ -939,11 +928,11 @@ BYTEA bytea variable-length byte array (null-safe)
939
928
problem with the SQL client because the backend is returning too much
940
929
data, try it before starting the client.
941
930
942
- 4.20 ) How do I tell what PostgreSQL version I am running?
931
+ 4.19 ) How do I tell what PostgreSQL version I am running?
943
932
944
933
From psql, type select version();
945
934
946
- 4.21 ) My large-object operations get invalid large obj descriptor. Why?
935
+ 4.20 ) My large-object operations get invalid large obj descriptor. Why?
947
936
948
937
You need to put BEGIN WORK and COMMIT around any use of a large object
949
938
handle, that is, surrounding lo_open ... lo_close.
@@ -957,12 +946,12 @@ BYTEA bytea variable-length byte array (null-safe)
957
946
If you are using a client interface like ODBC you may need to set
958
947
auto-commit off.
959
948
960
- 4.22 ) How do I create a column that will default to the current time?
949
+ 4.21 ) How do I create a column that will default to the current time?
961
950
962
951
Use CURRENT_TIMESTAMP:
963
952
CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
964
953
965
- 4.23 ) Why are my subqueries using IN so slow?
954
+ 4.22 ) Why are my subqueries using IN so slow?
966
955
967
956
Currently, we join subqueries to outer queries by sequentially
968
957
scanning the result of the subquery for each row of the outer query. A
@@ -978,7 +967,7 @@ SELECT *
978
967
979
968
We hope to fix this limitation in a future release.
980
969
981
- 4.24 ) How do I perform an outer join?
970
+ 4.23 ) How do I perform an outer join?
982
971
983
972
PostgreSQL 7.1 and later supports outer joins using the SQL standard
984
973
syntax. Here are two examples:
@@ -1008,7 +997,7 @@ SELECT *
1008
997
WHERE tab1.col1 NOT IN (SELECT tab2.col1 FROM tab2)
1009
998
ORDER BY col1
1010
999
1011
- 4.25 ) How do I perform queries using multiple databases?
1000
+ 4.24 ) How do I perform queries using multiple databases?
1012
1001
1013
1002
There is no way to query any database except the current one. Because
1014
1003
PostgreSQL loads database-specific system catalogs, it is uncertain