Replace functional-index facility with expressional indexes. Any column

of an index can now be a computed expression instead of a simple variable.
Restrictions on expressions are the same as for predicates (only immutable
functions, no sub-selects).  This fixes problems recently introduced with
inlining SQL functions, because the inlining transformation is applied to
both expression trees so the planner can still match them up.  Along the
way, improve efficiency of handling index predicates (both predicates and
index expressions are now cached by the relcache) and fix 7.3 oversight
that didn't record dependencies of predicate expressions.
This commit is contained in:
Tom Lane 2003-05-28 16:04:02 +00:00
parent e5f19598e0
commit fc8d970cbc
50 changed files with 1351 additions and 1283 deletions

View File

@ -1492,10 +1492,7 @@ get_pkey_attnames(Oid relid, int16 *numatts)
/* we're only interested if it is the primary key */ /* we're only interested if it is the primary key */
if (index->indisprimary == TRUE) if (index->indisprimary == TRUE)
{ {
i = 0; *numatts = index->indnatts;
while (index->indkey[i++] != 0)
(*numatts)++;
if (*numatts > 0) if (*numatts > 0)
{ {
result = (char **) palloc(*numatts * sizeof(char *)); result = (char **) palloc(*numatts * sizeof(char *));

View File

@ -1,6 +1,6 @@
<!-- <!--
Documentation of the system catalogs, directed toward PostgreSQL developers Documentation of the system catalogs, directed toward PostgreSQL developers
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.70 2003/05/08 22:19:55 tgl Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.71 2003/05/28 16:03:55 tgl Exp $
--> -->
<chapter id="catalogs"> <chapter id="catalogs">
@ -1933,26 +1933,18 @@
<entry>The OID of the <structname>pg_class</> entry for the table this index is for</entry> <entry>The OID of the <structname>pg_class</> entry for the table this index is for</entry>
</row> </row>
<row>
<entry><structfield>indproc</structfield></entry>
<entry><type>regproc</type></entry>
<entry><literal>pg_proc.oid</literal></entry>
<entry>The function's OID if this is a functional index,
else zero</entry>
</row>
<row> <row>
<entry><structfield>indkey</structfield></entry> <entry><structfield>indkey</structfield></entry>
<entry><type>int2vector</type></entry> <entry><type>int2vector</type></entry>
<entry>pg_attribute.attnum</entry> <entry>pg_attribute.attnum</entry>
<entry> <entry>
This is an array of up to This is an array of <structfield>indnatts</structfield> (up to
<symbol>INDEX_MAX_KEYS</symbol> values that indicate which <symbol>INDEX_MAX_KEYS</symbol>) values that indicate which
table columns this index pertains to. For example a value of table columns this index indexes. For example a value of
<literal>1 3</literal> would mean that the first and the third <literal>1 3</literal> would mean that the first and the third table
column make up the index key. For a functional index, these columns make up the index key. A zero in this array indicates that the
columns are the inputs to the function, and the function's return corresponding index attribute is an expression over the table columns,
value is the index key. rather than a simple column reference.
</entry> </entry>
</row> </row>
@ -1961,17 +1953,18 @@
<entry><type>oidvector</type></entry> <entry><type>oidvector</type></entry>
<entry>pg_opclass.oid</entry> <entry>pg_opclass.oid</entry>
<entry> <entry>
For each column in the index key this contains a reference to For each column in the index key this contains the OID of
the operator class to use. See the operator class to use. See
<structname>pg_opclass</structname> for details. <structname>pg_opclass</structname> for details.
</entry> </entry>
</row> </row>
<row> <row>
<entry><structfield>indisclustered</structfield></entry> <entry><structfield>indnatts</structfield></entry>
<entry><type>bool</type></entry> <entry><type>int2</type></entry>
<entry></entry> <entry></entry>
<entry>If true, the table was last clustered on this index.</entry> <entry>The number of columns in the index (duplicates
<literal>pg_class.relnatts</literal>)</entry>
</row> </row>
<row> <row>
@ -1990,19 +1983,28 @@
</row> </row>
<row> <row>
<entry><structfield>indreference</structfield></entry> <entry><structfield>indisclustered</structfield></entry>
<entry><type>oid</type></entry> <entry><type>bool</type></entry>
<entry></entry> <entry></entry>
<entry>unused</entry> <entry>If true, the table was last clustered on this index.</entry>
</row>
<row>
<entry><structfield>indexprs</structfield></entry>
<entry><type>text</type></entry>
<entry></entry>
<entry>Expression trees (in <function>nodeToString()</function> representation)
for index attributes that are not simple column references. This is a
list with one element for each zero entry in <structfield>indkey</>.
Null if all index attributes are simple references.</entry>
</row> </row>
<row> <row>
<entry><structfield>indpred</structfield></entry> <entry><structfield>indpred</structfield></entry>
<entry><type>text</type></entry> <entry><type>text</type></entry>
<entry></entry> <entry></entry>
<entry>Expression tree (in the form of a <function>nodeToString()</function> representation) <entry>Expression tree (in <function>nodeToString()</function> representation)
for partial index predicate. Empty string if not a partial for partial index predicate. Null if not a partial index.</entry>
index.</entry>
</row> </row>
</tbody> </tbody>
</tgroup> </tgroup>

View File

@ -1,4 +1,4 @@
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/indices.sgml,v 1.41 2003/05/15 15:50:18 petere Exp $ --> <!-- $Header: /cvsroot/pgsql/doc/src/sgml/indices.sgml,v 1.42 2003/05/28 16:03:55 tgl Exp $ -->
<chapter id="indexes"> <chapter id="indexes">
<title id="indexes-title">Indexes</title> <title id="indexes-title">Indexes</title>
@ -20,8 +20,7 @@
<title>Introduction</title> <title>Introduction</title>
<para> <para>
The classical example for the need of an index is if there is a Suppose we have a table similar to this:
table similar to this:
<programlisting> <programlisting>
CREATE TABLE test1 ( CREATE TABLE test1 (
id integer, id integer,
@ -32,24 +31,24 @@ CREATE TABLE test1 (
<programlisting> <programlisting>
SELECT content FROM test1 WHERE id = <replaceable>constant</replaceable>; SELECT content FROM test1 WHERE id = <replaceable>constant</replaceable>;
</programlisting> </programlisting>
Ordinarily, the system would have to scan the entire With no advance preparation, the system would have to scan the entire
<structname>test1</structname> table row by row to find all <structname>test1</structname> table, row by row, to find all
matching entries. If there are a lot of rows in matching entries. If there are a lot of rows in
<structname>test1</structname> and only a few rows (possibly zero <structname>test1</structname> and only a few rows (perhaps only zero
or one) returned by the query, then this is clearly an inefficient or one) that would be returned by such a query, then this is clearly an
method. If the system were instructed to maintain an index on the inefficient method. But if the system has been instructed to maintain an
<structfield>id</structfield> column, then it could use a more index on the <structfield>id</structfield> column, then it can use a more
efficient method for locating matching rows. For instance, it efficient method for locating matching rows. For instance, it
might only have to walk a few levels deep into a search tree. might only have to walk a few levels deep into a search tree.
</para> </para>
<para> <para>
A similar approach is used in most books of non-fiction: Terms and A similar approach is used in most books of non-fiction: terms and
concepts that are frequently looked up by readers are collected in concepts that are frequently looked up by readers are collected in
an alphabetic index at the end of the book. The interested reader an alphabetic index at the end of the book. The interested reader
can scan the index relatively quickly and flip to the appropriate can scan the index relatively quickly and flip to the appropriate
page, and would not have to read the entire book to find the page(s), rather than having to read the entire book to find the
interesting location. As it is the task of the author to material of interest. Just as it is the task of the author to
anticipate the items that the readers are most likely to look up, anticipate the items that the readers are most likely to look up,
it is the task of the database programmer to foresee which indexes it is the task of the database programmer to foresee which indexes
would be of advantage. would be of advantage.
@ -73,13 +72,14 @@ CREATE INDEX test1_id_index ON test1 (id);
<para> <para>
Once the index is created, no further intervention is required: the Once the index is created, no further intervention is required: the
system will use the index when it thinks it would be more efficient system will update the index when the table is modified, and it will
use the index in queries when it thinks this would be more efficient
than a sequential table scan. But you may have to run the than a sequential table scan. But you may have to run the
<command>ANALYZE</command> command regularly to update <command>ANALYZE</command> command regularly to update
statistics to allow the query planner to make educated decisions. statistics to allow the query planner to make educated decisions.
Also read <xref linkend="performance-tips"> for information about Also read <xref linkend="performance-tips"> for information about
how to find out whether an index is used and when and why the how to find out whether an index is used and when and why the
planner may choose to <emphasis>not</emphasis> use an index. planner may choose <emphasis>not</emphasis> to use an index.
</para> </para>
<para> <para>
@ -198,7 +198,7 @@ CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable>
than B-tree indexes, and the index size and build time for hash than B-tree indexes, and the index size and build time for hash
indexes is much worse. Hash indexes also suffer poor performance indexes is much worse. Hash indexes also suffer poor performance
under high concurrency. For these reasons, hash index use is under high concurrency. For these reasons, hash index use is
discouraged. presently discouraged.
</para> </para>
</note> </note>
</para> </para>
@ -250,14 +250,13 @@ CREATE INDEX test2_mm_idx ON test2 (major, minor);
Currently, only the B-tree and GiST implementations support multicolumn Currently, only the B-tree and GiST implementations support multicolumn
indexes. Up to 32 columns may be specified. (This limit can be indexes. Up to 32 columns may be specified. (This limit can be
altered when building <productname>PostgreSQL</productname>; see the altered when building <productname>PostgreSQL</productname>; see the
file <filename>pg_config.h</filename>.) file <filename>pg_config_manual.h</filename>.)
</para> </para>
<para> <para>
The query planner can use a multicolumn index for queries that The query planner can use a multicolumn index for queries that
involve the leftmost column in the index definition and any number involve the leftmost column in the index definition plus any number
of columns listed to the right of it without a gap (when of columns listed to the right of it, without a gap. For example,
used with appropriate operators). For example,
an index on <literal>(a, b, c)</literal> can be used in queries an index on <literal>(a, b, c)</literal> can be used in queries
involving all of <literal>a</literal>, <literal>b</literal>, and involving all of <literal>a</literal>, <literal>b</literal>, and
<literal>c</literal>, or in queries involving both <literal>c</literal>, or in queries involving both
@ -266,7 +265,9 @@ CREATE INDEX test2_mm_idx ON test2 (major, minor);
(In a query involving <literal>a</literal> and <literal>c</literal> (In a query involving <literal>a</literal> and <literal>c</literal>
the planner might choose to use the index for the planner might choose to use the index for
<literal>a</literal> only and treat <literal>c</literal> like an <literal>a</literal> only and treat <literal>c</literal> like an
ordinary unindexed column.) ordinary unindexed column.) Of course, each column must be used with
operators appropriate to the index type; clauses that involve other
operators will not be considered.
</para> </para>
<para> <para>
@ -283,8 +284,8 @@ SELECT name FROM test2 WHERE major = <replaceable>constant</replaceable> OR mino
<para> <para>
Multicolumn indexes should be used sparingly. Most of the time, Multicolumn indexes should be used sparingly. Most of the time,
an index on a single column is sufficient and saves space and time. an index on a single column is sufficient and saves space and time.
Indexes with more than three columns are almost certainly Indexes with more than three columns are unlikely to be helpful
inappropriate. unless the usage of the table is extremely stylized.
</para> </para>
</sect1> </sect1>
@ -332,19 +333,19 @@ CREATE UNIQUE INDEX <replaceable>name</replaceable> ON <replaceable>table</repla
</sect1> </sect1>
<sect1 id="indexes-functional"> <sect1 id="indexes-expressional">
<title>Functional Indexes</title> <title>Indexes on Expressions</title>
<indexterm zone="indexes-functional"> <indexterm zone="indexes-expressional">
<primary>indexes</primary> <primary>indexes</primary>
<secondary>on functions</secondary> <secondary>on expressions</secondary>
</indexterm> </indexterm>
<para> <para>
For a <firstterm>functional index</firstterm>, an index is defined An index column need not be just a column of the underlying table,
on the result of a function applied to one or more columns of a but can be a function or scalar expression computed from one or
single table. Functional indexes can be used to obtain fast access more columns of the table. This feature is useful to obtain fast
to data based on the result of function calls. access to tables based on the results of computations.
</para> </para>
<para> <para>
@ -362,20 +363,29 @@ CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));
</para> </para>
<para> <para>
The function in the index definition can take more than one As another example, if one often does queries like this:
argument, but they must be table columns, not constants. <programlisting>
Functional indexes are always single-column (namely, the function SELECT * FROM people WHERE (first_name || ' ' || last_name) = 'John Smith';
result) even if the function uses more than one input column; there </programlisting>
cannot be multicolumn indexes that contain function calls. then it might be worth creating an index like this:
<programlisting>
CREATE INDEX people_names ON people ((first_name || ' ' || last_name));
</programlisting>
</para> </para>
<tip> <para>
<para> The syntax of the <command>CREATE INDEX</> command normally requires
The restrictions mentioned in the previous paragraph can easily be writing parentheses around index expressions, as shown in the second
worked around by defining a custom function to use in the index example. The parentheses may be omitted when the expression is just
definition that computes any desired result internally. a function call, as in the first example.
</para> </para>
</tip>
<para>
Index expressions are relatively expensive to maintain, since the
derived expression(s) must be computed for each row upon insertion
or whenever it is updated. Therefore they should be used only when
queries that can use the index are very frequent.
</para>
</sect1> </sect1>
@ -391,8 +401,8 @@ CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable>
The operator class identifies the operators to be used by the index The operator class identifies the operators to be used by the index
for that column. For example, a B-tree index on the type <type>int4</type> for that column. For example, a B-tree index on the type <type>int4</type>
would use the <literal>int4_ops</literal> class; this operator would use the <literal>int4_ops</literal> class; this operator
class includes comparison functions for values of type <type>int4</type>. In class includes comparison functions for values of type <type>int4</type>.
practice the default operator class for the column's data type is In practice the default operator class for the column's data type is
usually sufficient. The main point of having operator classes is usually sufficient. The main point of having operator classes is
that for some data types, there could be more than one meaningful that for some data types, there could be more than one meaningful
ordering. For example, we might want to sort a complex-number data ordering. For example, we might want to sort a complex-number data
@ -427,24 +437,25 @@ CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable>
<literal>name_pattern_ops</literal> support B-tree indexes on <literal>name_pattern_ops</literal> support B-tree indexes on
the types <type>text</type>, <type>varchar</type>, the types <type>text</type>, <type>varchar</type>,
<type>char</type>, and <type>name</type>, respectively. The <type>char</type>, and <type>name</type>, respectively. The
difference to the ordinary operator classes is that the values difference from the ordinary operator classes is that the values
are compared strictly character by character rather than are compared strictly character by character rather than
according to the locale-specific collation rules. This makes according to the locale-specific collation rules. This makes
these operator classes suitable for use by queries involving these operator classes suitable for use by queries involving
pattern matching expressions (<literal>LIKE</literal> or POSIX pattern matching expressions (<literal>LIKE</literal> or POSIX
regular expressions) if the server does not use the standard regular expressions) if the server does not use the standard
<quote>C</quote> locale. As an example, to index a <quote>C</quote> locale. As an example, you might index a
<type>varchar</type> column like this: <type>varchar</type> column like this:
<programlisting> <programlisting>
CREATE INDEX test_index ON test_table (col varchar_pattern_ops); CREATE INDEX test_index ON test_table (col varchar_pattern_ops);
</programlisting> </programlisting>
If you do use the C locale, you should instead create an index If you do use the C locale, you may instead create an index
with the default operator class. Also note that you should with the default operator class, and it will still be useful
for pattern-matching queries. Also note that you should
create an index with the default operator class if you want create an index with the default operator class if you want
queries involving ordinary comparisons to use an index. Such queries involving ordinary comparisons to use an index. Such
queries cannot use the queries cannot use the
<literal><replaceable>xxx</replaceable>_pattern_ops</literal> <literal><replaceable>xxx</replaceable>_pattern_ops</literal>
operator classes. It is possible, however, to create multiple operator classes. It is allowed to create multiple
indexes on the same column with different operator classes. indexes on the same column with different operator classes.
</para> </para>
</listitem> </listitem>

View File

@ -1,5 +1,5 @@
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v 1.18 2003/04/27 22:21:22 tgl Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/plpgsql.sgml,v 1.19 2003/05/28 16:03:55 tgl Exp $
--> -->
<chapter id="plpgsql"> <chapter id="plpgsql">
@ -136,9 +136,10 @@ END;
<para> <para>
Except for input/output conversion and calculation functions Except for input/output conversion and calculation functions
for user-defined types, anything that can be defined in C language for user-defined types, anything that can be defined in C language
functions can also be done with <application>PL/pgSQL</application>. For example, it is possible to functions can also be done with <application>PL/pgSQL</application>.
For example, it is possible to
create complex conditional computation functions and later use create complex conditional computation functions and later use
them to define operators or use them in functional indexes. them to define operators or use them in index expressions.
</para> </para>
<sect2 id="plpgsql-advantages"> <sect2 id="plpgsql-advantages">

View File

@ -1,5 +1,5 @@
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_index.sgml,v 1.38 2003/04/22 10:08:08 petere Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_index.sgml,v 1.39 2003/05/28 16:03:55 tgl Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
@ -16,12 +16,8 @@ PostgreSQL documentation
<refsynopsisdiv> <refsynopsisdiv>
<synopsis> <synopsis>
CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table</replaceable> CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table</replaceable> [ USING <replaceable class="parameter">method</replaceable> ]
[ USING <replaceable class="parameter">method</replaceable> ] ( <replaceable class="parameter">column</replaceable> [ <replaceable class="parameter">ops_name</replaceable> ] [, ...] ) ( { <replaceable class="parameter">column</replaceable> | ( <replaceable class="parameter">expression</replaceable> ) } [ <replaceable class="parameter">opclass</replaceable> ] [, ...] )
[ WHERE <replaceable class="parameter">predicate</replaceable> ]
CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table</replaceable>
[ USING <replaceable class="parameter">method</replaceable> ] ( <replaceable class="parameter">func_name</replaceable>( <replaceable class="parameter">column</replaceable> [, ... ]) [ <replaceable class="parameter">ops_name</replaceable> ] )
[ WHERE <replaceable class="parameter">predicate</replaceable> ] [ WHERE <replaceable class="parameter">predicate</replaceable> ]
</synopsis> </synopsis>
</refsynopsisdiv> </refsynopsisdiv>
@ -32,25 +28,22 @@ CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable>
<para> <para>
<command>CREATE INDEX</command> constructs an index <replaceable <command>CREATE INDEX</command> constructs an index <replaceable
class="parameter">index_name</replaceable> on the specified table. class="parameter">index_name</replaceable> on the specified table.
Indexes are primarily used to enhance database performance. But Indexes are primarily used to enhance database performance (though
inappropriate use will result in slower performance. inappropriate use will result in slower performance).
</para> </para>
<para> <para>
In the first syntax shown above, the key field(s) for the The key field(s) for the index are specified as column names,
index are specified as column names. or alternatively as expressions written in parentheses.
Multiple fields can be specified if the index method supports Multiple fields can be specified if the index method supports
multicolumn indexes. multicolumn indexes.
</para> </para>
<para> <para>
In the second syntax shown above, an index is defined on the result An index field can be an expression computed from the values of
of a user-specified function <replaceable one or more columns of the table row. This feature can be used
class="parameter">func_name</replaceable> applied to one or more to obtain fast access to data based on some transformation of
columns of a single table. These <firstterm>functional the basic data. For example, an index computed on
indexes</firstterm> can be used to obtain fast access to data based
on operators that would normally require some transformation to apply
them to the base data. For example, a functional index on
<literal>upper(col)</> would allow the clause <literal>upper(col)</> would allow the clause
<literal>WHERE upper(col) = 'JIM'</> to use an index. <literal>WHERE upper(col) = 'JIM'</> to use an index.
</para> </para>
@ -84,6 +77,7 @@ CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable>
only to columns of the underlying table (but it can use all columns, only to columns of the underlying table (but it can use all columns,
not only the one(s) being indexed). Presently, subqueries and not only the one(s) being indexed). Presently, subqueries and
aggregate expressions are also forbidden in <literal>WHERE</literal>. aggregate expressions are also forbidden in <literal>WHERE</literal>.
The same restrictions apply to index fields that are expressions.
</para> </para>
<para> <para>
@ -92,8 +86,8 @@ CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable>
their arguments and never on any outside influence (such as their arguments and never on any outside influence (such as
the contents of another table or the current time). This restriction the contents of another table or the current time). This restriction
ensures that the behavior of the index is well-defined. To use a ensures that the behavior of the index is well-defined. To use a
user-defined function in an index, remember to mark the function immutable user-defined function in an index expression or <literal>WHERE</literal>
when you create it. clause, remember to mark the function immutable when you create it.
</para> </para>
</refsect1> </refsect1>
@ -156,19 +150,22 @@ CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><replaceable class="parameter">ops_name</replaceable></term> <term><replaceable class="parameter">expression</replaceable></term>
<listitem> <listitem>
<para> <para>
An associated operator class. See below for details. An expression based on one or more columns of the table. The
expression usually must be written with surrounding parentheses,
as shown in the syntax. However, the parentheses may be omitted
if the expression has the form of a function call.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><replaceable class="parameter">func_name</replaceable></term> <term><replaceable class="parameter">opclass</replaceable></term>
<listitem> <listitem>
<para> <para>
A function, which returns a value that can be indexed. The name of an operator class. See below for details.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -177,7 +174,7 @@ CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable>
<term><replaceable class="parameter">predicate</replaceable></term> <term><replaceable class="parameter">predicate</replaceable></term>
<listitem> <listitem>
<para> <para>
Defines the constraint expression for a partial index. The constraint expression for a partial index.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>

View File

@ -1,5 +1,5 @@
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.191 2003/05/26 18:58:26 tgl Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.192 2003/05/28 16:03:55 tgl Exp $
--> -->
<appendix id="release"> <appendix id="release">
@ -24,6 +24,7 @@ CDATA means the content is "SGML-free", so you can write without
worries about funny characters. worries about funny characters.
--> -->
<literallayout><![CDATA[ <literallayout><![CDATA[
Functional indexes have been generalized into expressional indexes
CHAR(n) to TEXT conversion automatically strips trailing blanks CHAR(n) to TEXT conversion automatically strips trailing blanks
Pattern matching operations can use indexes regardless of locale Pattern matching operations can use indexes regardless of locale
New frontend/backend protocol supports many long-requested features New frontend/backend protocol supports many long-requested features

View File

@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.57 2003/05/27 17:49:45 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.58 2003/05/28 16:03:55 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -273,7 +273,7 @@ boot_index_param:
{ {
IndexElem *n = makeNode(IndexElem); IndexElem *n = makeNode(IndexElem);
n->name = LexIDStr($1); n->name = LexIDStr($1);
n->funcname = n->args = NIL; /* no func indexes */ n->expr = NULL;
n->opclass = makeList1(makeString(LexIDStr($2))); n->opclass = makeList1(makeString(LexIDStr($2)));
$$ = n; $$ = n;
} }

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.158 2003/05/27 17:49:45 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.159 2003/05/28 16:03:55 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1169,6 +1169,10 @@ index_register(Oid heap,
newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo)); newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
memcpy(newind->il_info, indexInfo, sizeof(IndexInfo)); memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
/* expressions will likely be null, but may as well copy it */
newind->il_info->ii_Expressions = (List *)
copyObject(indexInfo->ii_Expressions);
newind->il_info->ii_ExpressionsState = NIL;
/* predicate will likely be null, but may as well copy it */ /* predicate will likely be null, but may as well copy it */
newind->il_info->ii_Predicate = (List *) newind->il_info->ii_Predicate = (List *)
copyObject(indexInfo->ii_Predicate); copyObject(indexInfo->ii_Predicate);

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/dependency.c,v 1.24 2003/05/27 17:49:45 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/catalog/dependency.c,v 1.25 2003/05/28 16:03:55 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -850,6 +850,91 @@ recordDependencyOnExpr(const ObjectAddress *depender,
term_object_addresses(&context.addrs); term_object_addresses(&context.addrs);
} }
/*
* recordDependencyOnSingleRelExpr - find expression dependencies
*
* As above, but only one relation is expected to be referenced (with
* varno = 1 and varlevelsup = 0). Pass the relation OID instead of a
* range table. An additional frammish is that dependencies on that
* relation (or its component columns) will be marked with 'self_behavior',
* whereas 'behavior' is used for everything else.
*/
void
recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
Node *expr, Oid relId,
DependencyType behavior,
DependencyType self_behavior)
{
find_expr_references_context context;
RangeTblEntry rte;
init_object_addresses(&context.addrs);
/* We gin up a rather bogus rangetable list to handle Vars */
MemSet(&rte, 0, sizeof(rte));
rte.type = T_RangeTblEntry;
rte.rtekind = RTE_RELATION;
rte.relid = relId;
context.rtables = makeList1(makeList1(&rte));
/* Scan the expression tree for referenceable objects */
find_expr_references_walker(expr, &context);
/* Remove any duplicates */
eliminate_duplicate_dependencies(&context.addrs);
/* Separate self-dependencies if necessary */
if (behavior != self_behavior && context.addrs.numrefs > 0)
{
ObjectAddresses self_addrs;
ObjectAddress *outobj;
int oldref,
outrefs;
init_object_addresses(&self_addrs);
outobj = context.addrs.refs;
outrefs = 0;
for (oldref = 0; oldref < context.addrs.numrefs; oldref++)
{
ObjectAddress *thisobj = context.addrs.refs + oldref;
if (thisobj->classId == RelOid_pg_class &&
thisobj->objectId == relId)
{
/* Move this ref into self_addrs */
add_object_address(OCLASS_CLASS, relId, thisobj->objectSubId,
&self_addrs);
}
else
{
/* Keep it in context.addrs */
outobj->classId = thisobj->classId;
outobj->objectId = thisobj->objectId;
outobj->objectSubId = thisobj->objectSubId;
outobj++;
outrefs++;
}
}
context.addrs.numrefs = outrefs;
/* Record the self-dependencies */
recordMultipleDependencies(depender,
self_addrs.refs, self_addrs.numrefs,
self_behavior);
term_object_addresses(&self_addrs);
}
/* Record the external dependencies */
recordMultipleDependencies(depender,
context.addrs.refs, context.addrs.numrefs,
behavior);
term_object_addresses(&context.addrs);
}
/* /*
* Recursively search an expression tree for object references. * Recursively search an expression tree for object references.
* *

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.244 2003/05/12 00:17:02 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.245 2003/05/28 16:03:55 tgl Exp $
* *
* *
* INTERFACE ROUTINES * INTERFACE ROUTINES
@ -1885,41 +1885,21 @@ RemoveStatistics(Relation rel, AttrNumber attnum)
static void static void
RelationTruncateIndexes(Oid heapId) RelationTruncateIndexes(Oid heapId)
{ {
Relation indexRelation; Relation heapRelation;
ScanKeyData entry; List *indlist;
SysScanDesc scan;
HeapTuple indexTuple;
/* Scan pg_index to find indexes on specified heap */ /*
indexRelation = heap_openr(IndexRelationName, AccessShareLock); * Open the heap rel. We need grab no lock because we assume
ScanKeyEntryInitialize(&entry, 0, * heap_truncate is holding an exclusive lock on the heap rel.
Anum_pg_index_indrelid, */
F_OIDEQ, heapRelation = heap_open(heapId, NoLock);
ObjectIdGetDatum(heapId));
scan = systable_beginscan(indexRelation, IndexIndrelidIndex, true,
SnapshotNow, 1, &entry);
while (HeapTupleIsValid(indexTuple = systable_getnext(scan))) /* Ask the relcache to produce a list of the indexes of the rel */
foreach(indlist, RelationGetIndexList(heapRelation))
{ {
Form_pg_index indexform = (Form_pg_index) GETSTRUCT(indexTuple); Oid indexId = lfirsto(indlist);
Oid indexId; Relation currentIndex;
IndexInfo *indexInfo; IndexInfo *indexInfo;
Relation heapRelation,
currentIndex;
/*
* For each index, fetch info needed for index_build
*/
indexId = indexform->indexrelid;
indexInfo = BuildIndexInfo(indexform);
/*
* We have to re-open the heap rel each time through this loop
* because index_build will close it again. We need grab no lock,
* however, because we assume heap_truncate is holding an
* exclusive lock on the heap rel.
*/
heapRelation = heap_open(heapId, NoLock);
/* Open the index relation */ /* Open the index relation */
currentIndex = index_open(indexId); currentIndex = index_open(indexId);
@ -1927,6 +1907,9 @@ RelationTruncateIndexes(Oid heapId)
/* Obtain exclusive lock on it, just to be sure */ /* Obtain exclusive lock on it, just to be sure */
LockRelation(currentIndex, AccessExclusiveLock); LockRelation(currentIndex, AccessExclusiveLock);
/* Fetch info needed for index_build */
indexInfo = BuildIndexInfo(currentIndex);
/* /*
* Drop any buffers associated with this index. If they're dirty, * Drop any buffers associated with this index. If they're dirty,
* they're just dropped without bothering to flush to disk. * they're just dropped without bothering to flush to disk.
@ -1943,13 +1926,14 @@ RelationTruncateIndexes(Oid heapId)
/* /*
* index_build will close both the heap and index relations (but * index_build will close both the heap and index relations (but
* not give up the locks we hold on them). * not give up the locks we hold on them). We're done with this
* index, but we must re-open the heap rel.
*/ */
heapRelation = heap_open(heapId, NoLock);
} }
/* Complete the scan and close pg_index */ /* Finish by closing the heap rel again */
systable_endscan(scan); heap_close(heapRelation, NoLock);
heap_close(indexRelation, AccessShareLock);
} }
/* /*

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.209 2003/02/19 04:06:28 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.210 2003/05/28 16:03:56 tgl Exp $
* *
* *
* INTERFACE ROUTINES * INTERFACE ROUTINES
@ -42,6 +42,7 @@
#include "miscadmin.h" #include "miscadmin.h"
#include "optimizer/clauses.h" #include "optimizer/clauses.h"
#include "optimizer/prep.h" #include "optimizer/prep.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h" #include "parser/parse_func.h"
#include "storage/sinval.h" #include "storage/sinval.h"
#include "storage/smgr.h" #include "storage/smgr.h"
@ -63,11 +64,9 @@
((natts) * AVG_ATTR_SIZE + MAXALIGN(sizeof(HeapTupleHeaderData)))) ((natts) * AVG_ATTR_SIZE + MAXALIGN(sizeof(HeapTupleHeaderData))))
/* non-export function prototypes */ /* non-export function prototypes */
static TupleDesc BuildFuncTupleDesc(Oid funcOid,
Oid *classObjectId);
static TupleDesc ConstructTupleDescriptor(Relation heapRelation, static TupleDesc ConstructTupleDescriptor(Relation heapRelation,
int numatts, AttrNumber *attNums, IndexInfo *indexInfo,
Oid *classObjectId); Oid *classObjectId);
static void UpdateRelationRelation(Relation indexRelation); static void UpdateRelationRelation(Relation indexRelation);
static void InitializeAttributeOids(Relation indexRelation, static void InitializeAttributeOids(Relation indexRelation,
int numatts, Oid indexoid); int numatts, Oid indexoid);
@ -98,95 +97,18 @@ IsReindexProcessing(void)
return reindexing; return reindexing;
} }
static TupleDesc /*
BuildFuncTupleDesc(Oid funcOid,
Oid *classObjectId)
{
TupleDesc funcTupDesc;
HeapTuple tuple;
Oid keyType;
Oid retType;
Form_pg_type typeTup;
/*
* Allocate and zero a tuple descriptor for a one-column tuple.
*/
funcTupDesc = CreateTemplateTupleDesc(1, false);
funcTupDesc->attrs[0] = (Form_pg_attribute) palloc0(ATTRIBUTE_TUPLE_SIZE);
/*
* Lookup the function to get its name and return type.
*/
tuple = SearchSysCache(PROCOID,
ObjectIdGetDatum(funcOid),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "Function %u does not exist", funcOid);
retType = ((Form_pg_proc) GETSTRUCT(tuple))->prorettype;
/*
* make the attributes name the same as the functions
*/
namestrcpy(&funcTupDesc->attrs[0]->attname,
NameStr(((Form_pg_proc) GETSTRUCT(tuple))->proname));
ReleaseSysCache(tuple);
/*
* Check the opclass to see if it provides a keytype (overriding the
* function result type).
*/
tuple = SearchSysCache(CLAOID,
ObjectIdGetDatum(classObjectId[0]),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "Opclass %u does not exist", classObjectId[0]);
keyType = ((Form_pg_opclass) GETSTRUCT(tuple))->opckeytype;
ReleaseSysCache(tuple);
if (!OidIsValid(keyType))
keyType = retType;
/*
* Lookup the key type in pg_type for the type length etc.
*/
tuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(keyType),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "Type %u does not exist", keyType);
typeTup = (Form_pg_type) GETSTRUCT(tuple);
/*
* Assign some of the attributes values. Leave the rest as 0.
*/
funcTupDesc->attrs[0]->attnum = 1;
funcTupDesc->attrs[0]->atttypid = keyType;
funcTupDesc->attrs[0]->attlen = typeTup->typlen;
funcTupDesc->attrs[0]->attbyval = typeTup->typbyval;
funcTupDesc->attrs[0]->attstorage = typeTup->typstorage;
funcTupDesc->attrs[0]->attalign = typeTup->typalign;
funcTupDesc->attrs[0]->attcacheoff = -1;
funcTupDesc->attrs[0]->atttypmod = -1;
funcTupDesc->attrs[0]->attislocal = true;
ReleaseSysCache(tuple);
return funcTupDesc;
}
/* ----------------------------------------------------------------
* ConstructTupleDescriptor * ConstructTupleDescriptor
* *
* Build an index tuple descriptor for a new index (plain not functional) * Build an index tuple descriptor for a new index
* ----------------------------------------------------------------
*/ */
static TupleDesc static TupleDesc
ConstructTupleDescriptor(Relation heapRelation, ConstructTupleDescriptor(Relation heapRelation,
int numatts, IndexInfo *indexInfo,
AttrNumber *attNums,
Oid *classObjectId) Oid *classObjectId)
{ {
int numatts = indexInfo->ii_NumIndexAttrs;
List *indexprs = indexInfo->ii_Expressions;
TupleDesc heapTupDesc; TupleDesc heapTupDesc;
TupleDesc indexTupDesc; TupleDesc indexTupDesc;
int natts; /* #atts in heap rel --- for error checks */ int natts; /* #atts in heap rel --- for error checks */
@ -198,70 +120,110 @@ ConstructTupleDescriptor(Relation heapRelation,
/* /*
* allocate the new tuple descriptor * allocate the new tuple descriptor
*/ */
indexTupDesc = CreateTemplateTupleDesc(numatts, false); indexTupDesc = CreateTemplateTupleDesc(numatts, false);
/* ---------------- /*
* for each attribute we are indexing, obtain its attribute * For simple index columns, we copy the pg_attribute row from the
* tuple form from either the static table of system attribute * parent relation and modify it as necessary. For expressions we
* tuple forms or the relation tuple descriptor * have to cons up a pg_attribute row the hard way.
* ----------------
*/ */
for (i = 0; i < numatts; i++) for (i = 0; i < numatts; i++)
{ {
AttrNumber atnum; /* attributeNumber[attributeOffset] */ AttrNumber atnum = indexInfo->ii_KeyAttrNumbers[i];
Form_pg_attribute from;
Form_pg_attribute to; Form_pg_attribute to;
HeapTuple tuple; HeapTuple tuple;
Form_pg_type typeTup;
Oid keyType; Oid keyType;
/* indexTupDesc->attrs[i] = to =
* get the attribute number and make sure it's valid; determine (Form_pg_attribute) palloc0(ATTRIBUTE_TUPLE_SIZE);
* which attribute descriptor to copy
*/
atnum = attNums[i];
if (!AttrNumberIsForUserDefinedAttr(atnum)) if (atnum != 0)
{ {
/* Simple index column */
Form_pg_attribute from;
if (atnum < 0)
{
/*
* here we are indexing on a system attribute (-1...-n)
*/
from = SystemAttributeDefinition(atnum,
heapRelation->rd_rel->relhasoids);
}
else
{
/*
* here we are indexing on a normal attribute (1...n)
*/
if (atnum > natts)
elog(ERROR, "cannot create index: column %d does not exist",
atnum);
from = heapTupDesc->attrs[AttrNumberGetAttrOffset(atnum)];
}
/* /*
* here we are indexing on a system attribute (-1...-n) * now that we've determined the "from", let's copy the tuple desc
* data...
*/ */
from = SystemAttributeDefinition(atnum, memcpy(to, from, ATTRIBUTE_TUPLE_SIZE);
heapRelation->rd_rel->relhasoids);
/*
* Fix the stuff that should not be the same as the underlying
* attr
*/
to->attnum = i + 1;
to->attstattarget = 0;
to->attcacheoff = -1;
to->attnotnull = false;
to->atthasdef = false;
to->attislocal = true;
to->attinhcount = 0;
} }
else else
{ {
/* Expressional index */
Node *indexkey;
if (indexprs == NIL)
elog(ERROR, "too few entries in indexprs list");
indexkey = (Node *) lfirst(indexprs);
indexprs = lnext(indexprs);
/* /*
* here we are indexing on a normal attribute (1...n) * Make the attribute's name "pg_expresssion_nnn" (maybe think
* of something better later)
*/ */
if (atnum > natts) sprintf(NameStr(to->attname), "pg_expression_%d", i + 1);
elog(ERROR, "cannot create index: column %d does not exist",
atnum);
from = heapTupDesc->attrs[AttrNumberGetAttrOffset(atnum)]; /*
* Lookup the expression type in pg_type for the type length etc.
*/
keyType = exprType(indexkey);
tuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(keyType),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "Type %u does not exist", keyType);
typeTup = (Form_pg_type) GETSTRUCT(tuple);
/*
* Assign some of the attributes values. Leave the rest as 0.
*/
to->attnum = i + 1;
to->atttypid = keyType;
to->attlen = typeTup->typlen;
to->attbyval = typeTup->typbyval;
to->attstorage = typeTup->typstorage;
to->attalign = typeTup->typalign;
to->attcacheoff = -1;
to->atttypmod = -1;
to->attislocal = true;
ReleaseSysCache(tuple);
} }
/*
* now that we've determined the "from", let's copy the tuple desc
* data...
*/
indexTupDesc->attrs[i] = to =
(Form_pg_attribute) palloc(ATTRIBUTE_TUPLE_SIZE);
memcpy(to, from, ATTRIBUTE_TUPLE_SIZE);
/*
* Fix the stuff that should not be the same as the underlying
* attr
*/
to->attnum = i + 1;
to->attstattarget = 0;
to->attcacheoff = -1;
to->attnotnull = false;
to->atthasdef = false;
to->attislocal = true;
to->attinhcount = 0;
/* /*
* We do not yet have the correct relation OID for the index, so * We do not yet have the correct relation OID for the index, so
* just set it invalid for now. InitializeAttributeOids() will * just set it invalid for now. InitializeAttributeOids() will
@ -284,8 +246,6 @@ ConstructTupleDescriptor(Relation heapRelation,
if (OidIsValid(keyType) && keyType != to->atttypid) if (OidIsValid(keyType) && keyType != to->atttypid)
{ {
/* index value and heap value have different types */ /* index value and heap value have different types */
Form_pg_type typeTup;
tuple = SearchSysCache(TYPEOID, tuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(keyType), ObjectIdGetDatum(keyType),
0, 0, 0); 0, 0, 0);
@ -421,6 +381,7 @@ UpdateIndexRelation(Oid indexoid,
{ {
int16 indkey[INDEX_MAX_KEYS]; int16 indkey[INDEX_MAX_KEYS];
Oid indclass[INDEX_MAX_KEYS]; Oid indclass[INDEX_MAX_KEYS];
Datum exprsDatum;
Datum predDatum; Datum predDatum;
Datum values[Natts_pg_index]; Datum values[Natts_pg_index];
char nulls[Natts_pg_index]; char nulls[Natts_pg_index];
@ -430,20 +391,32 @@ UpdateIndexRelation(Oid indexoid,
/* /*
* Copy the index key and opclass info into zero-filled vectors * Copy the index key and opclass info into zero-filled vectors
*
* (zero filling is essential 'cause we don't store the number of
* index columns explicitly in pg_index, which is pretty grotty...)
*/ */
MemSet(indkey, 0, sizeof(indkey)); MemSet(indkey, 0, sizeof(indkey));
for (i = 0; i < indexInfo->ii_NumKeyAttrs; i++)
indkey[i] = indexInfo->ii_KeyAttrNumbers[i];
MemSet(indclass, 0, sizeof(indclass)); MemSet(indclass, 0, sizeof(indclass));
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++) for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
{
indkey[i] = indexInfo->ii_KeyAttrNumbers[i];
indclass[i] = classOids[i]; indclass[i] = classOids[i];
}
/* /*
* Convert the index-predicate (if any) to a text datum * Convert the index expressions (if any) to a text datum
*/
if (indexInfo->ii_Expressions != NIL)
{
char *exprsString;
exprsString = nodeToString(indexInfo->ii_Expressions);
exprsDatum = DirectFunctionCall1(textin,
CStringGetDatum(exprsString));
pfree(exprsString);
}
else
exprsDatum = (Datum) 0;
/*
* Convert the index predicate (if any) to a text datum
*/ */
if (indexInfo->ii_Predicate != NIL) if (indexInfo->ii_Predicate != NIL)
{ {
@ -455,8 +428,7 @@ UpdateIndexRelation(Oid indexoid,
pfree(predString); pfree(predString);
} }
else else
predDatum = DirectFunctionCall1(textin, predDatum = (Datum) 0;
CStringGetDatum(""));
/* /*
* open the system catalog index relation * open the system catalog index relation
@ -470,14 +442,18 @@ UpdateIndexRelation(Oid indexoid,
values[Anum_pg_index_indexrelid - 1] = ObjectIdGetDatum(indexoid); values[Anum_pg_index_indexrelid - 1] = ObjectIdGetDatum(indexoid);
values[Anum_pg_index_indrelid - 1] = ObjectIdGetDatum(heapoid); values[Anum_pg_index_indrelid - 1] = ObjectIdGetDatum(heapoid);
values[Anum_pg_index_indproc - 1] = ObjectIdGetDatum(indexInfo->ii_FuncOid);
values[Anum_pg_index_indkey - 1] = PointerGetDatum(indkey); values[Anum_pg_index_indkey - 1] = PointerGetDatum(indkey);
values[Anum_pg_index_indclass - 1] = PointerGetDatum(indclass); values[Anum_pg_index_indclass - 1] = PointerGetDatum(indclass);
values[Anum_pg_index_indisclustered - 1] = BoolGetDatum(false); values[Anum_pg_index_indnatts - 1] = Int16GetDatum(indexInfo->ii_NumIndexAttrs);
values[Anum_pg_index_indisunique - 1] = BoolGetDatum(indexInfo->ii_Unique); values[Anum_pg_index_indisunique - 1] = BoolGetDatum(indexInfo->ii_Unique);
values[Anum_pg_index_indisprimary - 1] = BoolGetDatum(primary); values[Anum_pg_index_indisprimary - 1] = BoolGetDatum(primary);
values[Anum_pg_index_indreference - 1] = ObjectIdGetDatum(InvalidOid); values[Anum_pg_index_indisclustered - 1] = BoolGetDatum(false);
values[Anum_pg_index_indexprs - 1] = exprsDatum;
if (exprsDatum == (Datum) 0)
nulls[Anum_pg_index_indexprs - 1] = 'n';
values[Anum_pg_index_indpred - 1] = predDatum; values[Anum_pg_index_indpred - 1] = predDatum;
if (predDatum == (Datum) 0)
nulls[Anum_pg_index_indpred - 1] = 'n';
tuple = heap_formtuple(RelationGetDescr(pg_index), values, nulls); tuple = heap_formtuple(RelationGetDescr(pg_index), values, nulls);
@ -538,8 +514,7 @@ index_create(Oid heapRelationId,
/* /*
* check parameters * check parameters
*/ */
if (indexInfo->ii_NumIndexAttrs < 1 || if (indexInfo->ii_NumIndexAttrs < 1)
indexInfo->ii_NumKeyAttrs < 1)
elog(ERROR, "must index at least one column"); elog(ERROR, "must index at least one column");
if (!allow_system_table_mods && if (!allow_system_table_mods &&
@ -564,16 +539,9 @@ index_create(Oid heapRelationId,
/* /*
* construct tuple descriptor for index tuples * construct tuple descriptor for index tuples
*/ */
if (OidIsValid(indexInfo->ii_FuncOid)) indexTupDesc = ConstructTupleDescriptor(heapRelation,
indexTupDesc = BuildFuncTupleDesc(indexInfo->ii_FuncOid, indexInfo,
classObjectId); classObjectId);
else
indexTupDesc = ConstructTupleDescriptor(heapRelation,
indexInfo->ii_NumKeyAttrs,
indexInfo->ii_KeyAttrNumbers,
classObjectId);
indexTupDesc->tdhasoid = false;
/* /*
* create the index relation's relcache entry and physical disk file. * create the index relation's relcache entry and physical disk file.
@ -675,6 +643,10 @@ index_create(Oid heapRelationId,
constraintType = 0; /* keep compiler quiet */ constraintType = 0; /* keep compiler quiet */
} }
/* Shouldn't have any expressions */
if (indexInfo->ii_Expressions)
elog(ERROR, "constraints can't have index expressions");
conOid = CreateConstraintEntry(indexRelationName, conOid = CreateConstraintEntry(indexRelationName,
namespaceId, namespaceId,
constraintType, constraintType,
@ -682,7 +654,7 @@ index_create(Oid heapRelationId,
false, /* isDeferred */ false, /* isDeferred */
heapRelationId, heapRelationId,
indexInfo->ii_KeyAttrNumbers, indexInfo->ii_KeyAttrNumbers,
indexInfo->ii_NumKeyAttrs, indexInfo->ii_NumIndexAttrs,
InvalidOid, /* no domain */ InvalidOid, /* no domain */
InvalidOid, /* no foreign key */ InvalidOid, /* no foreign key */
NULL, NULL,
@ -703,13 +675,17 @@ index_create(Oid heapRelationId,
} }
else else
{ {
for (i = 0; i < indexInfo->ii_NumKeyAttrs; i++) /* Create auto dependencies on simply-referenced columns */
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
{ {
referenced.classId = RelOid_pg_class; if (indexInfo->ii_KeyAttrNumbers[i] != 0)
referenced.objectId = heapRelationId; {
referenced.objectSubId = indexInfo->ii_KeyAttrNumbers[i]; referenced.classId = RelOid_pg_class;
referenced.objectId = heapRelationId;
referenced.objectSubId = indexInfo->ii_KeyAttrNumbers[i];
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO); recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
}
} }
} }
@ -723,14 +699,24 @@ index_create(Oid heapRelationId,
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
} }
/* Store the dependency on the function (if appropriate) */ /* Store dependencies on anything mentioned in index expressions */
if (OidIsValid(indexInfo->ii_FuncOid)) if (indexInfo->ii_Expressions)
{ {
referenced.classId = RelOid_pg_proc; recordDependencyOnSingleRelExpr(&myself,
referenced.objectId = indexInfo->ii_FuncOid; (Node *) indexInfo->ii_Expressions,
referenced.objectSubId = 0; heapRelationId,
DEPENDENCY_NORMAL,
DEPENDENCY_AUTO);
}
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); /* Store dependencies on anything mentioned in predicate */
if (indexInfo->ii_Predicate)
{
recordDependencyOnSingleRelExpr(&myself,
(Node *) indexInfo->ii_Predicate,
heapRelationId,
DEPENDENCY_NORMAL,
DEPENDENCY_AUTO);
} }
} }
@ -864,7 +850,7 @@ index_drop(Oid indexId)
/* ---------------- /* ----------------
* BuildIndexInfo * BuildIndexInfo
* Construct an IndexInfo record given the index's pg_index tuple * Construct an IndexInfo record for an open index
* *
* IndexInfo stores the information about the index that's needed by * IndexInfo stores the information about the index that's needed by
* FormIndexDatum, which is used for both index_build() and later insertion * FormIndexDatum, which is used for both index_build() and later insertion
@ -873,62 +859,31 @@ index_drop(Oid indexId)
* ---------------- * ----------------
*/ */
IndexInfo * IndexInfo *
BuildIndexInfo(Form_pg_index indexStruct) BuildIndexInfo(Relation index)
{ {
IndexInfo *ii = makeNode(IndexInfo); IndexInfo *ii = makeNode(IndexInfo);
Form_pg_index indexStruct = index->rd_index;
int i; int i;
int numKeys; int numKeys;
/* /* check the number of keys, and copy attr numbers into the IndexInfo */
* count the number of keys, and copy them into the IndexInfo numKeys = indexStruct->indnatts;
*/ if (numKeys < 1 || numKeys > INDEX_MAX_KEYS)
numKeys = 0; elog(ERROR, "invalid indnatts %d for index %u",
for (i = 0; i < INDEX_MAX_KEYS && numKeys, RelationGetRelid(index));
indexStruct->indkey[i] != InvalidAttrNumber; i++) ii->ii_NumIndexAttrs = numKeys;
{ for (i = 0; i < numKeys; i++)
ii->ii_KeyAttrNumbers[i] = indexStruct->indkey[i]; ii->ii_KeyAttrNumbers[i] = indexStruct->indkey[i];
numKeys++;
}
ii->ii_NumKeyAttrs = numKeys;
/* /* fetch any expressions needed for expressional indexes */
* Handle functional index. ii->ii_Expressions = RelationGetIndexExpressions(index);
* ii->ii_ExpressionsState = NIL;
* If we have a functional index then the number of attributes defined in
* the index must be 1 (the function's single return value). Otherwise
* it's same as number of keys.
*/
ii->ii_FuncOid = indexStruct->indproc;
if (OidIsValid(indexStruct->indproc)) /* fetch index predicate if any */
{ ii->ii_Predicate = RelationGetIndexPredicate(index);
ii->ii_NumIndexAttrs = 1; ii->ii_PredicateState = NIL;
/* Do a lookup on the function, too */
fmgr_info(indexStruct->indproc, &ii->ii_FuncInfo);
}
else
ii->ii_NumIndexAttrs = numKeys;
/* /* other info */
* If partial index, convert predicate into expression nodetree
*/
if (VARSIZE(&indexStruct->indpred) > VARHDRSZ)
{
char *predString;
predString = DatumGetCString(DirectFunctionCall1(textout,
PointerGetDatum(&indexStruct->indpred)));
ii->ii_Predicate = stringToNode(predString);
ii->ii_PredicateState = NIL;
pfree(predString);
}
else
{
ii->ii_Predicate = NIL;
ii->ii_PredicateState = NIL;
}
/* Other info */
ii->ii_Unique = indexStruct->indisunique; ii->ii_Unique = indexStruct->indisunique;
return ii; return ii;
@ -941,10 +896,14 @@ BuildIndexInfo(Form_pg_index indexStruct)
* indexInfo Info about the index * indexInfo Info about the index
* heapTuple Heap tuple for which we must prepare an index entry * heapTuple Heap tuple for which we must prepare an index entry
* heapDescriptor tupledesc for heap tuple * heapDescriptor tupledesc for heap tuple
* resultCxt Temporary memory context for any palloc'd datums created * estate executor state for evaluating any index expressions
* datum Array of index Datums (output area) * datum Array of index Datums (output area)
* nullv Array of is-null indicators (output area) * nullv Array of is-null indicators (output area)
* *
* When there are no index expressions, estate may be NULL. Otherwise it
* must be supplied, *and* the ecxt_scantuple slot of its per-tuple expr
* context must point to the heap tuple passed in.
*
* For largely historical reasons, we don't actually call index_formtuple() * For largely historical reasons, we don't actually call index_formtuple()
* here, we just prepare its input arrays datum[] and nullv[]. * here, we just prepare its input arrays datum[] and nullv[].
* ---------------- * ----------------
@ -953,69 +912,58 @@ void
FormIndexDatum(IndexInfo *indexInfo, FormIndexDatum(IndexInfo *indexInfo,
HeapTuple heapTuple, HeapTuple heapTuple,
TupleDesc heapDescriptor, TupleDesc heapDescriptor,
MemoryContext resultCxt, EState *estate,
Datum *datum, Datum *datum,
char *nullv) char *nullv)
{ {
MemoryContext oldContext; List *indexprs;
int i; int i;
Datum iDatum;
bool isNull;
oldContext = MemoryContextSwitchTo(resultCxt); if (indexInfo->ii_Expressions != NIL &&
indexInfo->ii_ExpressionsState == NIL)
if (OidIsValid(indexInfo->ii_FuncOid))
{ {
/* /* First time through, set up expression evaluation state */
* Functional index --- compute the single index attribute indexInfo->ii_ExpressionsState = (List *)
*/ ExecPrepareExpr((Expr *) indexInfo->ii_Expressions,
FunctionCallInfoData fcinfo; estate);
bool anynull = false; /* Check caller has set up context correctly */
Assert(GetPerTupleExprContext(estate)->ecxt_scantuple->val == heapTuple);
}
indexprs = indexInfo->ii_ExpressionsState;
MemSet(&fcinfo, 0, sizeof(fcinfo)); for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
fcinfo.flinfo = &indexInfo->ii_FuncInfo; {
fcinfo.nargs = indexInfo->ii_NumKeyAttrs; int keycol = indexInfo->ii_KeyAttrNumbers[i];
Datum iDatum;
bool isNull;
for (i = 0; i < indexInfo->ii_NumKeyAttrs; i++) if (keycol != 0)
{ {
fcinfo.arg[i] = heap_getattr(heapTuple, /*
indexInfo->ii_KeyAttrNumbers[i], * Plain index column; get the value we need directly from the
heapDescriptor, * heap tuple.
&fcinfo.argnull[i]); */
anynull |= fcinfo.argnull[i]; iDatum = heap_getattr(heapTuple, keycol, heapDescriptor, &isNull);
}
if (indexInfo->ii_FuncInfo.fn_strict && anynull)
{
/* force a null result for strict function */
iDatum = (Datum) 0;
isNull = true;
} }
else else
{ {
iDatum = FunctionCallInvoke(&fcinfo); /*
isNull = fcinfo.isnull; * Index expression --- need to evaluate it.
} */
datum[0] = iDatum; if (indexprs == NIL)
nullv[0] = (isNull) ? 'n' : ' '; elog(ERROR, "wrong number of index expressions");
} iDatum = ExecEvalExprSwitchContext((ExprState *) lfirst(indexprs),
else GetPerTupleExprContext(estate),
{ &isNull,
/* NULL);
* Plain index --- for each attribute we need from the heap tuple, indexprs = lnext(indexprs);
* get the attribute and stick it into the datum and nullv arrays.
*/
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
{
iDatum = heap_getattr(heapTuple,
indexInfo->ii_KeyAttrNumbers[i],
heapDescriptor,
&isNull);
datum[i] = iDatum;
nullv[i] = (isNull) ? 'n' : ' ';
} }
datum[i] = iDatum;
nullv[i] = (isNull) ? 'n' : ' ';
} }
MemoryContextSwitchTo(oldContext); if (indexprs != NIL)
elog(ERROR, "wrong number of index expressions");
} }
@ -1501,7 +1449,7 @@ IndexBuildHeapScan(Relation heapRelation,
heapDescriptor = RelationGetDescr(heapRelation); heapDescriptor = RelationGetDescr(heapRelation);
/* /*
* Need an EState for evaluation of functional-index functions * Need an EState for evaluation of index expressions
* and partial-index predicates. * and partial-index predicates.
*/ */
estate = CreateExecutorState(); estate = CreateExecutorState();
@ -1510,9 +1458,9 @@ IndexBuildHeapScan(Relation heapRelation,
/* /*
* If this is a predicate (partial) index, we will need to evaluate * If this is a predicate (partial) index, we will need to evaluate
* the predicate using ExecQual, which requires the current tuple to * the predicate using ExecQual, which requires the current tuple to
* be in a slot of a TupleTable. * be in a slot of a TupleTable. Likewise if there are any expressions.
*/ */
if (indexInfo->ii_Predicate != NIL) if (indexInfo->ii_Predicate != NIL || indexInfo->ii_Expressions != NIL)
{ {
tupleTable = ExecCreateTupleTable(1); tupleTable = ExecCreateTupleTable(1);
slot = ExecAllocTableSlot(tupleTable); slot = ExecAllocTableSlot(tupleTable);
@ -1521,11 +1469,7 @@ IndexBuildHeapScan(Relation heapRelation,
/* Arrange for econtext's scan tuple to be the tuple under test */ /* Arrange for econtext's scan tuple to be the tuple under test */
econtext->ecxt_scantuple = slot; econtext->ecxt_scantuple = slot;
/* /* Set up execution state for predicate. */
* Set up execution state for predicate. Note: we mustn't attempt to
* cache this in the indexInfo, since we're building it in a transient
* EState.
*/
predicate = (List *) predicate = (List *)
ExecPrepareExpr((Expr *) indexInfo->ii_Predicate, ExecPrepareExpr((Expr *) indexInfo->ii_Predicate,
estate); estate);
@ -1676,12 +1620,12 @@ IndexBuildHeapScan(Relation heapRelation,
/* /*
* For the current heap tuple, extract all the attributes we use * For the current heap tuple, extract all the attributes we use
* in this index, and note which are null. This also performs * in this index, and note which are null. This also performs
* evaluation of the function, if this is a functional index. * evaluation of any expressions needed.
*/ */
FormIndexDatum(indexInfo, FormIndexDatum(indexInfo,
heapTuple, heapTuple,
heapDescriptor, heapDescriptor,
econtext->ecxt_per_tuple_memory, estate,
attdata, attdata,
nulls); nulls);
@ -1703,6 +1647,10 @@ IndexBuildHeapScan(Relation heapRelation,
FreeExecutorState(estate); FreeExecutorState(estate);
/* These may have been pointing to the now-gone estate */
indexInfo->ii_ExpressionsState = NIL;
indexInfo->ii_PredicateState = NIL;
return reltuples; return reltuples;
} }
@ -1807,7 +1755,7 @@ reindex_index(Oid indexId, bool force, bool inplace)
} }
/* Fetch info needed for index_build */ /* Fetch info needed for index_build */
indexInfo = BuildIndexInfo(iRel->rd_index); indexInfo = BuildIndexInfo(iRel);
if (inplace) if (inplace)
{ {

View File

@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.102 2002/09/04 20:31:14 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.103 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -30,11 +30,9 @@
* In the current implementation, we share code for opening/closing the * In the current implementation, we share code for opening/closing the
* indexes with execUtils.c. But we do not use ExecInsertIndexTuples, * indexes with execUtils.c. But we do not use ExecInsertIndexTuples,
* because we don't want to create an EState. This implies that we * because we don't want to create an EState. This implies that we
* do not support partial indexes on system catalogs. Nor do we handle * do not support partial or expressional indexes on system catalogs.
* functional indexes very well (the code will work, but will leak memory * This could be fixed with localized changes here if we wanted to pay
* intraquery, because the index function is called in the per-query context * the extra overhead of building an EState.
* that we are invoked in). This could be fixed with localized changes here
* if we wanted to pay the extra overhead of building an EState.
*/ */
CatalogIndexState CatalogIndexState
CatalogOpenIndexes(Relation heapRel) CatalogOpenIndexes(Relation heapRel)
@ -99,7 +97,11 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
indexInfo = indexInfoArray[i]; indexInfo = indexInfoArray[i];
/* Partial indexes on system catalogs are not supported */ /*
* Expressional and partial indexes on system catalogs are not
* supported
*/
Assert(indexInfo->ii_Expressions == NIL);
Assert(indexInfo->ii_Predicate == NIL); Assert(indexInfo->ii_Predicate == NIL);
/* /*
@ -109,7 +111,7 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
FormIndexDatum(indexInfo, FormIndexDatum(indexInfo,
heapTuple, heapTuple,
heapDescriptor, heapDescriptor,
CurrentMemoryContext, NULL, /* no expression eval to do */
datum, datum,
nullv); nullv);

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_constraint.c,v 1.12 2002/12/12 20:35:11 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/catalog/pg_constraint.c,v 1.13 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -250,18 +250,11 @@ CreateConstraintEntry(const char *constraintName,
{ {
/* /*
* Register dependencies from constraint to objects mentioned in * Register dependencies from constraint to objects mentioned in
* CHECK expression. We gin up a rather bogus rangetable list to * CHECK expression.
* handle any Vars in the constraint.
*/ */
RangeTblEntry rte; recordDependencyOnSingleRelExpr(&conobject, conExpr, relId,
DEPENDENCY_NORMAL,
MemSet(&rte, 0, sizeof(rte)); DEPENDENCY_NORMAL);
rte.type = T_RangeTblEntry;
rte.rtekind = RTE_RELATION;
rte.relid = relId;
recordDependencyOnExpr(&conobject, conExpr, makeList1(&rte),
DEPENDENCY_NORMAL);
} }
return conOid; return conOid;

View File

@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.109 2003/05/14 03:26:01 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.110 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -214,7 +214,7 @@ cluster(ClusterStmt *stmt)
/* Start a new transaction for each relation. */ /* Start a new transaction for each relation. */
StartTransactionCommand(); StartTransactionCommand();
SetQuerySnapshot(); /* might be needed for functional index */ SetQuerySnapshot(); /* might be needed for functions in indexes */
cluster_rel(rvtc, true); cluster_rel(rvtc, true);
CommitTransactionCommand(); CommitTransactionCommand();
} }
@ -320,7 +320,7 @@ cluster_rel(RelToCluster *rvtc, bool recheck)
* seqscan pass over the table to copy the missing rows, but that seems * seqscan pass over the table to copy the missing rows, but that seems
* expensive and tedious. * expensive and tedious.
*/ */
if (VARSIZE(&OldIndex->rd_index->indpred) > VARHDRSZ) /* partial? */ if (!heap_attisnull(OldIndex->rd_indextuple, Anum_pg_index_indpred))
elog(ERROR, "CLUSTER: cannot cluster on partial index"); elog(ERROR, "CLUSTER: cannot cluster on partial index");
if (!OldIndex->rd_am->amindexnulls) if (!OldIndex->rd_am->amindexnulls)
{ {
@ -332,14 +332,24 @@ cluster_rel(RelToCluster *rvtc, bool recheck)
* at the first column; multicolumn-capable AMs are *required* to * at the first column; multicolumn-capable AMs are *required* to
* index nulls in columns after the first. * index nulls in columns after the first.
*/ */
if (OidIsValid(OldIndex->rd_index->indproc))
elog(ERROR, "CLUSTER: cannot cluster on functional index when index access method does not handle nulls");
colno = OldIndex->rd_index->indkey[0]; colno = OldIndex->rd_index->indkey[0];
if (colno > 0) /* system columns are non-null */ if (colno > 0)
{
/* ordinary user attribute */
if (!OldHeap->rd_att->attrs[colno - 1]->attnotnull) if (!OldHeap->rd_att->attrs[colno - 1]->attnotnull)
elog(ERROR, "CLUSTER: cannot cluster when index access method does not handle nulls" elog(ERROR, "CLUSTER: cannot cluster when index access method does not handle nulls"
"\n\tYou may be able to work around this by marking column \"%s\" NOT NULL", "\n\tYou may be able to work around this by marking column \"%s\" NOT NULL",
NameStr(OldHeap->rd_att->attrs[colno - 1]->attname)); NameStr(OldHeap->rd_att->attrs[colno - 1]->attname));
}
else if (colno < 0)
{
/* system column --- okay, always non-null */
}
else
{
/* index expression, lose... */
elog(ERROR, "CLUSTER: cannot cluster on expressional index when index access method does not handle nulls");
}
} }
/* /*
@ -557,43 +567,24 @@ get_indexattr_list(Relation OldHeap, Oid OldIndex)
foreach(indlist, RelationGetIndexList(OldHeap)) foreach(indlist, RelationGetIndexList(OldHeap))
{ {
Oid indexOID = lfirsto(indlist); Oid indexOID = lfirsto(indlist);
HeapTuple indexTuple; Relation oldIndex;
HeapTuple classTuple;
Form_pg_index indexForm;
Form_pg_class classForm;
IndexAttrs *attrs; IndexAttrs *attrs;
indexTuple = SearchSysCache(INDEXRELID, oldIndex = index_open(indexOID);
ObjectIdGetDatum(indexOID),
0, 0, 0);
if (!HeapTupleIsValid(indexTuple))
elog(ERROR, "Cache lookup failed for index %u", indexOID);
indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
Assert(indexForm->indexrelid == indexOID);
attrs = (IndexAttrs *) palloc(sizeof(IndexAttrs)); attrs = (IndexAttrs *) palloc(sizeof(IndexAttrs));
attrs->indexOID = indexOID; attrs->indexOID = indexOID;
attrs->indexInfo = BuildIndexInfo(indexForm); attrs->indexName = pstrdup(NameStr(oldIndex->rd_rel->relname));
attrs->accessMethodOID = oldIndex->rd_rel->relam;
attrs->indexInfo = BuildIndexInfo(oldIndex);
attrs->classOID = (Oid *) attrs->classOID = (Oid *)
palloc(sizeof(Oid) * attrs->indexInfo->ii_NumIndexAttrs); palloc(sizeof(Oid) * attrs->indexInfo->ii_NumIndexAttrs);
memcpy(attrs->classOID, indexForm->indclass, memcpy(attrs->classOID, oldIndex->rd_index->indclass,
sizeof(Oid) * attrs->indexInfo->ii_NumIndexAttrs); sizeof(Oid) * attrs->indexInfo->ii_NumIndexAttrs);
/* We adjust the isclustered attribute to correct new state */ /* We adjust the isclustered attribute to correct new state */
attrs->isclustered = (indexOID == OldIndex); attrs->isclustered = (indexOID == OldIndex);
/* Name and access method of each index come from pg_class */ index_close(oldIndex);
classTuple = SearchSysCache(RELOID,
ObjectIdGetDatum(indexOID),
0, 0, 0);
if (!HeapTupleIsValid(classTuple))
elog(ERROR, "Cache lookup failed for index %u", indexOID);
classForm = (Form_pg_class) GETSTRUCT(classTuple);
attrs->indexName = pstrdup(NameStr(classForm->relname));
attrs->accessMethodOID = classForm->relam;
ReleaseSysCache(classTuple);
ReleaseSysCache(indexTuple);
/* /*
* Cons the gathered data into the list. We do not care about * Cons the gathered data into the list. We do not care about

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.99 2003/05/14 03:26:01 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.100 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -32,6 +32,7 @@
#include "optimizer/prep.h" #include "optimizer/prep.h"
#include "parser/parsetree.h" #include "parser/parsetree.h"
#include "parser/parse_coerce.h" #include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h" #include "parser/parse_func.h"
#include "utils/acl.h" #include "utils/acl.h"
#include "utils/builtins.h" #include "utils/builtins.h"
@ -39,19 +40,13 @@
#include "utils/syscache.h" #include "utils/syscache.h"
#define IsFuncIndex(ATTR_LIST) (((IndexElem*)lfirst(ATTR_LIST))->funcname != NIL)
/* non-export function prototypes */ /* non-export function prototypes */
static void CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid); static void CheckPredicate(List *predList);
static void FuncIndexArgs(IndexInfo *indexInfo, Oid *classOidP, static void ComputeIndexAttrs(IndexInfo *indexInfo, Oid *classOidP,
IndexElem *funcIndex,
Oid relId,
char *accessMethodName, Oid accessMethodId);
static void NormIndexAttrs(IndexInfo *indexInfo, Oid *classOidP,
List *attList, List *attList,
Oid relId, Oid relId,
char *accessMethodName, Oid accessMethodId); char *accessMethodName, Oid accessMethodId);
static Oid GetAttrOpClass(IndexElem *attribute, Oid attrType, static Oid GetIndexOpClass(List *opclass, Oid attrType,
char *accessMethodName, Oid accessMethodId); char *accessMethodName, Oid accessMethodId);
static Oid GetDefaultOpClass(Oid attrType, Oid accessMethodId); static Oid GetDefaultOpClass(Oid attrType, Oid accessMethodId);
@ -59,8 +54,8 @@ static Oid GetDefaultOpClass(Oid attrType, Oid accessMethodId);
* DefineIndex * DefineIndex
* Creates a new index. * Creates a new index.
* *
* 'attributeList' is a list of IndexElem specifying either a functional * 'attributeList' is a list of IndexElem specifying columns and expressions
* index or a list of attributes to index on. * to index on.
* 'predicate' is the qual specified in the where clause. * 'predicate' is the qual specified in the where clause.
* 'rangetable' is needed to interpret the predicate. * 'rangetable' is needed to interpret the predicate.
*/ */
@ -155,6 +150,16 @@ DefineIndex(RangeVar *heapRelation,
ReleaseSysCache(tuple); ReleaseSysCache(tuple);
/*
* If a range table was created then check that only the base rel is
* mentioned.
*/
if (rangetable != NIL)
{
if (length(rangetable) != 1 || getrelid(1, rangetable) != relationId)
elog(ERROR, "index expressions and predicates may refer only to the base relation");
}
/* /*
* Convert the partial-index predicate from parsetree form to an * Convert the partial-index predicate from parsetree form to an
* implicit-AND qual expression, for easier evaluation at runtime. * implicit-AND qual expression, for easier evaluation at runtime.
@ -164,14 +169,14 @@ DefineIndex(RangeVar *heapRelation,
if (predicate) if (predicate)
{ {
cnfPred = canonicalize_qual((Expr *) copyObject(predicate), true); cnfPred = canonicalize_qual((Expr *) copyObject(predicate), true);
CheckPredicate(cnfPred, rangetable, relationId); CheckPredicate(cnfPred);
} }
/* /*
* Check that all of the attributes in a primary key are marked * Check that all of the attributes in a primary key are marked
* as not null, otherwise attempt to ALTER TABLE .. SET NOT NULL * as not null, otherwise attempt to ALTER TABLE .. SET NOT NULL
*/ */
if (primary && !IsFuncIndex(attributeList)) if (primary)
{ {
List *keys; List *keys;
@ -180,6 +185,9 @@ DefineIndex(RangeVar *heapRelation,
IndexElem *key = (IndexElem *) lfirst(keys); IndexElem *key = (IndexElem *) lfirst(keys);
HeapTuple atttuple; HeapTuple atttuple;
if (!key->name)
elog(ERROR, "primary keys cannot be expressions");
/* System attributes are never null, so no problem */ /* System attributes are never null, so no problem */
if (SystemAttributeByName(key->name, rel->rd_rel->relhasoids)) if (SystemAttributeByName(key->name, rel->rd_rel->relhasoids))
continue; continue;
@ -216,43 +224,16 @@ DefineIndex(RangeVar *heapRelation,
* structure * structure
*/ */
indexInfo = makeNode(IndexInfo); indexInfo = makeNode(IndexInfo);
indexInfo->ii_NumIndexAttrs = numberOfAttributes;
indexInfo->ii_Expressions = NIL; /* for now */
indexInfo->ii_ExpressionsState = NIL;
indexInfo->ii_Predicate = cnfPred; indexInfo->ii_Predicate = cnfPred;
indexInfo->ii_PredicateState = NIL; indexInfo->ii_PredicateState = NIL;
indexInfo->ii_FuncOid = InvalidOid;
indexInfo->ii_Unique = unique; indexInfo->ii_Unique = unique;
if (IsFuncIndex(attributeList)) classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
{ ComputeIndexAttrs(indexInfo, classObjectId, attributeList,
IndexElem *funcIndex = (IndexElem *) lfirst(attributeList);
int nargs;
/* Parser should have given us only one list item, but check */
if (numberOfAttributes != 1)
elog(ERROR, "Functional index can only have one attribute");
nargs = length(funcIndex->args);
if (nargs > INDEX_MAX_KEYS)
elog(ERROR, "Index function can take at most %d arguments",
INDEX_MAX_KEYS);
indexInfo->ii_NumIndexAttrs = 1;
indexInfo->ii_NumKeyAttrs = nargs;
classObjectId = (Oid *) palloc(sizeof(Oid));
FuncIndexArgs(indexInfo, classObjectId, funcIndex,
relationId, accessMethodName, accessMethodId); relationId, accessMethodName, accessMethodId);
}
else
{
indexInfo->ii_NumIndexAttrs = numberOfAttributes;
indexInfo->ii_NumKeyAttrs = numberOfAttributes;
classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
NormIndexAttrs(indexInfo, classObjectId, attributeList,
relationId, accessMethodName, accessMethodId);
}
index_create(relationId, indexRelationName, index_create(relationId, indexRelationName,
indexInfo, accessMethodId, classObjectId, indexInfo, accessMethodId, classObjectId,
@ -271,8 +252,7 @@ DefineIndex(RangeVar *heapRelation,
/* /*
* CheckPredicate * CheckPredicate
* Checks that the given list of partial-index predicates refer * Checks that the given list of partial-index predicates is valid.
* (via the given range table) only to the given base relation oid.
* *
* This used to also constrain the form of the predicate to forms that * This used to also constrain the form of the predicate to forms that
* indxpath.c could do something with. However, that seems overly * indxpath.c could do something with. However, that seems overly
@ -281,14 +261,9 @@ DefineIndex(RangeVar *heapRelation,
* any evaluatable predicate will work. So accept any predicate here * any evaluatable predicate will work. So accept any predicate here
* (except ones requiring a plan), and let indxpath.c fend for itself. * (except ones requiring a plan), and let indxpath.c fend for itself.
*/ */
static void static void
CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid) CheckPredicate(List *predList)
{ {
if (length(rangeTable) != 1 || getrelid(1, rangeTable) != baseRelOid)
elog(ERROR,
"Partial-index predicates may refer only to the base relation");
/* /*
* We don't currently support generation of an actual query plan for a * We don't currently support generation of an actual query plan for a
* predicate, only simple scalar expressions; hence these * predicate, only simple scalar expressions; hence these
@ -301,119 +276,19 @@ CheckPredicate(List *predList, List *rangeTable, Oid baseRelOid)
/* /*
* A predicate using mutable functions is probably wrong, for the same * A predicate using mutable functions is probably wrong, for the same
* reasons that we don't allow a functional index to use one. * reasons that we don't allow an index expression to use one.
*/ */
if (contain_mutable_functions((Node *) predList)) if (contain_mutable_functions((Node *) predList))
elog(ERROR, "Functions in index predicate must be marked IMMUTABLE"); elog(ERROR, "Functions in index predicate must be marked IMMUTABLE");
} }
static void static void
FuncIndexArgs(IndexInfo *indexInfo, ComputeIndexAttrs(IndexInfo *indexInfo,
Oid *classOidP, Oid *classOidP,
IndexElem *funcIndex, List *attList, /* list of IndexElem's */
Oid relId, Oid relId,
char *accessMethodName, char *accessMethodName,
Oid accessMethodId) Oid accessMethodId)
{
Oid argTypes[FUNC_MAX_ARGS];
List *arglist;
int nargs = 0;
int i;
FuncDetailCode fdresult;
Oid funcid;
Oid rettype;
bool retset;
Oid *true_typeids;
/*
* process the function arguments, which are a list of T_String
* (someday ought to allow more general expressions?)
*
* Note caller already checked that list is not too long.
*/
MemSet(argTypes, 0, sizeof(argTypes));
foreach(arglist, funcIndex->args)
{
char *arg = strVal(lfirst(arglist));
HeapTuple tuple;
Form_pg_attribute att;
tuple = SearchSysCacheAttName(relId, arg);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "DefineIndex: column \"%s\" named in key does not exist", arg);
att = (Form_pg_attribute) GETSTRUCT(tuple);
indexInfo->ii_KeyAttrNumbers[nargs] = att->attnum;
argTypes[nargs] = att->atttypid;
ReleaseSysCache(tuple);
nargs++;
}
/*
* Lookup the function procedure to get its OID and result type.
*
* We rely on parse_func.c to find the correct function in the possible
* presence of binary-compatible types. However, parse_func may do
* too much: it will accept a function that requires run-time coercion
* of input types, and the executor is not currently set up to support
* that. So, check to make sure that the selected function has
* exact-match or binary-compatible input types.
*/
fdresult = func_get_detail(funcIndex->funcname, funcIndex->args,
nargs, argTypes,
&funcid, &rettype, &retset,
&true_typeids);
if (fdresult != FUNCDETAIL_NORMAL)
{
if (fdresult == FUNCDETAIL_AGGREGATE)
elog(ERROR, "DefineIndex: functional index may not use an aggregate function");
else if (fdresult == FUNCDETAIL_COERCION)
elog(ERROR, "DefineIndex: functional index must use a real function, not a type coercion"
"\n\tTry specifying the index opclass you want to use, instead");
else
func_error("DefineIndex", funcIndex->funcname, nargs, argTypes,
NULL);
}
if (retset)
elog(ERROR, "DefineIndex: cannot index on a function returning a set");
for (i = 0; i < nargs; i++)
{
if (!IsBinaryCoercible(argTypes[i], true_typeids[i]))
func_error("DefineIndex", funcIndex->funcname, nargs, true_typeids,
"Index function must be binary-compatible with table datatype");
}
/*
* Require that the function be marked immutable. Using a mutable
* function for a functional index is highly questionable, since if
* you aren't going to get the same result for the same data every
* time, it's not clear what the index entries mean at all.
*/
if (func_volatile(funcid) != PROVOLATILE_IMMUTABLE)
elog(ERROR, "DefineIndex: index function must be marked IMMUTABLE");
/* Process opclass, using func return type as default type */
classOidP[0] = GetAttrOpClass(funcIndex, rettype,
accessMethodName, accessMethodId);
/* OK, return results */
indexInfo->ii_FuncOid = funcid;
/* Need to do the fmgr function lookup now, too */
fmgr_info(funcid, &indexInfo->ii_FuncInfo);
}
static void
NormIndexAttrs(IndexInfo *indexInfo,
Oid *classOidP,
List *attList, /* list of IndexElem's */
Oid relId,
char *accessMethodName,
Oid accessMethodId)
{ {
List *rest; List *rest;
int attn = 0; int attn = 0;
@ -424,31 +299,75 @@ NormIndexAttrs(IndexInfo *indexInfo,
foreach(rest, attList) foreach(rest, attList)
{ {
IndexElem *attribute = (IndexElem *) lfirst(rest); IndexElem *attribute = (IndexElem *) lfirst(rest);
HeapTuple atttuple; Oid atttype;
Form_pg_attribute attform;
if (attribute->name == NULL) if (attribute->name != NULL)
elog(ERROR, "missing attribute for define index"); {
/* Simple index attribute */
HeapTuple atttuple;
Form_pg_attribute attform;
atttuple = SearchSysCacheAttName(relId, attribute->name); Assert(attribute->expr == NULL);
if (!HeapTupleIsValid(atttuple)) atttuple = SearchSysCacheAttName(relId, attribute->name);
elog(ERROR, "DefineIndex: attribute \"%s\" not found", if (!HeapTupleIsValid(atttuple))
attribute->name); elog(ERROR, "DefineIndex: attribute \"%s\" not found",
attform = (Form_pg_attribute) GETSTRUCT(atttuple); attribute->name);
attform = (Form_pg_attribute) GETSTRUCT(atttuple);
indexInfo->ii_KeyAttrNumbers[attn] = attform->attnum;
atttype = attform->atttypid;
ReleaseSysCache(atttuple);
}
else if (attribute->expr && IsA(attribute->expr, Var))
{
/* Tricky tricky, he wrote (column) ... treat as simple attr */
Var *var = (Var *) attribute->expr;
indexInfo->ii_KeyAttrNumbers[attn] = attform->attnum; indexInfo->ii_KeyAttrNumbers[attn] = var->varattno;
atttype = get_atttype(relId, var->varattno);
}
else
{
/* Index expression */
Assert(attribute->expr != NULL);
indexInfo->ii_KeyAttrNumbers[attn] = 0; /* marks expression */
indexInfo->ii_Expressions = lappend(indexInfo->ii_Expressions,
attribute->expr);
atttype = exprType(attribute->expr);
classOidP[attn] = GetAttrOpClass(attribute, attform->atttypid, /*
accessMethodName, accessMethodId); * We don't currently support generation of an actual query plan
* for an index expression, only simple scalar expressions;
* hence these restrictions.
*/
if (contain_subplans(attribute->expr))
elog(ERROR, "Cannot use subselect in index expression");
if (contain_agg_clause(attribute->expr))
elog(ERROR, "Cannot use aggregate in index expression");
ReleaseSysCache(atttuple); /*
* A expression using mutable functions is probably wrong,
* since if you aren't going to get the same result for the same
* data every time, it's not clear what the index entries mean at
* all.
*/
if (contain_mutable_functions(attribute->expr))
elog(ERROR, "Functions in index expression must be marked IMMUTABLE");
}
classOidP[attn] = GetIndexOpClass(attribute->opclass,
atttype,
accessMethodName,
accessMethodId);
attn++; attn++;
} }
} }
/*
* Resolve possibly-defaulted operator class specification
*/
static Oid static Oid
GetAttrOpClass(IndexElem *attribute, Oid attrType, GetIndexOpClass(List *opclass, Oid attrType,
char *accessMethodName, Oid accessMethodId) char *accessMethodName, Oid accessMethodId)
{ {
char *schemaname; char *schemaname;
char *opcname; char *opcname;
@ -456,7 +375,32 @@ GetAttrOpClass(IndexElem *attribute, Oid attrType,
Oid opClassId, Oid opClassId,
opInputType; opInputType;
if (attribute->opclass == NIL) /*
* Release 7.0 removed network_ops, timespan_ops, and
* datetime_ops, so we ignore those opclass names
* so the default *_ops is used. This can be
* removed in some later release. bjm 2000/02/07
*
* Release 7.1 removes lztext_ops, so suppress that too
* for a while. tgl 2000/07/30
*
* Release 7.2 renames timestamp_ops to timestamptz_ops,
* so suppress that too for awhile. I'm starting to
* think we need a better approach. tgl 2000/10/01
*/
if (length(opclass) == 1)
{
char *claname = strVal(lfirst(opclass));
if (strcmp(claname, "network_ops") == 0 ||
strcmp(claname, "timespan_ops") == 0 ||
strcmp(claname, "datetime_ops") == 0 ||
strcmp(claname, "lztext_ops") == 0 ||
strcmp(claname, "timestamp_ops") == 0)
opclass = NIL;
}
if (opclass == NIL)
{ {
/* no operator class specified, so find the default */ /* no operator class specified, so find the default */
opClassId = GetDefaultOpClass(attrType, accessMethodId); opClassId = GetDefaultOpClass(attrType, accessMethodId);
@ -473,7 +417,7 @@ GetAttrOpClass(IndexElem *attribute, Oid attrType,
*/ */
/* deconstruct the name list */ /* deconstruct the name list */
DeconstructQualifiedName(attribute->opclass, &schemaname, &opcname); DeconstructQualifiedName(opclass, &schemaname, &opcname);
if (schemaname) if (schemaname)
{ {
@ -501,7 +445,7 @@ GetAttrOpClass(IndexElem *attribute, Oid attrType,
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple))
elog(ERROR, "DefineIndex: operator class \"%s\" not supported by access method \"%s\"", elog(ERROR, "DefineIndex: operator class \"%s\" not supported by access method \"%s\"",
NameListToString(attribute->opclass), accessMethodName); NameListToString(opclass), accessMethodName);
/* /*
* Verify that the index operator class accepts this datatype. Note * Verify that the index operator class accepts this datatype. Note
@ -512,7 +456,7 @@ GetAttrOpClass(IndexElem *attribute, Oid attrType,
if (!IsBinaryCoercible(attrType, opInputType)) if (!IsBinaryCoercible(attrType, opInputType))
elog(ERROR, "operator class \"%s\" does not accept data type %s", elog(ERROR, "operator class \"%s\" does not accept data type %s",
NameListToString(attribute->opclass), format_type_be(attrType)); NameListToString(opclass), format_type_be(attrType));
ReleaseSysCache(tuple); ReleaseSysCache(tuple);
@ -773,7 +717,7 @@ ReindexDatabase(const char *dbname, bool force, bool all)
for (i = 0; i < relcnt; i++) for (i = 0; i < relcnt; i++)
{ {
StartTransactionCommand(); StartTransactionCommand();
SetQuerySnapshot(); /* might be needed for functional index */ SetQuerySnapshot(); /* might be needed for functions in indexes */
if (reindex_relation(relids[i], force)) if (reindex_relation(relids[i], force))
elog(NOTICE, "relation %u was reindexed", relids[i]); elog(NOTICE, "relation %u was reindexed", relids[i]);
CommitTransactionCommand(); CommitTransactionCommand();

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.72 2003/04/29 22:13:08 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.73 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1103,6 +1103,7 @@ renameatt(Oid myrelid,
Relation attrelation; Relation attrelation;
HeapTuple atttup; HeapTuple atttup;
Form_pg_attribute attform; Form_pg_attribute attform;
int attnum;
List *indexoidlist; List *indexoidlist;
List *indexoidscan; List *indexoidscan;
@ -1178,7 +1179,8 @@ renameatt(Oid myrelid,
oldattname); oldattname);
attform = (Form_pg_attribute) GETSTRUCT(atttup); attform = (Form_pg_attribute) GETSTRUCT(atttup);
if (attform->attnum < 0) attnum = attform->attnum;
if (attnum < 0)
elog(ERROR, "renameatt: system attribute \"%s\" may not be renamed", elog(ERROR, "renameatt: system attribute \"%s\" may not be renamed",
oldattname); oldattname);
@ -1217,43 +1219,48 @@ renameatt(Oid myrelid,
{ {
Oid indexoid = lfirsto(indexoidscan); Oid indexoid = lfirsto(indexoidscan);
HeapTuple indextup; HeapTuple indextup;
Form_pg_index indexform;
int i;
/* /*
* First check to see if index is a functional index. If so, its * Scan through index columns to see if there's any simple index
* column name is a function name and shouldn't be renamed here. * entries for this attribute. We ignore expressional entries.
*/ */
indextup = SearchSysCache(INDEXRELID, indextup = SearchSysCache(INDEXRELID,
ObjectIdGetDatum(indexoid), ObjectIdGetDatum(indexoid),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(indextup)) if (!HeapTupleIsValid(indextup))
elog(ERROR, "renameatt: can't find index id %u", indexoid); elog(ERROR, "renameatt: can't find index id %u", indexoid);
if (OidIsValid(((Form_pg_index) GETSTRUCT(indextup))->indproc)) indexform = (Form_pg_index) GETSTRUCT(indextup);
for (i = 0; i < indexform->indnatts; i++)
{ {
ReleaseSysCache(indextup); if (attnum != indexform->indkey[i])
continue; continue;
/*
* Found one, rename it.
*/
atttup = SearchSysCacheCopy(ATTNUM,
ObjectIdGetDatum(indexoid),
Int16GetDatum(i + 1),
0, 0);
if (!HeapTupleIsValid(atttup))
continue; /* should we raise an error? */
/*
* Update the (copied) attribute tuple.
*/
namestrcpy(&(((Form_pg_attribute) GETSTRUCT(atttup))->attname),
newattname);
simple_heap_update(attrelation, &atttup->t_self, atttup);
/* keep system catalog indexes current */
CatalogUpdateIndexes(attrelation, atttup);
heap_freetuple(atttup);
} }
ReleaseSysCache(indextup); ReleaseSysCache(indextup);
/*
* Okay, look to see if any column name of the index matches the
* old attribute name.
*/
atttup = SearchSysCacheCopyAttName(indexoid, oldattname);
if (!HeapTupleIsValid(atttup))
continue; /* Nope, so ignore it */
/*
* Update the (copied) attribute tuple.
*/
namestrcpy(&(((Form_pg_attribute) GETSTRUCT(atttup))->attname),
newattname);
simple_heap_update(attrelation, &atttup->t_self, atttup);
/* keep system catalog indexes current */
CatalogUpdateIndexes(attrelation, atttup);
heap_freetuple(atttup);
} }
freeList(indexoidlist); freeList(indexoidlist);
@ -1986,8 +1993,7 @@ AlterTableAlterColumnDropNotNull(Oid myrelid, bool recurse,
* Loop over each attribute in the primary key and see if it * Loop over each attribute in the primary key and see if it
* matches the to-be-altered attribute * matches the to-be-altered attribute
*/ */
for (i = 0; i < INDEX_MAX_KEYS && for (i = 0; i < indexStruct->indnatts; i++)
indexStruct->indkey[i] != InvalidAttrNumber; i++)
{ {
if (indexStruct->indkey[i] == attnum) if (indexStruct->indkey[i] == attnum)
elog(ERROR, "ALTER TABLE: Attribute \"%s\" is in a primary key", colName); elog(ERROR, "ALTER TABLE: Attribute \"%s\" is in a primary key", colName);
@ -3185,9 +3191,10 @@ transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
/* /*
* Now build the list of PK attributes from the indkey definition * Now build the list of PK attributes from the indkey definition
* (we assume a primary key cannot have expressional elements)
*/ */
*attnamelist = NIL; *attnamelist = NIL;
for (i = 0; i < INDEX_MAX_KEYS && indexStruct->indkey[i] != 0; i++) for (i = 0; i < indexStruct->indnatts; i++)
{ {
int pkattno = indexStruct->indkey[i]; int pkattno = indexStruct->indkey[i];
@ -3241,26 +3248,40 @@ transformFkeyCheckAttrs(Relation pkrel,
indexStruct = (Form_pg_index) GETSTRUCT(indexTuple); indexStruct = (Form_pg_index) GETSTRUCT(indexTuple);
/* /*
* Must be unique, not a functional index, and not a partial index * Must have the right number of columns; must be unique and not a
* partial index; forget it if there are any expressions, too
*/ */
if (indexStruct->indisunique && if (indexStruct->indnatts == numattrs &&
indexStruct->indproc == InvalidOid && indexStruct->indisunique &&
VARSIZE(&indexStruct->indpred) <= VARHDRSZ) heap_attisnull(indexTuple, Anum_pg_index_indpred) &&
heap_attisnull(indexTuple, Anum_pg_index_indexprs))
{ {
for (i = 0; i < INDEX_MAX_KEYS && indexStruct->indkey[i] != 0; i++) /*
; * The given attnum list may match the index columns in any
if (i == numattrs) * order. Check that each list is a subset of the other.
*/
for (i = 0; i < numattrs; i++)
{
found = false;
for (j = 0; j < numattrs; j++)
{
if (attnums[i] == indexStruct->indkey[j])
{
found = true;
break;
}
}
if (!found)
break;
}
if (found)
{ {
/*
* The given attnum list may match the index columns in any
* order. Check that each list is a subset of the other.
*/
for (i = 0; i < numattrs; i++) for (i = 0; i < numattrs; i++)
{ {
found = false; found = false;
for (j = 0; j < numattrs; j++) for (j = 0; j < numattrs; j++)
{ {
if (attnums[i] == indexStruct->indkey[j]) if (attnums[j] == indexStruct->indkey[i])
{ {
found = true; found = true;
break; break;
@ -3269,23 +3290,6 @@ transformFkeyCheckAttrs(Relation pkrel,
if (!found) if (!found)
break; break;
} }
if (found)
{
for (i = 0; i < numattrs; i++)
{
found = false;
for (j = 0; j < numattrs; j++)
{
if (attnums[j] == indexStruct->indkey[i])
{
found = true;
break;
}
}
if (!found)
break;
}
}
} }
} }
ReleaseSysCache(indexTuple); ReleaseSysCache(indexTuple);
@ -4020,12 +4024,12 @@ AlterTableCreateToastTable(Oid relOid, bool silent)
indexInfo = makeNode(IndexInfo); indexInfo = makeNode(IndexInfo);
indexInfo->ii_NumIndexAttrs = 2; indexInfo->ii_NumIndexAttrs = 2;
indexInfo->ii_NumKeyAttrs = 2;
indexInfo->ii_KeyAttrNumbers[0] = 1; indexInfo->ii_KeyAttrNumbers[0] = 1;
indexInfo->ii_KeyAttrNumbers[1] = 2; indexInfo->ii_KeyAttrNumbers[1] = 2;
indexInfo->ii_Expressions = NIL;
indexInfo->ii_ExpressionsState = NIL;
indexInfo->ii_Predicate = NIL; indexInfo->ii_Predicate = NIL;
indexInfo->ii_PredicateState = NIL; indexInfo->ii_PredicateState = NIL;
indexInfo->ii_FuncOid = InvalidOid;
indexInfo->ii_Unique = true; indexInfo->ii_Unique = true;
classObjectId[0] = OID_BTREE_OPS_OID; classObjectId[0] = OID_BTREE_OPS_OID;

View File

@ -13,7 +13,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.254 2003/05/27 17:49:45 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.255 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -304,7 +304,7 @@ vacuum(VacuumStmt *vacstmt)
if (vacstmt->vacuum) if (vacstmt->vacuum)
{ {
StartTransactionCommand(); StartTransactionCommand();
SetQuerySnapshot(); /* might be needed for functional index */ SetQuerySnapshot(); /* might be needed for functions in indexes */
} }
else else
old_context = MemoryContextSwitchTo(anl_context); old_context = MemoryContextSwitchTo(anl_context);
@ -728,7 +728,7 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, char expected_relkind)
/* Begin a transaction for vacuuming this relation */ /* Begin a transaction for vacuuming this relation */
StartTransactionCommand(); StartTransactionCommand();
SetQuerySnapshot(); /* might be needed for functional index */ SetQuerySnapshot(); /* might be needed for functions in indexes */
/* /*
* Check for user-requested abort. Note we want this to be inside a * Check for user-requested abort. Note we want this to be inside a
@ -3028,7 +3028,10 @@ vac_is_partial_index(Relation indrel)
return true; return true;
/* Otherwise, look to see if there's a partial-index predicate */ /* Otherwise, look to see if there's a partial-index predicate */
return (VARSIZE(&indrel->rd_index->indpred) > VARHDRSZ); if (!heap_attisnull(indrel->rd_indextuple, Anum_pg_index_indpred))
return true;
return false;
} }

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/execUtils.c,v 1.99 2003/05/05 17:57:47 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/executor/execUtils.c,v 1.100 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -706,10 +706,8 @@ ExecOpenIndices(ResultRelInfo *resultRelInfo)
if (!indexDesc->rd_am->amconcurrent) if (!indexDesc->rd_am->amconcurrent)
LockRelation(indexDesc, AccessExclusiveLock); LockRelation(indexDesc, AccessExclusiveLock);
/* /* extract index key information from the index's pg_index info */
* extract index key information from the index's pg_index tuple ii = BuildIndexInfo(indexDesc);
*/
ii = BuildIndexInfo(indexDesc->rd_index);
relationDescs[i] = indexDesc; relationDescs[i] = indexDesc;
indexInfoArray[i] = ii; indexInfoArray[i] = ii;
@ -797,7 +795,7 @@ ExecInsertIndexTuples(TupleTableSlot *slot,
/* /*
* We will use the EState's per-tuple context for evaluating * We will use the EState's per-tuple context for evaluating
* predicates and functional-index functions (creating it if it's not * predicates and index expressions (creating it if it's not
* already there). * already there).
*/ */
econtext = GetPerTupleExprContext(estate); econtext = GetPerTupleExprContext(estate);
@ -844,11 +842,12 @@ ExecInsertIndexTuples(TupleTableSlot *slot,
/* /*
* FormIndexDatum fills in its datum and null parameters with * FormIndexDatum fills in its datum and null parameters with
* attribute information taken from the given heap tuple. * attribute information taken from the given heap tuple.
* It also computes any expressions needed.
*/ */
FormIndexDatum(indexInfo, FormIndexDatum(indexInfo,
heapTuple, heapTuple,
heapDescriptor, heapDescriptor,
econtext->ecxt_per_tuple_memory, estate,
datum, datum,
nullv); nullv);

View File

@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.250 2003/05/06 00:20:32 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.251 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1421,8 +1421,7 @@ _copyIndexElem(IndexElem *from)
IndexElem *newnode = makeNode(IndexElem); IndexElem *newnode = makeNode(IndexElem);
COPY_STRING_FIELD(name); COPY_STRING_FIELD(name);
COPY_NODE_FIELD(funcname); COPY_NODE_FIELD(expr);
COPY_NODE_FIELD(args);
COPY_NODE_FIELD(opclass); COPY_NODE_FIELD(opclass);
return newnode; return newnode;

View File

@ -18,7 +18,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.193 2003/05/06 00:20:32 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.194 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1466,8 +1466,7 @@ static bool
_equalIndexElem(IndexElem *a, IndexElem *b) _equalIndexElem(IndexElem *a, IndexElem *b)
{ {
COMPARE_STRING_FIELD(name); COMPARE_STRING_FIELD(name);
COMPARE_NODE_FIELD(funcname); COMPARE_NODE_FIELD(expr);
COMPARE_NODE_FIELD(args);
COMPARE_NODE_FIELD(opclass); COMPARE_NODE_FIELD(opclass);
return true; return true;

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.205 2003/05/06 00:20:32 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.206 2003/05/28 16:03:56 tgl Exp $
* *
* NOTES * NOTES
* Every node type that can appear in stored rules' parsetrees *must* * Every node type that can appear in stored rules' parsetrees *must*
@ -1181,8 +1181,7 @@ _outIndexElem(StringInfo str, IndexElem *node)
WRITE_NODE_TYPE("INDEXELEM"); WRITE_NODE_TYPE("INDEXELEM");
WRITE_STRING_FIELD(name); WRITE_STRING_FIELD(name);
WRITE_NODE_FIELD(funcname); WRITE_NODE_FIELD(expr);
WRITE_NODE_FIELD(args);
WRITE_NODE_FIELD(opclass); WRITE_NODE_FIELD(opclass);
} }

View File

@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.141 2003/05/27 17:49:46 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.142 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -43,12 +43,8 @@
/* /*
* DoneMatchingIndexKeys() - MACRO * DoneMatchingIndexKeys() - MACRO
*
* Formerly this looked at indexkeys, but that's the wrong thing for a
* functional index.
*/ */
#define DoneMatchingIndexKeys(indexkeys, classes) \ #define DoneMatchingIndexKeys(classes) (classes[0] == InvalidOid)
(classes[0] == InvalidOid)
#define is_indexable_operator(clause,opclass,indexkey_on_left) \ #define is_indexable_operator(clause,opclass,indexkey_on_left) \
(indexable_operator(clause,opclass,indexkey_on_left) != InvalidOid) (indexable_operator(clause,opclass,indexkey_on_left) != InvalidOid)
@ -67,14 +63,14 @@ static List *group_clauses_by_indexkey_for_join(RelOptInfo *rel,
IndexOptInfo *index, IndexOptInfo *index,
Relids outer_relids, Relids outer_relids,
bool isouterjoin); bool isouterjoin);
static bool match_clause_to_indexkey(RelOptInfo *rel, IndexOptInfo *index, static bool match_clause_to_indexcol(RelOptInfo *rel, IndexOptInfo *index,
int indexkey, Oid opclass, Expr *clause); int indexcol, Oid opclass, Expr *clause);
static bool match_join_clause_to_indexkey(RelOptInfo *rel, IndexOptInfo *index, static bool match_join_clause_to_indexcol(RelOptInfo *rel, IndexOptInfo *index,
int indexkey, Oid opclass, Expr *clause); int indexcol, Oid opclass, Expr *clause);
static Oid indexable_operator(Expr *clause, Oid opclass, static Oid indexable_operator(Expr *clause, Oid opclass,
bool indexkey_on_left); bool indexkey_on_left);
static bool pred_test(List *predicate_list, List *restrictinfo_list, static bool pred_test(List *predicate_list, List *restrictinfo_list,
List *joininfo_list, int relvarno); List *joininfo_list);
static bool pred_test_restrict_list(Expr *predicate, List *restrictinfo_list); static bool pred_test_restrict_list(Expr *predicate, List *restrictinfo_list);
static bool pred_test_recurse_clause(Expr *predicate, Node *clause); static bool pred_test_recurse_clause(Expr *predicate, Node *clause);
static bool pred_test_recurse_pred(Expr *predicate, Node *clause); static bool pred_test_recurse_pred(Expr *predicate, Node *clause);
@ -83,10 +79,8 @@ static Relids indexable_outerrelids(RelOptInfo *rel, IndexOptInfo *index);
static Path *make_innerjoin_index_path(Query *root, static Path *make_innerjoin_index_path(Query *root,
RelOptInfo *rel, IndexOptInfo *index, RelOptInfo *rel, IndexOptInfo *index,
List *clausegroups); List *clausegroups);
static bool match_index_to_operand(int indexkey, Node *operand, static bool match_index_to_operand(Node *operand, int indexcol,
RelOptInfo *rel, IndexOptInfo *index); RelOptInfo *rel, IndexOptInfo *index);
static bool function_index_operand(Expr *funcOpnd, RelOptInfo *rel,
IndexOptInfo *index);
static bool match_special_index_operator(Expr *clause, Oid opclass, static bool match_special_index_operator(Expr *clause, Oid opclass,
bool indexkey_on_left); bool indexkey_on_left);
static List *expand_indexqual_condition(Expr *clause, Oid opclass); static List *expand_indexqual_condition(Expr *clause, Oid opclass);
@ -149,8 +143,7 @@ create_index_paths(Query *root, RelOptInfo *rel)
* predicate test. * predicate test.
*/ */
if (index->indpred != NIL) if (index->indpred != NIL)
if (!pred_test(index->indpred, restrictinfo_list, joininfo_list, if (!pred_test(index->indpred, restrictinfo_list, joininfo_list))
rel->relid))
continue; continue;
/* /*
@ -371,7 +364,6 @@ match_or_subclause_to_indexkey(RelOptInfo *rel,
IndexOptInfo *index, IndexOptInfo *index,
Expr *clause) Expr *clause)
{ {
int indexkey = index->indexkeys[0];
Oid opclass = index->classlist[0]; Oid opclass = index->classlist[0];
if (and_clause((Node *) clause)) if (and_clause((Node *) clause))
@ -380,14 +372,14 @@ match_or_subclause_to_indexkey(RelOptInfo *rel,
foreach(item, ((BoolExpr *) clause)->args) foreach(item, ((BoolExpr *) clause)->args)
{ {
if (match_clause_to_indexkey(rel, index, indexkey, opclass, if (match_clause_to_indexcol(rel, index, 0, opclass,
lfirst(item))) lfirst(item)))
return true; return true;
} }
return false; return false;
} }
else else
return match_clause_to_indexkey(rel, index, indexkey, opclass, return match_clause_to_indexcol(rel, index, 0, opclass,
clause); clause);
} }
@ -426,7 +418,7 @@ extract_or_indexqual_conditions(RelOptInfo *rel,
Expr *orsubclause) Expr *orsubclause)
{ {
List *quals = NIL; List *quals = NIL;
int *indexkeys = index->indexkeys; int indexcol = 0;
Oid *classes = index->classlist; Oid *classes = index->classlist;
/* /*
@ -437,7 +429,6 @@ extract_or_indexqual_conditions(RelOptInfo *rel,
*/ */
do do
{ {
int curIndxKey = indexkeys[0];
Oid curClass = classes[0]; Oid curClass = classes[0];
List *clausegroup = NIL; List *clausegroup = NIL;
List *item; List *item;
@ -448,16 +439,16 @@ extract_or_indexqual_conditions(RelOptInfo *rel,
{ {
Expr *subsubclause = (Expr *) lfirst(item); Expr *subsubclause = (Expr *) lfirst(item);
if (match_clause_to_indexkey(rel, index, if (match_clause_to_indexcol(rel, index,
curIndxKey, curClass, indexcol, curClass,
subsubclause)) subsubclause))
clausegroup = nconc(clausegroup, clausegroup = nconc(clausegroup,
expand_indexqual_condition(subsubclause, expand_indexqual_condition(subsubclause,
curClass)); curClass));
} }
} }
else if (match_clause_to_indexkey(rel, index, else if (match_clause_to_indexcol(rel, index,
curIndxKey, curClass, indexcol, curClass,
orsubclause)) orsubclause))
clausegroup = expand_indexqual_condition(orsubclause, curClass); clausegroup = expand_indexqual_condition(orsubclause, curClass);
@ -471,8 +462,8 @@ extract_or_indexqual_conditions(RelOptInfo *rel,
{ {
RestrictInfo *rinfo = (RestrictInfo *) lfirst(item); RestrictInfo *rinfo = (RestrictInfo *) lfirst(item);
if (match_clause_to_indexkey(rel, index, if (match_clause_to_indexcol(rel, index,
curIndxKey, curClass, indexcol, curClass,
rinfo->clause)) rinfo->clause))
clausegroup = nconc(clausegroup, clausegroup = nconc(clausegroup,
expand_indexqual_condition(rinfo->clause, expand_indexqual_condition(rinfo->clause,
@ -489,10 +480,10 @@ extract_or_indexqual_conditions(RelOptInfo *rel,
quals = nconc(quals, clausegroup); quals = nconc(quals, clausegroup);
indexkeys++; indexcol++;
classes++; classes++;
} while (!DoneMatchingIndexKeys(indexkeys, classes)); } while (!DoneMatchingIndexKeys(classes));
if (quals == NIL) if (quals == NIL)
elog(ERROR, "extract_or_indexqual_conditions: no matching clause"); elog(ERROR, "extract_or_indexqual_conditions: no matching clause");
@ -531,7 +522,7 @@ group_clauses_by_indexkey(RelOptInfo *rel, IndexOptInfo *index)
{ {
List *clausegroup_list = NIL; List *clausegroup_list = NIL;
List *restrictinfo_list = rel->baserestrictinfo; List *restrictinfo_list = rel->baserestrictinfo;
int *indexkeys = index->indexkeys; int indexcol = 0;
Oid *classes = index->classlist; Oid *classes = index->classlist;
if (restrictinfo_list == NIL) if (restrictinfo_list == NIL)
@ -539,7 +530,6 @@ group_clauses_by_indexkey(RelOptInfo *rel, IndexOptInfo *index)
do do
{ {
int curIndxKey = indexkeys[0];
Oid curClass = classes[0]; Oid curClass = classes[0];
List *clausegroup = NIL; List *clausegroup = NIL;
List *i; List *i;
@ -548,9 +538,9 @@ group_clauses_by_indexkey(RelOptInfo *rel, IndexOptInfo *index)
{ {
RestrictInfo *rinfo = (RestrictInfo *) lfirst(i); RestrictInfo *rinfo = (RestrictInfo *) lfirst(i);
if (match_clause_to_indexkey(rel, if (match_clause_to_indexcol(rel,
index, index,
curIndxKey, indexcol,
curClass, curClass,
rinfo->clause)) rinfo->clause))
clausegroup = lappend(clausegroup, rinfo); clausegroup = lappend(clausegroup, rinfo);
@ -565,10 +555,10 @@ group_clauses_by_indexkey(RelOptInfo *rel, IndexOptInfo *index)
clausegroup_list = lappend(clausegroup_list, clausegroup); clausegroup_list = lappend(clausegroup_list, clausegroup);
indexkeys++; indexcol++;
classes++; classes++;
} while (!DoneMatchingIndexKeys(indexkeys, classes)); } while (!DoneMatchingIndexKeys(classes));
return clausegroup_list; return clausegroup_list;
} }
@ -592,12 +582,11 @@ group_clauses_by_indexkey_for_join(RelOptInfo *rel, IndexOptInfo *index,
{ {
List *clausegroup_list = NIL; List *clausegroup_list = NIL;
bool jfound = false; bool jfound = false;
int *indexkeys = index->indexkeys; int indexcol = 0;
Oid *classes = index->classlist; Oid *classes = index->classlist;
do do
{ {
int curIndxKey = indexkeys[0];
Oid curClass = classes[0]; Oid curClass = classes[0];
List *clausegroup = NIL; List *clausegroup = NIL;
List *i; List *i;
@ -619,9 +608,9 @@ group_clauses_by_indexkey_for_join(RelOptInfo *rel, IndexOptInfo *index,
if (isouterjoin && rinfo->ispusheddown) if (isouterjoin && rinfo->ispusheddown)
continue; continue;
if (match_join_clause_to_indexkey(rel, if (match_join_clause_to_indexcol(rel,
index, index,
curIndxKey, indexcol,
curClass, curClass,
rinfo->clause)) rinfo->clause))
{ {
@ -640,9 +629,9 @@ group_clauses_by_indexkey_for_join(RelOptInfo *rel, IndexOptInfo *index,
if (isouterjoin && rinfo->ispusheddown) if (isouterjoin && rinfo->ispusheddown)
continue; continue;
if (match_clause_to_indexkey(rel, if (match_clause_to_indexcol(rel,
index, index,
curIndxKey, indexcol,
curClass, curClass,
rinfo->clause)) rinfo->clause))
clausegroup = lappend(clausegroup, rinfo); clausegroup = lappend(clausegroup, rinfo);
@ -657,10 +646,10 @@ group_clauses_by_indexkey_for_join(RelOptInfo *rel, IndexOptInfo *index,
clausegroup_list = lappend(clausegroup_list, clausegroup); clausegroup_list = lappend(clausegroup_list, clausegroup);
indexkeys++; indexcol++;
classes++; classes++;
} while (!DoneMatchingIndexKeys(indexkeys, classes)); } while (!DoneMatchingIndexKeys(classes));
/* if no join clause was matched then forget it, per comments above */ /* if no join clause was matched then forget it, per comments above */
if (!jfound) if (!jfound)
@ -674,15 +663,15 @@ group_clauses_by_indexkey_for_join(RelOptInfo *rel, IndexOptInfo *index,
/* /*
* match_clause_to_indexkey() * match_clause_to_indexcol()
* Determines whether a restriction clause matches a key of an index. * Determines whether a restriction clause matches a column of an index.
* *
* To match, the clause: * To match, the clause:
* *
* (1) must be in the form (indexkey op const) or (const op indexkey); * (1) must be in the form (indexkey op const) or (const op indexkey);
* and * and
* (2) must contain an operator which is in the same class as the index * (2) must contain an operator which is in the same class as the index
* operator for this key, or is a "special" operator as recognized * operator for this column, or is a "special" operator as recognized
* by match_special_index_operator(). * by match_special_index_operator().
* *
* Presently, the executor can only deal with indexquals that have the * Presently, the executor can only deal with indexquals that have the
@ -693,7 +682,7 @@ group_clauses_by_indexkey_for_join(RelOptInfo *rel, IndexOptInfo *index,
* *
* 'rel' is the relation of interest. * 'rel' is the relation of interest.
* 'index' is an index on 'rel'. * 'index' is an index on 'rel'.
* 'indexkey' is a key of 'index'. * 'indexcol' is a column number of 'index' (counting from 0).
* 'opclass' is the corresponding operator class. * 'opclass' is the corresponding operator class.
* 'clause' is the clause to be tested. * 'clause' is the clause to be tested.
* *
@ -703,9 +692,9 @@ group_clauses_by_indexkey_for_join(RelOptInfo *rel, IndexOptInfo *index,
* responsibility of higher-level routines to cope with those. * responsibility of higher-level routines to cope with those.
*/ */
static bool static bool
match_clause_to_indexkey(RelOptInfo *rel, match_clause_to_indexcol(RelOptInfo *rel,
IndexOptInfo *index, IndexOptInfo *index,
int indexkey, int indexcol,
Oid opclass, Oid opclass,
Expr *clause) Expr *clause)
{ {
@ -725,7 +714,7 @@ match_clause_to_indexkey(RelOptInfo *rel,
* (indexkey operator constant) or (constant operator indexkey). * (indexkey operator constant) or (constant operator indexkey).
* Anything that is a "pseudo constant" expression will do. * Anything that is a "pseudo constant" expression will do.
*/ */
if (match_index_to_operand(indexkey, leftop, rel, index) && if (match_index_to_operand(leftop, indexcol, rel, index) &&
is_pseudo_constant_clause(rightop)) is_pseudo_constant_clause(rightop))
{ {
if (is_indexable_operator(clause, opclass, true)) if (is_indexable_operator(clause, opclass, true))
@ -740,7 +729,7 @@ match_clause_to_indexkey(RelOptInfo *rel,
return false; return false;
} }
if (match_index_to_operand(indexkey, rightop, rel, index) && if (match_index_to_operand(rightop, indexcol, rel, index) &&
is_pseudo_constant_clause(leftop)) is_pseudo_constant_clause(leftop))
{ {
if (is_indexable_operator(clause, opclass, false)) if (is_indexable_operator(clause, opclass, false))
@ -759,8 +748,8 @@ match_clause_to_indexkey(RelOptInfo *rel,
} }
/* /*
* match_join_clause_to_indexkey() * match_join_clause_to_indexcol()
* Determines whether a join clause matches a key of an index. * Determines whether a join clause matches a column of an index.
* *
* To match, the clause: * To match, the clause:
* *
@ -768,7 +757,7 @@ match_clause_to_indexkey(RelOptInfo *rel,
* where others is an expression involving only vars of the other * where others is an expression involving only vars of the other
* relation(s); and * relation(s); and
* (2) must contain an operator which is in the same class as the index * (2) must contain an operator which is in the same class as the index
* operator for this key, or is a "special" operator as recognized * operator for this column, or is a "special" operator as recognized
* by match_special_index_operator(). * by match_special_index_operator().
* *
* As above, we must be able to commute the clause to put the indexkey * As above, we must be able to commute the clause to put the indexkey
@ -781,7 +770,7 @@ match_clause_to_indexkey(RelOptInfo *rel,
* *
* 'rel' is the relation of interest. * 'rel' is the relation of interest.
* 'index' is an index on 'rel'. * 'index' is an index on 'rel'.
* 'indexkey' is a key of 'index'. * 'indexcol' is a column number of 'index' (counting from 0).
* 'opclass' is the corresponding operator class. * 'opclass' is the corresponding operator class.
* 'clause' is the clause to be tested. * 'clause' is the clause to be tested.
* *
@ -791,9 +780,9 @@ match_clause_to_indexkey(RelOptInfo *rel,
* responsibility of higher-level routines to cope with those. * responsibility of higher-level routines to cope with those.
*/ */
static bool static bool
match_join_clause_to_indexkey(RelOptInfo *rel, match_join_clause_to_indexcol(RelOptInfo *rel,
IndexOptInfo *index, IndexOptInfo *index,
int indexkey, int indexcol,
Oid opclass, Oid opclass,
Expr *clause) Expr *clause)
{ {
@ -814,7 +803,7 @@ match_join_clause_to_indexkey(RelOptInfo *rel,
* expression that uses none of the indexed relation's vars and * expression that uses none of the indexed relation's vars and
* contains no volatile functions. * contains no volatile functions.
*/ */
if (match_index_to_operand(indexkey, leftop, rel, index)) if (match_index_to_operand(leftop, indexcol, rel, index))
{ {
Relids othervarnos = pull_varnos(rightop); Relids othervarnos = pull_varnos(rightop);
bool isIndexable; bool isIndexable;
@ -827,7 +816,7 @@ match_join_clause_to_indexkey(RelOptInfo *rel,
return isIndexable; return isIndexable;
} }
if (match_index_to_operand(indexkey, rightop, rel, index)) if (match_index_to_operand(rightop, indexcol, rel, index))
{ {
Relids othervarnos = pull_varnos(leftop); Relids othervarnos = pull_varnos(leftop);
bool isIndexable; bool isIndexable;
@ -895,8 +884,7 @@ indexable_operator(Expr *clause, Oid opclass, bool indexkey_on_left)
* to CNF format). --Nels, Jan '93 * to CNF format). --Nels, Jan '93
*/ */
static bool static bool
pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list, pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list)
int relvarno)
{ {
List *pred; List *pred;
@ -919,18 +907,6 @@ pred_test(List *predicate_list, List *restrictinfo_list, List *joininfo_list,
return false; /* no restriction clauses: the test must return false; /* no restriction clauses: the test must
* fail */ * fail */
/*
* The predicate as stored in the index definition will use varno 1
* for its Vars referencing the indexed relation. If the indexed
* relation isn't varno 1 in the query, we must adjust the predicate
* to make the Vars match, else equal() won't work.
*/
if (relvarno != 1)
{
predicate_list = copyObject(predicate_list);
ChangeVarNodes((Node *) predicate_list, 1, relvarno, 0);
}
foreach(pred, predicate_list) foreach(pred, predicate_list)
{ {
/* /*
@ -1320,17 +1296,16 @@ indexable_outerrelids(RelOptInfo *rel, IndexOptInfo *index)
{ {
RestrictInfo *rinfo = (RestrictInfo *) lfirst(j); RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
Expr *clause = rinfo->clause; Expr *clause = rinfo->clause;
int *indexkeys = index->indexkeys; int indexcol = 0;
Oid *classes = index->classlist; Oid *classes = index->classlist;
do do
{ {
int curIndxKey = indexkeys[0];
Oid curClass = classes[0]; Oid curClass = classes[0];
if (match_join_clause_to_indexkey(rel, if (match_join_clause_to_indexcol(rel,
index, index,
curIndxKey, indexcol,
curClass, curClass,
clause)) clause))
{ {
@ -1338,10 +1313,10 @@ indexable_outerrelids(RelOptInfo *rel, IndexOptInfo *index)
break; break;
} }
indexkeys++; indexcol++;
classes++; classes++;
} while (!DoneMatchingIndexKeys(indexkeys, classes)); } while (!DoneMatchingIndexKeys(classes));
if (match_found) if (match_found)
break; break;
@ -1611,16 +1586,22 @@ make_innerjoin_index_path(Query *root,
* match_index_to_operand() * match_index_to_operand()
* Generalized test for a match between an index's key * Generalized test for a match between an index's key
* and the operand on one side of a restriction or join clause. * and the operand on one side of a restriction or join clause.
* Now check for functional indices as well. *
* operand: the nodetree to be compared to the index
* indexcol: the column number of the index (counting from 0)
* rel: the parent relation
* index: the index of interest
*/ */
static bool static bool
match_index_to_operand(int indexkey, match_index_to_operand(Node *operand,
Node *operand, int indexcol,
RelOptInfo *rel, RelOptInfo *rel,
IndexOptInfo *index) IndexOptInfo *index)
{ {
int indkey;
/* /*
* Ignore any RelabelType node above the indexkey. This is needed to * Ignore any RelabelType node above the operand. This is needed to
* be able to apply indexscanning in binary-compatible-operator cases. * be able to apply indexscanning in binary-compatible-operator cases.
* Note: we can assume there is at most one RelabelType node; * Note: we can assume there is at most one RelabelType node;
* eval_const_expressions() will have simplified if more than one. * eval_const_expressions() will have simplified if more than one.
@ -1628,77 +1609,52 @@ match_index_to_operand(int indexkey,
if (operand && IsA(operand, RelabelType)) if (operand && IsA(operand, RelabelType))
operand = (Node *) ((RelabelType *) operand)->arg; operand = (Node *) ((RelabelType *) operand)->arg;
if (index->indproc == InvalidOid) indkey = index->indexkeys[indexcol];
if (indkey != 0)
{ {
/* /*
* Simple index. * Simple index column; operand must be a matching Var.
*/ */
if (operand && IsA(operand, Var) && if (operand && IsA(operand, Var) &&
rel->relid == ((Var *) operand)->varno && rel->relid == ((Var *) operand)->varno &&
indexkey == ((Var *) operand)->varattno) indkey == ((Var *) operand)->varattno)
return true; return true;
else
return false;
} }
else
/*
* Functional index.
*/
return function_index_operand((Expr *) operand, rel, index);
}
static bool
function_index_operand(Expr *funcOpnd, RelOptInfo *rel, IndexOptInfo *index)
{
FuncExpr *function;
List *funcargs;
int *indexKeys = index->indexkeys;
List *arg;
int i;
/*
* sanity check, make sure we know what we're dealing with here.
*/
if (funcOpnd == NULL || !IsA(funcOpnd, FuncExpr) ||
indexKeys == NULL)
return false;
function = (FuncExpr *) funcOpnd;
funcargs = function->args;
if (function->funcid != index->indproc)
return false;
/*----------
* Check that the arguments correspond to the same arguments used to
* create the functional index. To do this we must check that
* 1. they refer to the right relation.
* 2. the args have the right attr. numbers in the right order.
* We must ignore RelabelType nodes above the argument Vars in order
* to recognize binary-compatible-function cases correctly.
*----------
*/
i = 0;
foreach(arg, funcargs)
{ {
Var *var = (Var *) lfirst(arg); /*
* Index expression; find the correct expression. (This search could
* be avoided, at the cost of complicating all the callers of this
* routine; doesn't seem worth it.)
*/
List *indexprs;
int i;
Node *indexkey;
if (var && IsA(var, RelabelType)) indexprs = index->indexprs;
var = (Var *) ((RelabelType *) var)->arg; for (i = 0; i < indexcol; i++)
if (var == NULL || !IsA(var, Var)) {
return false; if (index->indexkeys[i] == 0)
if (indexKeys[i] == 0) {
return false; if (indexprs == NIL)
if (var->varno != rel->relid || var->varattno != indexKeys[i]) elog(ERROR, "wrong number of index expressions");
return false; indexprs = lnext(indexprs);
}
}
if (indexprs == NIL)
elog(ERROR, "wrong number of index expressions");
indexkey = (Node *) lfirst(indexprs);
/*
* Does it match the operand? Again, strip any relabeling.
*/
if (indexkey && IsA(indexkey, RelabelType))
indexkey = (Node *) ((RelabelType *) indexkey)->arg;
i++; if (equal(indexkey, operand))
return true;
} }
if (indexKeys[i] != 0) return false;
return false; /* not enough arguments */
return true;
} }
/**************************************************************************** /****************************************************************************
@ -1728,7 +1684,7 @@ function_index_operand(Expr *funcOpnd, RelOptInfo *rel, IndexOptInfo *index)
* *
* Two routines are provided here, match_special_index_operator() and * Two routines are provided here, match_special_index_operator() and
* expand_indexqual_conditions(). match_special_index_operator() is * expand_indexqual_conditions(). match_special_index_operator() is
* just an auxiliary function for match_clause_to_indexkey(); after * just an auxiliary function for match_clause_to_indexcol(); after
* the latter fails to recognize a restriction opclause's operator * the latter fails to recognize a restriction opclause's operator
* as a member of an index's opclass, it asks match_special_index_operator() * as a member of an index's opclass, it asks match_special_index_operator()
* whether the clause should be considered an indexqual anyway. * whether the clause should be considered an indexqual anyway.
@ -1915,7 +1871,6 @@ List *
expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups) expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
{ {
List *resultquals = NIL; List *resultquals = NIL;
int *indexkeys = index->indexkeys;
Oid *classes = index->classlist; Oid *classes = index->classlist;
if (clausegroups == NIL) if (clausegroups == NIL)
@ -1937,11 +1892,9 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
clausegroups = lnext(clausegroups); clausegroups = lnext(clausegroups);
indexkeys++;
classes++; classes++;
} while (clausegroups != NIL && } while (clausegroups != NIL && !DoneMatchingIndexKeys(classes));
!DoneMatchingIndexKeys(indexkeys, classes));
Assert(clausegroups == NIL); /* else more groups than indexkeys... */ Assert(clausegroups == NIL); /* else more groups than indexkeys... */

View File

@ -11,7 +11,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/pathkeys.c,v 1.48 2003/05/02 19:48:53 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/path/pathkeys.c,v 1.49 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -636,73 +636,50 @@ build_index_pathkeys(Query *root,
List *retval = NIL; List *retval = NIL;
int *indexkeys = index->indexkeys; int *indexkeys = index->indexkeys;
Oid *ordering = index->ordering; Oid *ordering = index->ordering;
PathKeyItem *item; List *indexprs = index->indexprs;
Oid sortop;
if (!indexkeys || indexkeys[0] == 0 || while (*ordering != InvalidOid)
!ordering || ordering[0] == InvalidOid)
return NIL; /* unordered index? */
if (index->indproc)
{ {
/* Functional index: build a representation of the function call */ PathKeyItem *item;
Expr *funcnode; Oid sortop;
List *funcargs = NIL; Node *indexkey;
List *cpathkey;
sortop = *ordering; sortop = *ordering;
if (ScanDirectionIsBackward(scandir)) if (ScanDirectionIsBackward(scandir))
{ {
sortop = get_commutator(sortop); sortop = get_commutator(sortop);
if (sortop == InvalidOid) if (sortop == InvalidOid)
return NIL; /* oops, no reverse sort operator? */ break; /* oops, no reverse sort operator? */
} }
while (*indexkeys != 0) if (*indexkeys != 0)
{ {
funcargs = lappend(funcargs, /* simple index column */
find_indexkey_var(root, rel, *indexkeys)); indexkey = (Node *) find_indexkey_var(root, rel, *indexkeys);
indexkeys++;
} }
else
funcnode = make_funcclause(index->indproc,
get_func_rettype(index->indproc),
false, /* cannot be a set */
COERCE_DONTCARE, /* to match any user expr */
funcargs);
/* Make a one-sublist pathkeys list for the function expression */
item = makePathKeyItem((Node *) funcnode, sortop);
retval = makeList1(make_canonical_pathkey(root, item));
}
else
{
/* Normal non-functional index */
while (*indexkeys != 0 && *ordering != InvalidOid)
{ {
Var *relvar = find_indexkey_var(root, rel, *indexkeys); /* expression --- assume we need not copy it */
List *cpathkey; if (indexprs == NIL)
elog(ERROR, "wrong number of index expressions");
sortop = *ordering; indexkey = (Node *) lfirst(indexprs);
if (ScanDirectionIsBackward(scandir)) indexprs = lnext(indexprs);
{
sortop = get_commutator(sortop);
if (sortop == InvalidOid)
break; /* oops, no reverse sort operator? */
}
/* OK, make a sublist for this sort key */
item = makePathKeyItem((Node *) relvar, sortop);
cpathkey = make_canonical_pathkey(root, item);
/*
* Eliminate redundant ordering info; could happen if query is
* such that index keys are equijoined...
*/
if (!ptrMember(cpathkey, retval))
retval = lappend(retval, cpathkey);
indexkeys++;
ordering++;
} }
/* OK, make a sublist for this sort key */
item = makePathKeyItem(indexkey, sortop);
cpathkey = make_canonical_pathkey(root, item);
/*
* Eliminate redundant ordering info; could happen if query is
* such that index keys are equijoined...
*/
if (!ptrMember(cpathkey, retval))
retval = lappend(retval, cpathkey);
indexkeys++;
ordering++;
} }
return retval; return retval;

View File

@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.142 2003/05/12 00:17:03 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.143 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1197,12 +1197,6 @@ static Node *
fix_indxqual_operand(Node *node, int baserelid, IndexOptInfo *index, fix_indxqual_operand(Node *node, int baserelid, IndexOptInfo *index,
Oid *opclass) Oid *opclass)
{ {
/*
* Remove any binary-compatible relabeling of the indexkey
*/
if (IsA(node, RelabelType))
node = (Node *) ((RelabelType *) node)->arg;
/* /*
* We represent index keys by Var nodes having the varno of the base * We represent index keys by Var nodes having the varno of the base
* table but varattno equal to the index's attribute number (index * table but varattno equal to the index's attribute number (index
@ -1210,48 +1204,68 @@ fix_indxqual_operand(Node *node, int baserelid, IndexOptInfo *index,
* a special-purpose node type that could not be mistaken for a * a special-purpose node type that could not be mistaken for a
* regular Var. But it will do for now. * regular Var. But it will do for now.
*/ */
if (IsA(node, Var)) Var *result;
int pos;
List *indexprs;
/*
* Remove any binary-compatible relabeling of the indexkey
*/
if (IsA(node, RelabelType))
node = (Node *) ((RelabelType *) node)->arg;
if (IsA(node, Var) &&
((Var *) node)->varno == baserelid)
{ {
/* If it's a var, find which index key position it occupies */ /* Try to match against simple index columns */
Assert(index->indproc == InvalidOid); int varatt = ((Var *) node)->varattno;
if (((Var *) node)->varno == baserelid) if (varatt != 0)
{ {
int varatt = ((Var *) node)->varattno; for (pos = 0; pos < index->ncolumns; pos++)
int pos;
for (pos = 0; pos < index->nkeys; pos++)
{ {
if (index->indexkeys[pos] == varatt) if (index->indexkeys[pos] == varatt)
{ {
Node *newnode = copyObject(node); result = (Var *) copyObject(node);
result->varattno = pos + 1;
((Var *) newnode)->varattno = pos + 1;
/* return the correct opclass, too */ /* return the correct opclass, too */
*opclass = index->classlist[pos]; *opclass = index->classlist[pos];
return newnode; return (Node *) result;
} }
} }
} }
/*
* Oops, this Var isn't an indexkey!
*/
elog(ERROR, "fix_indxqual_operand: var is not index attribute");
} }
/* /* Try to match against index expressions */
* Else, it must be a func expression matching a functional index. indexprs = index->indexprs;
* Since we currently only support single-column functional indexes, for (pos = 0; pos < index->ncolumns; pos++)
* the returned varattno must be 1. {
*/ if (index->indexkeys[pos] == 0)
Assert(index->indproc != InvalidOid); {
Assert(is_funcclause(node)); /* not a very thorough check, but easy */ Node *indexkey;
/* classlist[0] is the only class of a functional index */ if (indexprs == NIL)
*opclass = index->classlist[0]; elog(ERROR, "too few entries in indexprs list");
indexkey = (Node *) lfirst(indexprs);
if (indexkey && IsA(indexkey, RelabelType))
indexkey = (Node *) ((RelabelType *) indexkey)->arg;
if (equal(node, indexkey))
{
/* Found a match */
result = makeVar(baserelid, pos + 1,
exprType(lfirst(indexprs)), -1,
0);
/* return the correct opclass, too */
*opclass = index->classlist[pos];
return (Node *) result;
}
indexprs = lnext(indexprs);
}
}
return (Node *) makeVar(baserelid, 1, exprType(node), -1, 0); /* Ooops... */
elog(ERROR, "fix_indxqual_operand: node is not index attribute");
return NULL; /* keep compiler quiet */
} }
/* /*

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.136 2003/04/29 22:13:09 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.137 2003/05/28 16:03:56 tgl Exp $
* *
* HISTORY * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
@ -132,28 +132,6 @@ get_rightop(Expr *clause)
return NULL; return NULL;
} }
/*****************************************************************************
* FUNCTION clause functions
*****************************************************************************/
/*
* make_funcclause
* Creates a function clause given its function info and argument list.
*/
Expr *
make_funcclause(Oid funcid, Oid funcresulttype, bool funcretset,
CoercionForm funcformat, List *funcargs)
{
FuncExpr *expr = makeNode(FuncExpr);
expr->funcid = funcid;
expr->funcresulttype = funcresulttype;
expr->funcretset = funcretset;
expr->funcformat = funcformat;
expr->args = funcargs;
return (Expr *) expr;
}
/***************************************************************************** /*****************************************************************************
* NOT clause functions * NOT clause functions
*****************************************************************************/ *****************************************************************************/

View File

@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/plancat.c,v 1.82 2003/05/12 00:17:03 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/util/plancat.c,v 1.83 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -27,6 +27,7 @@
#include "optimizer/clauses.h" #include "optimizer/clauses.h"
#include "optimizer/plancat.h" #include "optimizer/plancat.h"
#include "parser/parsetree.h" #include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/fmgroids.h" #include "utils/fmgroids.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
@ -116,78 +117,67 @@ get_relation_info(Oid relationObjectId, RelOptInfo *rel)
Relation indexRelation; Relation indexRelation;
Form_pg_index index; Form_pg_index index;
IndexOptInfo *info; IndexOptInfo *info;
int ncolumns;
int i; int i;
int16 amorderstrategy; int16 amorderstrategy;
/* Extract info from the relation descriptor for the index */ /* Extract info from the relation descriptor for the index */
indexRelation = index_open(indexoid); indexRelation = index_open(indexoid);
index = indexRelation->rd_index;
info = makeNode(IndexOptInfo); info = makeNode(IndexOptInfo);
/*
* Need to make these arrays large enough to be sure there is room
* for a terminating 0 at the end of each one.
*/
info->classlist = (Oid *) palloc(sizeof(Oid) * (INDEX_MAX_KEYS + 1));
info->indexkeys = (int *) palloc(sizeof(int) * (INDEX_MAX_KEYS + 1));
info->ordering = (Oid *) palloc(sizeof(Oid) * (INDEX_MAX_KEYS + 1));
/* Extract info from the pg_index tuple */
index = indexRelation->rd_index;
info->indexoid = index->indexrelid; info->indexoid = index->indexrelid;
info->indproc = index->indproc; /* functional index ?? */ info->ncolumns = ncolumns = index->indnatts;
if (VARSIZE(&index->indpred) > VARHDRSZ) /* partial index ?? */
{
char *predString;
predString = DatumGetCString(DirectFunctionCall1(textout, /*
PointerGetDatum(&index->indpred))); * Need to make classlist and ordering arrays large enough to put
info->indpred = (List *) stringToNode(predString); * a terminating 0 at the end of each one.
pfree(predString); */
} info->indexkeys = (int *) palloc(sizeof(int) * ncolumns);
else info->classlist = (Oid *) palloc0(sizeof(Oid) * (ncolumns + 1));
info->indpred = NIL; info->ordering = (Oid *) palloc0(sizeof(Oid) * (ncolumns + 1));
info->unique = index->indisunique;
for (i = 0; i < INDEX_MAX_KEYS; i++) for (i = 0; i < ncolumns; i++)
{ {
if (index->indclass[i] == (Oid) 0)
break;
info->classlist[i] = index->indclass[i]; info->classlist[i] = index->indclass[i];
}
info->classlist[i] = (Oid) 0;
info->ncolumns = i;
for (i = 0; i < INDEX_MAX_KEYS; i++)
{
if (index->indkey[i] == 0)
break;
info->indexkeys[i] = index->indkey[i]; info->indexkeys[i] = index->indkey[i];
} }
info->indexkeys[i] = 0;
info->nkeys = i;
info->relam = indexRelation->rd_rel->relam; info->relam = indexRelation->rd_rel->relam;
info->pages = indexRelation->rd_rel->relpages; info->pages = indexRelation->rd_rel->relpages;
info->tuples = indexRelation->rd_rel->reltuples; info->tuples = indexRelation->rd_rel->reltuples;
info->amcostestimate = index_cost_estimator(indexRelation); info->amcostestimate = index_cost_estimator(indexRelation);
amorderstrategy = indexRelation->rd_am->amorderstrategy;
/* /*
* Fetch the ordering operators associated with the index, if any. * Fetch the ordering operators associated with the index, if any.
*/ */
MemSet(info->ordering, 0, sizeof(Oid) * (INDEX_MAX_KEYS + 1)); amorderstrategy = indexRelation->rd_am->amorderstrategy;
if (amorderstrategy != 0) if (amorderstrategy != 0)
{ {
int oprindex = amorderstrategy - 1; int oprindex = amorderstrategy - 1;
for (i = 0; i < info->ncolumns; i++) for (i = 0; i < ncolumns; i++)
{ {
info->ordering[i] = indexRelation->rd_operator[oprindex]; info->ordering[i] = indexRelation->rd_operator[oprindex];
oprindex += indexRelation->rd_am->amstrategies; oprindex += indexRelation->rd_am->amstrategies;
} }
} }
/*
* Fetch the index expressions and predicate, if any. We must
* modify the copies we obtain from the relcache to have the
* correct varno for the parent relation, so that they match up
* correctly against qual clauses.
*/
info->indexprs = RelationGetIndexExpressions(indexRelation);
info->indpred = RelationGetIndexPredicate(indexRelation);
if (info->indexprs && varno != 1)
ChangeVarNodes((Node *) info->indexprs, 1, varno, 0);
if (info->indpred && varno != 1)
ChangeVarNodes((Node *) info->indpred, 1, varno, 0);
info->unique = index->indisunique;
/* initialize cached join info to empty */ /* initialize cached join info to empty */
info->outer_relids = NULL; info->outer_relids = NULL;
info->inner_paths = NIL; info->inner_paths = NIL;
@ -372,15 +362,15 @@ has_unique_index(RelOptInfo *rel, AttrNumber attno)
IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist); IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
/* /*
* Note: ignore functional and partial indexes, since they don't * Note: ignore partial indexes, since they don't allow us to conclude
* allow us to conclude that all attr values are distinct. Also, a * that all attr values are distinct. We don't take any interest in
* multicolumn unique index doesn't allow us to conclude that just * expressional indexes either. Also, a multicolumn unique index
* the specified attr is unique. * doesn't allow us to conclude that just the specified attr is
* unique.
*/ */
if (index->unique && if (index->unique &&
index->nkeys == 1 && index->ncolumns == 1 &&
index->indexkeys[0] == attno && index->indexkeys[0] == attno &&
index->indproc == InvalidOid &&
index->indpred == NIL) index->indpred == NIL)
return true; return true;
} }

View File

@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.271 2003/05/06 00:20:32 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.272 2003/05/28 16:03:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1311,8 +1311,7 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
/* OK, add it to the index definition */ /* OK, add it to the index definition */
iparam = makeNode(IndexElem); iparam = makeNode(IndexElem);
iparam->name = pstrdup(key); iparam->name = pstrdup(key);
iparam->funcname = NIL; iparam->expr = NULL;
iparam->args = NIL;
iparam->opclass = NIL; iparam->opclass = NIL;
index->indexParams = lappend(index->indexParams, iparam); index->indexParams = lappend(index->indexParams, iparam);
} }
@ -1386,11 +1385,13 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
if (index->idxname == NULL && index->indexParams != NIL) if (index->idxname == NULL && index->indexParams != NIL)
{ {
iparam = lfirst(index->indexParams); iparam = (IndexElem *) lfirst(index->indexParams);
/* we should never see an expression item here */
Assert(iparam->expr == NULL);
index->idxname = CreateIndexName(cxt->relation->relname, index->idxname = CreateIndexName(cxt->relation->relname,
iparam->name ? iparam->name : iparam->name,
strVal(llast(iparam->funcname)), "key",
"key", cxt->alist); cxt->alist);
} }
if (index->idxname == NULL) /* should not happen */ if (index->idxname == NULL) /* should not happen */
elog(ERROR, "%s: failed to make implicit index name", elog(ERROR, "%s: failed to make implicit index name",
@ -1454,7 +1455,8 @@ static Query *
transformIndexStmt(ParseState *pstate, IndexStmt *stmt) transformIndexStmt(ParseState *pstate, IndexStmt *stmt)
{ {
Query *qry; Query *qry;
RangeTblEntry *rte; RangeTblEntry *rte = NULL;
List *l;
qry = makeNode(Query); qry = makeNode(Query);
qry->commandType = CMD_UTILITY; qry->commandType = CMD_UTILITY;
@ -1477,6 +1479,32 @@ transformIndexStmt(ParseState *pstate, IndexStmt *stmt)
stmt->whereClause = transformWhereClause(pstate, stmt->whereClause); stmt->whereClause = transformWhereClause(pstate, stmt->whereClause);
} }
/* take care of any index expressions */
foreach(l, stmt->indexParams)
{
IndexElem *ielem = (IndexElem *) lfirst(l);
if (ielem->expr)
{
/* Set up rtable as for predicate, see notes above */
if (rte == NULL)
{
rte = addRangeTableEntry(pstate, stmt->relation, NULL,
false, true);
/* no to join list, yes to namespace */
addRTEtoQuery(pstate, rte, false, true);
}
ielem->expr = transformExpr(pstate, ielem->expr);
/*
* We check only that the result type is legitimate; this is
* for consistency with what transformWhereClause() checks for
* the predicate. DefineIndex() will make more checks.
*/
if (expression_returns_set(ielem->expr))
elog(ERROR, "index expression may not return a set");
}
}
qry->hasSubLinks = pstate->p_hasSubLinks; qry->hasSubLinks = pstate->p_hasSubLinks;
stmt->rangetable = pstate->p_rtable; stmt->rangetable = pstate->p_rtable;

View File

@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.414 2003/05/15 16:35:28 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.415 2003/05/28 16:03:57 tgl Exp $
* *
* HISTORY * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
@ -210,7 +210,7 @@ static void doNegateFloat(Value *v);
oper_argtypes RuleActionList RuleActionMulti oper_argtypes RuleActionList RuleActionMulti
opt_column_list columnList opt_name_list opt_column_list columnList opt_name_list
sort_clause opt_sort_clause sortby_list index_params sort_clause opt_sort_clause sortby_list index_params
index_list name_list from_clause from_list opt_array_bounds name_list from_clause from_list opt_array_bounds
qualified_name_list any_name any_name_list qualified_name_list any_name any_name_list
any_operator expr_list dotted_name attrs any_operator expr_list dotted_name attrs
target_list update_target_list insert_column_list target_list update_target_list insert_column_list
@ -276,7 +276,7 @@ static void doNegateFloat(Value *v);
%type <columnref> columnref %type <columnref> columnref
%type <alias> alias_clause %type <alias> alias_clause
%type <sortgroupby> sortby %type <sortgroupby> sortby
%type <ielem> index_elem func_index %type <ielem> index_elem
%type <node> table_ref %type <node> table_ref
%type <jexpr> joined_table %type <jexpr> joined_table
%type <range> relation_expr %type <range> relation_expr
@ -408,7 +408,7 @@ static void doNegateFloat(Value *v);
%token UNIONJOIN %token UNIONJOIN
/* Special keywords, not in the query language - see the "lex" file */ /* Special keywords, not in the query language - see the "lex" file */
%token <str> IDENT FCONST SCONST NCONST BCONST XCONST Op %token <str> IDENT FCONST SCONST BCONST XCONST Op
%token <ival> ICONST PARAM %token <ival> ICONST PARAM
/* precedence: lowest to highest */ /* precedence: lowest to highest */
@ -2932,7 +2932,7 @@ function_with_argtypes:
* *
* QUERY: * QUERY:
* create index <indexname> on <relname> * create index <indexname> on <relname>
* [ using <access> ] "(" (<col> with <op>)+ ")" * [ using <access> ] "(" ( <col> [ using <opclass> ] )+ ")"
* [ where <predicate> ] * [ where <predicate> ]
* *
*****************************************************************************/ *****************************************************************************/
@ -2958,70 +2958,48 @@ index_opt_unique:
access_method_clause: access_method_clause:
USING access_method { $$ = $2; } USING access_method { $$ = $2; }
/* If btree changes as our default, update pg_get_indexdef() */
| /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; } | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
; ;
index_params: index_params: index_elem { $$ = makeList1($1); }
index_list { $$ = $1; } | index_params ',' index_elem { $$ = lappend($1, $3); }
| func_index { $$ = makeList1($1); }
; ;
index_list: index_elem { $$ = makeList1($1); } /*
| index_list ',' index_elem { $$ = lappend($1, $3); } * Index attributes can be either simple column references, or arbitrary
; * expressions in parens. For backwards-compatibility reasons, we allow
* an expression that's just a function call to be written without parens.
func_index: func_name '(' name_list ')' opt_class */
{ index_elem: attr_name opt_class
$$ = makeNode(IndexElem);
$$->name = NULL;
$$->funcname = $1;
$$->args = $3;
$$->opclass = $5;
}
;
index_elem: attr_name opt_class
{ {
$$ = makeNode(IndexElem); $$ = makeNode(IndexElem);
$$->name = $1; $$->name = $1;
$$->funcname = NIL; $$->expr = NULL;
$$->args = NIL;
$$->opclass = $2; $$->opclass = $2;
} }
| func_name '(' expr_list ')' opt_class
{
FuncCall *n = makeNode(FuncCall);
n->funcname = $1;
n->args = $3;
n->agg_star = FALSE;
n->agg_distinct = FALSE;
$$ = makeNode(IndexElem);
$$->name = NULL;
$$->expr = (Node *)n;
$$->opclass = $5;
}
| '(' a_expr ')' opt_class
{
$$ = makeNode(IndexElem);
$$->name = NULL;
$$->expr = $2;
$$->opclass = $4;
}
; ;
opt_class: any_name opt_class: any_name { $$ = $1; }
{
/*
* Release 7.0 removed network_ops, timespan_ops, and
* datetime_ops, so we suppress it from being passed to
* the parser so the default *_ops is used. This can be
* removed in some later release. bjm 2000/02/07
*
* Release 7.1 removes lztext_ops, so suppress that too
* for a while. tgl 2000/07/30
*
* Release 7.2 renames timestamp_ops to timestamptz_ops,
* so suppress that too for awhile. I'm starting to
* think we need a better approach. tgl 2000/10/01
*/
if (length($1) == 1)
{
char *claname = strVal(lfirst($1));
if (strcmp(claname, "network_ops") != 0 &&
strcmp(claname, "timespan_ops") != 0 &&
strcmp(claname, "datetime_ops") != 0 &&
strcmp(claname, "lztext_ops") != 0 &&
strcmp(claname, "timestamp_ops") != 0)
$$ = $1;
else
$$ = NIL;
}
else
$$ = $1;
}
| USING any_name { $$ = $2; } | USING any_name { $$ = $2; }
| /*EMPTY*/ { $$ = NIL; } | /*EMPTY*/ { $$ = NIL; }
; ;

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/pquery.c,v 1.65 2003/05/27 17:49:46 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/pquery.c,v 1.66 2003/05/28 16:03:58 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -713,8 +713,9 @@ PortalRunUtility(Portal portal, Query *query,
* without freezing a snapshot. By extension we allow SHOW * without freezing a snapshot. By extension we allow SHOW
* not to set a snapshot. The other stmts listed are just * not to set a snapshot. The other stmts listed are just
* efficiency hacks. Beware of listing anything that can * efficiency hacks. Beware of listing anything that can
* modify the database --- if, say, it has to update a * modify the database --- if, say, it has to update an
* functional index, then it had better have a snapshot. * index with expressions that invoke user-defined functions,
* then it had better have a snapshot.
*/ */
if (! (IsA(utilityStmt, TransactionStmt) || if (! (IsA(utilityStmt, TransactionStmt) ||
IsA(utilityStmt, LockStmt) || IsA(utilityStmt, LockStmt) ||

View File

@ -3,7 +3,7 @@
* back to source text * back to source text
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.140 2003/05/20 20:35:10 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.141 2003/05/28 16:03:59 tgl Exp $
* *
* This software is copyrighted by Jan Wieck - Hamburg. * This software is copyrighted by Jan Wieck - Hamburg.
* *
@ -535,12 +535,14 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
Form_pg_index idxrec; Form_pg_index idxrec;
Form_pg_class idxrelrec; Form_pg_class idxrelrec;
Form_pg_am amrec; Form_pg_am amrec;
List *indexprs;
List *context;
Oid indrelid; Oid indrelid;
int len; int len;
int keyno; int keyno;
Oid keycoltypes[INDEX_MAX_KEYS]; Oid keycoltype;
StringInfoData buf; StringInfoData buf;
StringInfoData keybuf; char *str;
char *sep; char *sep;
/* /*
@ -576,6 +578,30 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
elog(ERROR, "syscache lookup for AM %u failed", idxrelrec->relam); elog(ERROR, "syscache lookup for AM %u failed", idxrelrec->relam);
amrec = (Form_pg_am) GETSTRUCT(ht_am); amrec = (Form_pg_am) GETSTRUCT(ht_am);
/*
* Get the index expressions, if any. (NOTE: we do not use the relcache
* versions of the expressions and predicate, because we want to display
* non-const-folded expressions.)
*/
if (!heap_attisnull(ht_idx, Anum_pg_index_indexprs))
{
Datum exprsDatum;
bool isnull;
char *exprsString;
exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
Anum_pg_index_indexprs, &isnull);
Assert(!isnull);
exprsString = DatumGetCString(DirectFunctionCall1(textout,
exprsDatum));
indexprs = (List *) stringToNode(exprsString);
pfree(exprsString);
}
else
indexprs = NIL;
context = deparse_context_for(get_rel_name(indrelid), indrelid);
/* /*
* Start the index definition. Note that the index's name should * Start the index definition. Note that the index's name should
* never be schema-qualified, but the indexed rel's name may be. * never be schema-qualified, but the indexed rel's name may be.
@ -588,76 +614,72 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
quote_identifier(NameStr(amrec->amname))); quote_identifier(NameStr(amrec->amname)));
/* /*
* Collect the indexed attributes in keybuf * Report the indexed attributes
*/ */
initStringInfo(&keybuf);
sep = ""; sep = "";
for (keyno = 0; keyno < INDEX_MAX_KEYS; keyno++) for (keyno = 0; keyno < idxrec->indnatts; keyno++)
{ {
AttrNumber attnum = idxrec->indkey[keyno]; AttrNumber attnum = idxrec->indkey[keyno];
char *attname;
if (attnum == InvalidAttrNumber) appendStringInfo(&buf, sep);
break;
attname = get_relid_attribute_name(indrelid, attnum);
keycoltypes[keyno] = get_atttype(indrelid, attnum);
appendStringInfo(&keybuf, sep);
sep = ", "; sep = ", ";
/* if (attnum != 0)
* Add the indexed field name {
*/ /* Simple index column */
appendStringInfo(&keybuf, "%s", quote_identifier(attname)); char *attname;
attname = get_relid_attribute_name(indrelid, attnum);
appendStringInfo(&buf, "%s", quote_identifier(attname));
keycoltype = get_atttype(indrelid, attnum);
}
else
{
/* expressional index */
Node *indexkey;
if (indexprs == NIL)
elog(ERROR, "too few entries in indexprs list");
indexkey = (Node *) lfirst(indexprs);
indexprs = lnext(indexprs);
/* Deparse */
str = deparse_expression(indexkey, context, false, false);
/* Need parens if it's not a bare function call */
if (indexkey && IsA(indexkey, FuncExpr) &&
((FuncExpr *) indexkey)->funcformat == COERCE_EXPLICIT_CALL)
appendStringInfo(&buf, "%s", str);
else
appendStringInfo(&buf, "(%s)", str);
keycoltype = exprType(indexkey);
}
/* /*
* If not a functional index, add the operator class name * Add the operator class name
*/ */
if (idxrec->indproc == InvalidOid) get_opclass_name(idxrec->indclass[keyno], keycoltype,
get_opclass_name(idxrec->indclass[keyno],
keycoltypes[keyno],
&keybuf);
}
if (idxrec->indproc != InvalidOid)
{
/*
* For functional index say 'func (attrs) opclass'
*/
appendStringInfo(&buf, "%s(%s)",
generate_function_name(idxrec->indproc,
keyno, keycoltypes),
keybuf.data);
get_opclass_name(idxrec->indclass[0],
get_func_rettype(idxrec->indproc),
&buf); &buf);
} }
else
{
/*
* Otherwise say 'attr opclass [, ...]'
*/
appendStringInfo(&buf, "%s", keybuf.data);
}
appendStringInfoChar(&buf, ')'); appendStringInfoChar(&buf, ')');
/* /*
* If it's a partial index, decompile and append the predicate * If it's a partial index, decompile and append the predicate
*/ */
if (VARSIZE(&idxrec->indpred) > VARHDRSZ) if (!heap_attisnull(ht_idx, Anum_pg_index_indpred))
{ {
Node *node; Node *node;
List *context; Datum predDatum;
char *exprstr; bool isnull;
char *str; char *predString;
/* Convert TEXT object to C string */ /* Convert text string to node tree */
exprstr = DatumGetCString(DirectFunctionCall1(textout, predDatum = SysCacheGetAttr(INDEXRELID, ht_idx,
PointerGetDatum(&idxrec->indpred))); Anum_pg_index_indpred, &isnull);
/* Convert expression to node tree */ Assert(!isnull);
node = (Node *) stringToNode(exprstr); predString = DatumGetCString(DirectFunctionCall1(textout,
predDatum));
node = (Node *) stringToNode(predString);
pfree(predString);
/* /*
* If top level is a List, assume it is an implicit-AND structure, * If top level is a List, assume it is an implicit-AND structure,
@ -667,7 +689,6 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
if (node && IsA(node, List)) if (node && IsA(node, List))
node = (Node *) make_ands_explicit((List *) node); node = (Node *) make_ands_explicit((List *) node);
/* Deparse */ /* Deparse */
context = deparse_context_for(get_rel_name(indrelid), indrelid);
str = deparse_expression(node, context, false, false); str = deparse_expression(node, context, false, false);
appendStringInfo(&buf, " WHERE %s", str); appendStringInfo(&buf, " WHERE %s", str);
} }
@ -681,7 +702,6 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
memcpy(VARDATA(indexdef), buf.data, buf.len); memcpy(VARDATA(indexdef), buf.data, buf.len);
pfree(buf.data); pfree(buf.data);
pfree(keybuf.data);
ReleaseSysCache(ht_idx); ReleaseSysCache(ht_idx);
ReleaseSysCache(ht_idxrel); ReleaseSysCache(ht_idxrel);

View File

@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.138 2003/05/26 00:11:27 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.139 2003/05/28 16:03:59 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -3905,14 +3905,13 @@ btcostestimate(PG_FUNCTION_ARGS)
indexSelectivity, indexCorrelation); indexSelectivity, indexCorrelation);
/* /*
* If it's a functional index, leave the default zero-correlation * If the first column is a simple variable, and we can get an estimate
* estimate in place. If not, and if we can get an estimate for the * for its ordering correlation C from pg_statistic, estimate
* first variable's ordering correlation C from pg_statistic, estimate
* the index correlation as C / number-of-columns. (The idea here is * the index correlation as C / number-of-columns. (The idea here is
* that multiple columns dilute the importance of the first column's * that multiple columns dilute the importance of the first column's
* ordering, but don't negate it entirely.) * ordering, but don't negate it entirely.)
*/ */
if (index->indproc == InvalidOid) if (index->indexkeys[0] != 0)
{ {
Oid relid; Oid relid;
HeapTuple tuple; HeapTuple tuple;
@ -3942,8 +3941,7 @@ btcostestimate(PG_FUNCTION_ARGS)
Assert(nnumbers == 1); Assert(nnumbers == 1);
varCorrelation = numbers[0]; varCorrelation = numbers[0];
for (nKeys = 1; index->indexkeys[nKeys] != 0; nKeys++) nKeys = index->ncolumns;
/* skip */ ;
*indexCorrelation = varCorrelation / nKeys; *indexCorrelation = varCorrelation / nKeys;

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.184 2003/02/09 06:56:28 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.185 2003/05/28 16:03:59 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -52,6 +52,8 @@
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "commands/trigger.h" #include "commands/trigger.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "optimizer/clauses.h"
#include "optimizer/planmain.h"
#include "storage/smgr.h" #include "storage/smgr.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/catcache.h" #include "utils/catcache.h"
@ -939,10 +941,9 @@ void
RelationInitIndexAccessInfo(Relation relation) RelationInitIndexAccessInfo(Relation relation)
{ {
HeapTuple tuple; HeapTuple tuple;
Size iformsize;
Form_pg_index iform;
Form_pg_am aform; Form_pg_am aform;
MemoryContext indexcxt; MemoryContext indexcxt;
MemoryContext oldcontext;
IndexStrategy strategy; IndexStrategy strategy;
Oid *operator; Oid *operator;
RegProcedure *support; RegProcedure *support;
@ -952,8 +953,9 @@ RelationInitIndexAccessInfo(Relation relation)
uint16 amsupport; uint16 amsupport;
/* /*
* Make a copy of the pg_index entry for the index. Note that this is * Make a copy of the pg_index entry for the index. Since pg_index
* a variable-length tuple. * contains variable-length and possibly-null fields, we have to do
* this honestly rather than just treating it as a Form_pg_index struct.
*/ */
tuple = SearchSysCache(INDEXRELID, tuple = SearchSysCache(INDEXRELID,
ObjectIdGetDatum(RelationGetRelid(relation)), ObjectIdGetDatum(RelationGetRelid(relation)),
@ -961,11 +963,11 @@ RelationInitIndexAccessInfo(Relation relation)
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple))
elog(ERROR, "RelationInitIndexAccessInfo: no pg_index entry for index %u", elog(ERROR, "RelationInitIndexAccessInfo: no pg_index entry for index %u",
RelationGetRelid(relation)); RelationGetRelid(relation));
iformsize = tuple->t_len - tuple->t_data->t_hoff; oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
iform = (Form_pg_index) MemoryContextAlloc(CacheMemoryContext, iformsize); relation->rd_indextuple = heap_copytuple(tuple);
memcpy(iform, GETSTRUCT(tuple), iformsize); relation->rd_index = (Form_pg_index) GETSTRUCT(relation->rd_indextuple);
MemoryContextSwitchTo(oldcontext);
ReleaseSysCache(tuple); ReleaseSysCache(tuple);
relation->rd_index = iform;
/* /*
* Make a copy of the pg_am entry for the index's access method * Make a copy of the pg_am entry for the index's access method
@ -982,6 +984,9 @@ RelationInitIndexAccessInfo(Relation relation)
relation->rd_am = aform; relation->rd_am = aform;
natts = relation->rd_rel->relnatts; natts = relation->rd_rel->relnatts;
if (natts != relation->rd_index->indnatts)
elog(ERROR, "RelationInitIndexAccessInfo: relnatts disagrees with indnatts for index %u",
RelationGetRelid(relation));
amstrategies = aform->amstrategies; amstrategies = aform->amstrategies;
amsupport = aform->amsupport; amsupport = aform->amsupport;
@ -1047,9 +1052,15 @@ RelationInitIndexAccessInfo(Relation relation)
* Fill the strategy map and the support RegProcedure arrays. * Fill the strategy map and the support RegProcedure arrays.
* (supportinfo is left as zeroes, and is filled on-the-fly when used) * (supportinfo is left as zeroes, and is filled on-the-fly when used)
*/ */
IndexSupportInitialize(iform, IndexSupportInitialize(relation->rd_index,
strategy, operator, support, strategy, operator, support,
amstrategies, amsupport, natts); amstrategies, amsupport, natts);
/*
* expressions and predicate cache will be filled later
*/
relation->rd_indexprs = NIL;
relation->rd_indpred = NIL;
} }
/* /*
@ -1087,8 +1098,7 @@ IndexSupportInitialize(Form_pg_index iform,
{ {
OpClassCacheEnt *opcentry; OpClassCacheEnt *opcentry;
if (iform->indkey[attIndex] == InvalidAttrNumber || if (!OidIsValid(iform->indclass[attIndex]))
!OidIsValid(iform->indclass[attIndex]))
elog(ERROR, "IndexSupportInitialize: bogus pg_index tuple"); elog(ERROR, "IndexSupportInitialize: bogus pg_index tuple");
/* look up the info for this opclass, using a cache */ /* look up the info for this opclass, using a cache */
@ -1729,8 +1739,8 @@ RelationClearRelation(Relation relation, bool rebuild)
* with, we can only get rid of these fields: * with, we can only get rid of these fields:
*/ */
FreeTriggerDesc(relation->trigdesc); FreeTriggerDesc(relation->trigdesc);
if (relation->rd_index) if (relation->rd_indextuple)
pfree(relation->rd_index); pfree(relation->rd_indextuple);
if (relation->rd_am) if (relation->rd_am)
pfree(relation->rd_am); pfree(relation->rd_am);
if (relation->rd_rel) if (relation->rd_rel)
@ -2659,6 +2669,136 @@ insert_ordered_oid(List *list, Oid datum)
return list; return list;
} }
/*
* RelationGetIndexExpressions -- get the index expressions for an index
*
* We cache the result of transforming pg_index.indexprs into a node tree.
* If the rel is not an index or has no expressional columns, we return NIL.
* Otherwise, the returned tree is copied into the caller's memory context.
* (We don't want to return a pointer to the relcache copy, since it could
* disappear due to relcache invalidation.)
*/
List *
RelationGetIndexExpressions(Relation relation)
{
List *result;
Datum exprsDatum;
bool isnull;
char *exprsString;
MemoryContext oldcxt;
/* Quick exit if we already computed the result. */
if (relation->rd_indexprs)
return (List *) copyObject(relation->rd_indexprs);
/* Quick exit if there is nothing to do. */
if (relation->rd_indextuple == NULL ||
heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs))
return NIL;
/*
* We build the tree we intend to return in the caller's context.
* After successfully completing the work, we copy it into the relcache
* entry. This avoids problems if we get some sort of
* error partway through.
*
* We make use of the syscache's copy of pg_index's tupledesc
* to access the non-fixed fields of the tuple. We assume that
* the syscache will be initialized before any access of a
* partial index could occur. (This would probably fail if we
* were to allow partial indexes on system catalogs.)
*/
exprsDatum = SysCacheGetAttr(INDEXRELID, relation->rd_indextuple,
Anum_pg_index_indexprs, &isnull);
Assert(!isnull);
exprsString = DatumGetCString(DirectFunctionCall1(textout, exprsDatum));
result = (List *) stringToNode(exprsString);
pfree(exprsString);
/*
* Run the expressions through eval_const_expressions. This is not just
* an optimization, but is necessary, because the planner will be
* comparing them to const-folded qual clauses, and may fail to detect
* valid matches without this.
*/
result = (List *) eval_const_expressions((Node *) result);
/* May as well fix opfuncids too */
fix_opfuncids((Node *) result);
/* Now save a copy of the completed tree in the relcache entry. */
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
relation->rd_indexprs = (List *) copyObject(result);
MemoryContextSwitchTo(oldcxt);
return result;
}
/*
* RelationGetIndexPredicate -- get the index predicate for an index
*
* We cache the result of transforming pg_index.indpred into a node tree.
* If the rel is not an index or has no predicate, we return NIL.
* Otherwise, the returned tree is copied into the caller's memory context.
* (We don't want to return a pointer to the relcache copy, since it could
* disappear due to relcache invalidation.)
*/
List *
RelationGetIndexPredicate(Relation relation)
{
List *result;
Datum predDatum;
bool isnull;
char *predString;
MemoryContext oldcxt;
/* Quick exit if we already computed the result. */
if (relation->rd_indpred)
return (List *) copyObject(relation->rd_indpred);
/* Quick exit if there is nothing to do. */
if (relation->rd_indextuple == NULL ||
heap_attisnull(relation->rd_indextuple, Anum_pg_index_indpred))
return NIL;
/*
* We build the tree we intend to return in the caller's context.
* After successfully completing the work, we copy it into the relcache
* entry. This avoids problems if we get some sort of
* error partway through.
*
* We make use of the syscache's copy of pg_index's tupledesc
* to access the non-fixed fields of the tuple. We assume that
* the syscache will be initialized before any access of a
* partial index could occur. (This would probably fail if we
* were to allow partial indexes on system catalogs.)
*/
predDatum = SysCacheGetAttr(INDEXRELID, relation->rd_indextuple,
Anum_pg_index_indpred, &isnull);
Assert(!isnull);
predString = DatumGetCString(DirectFunctionCall1(textout, predDatum));
result = (List *) stringToNode(predString);
pfree(predString);
/*
* Run the expression through eval_const_expressions. This is not just
* an optimization, but is necessary, because the planner will be
* comparing it to const-folded qual clauses, and may fail to detect
* valid matches without this.
*/
result = (List *) eval_const_expressions((Node *) result);
/* May as well fix opfuncids too */
fix_opfuncids((Node *) result);
/* Now save a copy of the completed tree in the relcache entry. */
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
relation->rd_indpred = (List *) copyObject(result);
MemoryContextSwitchTo(oldcxt);
return result;
}
/* /*
* load_relcache_init_file, write_relcache_init_file * load_relcache_init_file, write_relcache_init_file
@ -2829,14 +2969,19 @@ load_relcache_init_file(void)
if (rel->rd_isnailed) if (rel->rd_isnailed)
nailed_indexes++; nailed_indexes++;
/* next, read the pg_index tuple form */ /* next, read the pg_index tuple */
if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len)) if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
goto read_failed; goto read_failed;
rel->rd_index = (Form_pg_index) palloc(len); rel->rd_indextuple = (HeapTuple) palloc(len);
if ((nread = fread(rel->rd_index, 1, len, fp)) != len) if ((nread = fread(rel->rd_indextuple, 1, len, fp)) != len)
goto read_failed; goto read_failed;
/* Fix up internal pointers in the tuple -- see heap_copytuple */
rel->rd_indextuple->t_datamcxt = CurrentMemoryContext;
rel->rd_indextuple->t_data = (HeapTupleHeader) ((char *) rel->rd_indextuple + HEAPTUPLESIZE);
rel->rd_index = (Form_pg_index) GETSTRUCT(rel->rd_indextuple);
/* next, read the access method tuple form */ /* next, read the access method tuple form */
if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len)) if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
goto read_failed; goto read_failed;
@ -2904,6 +3049,7 @@ load_relcache_init_file(void)
nailed_rels++; nailed_rels++;
Assert(rel->rd_index == NULL); Assert(rel->rd_index == NULL);
Assert(rel->rd_indextuple == NULL);
Assert(rel->rd_am == NULL); Assert(rel->rd_am == NULL);
Assert(rel->rd_indexcxt == NULL); Assert(rel->rd_indexcxt == NULL);
Assert(rel->rd_istrat == NULL); Assert(rel->rd_istrat == NULL);
@ -2917,11 +3063,13 @@ load_relcache_init_file(void)
* format is complex and subject to change). They must be rebuilt * format is complex and subject to change). They must be rebuilt
* if needed by RelationCacheInitializePhase2. This is not * if needed by RelationCacheInitializePhase2. This is not
* expected to be a big performance hit since few system catalogs * expected to be a big performance hit since few system catalogs
* have such. * have such. Ditto for index expressions and predicates.
*/ */
rel->rd_rules = NULL; rel->rd_rules = NULL;
rel->rd_rulescxt = NULL; rel->rd_rulescxt = NULL;
rel->trigdesc = NULL; rel->trigdesc = NULL;
rel->rd_indexprs = NIL;
rel->rd_indpred = NIL;
/* /*
* Reset transient-state fields in the relcache entry * Reset transient-state fields in the relcache entry
@ -3076,26 +3224,15 @@ write_relcache_init_file(void)
if (rel->rd_rel->relkind == RELKIND_INDEX) if (rel->rd_rel->relkind == RELKIND_INDEX)
{ {
Form_pg_am am = rel->rd_am; Form_pg_am am = rel->rd_am;
HeapTuple tuple;
/* /* write the pg_index tuple */
* We need to write the index tuple form, but this is a bit /* we assume this was created by heap_copytuple! */
* tricky since it's a variable-length struct. Rather than len = HEAPTUPLESIZE + rel->rd_indextuple->t_len;
* hoping to intuit the length, fetch the pg_index tuple
* afresh using the syscache, and write that.
*/
tuple = SearchSysCache(INDEXRELID,
ObjectIdGetDatum(RelationGetRelid(rel)),
0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "write_relcache_init_file: no pg_index entry for index %u",
RelationGetRelid(rel));
len = tuple->t_len - tuple->t_data->t_hoff;
if (fwrite(&len, 1, sizeof(len), fp) != sizeof(len)) if (fwrite(&len, 1, sizeof(len), fp) != sizeof(len))
elog(FATAL, "cannot write init file -- index tuple form length"); elog(FATAL, "cannot write init file -- index tuple length");
if (fwrite(GETSTRUCT(tuple), 1, len, fp) != len)
elog(FATAL, "cannot write init file -- index tuple form"); if (fwrite(rel->rd_indextuple, 1, len, fp) != len)
ReleaseSysCache(tuple); elog(FATAL, "cannot write init file -- index tuple");
/* next, write the access method tuple form */ /* next, write the access method tuple form */
len = sizeof(FormData_pg_am); len = sizeof(FormData_pg_am);

View File

@ -3,7 +3,7 @@
* *
* Copyright 2000-2002 by PostgreSQL Global Development Group * Copyright 2000-2002 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.76 2003/03/27 16:57:39 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.77 2003/05/28 16:03:59 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "describe.h" #include "describe.h"
@ -711,10 +711,7 @@ describeOneTableDetails(const char *schemaname,
headers[cols] = NULL; headers[cols] = NULL;
/* Get column info (index requires additional checks) */ /* Get column info (index requires additional checks) */
if (tableinfo.relkind == 'i') printfPQExpBuffer(&buf, "SELECT a.attname,");
printfPQExpBuffer(&buf, "SELECT\n CASE i.indproc WHEN ('-'::pg_catalog.regproc) THEN a.attname\n ELSE SUBSTR(pg_catalog.pg_get_indexdef(attrelid),\n POSITION('(' in pg_catalog.pg_get_indexdef(attrelid)))\n END,");
else
printfPQExpBuffer(&buf, "SELECT a.attname,");
appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod)," appendPQExpBuffer(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod),"
"\n (SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d" "\n (SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d"
"\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)," "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"

View File

@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: catversion.h,v 1.196 2003/05/26 00:11:27 tgl Exp $ * $Id: catversion.h,v 1.197 2003/05/28 16:03:59 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 200305241 #define CATALOG_VERSION_NO 200305271
#endif #endif

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: dependency.h,v 1.7 2003/03/06 22:54:49 tgl Exp $ * $Id: dependency.h,v 1.8 2003/05/28 16:03:59 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -91,6 +91,11 @@ extern void recordDependencyOnExpr(const ObjectAddress *depender,
Node *expr, List *rtable, Node *expr, List *rtable,
DependencyType behavior); DependencyType behavior);
extern void recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
Node *expr, Oid relId,
DependencyType behavior,
DependencyType self_behavior);
/* in pg_depend.c */ /* in pg_depend.c */
extern void recordDependencyOn(const ObjectAddress *depender, extern void recordDependencyOn(const ObjectAddress *depender,

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: index.h,v 1.50 2002/09/23 00:42:48 tgl Exp $ * $Id: index.h,v 1.51 2003/05/28 16:04:00 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -18,6 +18,7 @@
#include "catalog/pg_index.h" #include "catalog/pg_index.h"
#include "nodes/execnodes.h" #include "nodes/execnodes.h"
#define DEFAULT_INDEX_TYPE "btree" #define DEFAULT_INDEX_TYPE "btree"
/* Typedef for callback function for IndexBuildHeapScan */ /* Typedef for callback function for IndexBuildHeapScan */
@ -40,12 +41,12 @@ extern Oid index_create(Oid heapRelationId,
extern void index_drop(Oid indexId); extern void index_drop(Oid indexId);
extern IndexInfo *BuildIndexInfo(Form_pg_index indexStruct); extern IndexInfo *BuildIndexInfo(Relation index);
extern void FormIndexDatum(IndexInfo *indexInfo, extern void FormIndexDatum(IndexInfo *indexInfo,
HeapTuple heapTuple, HeapTuple heapTuple,
TupleDesc heapDescriptor, TupleDesc heapDescriptor,
MemoryContext resultCxt, EState *estate,
Datum *datum, Datum *datum,
char *nullv); char *nullv);

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: pg_index.h,v 1.30 2003/03/10 22:28:19 tgl Exp $ * $Id: pg_index.h,v 1.31 2003/05/28 16:04:00 tgl Exp $
* *
* NOTES * NOTES
* the genbki.sh script reads this file and generates .bki * the genbki.sh script reads this file and generates .bki
@ -35,18 +35,19 @@ CATALOG(pg_index) BKI_WITHOUT_OIDS
{ {
Oid indexrelid; /* OID of the index */ Oid indexrelid; /* OID of the index */
Oid indrelid; /* OID of the relation it indexes */ Oid indrelid; /* OID of the relation it indexes */
regproc indproc; /* OID of function for functional index */ int2vector indkey; /* column numbers of indexed cols, or 0 */
int2vector indkey; /* column numbers of indexed attributes */
oidvector indclass; /* opclass identifiers */ oidvector indclass; /* opclass identifiers */
bool indisclustered; /* is this the index last clustered by? */ int2 indnatts; /* number of columns in index */
bool indisunique; /* is this a unique index? */ bool indisunique; /* is this a unique index? */
bool indisprimary; /* is this index for primary key? */ bool indisprimary; /* is this index for primary key? */
Oid indreference; /* oid of index of referenced relation (ie bool indisclustered; /* is this the index last clustered by? */
* - this index for foreign key) */
/* VARIABLE LENGTH FIELD: */ /* VARIABLE LENGTH FIELDS: */
text indexprs; /* expression trees for index attributes
* that are not simple column references;
* one for each zero entry in indkey[] */
text indpred; /* expression tree for predicate, if a text indpred; /* expression tree for predicate, if a
* partial index */ * partial index; else NULL */
} FormData_pg_index; } FormData_pg_index;
/* ---------------- /* ----------------
@ -63,13 +64,13 @@ typedef FormData_pg_index *Form_pg_index;
#define Natts_pg_index 10 #define Natts_pg_index 10
#define Anum_pg_index_indexrelid 1 #define Anum_pg_index_indexrelid 1
#define Anum_pg_index_indrelid 2 #define Anum_pg_index_indrelid 2
#define Anum_pg_index_indproc 3 #define Anum_pg_index_indkey 3
#define Anum_pg_index_indkey 4 #define Anum_pg_index_indclass 4
#define Anum_pg_index_indclass 5 #define Anum_pg_index_indnatts 5
#define Anum_pg_index_indisclustered 6 #define Anum_pg_index_indisunique 6
#define Anum_pg_index_indisunique 7 #define Anum_pg_index_indisprimary 7
#define Anum_pg_index_indisprimary 8 #define Anum_pg_index_indisclustered 8
#define Anum_pg_index_indreference 9 #define Anum_pg_index_indexprs 9
#define Anum_pg_index_indpred 10 #define Anum_pg_index_indpred 10
#endif /* PG_INDEX_H */ #endif /* PG_INDEX_H */

View File

@ -8,7 +8,7 @@
* <opcamid, opcname> --- that is, there is a row for each valid combination * <opcamid, opcname> --- that is, there is a row for each valid combination
* of opclass name and index access method type. This row specifies the * of opclass name and index access method type. This row specifies the
* expected input data type for the opclass (the type of the heap column, * expected input data type for the opclass (the type of the heap column,
* or the function output type in the case of a functional index). Note * or the expression output type in the case of an index expression). Note
* that types binary-coercible to the specified type will be accepted too. * that types binary-coercible to the specified type will be accepted too.
* *
* For a given <opcamid, opcintype> pair, there can be at most one row that * For a given <opcamid, opcintype> pair, there can be at most one row that
@ -26,7 +26,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: pg_opclass.h,v 1.49 2003/05/26 00:11:27 tgl Exp $ * $Id: pg_opclass.h,v 1.50 2003/05/28 16:04:00 tgl Exp $
* *
* NOTES * NOTES
* the genbki.sh script reads this file and generates .bki * the genbki.sh script reads this file and generates .bki

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: execnodes.h,v 1.97 2003/04/08 23:20:04 tgl Exp $ * $Id: execnodes.h,v 1.98 2003/05/28 16:04:00 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -27,19 +27,17 @@
/* ---------------- /* ----------------
* IndexInfo information * IndexInfo information
* *
* this class holds the information needed to construct new index * this struct holds the information needed to construct new index
* entries for a particular index. Used for both index_build and * entries for a particular index. Used for both index_build and
* retail creation of index entries. * retail creation of index entries.
* *
* NumIndexAttrs number of columns in this index * NumIndexAttrs number of columns in this index
* (1 if a func. index, else same as NumKeyAttrs)
* NumKeyAttrs number of key attributes for this index
* (ie, number of attrs from underlying relation)
* KeyAttrNumbers underlying-rel attribute numbers used as keys * KeyAttrNumbers underlying-rel attribute numbers used as keys
* (zeroes indicate expressions)
* Expressions expr trees for expression entries, or NIL if none
* ExpressionsState exec state for expressions, or NIL if none
* Predicate partial-index predicate, or NIL if none * Predicate partial-index predicate, or NIL if none
* PredicateState exec state for predicate, or NIL if none * PredicateState exec state for predicate, or NIL if none
* FuncOid OID of function, or InvalidOid if not f. index
* FuncInfo fmgr lookup data for function, if FuncOid valid
* Unique is it a unique index? * Unique is it a unique index?
* ---------------- * ----------------
*/ */
@ -47,12 +45,11 @@ typedef struct IndexInfo
{ {
NodeTag type; NodeTag type;
int ii_NumIndexAttrs; int ii_NumIndexAttrs;
int ii_NumKeyAttrs;
AttrNumber ii_KeyAttrNumbers[INDEX_MAX_KEYS]; AttrNumber ii_KeyAttrNumbers[INDEX_MAX_KEYS];
List *ii_Expressions; /* list of Expr */
List *ii_ExpressionsState; /* list of ExprState */
List *ii_Predicate; /* list of Expr */ List *ii_Predicate; /* list of Expr */
List *ii_PredicateState; /* list of ExprState */ List *ii_PredicateState; /* list of ExprState */
Oid ii_FuncOid;
FmgrInfo ii_FuncInfo;
bool ii_Unique; bool ii_Unique;
} IndexInfo; } IndexInfo;

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: parsenodes.h,v 1.237 2003/05/02 20:54:36 tgl Exp $ * $Id: parsenodes.h,v 1.238 2003/05/28 16:04:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -353,17 +353,15 @@ typedef struct ColumnDef
/* /*
* IndexElem - index parameters (used in CREATE INDEX) * IndexElem - index parameters (used in CREATE INDEX)
* *
* For a plain index, each 'name' is an attribute name in the heap relation; * For a plain index attribute, 'name' is the name of the table column to
* 'funcname' and 'args' are NIL. For a functional index, only one IndexElem * index, and 'expr' is NULL. For an index expression, 'name' is NULL and
* is allowed. It has name = NULL, funcname = name of function and args = * 'expr' is the expression tree.
* list of attribute names that are the function's arguments.
*/ */
typedef struct IndexElem typedef struct IndexElem
{ {
NodeTag type; NodeTag type;
char *name; /* name of attribute to index, or NULL */ char *name; /* name of attribute to index, or NULL */
List *funcname; /* qualified name of function */ Node *expr; /* expression to index, or NULL */
List *args; /* list of names of function arguments */
List *opclass; /* name of desired opclass; NIL = default */ List *opclass; /* name of desired opclass; NIL = default */
} IndexElem; } IndexElem;
@ -1271,8 +1269,8 @@ typedef struct IndexStmt
char *accessMethod; /* name of access method (eg. btree) */ char *accessMethod; /* name of access method (eg. btree) */
List *indexParams; /* a list of IndexElem */ List *indexParams; /* a list of IndexElem */
Node *whereClause; /* qualification (partial-index predicate) */ Node *whereClause; /* qualification (partial-index predicate) */
List *rangetable; /* range table for qual, filled in by List *rangetable; /* range table for qual and/or expressions,
* transformStmt() */ * filled in by transformStmt() */
bool unique; /* is index unique? */ bool unique; /* is index unique? */
bool primary; /* is index on primary key? */ bool primary; /* is index on primary key? */
bool isconstraint; /* is it from a CONSTRAINT clause? */ bool isconstraint; /* is it from a CONSTRAINT clause? */

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: relation.h,v 1.79 2003/02/08 20:20:55 tgl Exp $ * $Id: relation.h,v 1.80 2003/05/28 16:04:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -227,15 +227,16 @@ typedef struct RelOptInfo
* and indexes, but that created confusion without actually doing anything * and indexes, but that created confusion without actually doing anything
* useful. So now we have a separate IndexOptInfo struct for indexes. * useful. So now we have a separate IndexOptInfo struct for indexes.
* *
* ncolumns and nkeys are the same except for a functional index, * classlist[], indexkeys[], and ordering[] have ncolumns entries.
* wherein ncolumns is 1 (the single function output) while nkeys * Zeroes in the indexkeys[] array indicate index columns that are
* is the number of table columns passed to the function. classlist[] * expressions; there is one element in indexprs for each such column.
* and ordering[] have ncolumns entries, while indexkeys[] has nkeys
* entries.
* *
* Note: for historical reasons, the arrays classlist, indexkeys and * Note: for historical reasons, the classlist and ordering arrays have
* ordering have an extra entry that is always zero. Some code scans * an extra entry that is always zero. Some code scans until it sees a
* until it sees a zero rather than looking at ncolumns or nkeys. * zero entry, rather than looking at ncolumns.
*
* The indexprs and indpred expressions have been run through
* eval_const_expressions() for ease of matching to WHERE clauses.
*/ */
typedef struct IndexOptInfo typedef struct IndexOptInfo
@ -250,15 +251,14 @@ typedef struct IndexOptInfo
/* index descriptor information */ /* index descriptor information */
int ncolumns; /* number of columns in index */ int ncolumns; /* number of columns in index */
int nkeys; /* number of keys used by index */
Oid *classlist; /* OIDs of operator classes for columns */ Oid *classlist; /* OIDs of operator classes for columns */
int *indexkeys; /* column numbers of index's keys */ int *indexkeys; /* column numbers of index's keys, or 0 */
Oid *ordering; /* OIDs of sort operators for each column */ Oid *ordering; /* OIDs of sort operators for each column */
Oid relam; /* OID of the access method (in pg_am) */ Oid relam; /* OID of the access method (in pg_am) */
RegProcedure amcostestimate; /* OID of the access method's cost fcn */ RegProcedure amcostestimate; /* OID of the access method's cost fcn */
Oid indproc; /* OID of func if functional index, else 0 */ List *indexprs; /* expressions for non-simple index columns */
List *indpred; /* predicate if a partial index, else NIL */ List *indpred; /* predicate if a partial index, else NIL */
bool unique; /* true if a unique index */ bool unique; /* true if a unique index */
@ -289,9 +289,8 @@ typedef struct PathKeyItem
/* /*
* key typically points to a Var node, ie a relation attribute, but it * key typically points to a Var node, ie a relation attribute, but it
* can also point to a FuncExpr clause representing the value indexed by a * can also point to an arbitrary expression representing the value
* functional index. Someday we might allow arbitrary expressions as * indexed by an index expression.
* path keys, so don't assume more than you must.
*/ */
} PathKeyItem; } PathKeyItem;

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: clauses.h,v 1.62 2003/02/04 00:50:01 tgl Exp $ * $Id: clauses.h,v 1.63 2003/05/28 16:04:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -28,9 +28,6 @@ extern Expr *make_opclause(Oid opno, Oid opresulttype, bool opretset,
extern Node *get_leftop(Expr *clause); extern Node *get_leftop(Expr *clause);
extern Node *get_rightop(Expr *clause); extern Node *get_rightop(Expr *clause);
extern Expr *make_funcclause(Oid funcid, Oid funcresulttype, bool funcretset,
CoercionForm funcformat, List *funcargs);
extern bool not_clause(Node *clause); extern bool not_clause(Node *clause);
extern Expr *make_notclause(Expr *notclause); extern Expr *make_notclause(Expr *notclause);
extern Expr *get_notclausearg(Expr *notclause); extern Expr *get_notclausearg(Expr *notclause);

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: rel.h,v 1.64 2002/11/23 03:59:09 momjian Exp $ * $Id: rel.h,v 1.65 2003/05/28 16:04:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -132,6 +132,8 @@ typedef struct RelationData
/* These are non-NULL only for an index relation: */ /* These are non-NULL only for an index relation: */
Form_pg_index rd_index; /* pg_index tuple describing this index */ Form_pg_index rd_index; /* pg_index tuple describing this index */
struct HeapTupleData *rd_indextuple; /* all of pg_index tuple */
/* "struct HeapTupleData *" avoids need to include htup.h here */
Form_pg_am rd_am; /* pg_am tuple for index's AM */ Form_pg_am rd_am; /* pg_am tuple for index's AM */
/* index access support info (used only for an index relation) */ /* index access support info (used only for an index relation) */
@ -142,6 +144,8 @@ typedef struct RelationData
struct FmgrInfo *rd_supportinfo; /* lookup info for support struct FmgrInfo *rd_supportinfo; /* lookup info for support
* procedures */ * procedures */
/* "struct FmgrInfo" avoids need to include fmgr.h here */ /* "struct FmgrInfo" avoids need to include fmgr.h here */
List *rd_indexprs; /* index expression trees, if any */
List *rd_indpred; /* index predicate tree, if any */
/* statistics collection area */ /* statistics collection area */
PgStat_Info pgstat_info; PgStat_Info pgstat_info;

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: relcache.h,v 1.34 2002/08/06 02:36:35 tgl Exp $ * $Id: relcache.h,v 1.35 2003/05/28 16:04:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -32,6 +32,8 @@ extern void RelationClose(Relation relation);
* Routines to compute/retrieve additional cached information * Routines to compute/retrieve additional cached information
*/ */
extern List *RelationGetIndexList(Relation relation); extern List *RelationGetIndexList(Relation relation);
extern List *RelationGetIndexExpressions(Relation relation);
extern List *RelationGetIndexPredicate(Relation relation);
extern void RelationInitIndexAccessInfo(Relation relation); extern void RelationInitIndexAccessInfo(Relation relation);

View File

@ -36,8 +36,8 @@ def list_simple_ind(pgcnx):
ic.relname AS index_name, a.attname ic.relname AS index_name, a.attname
FROM pg_class bc, pg_class ic, pg_index i, pg_attribute a FROM pg_class bc, pg_class ic, pg_index i, pg_attribute a
WHERE i.indrelid = bc.oid AND i.indexrelid = bc.oid WHERE i.indrelid = bc.oid AND i.indexrelid = bc.oid
AND i.indkey[0] = a.attnum AND a.attrelid = bc.oid AND i.indkey[0] = a.attnum AND i.indnatts = 1
AND i.indproc = '0'::oid AND a.attisdropped = 'f' AND a.attrelid = bc.oid AND a.attisdropped = 'f'
ORDER BY class_name, index_name, attname""") ORDER BY class_name, index_name, attname""")
return result return result

View File

@ -81,3 +81,17 @@ INSERT INTO func_index_heap VALUES('ABCD', 'EF');
ERROR: Cannot insert a duplicate key into unique index func_index_index ERROR: Cannot insert a duplicate key into unique index func_index_index
-- but this shouldn't: -- but this shouldn't:
INSERT INTO func_index_heap VALUES('QWERTY'); INSERT INTO func_index_heap VALUES('QWERTY');
--
-- Same test, expressional index
--
DROP TABLE func_index_heap;
CREATE TABLE func_index_heap (f1 text, f2 text);
CREATE UNIQUE INDEX func_index_index on func_index_heap ((f1 || f2) text_ops);
INSERT INTO func_index_heap VALUES('ABC','DEF');
INSERT INTO func_index_heap VALUES('AB','CDEFG');
INSERT INTO func_index_heap VALUES('QWE','RTY');
-- this should fail because of unique index:
INSERT INTO func_index_heap VALUES('ABCD', 'EF');
ERROR: Cannot insert a duplicate key into unique index func_index_index
-- but this shouldn't:
INSERT INTO func_index_heap VALUES('QWERTY');

View File

@ -75,7 +75,7 @@ FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = relnamespace
WHERE relhasoids WHERE relhasoids
AND ((nspname ~ '^pg_') IS NOT FALSE) AND ((nspname ~ '^pg_') IS NOT FALSE)
AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE indrelid = c.oid AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE indrelid = c.oid
AND indkey[0] = -2 AND indkey[1] = 0 AND indisunique); AND indkey[0] = -2 AND indnatts = 1 AND indisunique);
relname | nspname relname | nspname
---------+--------- ---------+---------
(0 rows) (0 rows)

View File

@ -104,3 +104,19 @@ INSERT INTO func_index_heap VALUES('QWE','RTY');
INSERT INTO func_index_heap VALUES('ABCD', 'EF'); INSERT INTO func_index_heap VALUES('ABCD', 'EF');
-- but this shouldn't: -- but this shouldn't:
INSERT INTO func_index_heap VALUES('QWERTY'); INSERT INTO func_index_heap VALUES('QWERTY');
--
-- Same test, expressional index
--
DROP TABLE func_index_heap;
CREATE TABLE func_index_heap (f1 text, f2 text);
CREATE UNIQUE INDEX func_index_index on func_index_heap ((f1 || f2) text_ops);
INSERT INTO func_index_heap VALUES('ABC','DEF');
INSERT INTO func_index_heap VALUES('AB','CDEFG');
INSERT INTO func_index_heap VALUES('QWE','RTY');
-- this should fail because of unique index:
INSERT INTO func_index_heap VALUES('ABCD', 'EF');
-- but this shouldn't:
INSERT INTO func_index_heap VALUES('QWERTY');

View File

@ -21,4 +21,4 @@ FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = relnamespace
WHERE relhasoids WHERE relhasoids
AND ((nspname ~ '^pg_') IS NOT FALSE) AND ((nspname ~ '^pg_') IS NOT FALSE)
AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE indrelid = c.oid AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE indrelid = c.oid
AND indkey[0] = -2 AND indkey[1] = 0 AND indisunique); AND indkey[0] = -2 AND indnatts = 1 AND indisunique);

View File

@ -7,7 +7,7 @@
-- Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group -- Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
-- Portions Copyright (c) 1994, Regents of the University of California -- Portions Copyright (c) 1994, Regents of the University of California
-- --
-- $Id: syscat.source,v 1.7 2002/06/20 20:29:54 momjian Exp $ -- $Id: syscat.source,v 1.8 2003/05/28 16:04:02 tgl Exp $
-- --
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
@ -31,8 +31,8 @@ SELECT relname
-- --
-- lists all simple indices (ie. those that are not defined over a function -- lists all simple indices (ie. those that are defined over one simple
-- of several attributes) -- column reference)
-- --
SELECT bc.relname AS class_name, SELECT bc.relname AS class_name,
ic.relname AS index_name, ic.relname AS index_name,
@ -44,8 +44,8 @@ SELECT bc.relname AS class_name,
WHERE i.indrelid = bc.oid WHERE i.indrelid = bc.oid
and i.indexrelid = ic.oid and i.indexrelid = ic.oid
and i.indkey[0] = a.attnum and i.indkey[0] = a.attnum
and i.indnatts = 1
and a.attrelid = bc.oid and a.attrelid = bc.oid
and i.indproc = '0'::oid -- no functional indices
ORDER BY class_name, index_name, attname; ORDER BY class_name, index_name, attname;