Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section on INCLUDE option to postgres doc #48027

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions guides/source/active_record_postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ After reading this guide, you will know:

* How to use PostgreSQL's datatypes.
* How to use UUID primary keys.
* How to include non-key columns in indexes.
* How to use deferrable foreign keys.
* How to implement full text search with PostgreSQL.
* How to back your Active Record models with database views.
Expand Down Expand Up @@ -543,6 +544,34 @@ When building a model with a foreign key that will reference this UUID, treat
$ rails generate model Case device_id:uuid
```

Indexing
--------

* [index creation](https://www.postgresql.org/docs/current/sql-createindex.html)

PostgreSQL includes a variety of index options. The following options are
supported by the PostgreSQL adapter in addition to the
[common index options](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_index)

### Include
steve-abrams marked this conversation as resolved.
Show resolved Hide resolved

When creating a new index, non-key columns can be included with the `:include` option.
These keys are not used in index scans for searching, but can be read during an index
only scan without having to visit the associated table.

```ruby
# db/migrate/20131220144913_add_index_users_on_email_include_id.rb

add_index :users, :email, include: :id
```

Multiple columns are supported:

```ruby
# db/migrate/20131220144913_add_index_users_on_email_include_id_and_created_at.rb

add_index :users, :email, include: [:id, :created_at]
```

Generated Columns
-----------------
Expand Down