Commits

Mike Bayer  committed a6f1f70

- Added "postgresql_using" argument to Index(), produces
USING clause to specify index implementation for
PG. [ticket:2290]. Thanks to Ryan P. Kelly for
the patch.

  • Participants
  • Parent commits e42476d

Comments (0)

Files changed (3)

     extensions are in use or not.
 
 - postgresql
+  - Added "postgresql_using" argument to Index(), produces
+    USING clause to specify index implementation for
+    PG.  [ticket:2290].  Thanks to Ryan P. Kelly for
+    the patch.
+
   - Reflection functions for Table, Sequence no longer
     case insensitive.  Names can be differ only in case
     and will be correctly distinguished.  [ticket:2256]

File lib/sqlalchemy/dialects/postgresql/base.py

 of :class:`.Table`, which can be configured to be different than the actual
 name of the column as expressed in the database.
 
+Index Types
+^^^^^^^^^^^^
+
+PostgreSQL provides several index types: B-Tree, Hash, GiST, and GIN, as well as
+the ability for users to create their own (see
+http://www.postgresql.org/docs/8.3/static/indexes-types.html). These can be
+specified on :class:`.Index` using the ``postgresql_using`` keyword argument::
+
+    Index('my_index', my_table.c.data, postgresql_using='gin')
+
+The value passed to the keyword argument will be simply passed through to the
+underlying CREATE INDEX command, so it *must* be a valid index type for your
+version of PostgreSQL.
+
 """
 
 import re
         if index.unique:
             text += "UNIQUE "
         ops = index.kwargs.get('postgresql_ops', {})
-        text += "INDEX %s ON %s (%s)" \
-                % (
+        text += "INDEX %s ON %s " % (
                     preparer.quote(
                         self._index_identifier(index.name), index.quote),
-                    preparer.format_table(index.table),
+                    preparer.format_table(index.table)
+                )
+
+        if 'postgresql_using' in index.kwargs:
+            using = index.kwargs['postgresql_using']
+            text += "USING %s " % preparer.quote(using, index.quote)
+
+        text += "(%s)" \
+                % (
                     ', '.join([
                         preparer.format_column(c) + 
                         (c.key in ops and (' ' + ops[c.key]) or '')

File test/dialect/test_postgresql.py

                             '(data text_pattern_ops, data2 int4_ops)',
                             dialect=postgresql.dialect())
 
+    def test_create_index_with_using(self):
+        m = MetaData()
+        tbl = Table('testtbl', m, Column('data', String))
+
+        idx1 = Index('test_idx1', tbl.c.data)
+        idx2 = Index('test_idx2', tbl.c.data, postgresql_using='btree')
+        idx3 = Index('test_idx3', tbl.c.data, postgresql_using='hash')
+
+        self.assert_compile(schema.CreateIndex(idx1),
+                            'CREATE INDEX test_idx1 ON testtbl '
+                            '(data)',
+                            dialect=postgresql.dialect())
+        self.assert_compile(schema.CreateIndex(idx2),
+                            'CREATE INDEX test_idx2 ON testtbl '
+                            'USING btree (data)',
+                            dialect=postgresql.dialect())
+        self.assert_compile(schema.CreateIndex(idx3),
+                            'CREATE INDEX test_idx3 ON testtbl '
+                            'USING hash (data)',
+                            dialect=postgresql.dialect())
+
     @testing.uses_deprecated(r".*'postgres_where' argument has been "
                              "renamed.*")
     def test_old_create_partial_index(self):