-
+
Indexes
PostgreSQL provides several index types:
- B-tree, R-tree, GiST, and Hash. Each index type uses a different
+ B-tree, R-tree, Hash, and GiST. Each index type uses a different
algorithm that is best suited to different types of queries.
+ By default, the CREATE INDEX command will create a
+ B-tree index, which fits the most common situations.
+
+
B-tree
index
- By default, the CREATE INDEX command will create a
- B-tree index, which fits the most common situations. B-trees can
- handle equality and range queries on data that can be sorted into
- some ordering. In
- particular, the
PostgreSQL query planner
+ B-trees can handle equality and range queries on data that can be sorted
+ into some ordering.
+ In particular, the
PostgreSQL query planner
will consider using a B-tree index whenever an indexed column is
involved in a comparison using one of these operators:
- type="inline">
+
<
<=
=
>=
>
+
+ Constructs equivalent to combinations of these operators, such as
+ BETWEEN> and IN>, can also be implemented with
+ a B-tree index search. (But note that IS NULL> is not
+ equivalent to => and is not indexable.)
'foo%' or col ~ '^foo', but not
col LIKE '%bar'. However, if your server does
not use the C locale you will need to create the index with a
- special operator class. See
- below.
+ special operator class to support indexing of pattern-matching queries.
+ See below.
consider using an R-tree index whenever an indexed column is
involved in a comparison using one of these operators:
- type="inline">
+
<<
&<
&>
~=
&&
- (Refer to about the meaning of
+
+ (See for the meaning of
these operators.)
+ GiST indexes are not a single kind of index, but rather an infrastructure
+ within which many different indexing strategies can be implemented.
+ Accordingly, the particular operators with which a GiST index can be
+ used vary depending on the indexing strategy (the operator
+ class>). For more information see .
+
+
The B-tree index method is an implementation of Lehman-Yao
high-concurrency B-trees. The R-tree index method implements
or'ing together the bits INV_READ and
INV_WRITE. The low-order sixteen bits of the mask have
historically been used at Berkeley to designate the storage manager number on which the large object
- should reside. These
- bits should always be zero now.
- The return value is the OID that was assigned to the new large object.
+ should reside. These bits should always be zero now. (The access type
+ does not actually do anything anymore either, but one or both flag bits
+ must be set to avoid an error.)
+ The return value is the OID that was assigned to the new large object,
+ or InvalidOid (zero) on failure.
An example:
-inv_oid = lo_creat(INV_READ|INV_WRITE);
+inv_oid = lo_creat(conn, INV_READ|INV_WRITE);
filename
specifies the operating system name of
the file to be imported as a large object.
- The return value is the OID that was assigned to the new large object.
+ The return value is the OID that was assigned to the new large object,
+ or InvalidOid (zero) on failure.
Note that the file is read by the client interface library, not by
the server; so it must exist in the client filesystem and be readable
by the client application.
int lo_export(PGconn *conn, Oid lobjId, const char *filename);
- The
lobjId argument specifies
the OID of the large
- object
to export and the filename argument specifies
- the operating system name of the file.
- Note that the file is written by the client interface library, not by
- the server.
+ The
lobjId argument specifies
the OID of the large
+ object
to export and the filename argument
+ specifies the operating system name of the file. Note that the file is
+ written by the client interface library, not by the server. Returns 1
+ on success, -1 on failure.
Opening an Existing Large Object
- To open an existing large object, call
+ To open an existing large object for reading or writing, call
int lo_open(PGconn *conn, Oid lobjId, int mode);
object is opened for reading (INV_READ>), writing (INV_WRITE), or
both.
A large object cannot be opened before it is created.
- lo_open returns a large object descriptor
- for later use in lo_read, lo_write,
- lo_lseek, lo_tell, and
- lo_close. The descriptor is only valid for
+ lo_open returns a (non-negative) large object
+ descriptor for later use in lo_read,
+ lo_write, lo_lseek,
+ lo_tell, and lo_close.
+ The descriptor is only valid for
the duration of the current transaction.
+ On failure, -1 is returned.
are SEEK_SET> (seek from object start),
SEEK_CUR> (seek from current position), and
SEEK_END> (seek from object end). The return value is
- the new location pointer.
+ the new location pointer, or -1 on error.
lobjId argument specifies the OID of the
- large object to remove. In the event of an error, the return
- value is negative.
+ large object to remove. Returns 1 if successful, -1 on failure.
-
Server-Side Functions
- There are two built-in server-side functions,
- and
- for large object access, which are available for use in
-
SQL commands. Here is an example of their
- use:
+ There are server-side functions callable from SQL that correspond to
+ each of the client-side functions described above; indeed, for the
+ most part the client-side functions are simply interfaces to the
+ equivalent server-side functions. The ones that are actually useful
+ to call via SQL commands are
+
lo_importlo_import>>, and
+ Here are examples of their use:
+
CREATE TABLE image (
name text,
raster oid
);
+SELECT lo_creat(-1); -- returns OID of new, empty large object
+
+SELECT lo_unlink(173454); -- deletes large object with OID 173454
+
INSERT INTO image (name, raster)
VALUES ('beautiful image', lo_import('/etc/motd'));
SELECT lo_export(image.raster, '/tmp/motd') FROM image
WHERE name = 'beautiful image';
-
-
-These functions read and write files in the server's file system, using the
-permissions of the database's owning user. Therefore, their use is restricted
-to superusers. (In contrast, the client-side import and export functions
-read and write files in the client's file system, using the permissions of
-the client program. Their use is not restricted.)
-
+
+
+ The server-side lo_import and
+ lo_export functions behave considerably differently
+ from their client-side analogs. These two functions read and write files
+ in the server's file system, using the permissions of the database's
+ owning user. Therefore, their use is restricted to superusers. In
+ contrast, the client-side import and export functions read and write files
+ in the client's file system, using the permissions of the client program.
+ The client-side functions can be used by any
+
managing schemas is in .
+ Databases are created with the CREATE DATABASE> command
+ (see ) and destroyed with the
+ DROP DATABASE> command
+ (see ).
+ To determine the set of existing databases, examine the
+ pg_database> system catalog, for example
+
+SELECT datname FROM pg_database;
+
+ The program's \l> meta-command
+ and
+ existing databases.
+
+
The
SQL> standard calls databases catalogs>, but there
- To simplify the implementation of tablespaces,
-
PostgreSQL> makes extensive use of symbolic links. This
+ To remove an empty tablespace, use the
+ command.
+
+
+ To determine the set of existing tablespaces, examine the
+ pg_tablespace> system catalog, for example
+
+SELECT spcname FROM pg_tablespace;
+
+ The program's \db> meta-command
+ is also useful for listing the existing tablespaces.
+
+
+
PostgreSQL> makes extensive use of symbolic links
+ to simplify the implementation of tablespaces. This
means that tablespaces can be used only> on systems
that support symbolic links.
default, although locations such as
/usr/local/pgsql/data or
/var/lib/pgsql/data are popular. To initialize a
- database cluster, use the command
initdb,
initdb>> which is
+ database cluster, use the command
+ linkend="app-initdb">,
initdb>> which is
installed with
PostgreSQL. The desired
file system location of your database cluster is indicated by the
option, for example
other than C> or POSIX>. Therefore, it is
important to make this choice correctly the first time.
+
+ initdb also sets the default character set encoding
+ for the database cluster. Normally this should be chosen to match the
+ locale setting. For details see .
+
present. The default is true> for compatibility with
previous releases of
PostgreSQL>. However, this
behavior is not SQL-standard, and many people dislike it because it
- can mask mistakes. Set to false> for the SQL-standard
+ can mask mistakes (such as referencing a table where you should have
+ referenced its alias). Set to false> for the SQL-standard
behavior of rejecting references to tables that are not listed in
FROM>.
operating system users. In practice it might be convenient to
maintain a correspondence, but this is not required. Database user
names are global across a database cluster installation (and not
- per individual database). To create a user use the <command>CREATE
- USER> SQL command:
+ per individual database). To create a user use the <xref
+ linkend="sql-createuser" endterm="sql-createuser-title"> SQL command:
CREATE USER name;
name follows the rules for SQL
identifiers: either unadorned without special characters, or
double-quoted. To remove an existing user, use the analogous
- <command>DROP USER> command:
+ <xref linkend="sql-dropuser" endterm="sql-dropuser-title"> command:
DROP USER name;
- For convenience, the programs <command>createuser>
- and <command>dropuser> are provided as wrappers
+ For convenience, the programs <xref linkend="app-createuser">
+ and <xref linkend="app-dropuser"> are provided as wrappers
around these SQL commands that can be called from the shell command
line:
+ To determine the set of existing users, examine the pg_user>
+ system catalog, for example
+
+SELECT usename FROM pg_user;
+
+ The program's \du> meta-command
+ is also useful for listing the existing users.
+
+
In order to bootstrap the database system, a freshly initialized
system always contains one predefined user. This user will have the
is determined by the client authentication setup, as explained in
. (Thus, a client is not
necessarily limited to connect as the user with the same name as
- its operating system user, in the same way a person is not
- constrained in its login name by her real name.) Since the user
+ its operating system user, just as a person's login name
+ need not match her real name.) Since the user
identity determines the set of privileges available to a connected
client, it is important to carefully configure this when setting up
a multiuser environment.
As in Unix, groups are a way of logically grouping users to ease
management of privileges: privileges can be granted to, or revoked
- from, a group as a whole. To create a group, use
+ from, a group as a whole. To create a group, use the
+ linkend="sql-creategroup" endterm="sql-creategroup-title"> SQL command:
CREATE GROUP name;
- To add users to or remove users from a group, use
+
+ To add users to or remove users from an existing group, use
+ linkend="sql-altergroup" endterm="sql-altergroup-title">:
ALTER GROUP name ADD USER uname1, ... ;
ALTER GROUP name DROP USER uname1, ... ;
+
+ To destroy a group, use
+ linkend="sql-dropgroup" endterm="sql-dropgroup-title">:
+
+DROP GROUP name;
+
+ This only drops the group, not its member users.
+
+
+ To determine the set of existing groups, examine the pg_group>
+ system catalog, for example
+
+SELECT groname FROM pg_group;
+
+ The program's \dg> meta-command
+ is also useful for listing the existing groups.
refers to the first argument, $2> to the second, and so on.
If an argument is of a composite type, then the dot notation,
e.g., $1.name, may be used to access attributes
- of the argument.
+ of the argument. The arguments can only be used as data values,
+ not as identifiers. Thus for example this is reasonable:
+INSERT INTO mytable VALUES ($1);
+
+but this will not work:
+INSERT INTO $1 VALUES (42);
+
data: the first four bytes must be an int32 containing
the total length in bytes of the datum (including itself). The C
functions operating on the data type must be careful to unpack any
- toasted values they are handed (this detail can normally be hidden in the
- GETARG macros). Then,
+ toasted values they are handed, by using PG_DETOAST_DATUM>.
+ (This detail is customarily hidden by defining type-specific
+ GETARG macros.) Then,
when running the CREATE TYPE command, specify the
internal length as variable> and select the appropriate
storage option.