Skip to content

Commit c5a91a3

Browse files
authored
Merge pull request rails#48027 from steve-abrams/sabrams-update-postgres-doc
Add section on INCLUDE option to postgres doc [ci-skip]
2 parents 719558c + 95f4d75 commit c5a91a3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

guides/source/active_record_postgresql.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ After reading this guide, you will know:
99

1010
* How to use PostgreSQL's datatypes.
1111
* How to use UUID primary keys.
12+
* How to include non-key columns in indexes.
1213
* How to use deferrable foreign keys.
1314
* How to use unique constraints.
1415
* How to implement full text search with PostgreSQL.
@@ -544,6 +545,34 @@ When building a model with a foreign key that will reference this UUID, treat
544545
$ rails generate model Case device_id:uuid
545546
```
546547

548+
Indexing
549+
--------
550+
551+
* [index creation](https://www.postgresql.org/docs/current/sql-createindex.html)
552+
553+
PostgreSQL includes a variety of index options. The following options are
554+
supported by the PostgreSQL adapter in addition to the
555+
[common index options](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_index)
556+
557+
### Include
558+
559+
When creating a new index, non-key columns can be included with the `:include` option.
560+
These keys are not used in index scans for searching, but can be read during an index
561+
only scan without having to visit the associated table.
562+
563+
```ruby
564+
# db/migrate/20131220144913_add_index_users_on_email_include_id.rb
565+
566+
add_index :users, :email, include: :id
567+
```
568+
569+
Multiple columns are supported:
570+
571+
```ruby
572+
# db/migrate/20131220144913_add_index_users_on_email_include_id_and_created_at.rb
573+
574+
add_index :users, :email, include: [:id, :created_at]
575+
```
547576

548577
Generated Columns
549578
-----------------

0 commit comments

Comments
 (0)