2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_index.sgml,v 1.27 2002/03/22 19:20:38 petere Exp $
3 PostgreSQL documentation
6 <refentry id="SQL-CREATEINDEX">
8 <refentrytitle id="sql-createindex-title">
11 <refmiscinfo>SQL - Language Statements</refmiscinfo>
23 <date>2001-07-15</date>
26 CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table</replaceable>
27 [ USING <replaceable class="parameter">acc_method</replaceable> ] ( <replaceable class="parameter">column</replaceable> [ <replaceable class="parameter">ops_name</replaceable> ] [, ...] )
28 [ WHERE <replaceable class="parameter">predicate</replaceable> ]
29 CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table</replaceable>
30 [ USING <replaceable class="parameter">acc_method</replaceable> ] ( <replaceable class="parameter">func_name</replaceable>( <replaceable class="parameter">column</replaceable> [, ... ]) [ <replaceable class="parameter">ops_name</replaceable> ] )
31 [ WHERE <replaceable class="parameter">predicate</replaceable> ]
34 <refsect2 id="R2-SQL-CREATEINDEX-1">
36 <date>1998-09-09</date>
48 Causes the system to check for
49 duplicate values in the table when the index is created (if data
50 already exist) and each time data is added. Attempts to
51 insert or update data which would result in duplicate entries
52 will generate an error.
58 <term><replaceable class="parameter">index_name</replaceable></term>
61 The name of the index to be created.
67 <term><replaceable class="parameter">table</replaceable></term>
70 The name of the table to be indexed.
76 <term><replaceable class="parameter">acc_method</replaceable></term>
79 The name of the access method to be used for
80 the index. The default access method is <literal>BTREE</literal>.
81 <application>PostgreSQL</application> provides four access methods for indexes:
85 <term><literal>BTREE</></term>
88 an implementation of Lehman-Yao
89 high-concurrency B-trees.
95 <term><literal>RTREE</></term>
97 <para>implements standard R-trees using Guttman's
98 quadratic split algorithm.
104 <term><literal>HASH</></term>
107 an implementation of Litwin's linear hashing.
113 <term><literal>GIST</></term>
116 Generalized Index Search Trees.
126 <term><replaceable class="parameter">column</replaceable></term>
129 The name of a column of the table.
135 <term><replaceable class="parameter">ops_name</replaceable></term>
138 An associated operator class. See below for details.
144 <term><replaceable class="parameter">func_name</replaceable></term>
147 A function, which returns a value that can be indexed.
153 <term><replaceable class="parameter">predicate</replaceable></term>
156 Defines the constraint expression for a partial index.
164 <refsect2 id="R2-SQL-CREATEINDEX-2">
166 <date>1998-09-09</date>
175 <term><computeroutput>
177 </computeroutput></term>
180 The message returned if the index is successfully created.
186 <term><computeroutput>
187 ERROR: Cannot create index: 'index_name' already exists.
188 </computeroutput></term>
191 This error occurs if it is impossible to create the index.
200 <refsect1 id="R1-SQL-CREATEINDEX-1">
202 <date>1998-09-09</date>
208 <command>CREATE INDEX</command> constructs an index
209 <replaceable class="parameter">index_name</replaceable>
210 on the specified <replaceable class="parameter">table</replaceable>.
214 Indexes are primarily used to enhance database performance.
215 But inappropriate use will result in slower performance.
221 In the first syntax shown above, the key field(s) for the
222 index are specified as column names.
223 Multiple fields can be specified if the index access method supports
228 In the second syntax shown above, an index is defined
229 on the result of a user-specified function
230 <replaceable class="parameter">func_name</replaceable> applied
231 to one or more columns of a single table.
232 These <firstterm>functional indexes</firstterm>
233 can be used to obtain fast access to data
234 based on operators that would normally require some
235 transformation to apply them to the base data.
239 <application>PostgreSQL</application> provides B-tree, R-tree, hash, and GiST access methods for
240 indexes. The B-tree access method is an implementation of
241 Lehman-Yao high-concurrency B-trees. The R-tree access method
242 implements standard R-trees using Guttman's quadratic split algorithm.
243 The hash access method is an implementation of Litwin's linear
244 hashing. We mention the algorithms used solely to indicate that all
245 of these access methods are fully dynamic and do not have to be
246 optimized periodically (as is the case with, for example, static hash
251 When the <command>WHERE</command> clause is present, a
252 <firstterm>partial index</firstterm> is created.
253 A partial index is an index that contains entries for only a portion of
254 a table, usually a portion that is somehow more interesting than the
255 rest of the table. For example, if you have a table that contains both
256 billed and unbilled orders where the unbilled orders take up a small
257 fraction of the total table and yet that is an often used section, you
258 can improve performance by creating an index on just that portion.
259 Another possible application is to use <command>WHERE</command> with
260 <command>UNIQUE</command> to enforce uniqueness over a subset of a
265 The expression used in the <command>WHERE</command> clause may refer
266 only to columns of the underlying table (but it can use all columns,
267 not only the one(s) being indexed). Presently, sub-SELECTs and
268 aggregate expressions are also forbidden in <command>WHERE</command>.
272 All functions and operators used in an index definition must be
273 <firstterm>cacheable</>, that is, their results must depend only on
274 their input arguments and never on any outside influence (such as
275 the contents of another table or the current time). This restriction
276 ensures that the behavior of the index is well-defined. To use a
277 user-defined function in an index, remember to mark the function cacheable
282 Use <xref linkend="sql-dropindex" endterm="sql-dropindex-title">
286 <refsect2 id="R2-SQL-CREATEINDEX-3">
288 <date>1998-09-09</date>
295 The <productname>PostgreSQL</productname>
296 query optimizer will consider using a B-tree index whenever
297 an indexed attribute is involved in a comparison using one of:
299 <simplelist type="inline">
300 <member><</member>
301 <member><=</member>
303 <member>>=</member>
304 <member>></member>
309 The <productname>PostgreSQL</productname>
310 query optimizer will consider using an R-tree index whenever
311 an indexed attribute is involved in a comparison using one of:
313 <simplelist type="inline">
314 <member><<</member>
315 <member>&<</member>
316 <member>&></member>
317 <member>>></member>
320 <member>&&</member>
325 The <productname>PostgreSQL</productname>
326 query optimizer will consider using a hash index whenever
327 an indexed attribute is involved in a comparison using
328 the <literal>=</literal> operator.
332 Currently, only the B-tree and gist access methods support multicolumn
333 indexes. Up to 16 keys may be specified by default (this limit
334 can be altered when building
335 <application>PostgreSQL</application>). Only B-tree currently supports
340 An <firstterm>operator class</firstterm> can be specified for each
341 column of an index. The operator class identifies the operators to
342 be used by the index for that column. For example, a B-tree index on
343 four-byte integers would use the <literal>int4_ops</literal> class;
344 this operator class includes comparison functions for four-byte
345 integers. In practice the default operator class for the field's
346 data type is usually sufficient. The main point of having operator classes
347 is that for some data types, there could be more than one meaningful
348 ordering. For example, we might want to sort a complex-number data type
349 either by absolute value or by real part. We could do this by defining
350 two operator classes for the data type and then selecting the proper
351 class when making an index. There are also some operator classes with
357 The operator classes <literal>box_ops</literal> and
358 <literal>bigbox_ops</literal> both support R-tree indexes on the
359 <literal>box</literal> data type.
360 The difference between them is that <literal>bigbox_ops</literal>
361 scales box coordinates down, to avoid floating-point exceptions from
362 doing multiplication, addition, and subtraction on very large
363 floating-point coordinates. (Note: this was true some time ago,
364 but currently the two operator classes both use floating point
365 and are effectively identical.)
372 The following query shows all defined operator classes:
375 SELECT am.amname AS acc_method,
376 opc.opcname AS ops_name,
377 opr.oprname AS ops_comp
378 FROM pg_am am, pg_opclass opc, pg_amop amop, pg_operator opr
379 WHERE opc.opcamid = am.oid AND
380 amop.amopclaid = opc.oid AND
381 amop.amopopr = opr.oid
382 ORDER BY acc_method, ops_name, ops_comp;
388 <refsect1 id="R1-SQL-CREATEINDEX-2">
392 <para>To create a B-tree index on the field <literal>title</literal>
393 in the table <literal>films</literal>:
396 CREATE UNIQUE INDEX title_idx
402 Is this example correct?
405 To create a R-tree index on a point attribute so that we
406 can efficiently use box operators on the result of the
410 CREATE INDEX pointloc
411 ON points USING RTREE (point2box(location) box_ops);
413 WHERE point2box(points.pointloc) = boxes.box;
419 <refsect1 id="R1-SQL-CREATEINDEX-3">
424 <refsect2 id="R2-SQL-CREATEINDEX-4">
426 <date>1998-09-09</date>
432 CREATE INDEX is a <productname>PostgreSQL</productname> language extension.
435 There is no <command>CREATE INDEX</command> command in SQL92.
441 <!-- Keep this comment at the end of the file
446 sgml-minimize-attributes:nil
447 sgml-always-quote-attributes:t
450 sgml-parent-document:nil
451 sgml-default-dtd-file:"../reference.ced"
452 sgml-exposed-tags:nil
453 sgml-local-catalogs:"/usr/lib/sgml/catalog"
454 sgml-local-ecat-files:nil