Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/postgresql.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,12 @@ handle locking:
# INSERT INTO "table" ("id") VALUES (2) RETURNING NULL;
# COMMIT;

== PostgreSQL-specific reflection Support

The Database#indexes method supports the options :include_partial and :include_invalid.
These options allow partial and invalid indexes to be included in the results, respectively.
By default, Sequel excludes both partial and invalid indexes.

== Extended Error Info (<tt>postgres/pg only</tt>)

If you run a query that raises a Sequel::DatabaseError, you can pass the exception object to
Expand Down
6 changes: 3 additions & 3 deletions lib/sequel/adapters/shared/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ def indexes(table, opts=OPTS)
m = output_identifier_meth
cond = {Sequel[:tab][:oid]=>regclass_oid(table, opts)}
cond[:indpred] = nil unless opts[:include_partial]
cond[:indisvalid] = true unless opts[:include_invalid]

indexes = {}
_indexes_ds.where_each(cond) do |r|
Expand Down Expand Up @@ -1049,8 +1050,7 @@ def _indexes_ds
where{{
indc[:relkind]=>%w'i I',
ind[:indisprimary]=>false,
:indexprs=>nil,
:indisvalid=>true}}.
:indexprs=>nil}}.
order(*order).
select{[indc[:relname].as(:name), ind[:indisunique].as(:unique), att[:attname].as(:column), con[:condeferrable].as(:deferrable)]}

Expand Down Expand Up @@ -1757,7 +1757,7 @@ def index_definition_sql(table_name, index)
index_type = :gist
end

"CREATE #{unique}INDEX#{' CONCURRENTLY' if index[:concurrently]}#{if_not_exists} #{quote_identifier(index_name)} ON #{quote_schema_table(table_name)} #{"USING #{index_type} " if index_type}#{expr}#{" INCLUDE #{literal(Array(index[:include]))}" if index[:include]}#{nulls_distinct}#{" TABLESPACE #{quote_identifier(index[:tablespace])}" if index[:tablespace]}#{filter}"
"CREATE #{unique}INDEX#{' CONCURRENTLY' if index[:concurrently]}#{if_not_exists} #{quote_identifier(index_name)} ON#{' ONLY' if index[:only]} #{quote_schema_table(table_name)} #{"USING #{index_type} " if index_type}#{expr}#{" INCLUDE #{literal(Array(index[:include]))}" if index[:include]}#{nulls_distinct}#{" TABLESPACE #{quote_identifier(index[:tablespace])}" if index[:tablespace]}#{filter}"
end

# Setup datastructures shared by all postgres adapters.
Expand Down
1 change: 1 addition & 0 deletions lib/sequel/database/schema_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ def has_column?(name)
# :opclass :: Set an opclass to use for all columns (per-column opclasses require
# custom SQL).
# :tablespace :: Specify tablespace for index.
# :only :: Create index only for given table, not for any child tables (PostgreSQL 11+)
#
# Microsoft SQL Server specific options:
#
Expand Down
16 changes: 16 additions & 0 deletions spec/adapters/postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,13 @@ def before_create
@db.indexes(:test, :include_partial=>true)[:tnv_partial].must_equal(:columns=>[:name, :value], :unique=>false, :deferrable=>nil)
end

it "should support parsing invalid indexes with :include_invalid option" do
@db.add_index :test, [:name, :value], :name=>:tnv_invalid
@db.run("UPDATE pg_index SET indisvalid = false WHERE indexrelid = 'tnv_invalid'::regclass;")
@db.indexes(:test)[:tnv_invalid].must_be_nil
@db.indexes(:test, :include_invalid=>true)[:tnv_invalid].must_equal(:columns=>[:name, :value], :unique=>false, :deferrable=>nil)
end

it "should support creating indexes concurrently" do
@db.add_index :test, [:name, :value], :concurrently=>true, :name=>'tnv0'
end
Expand Down Expand Up @@ -1634,6 +1641,15 @@ def before_create
@db.add_index :atest, :a, :tablespace=>:pg_default
end

it "should support creating indexes with ONLY option" do
@db.create_table(:atest, partition_by: :id, :partition_type=>:range){Integer :id}
@db.create_table(:btest, partition_of: :atest){from 1; to 3}
@db.add_index :atest, :id, :only=>true

@db.indexes(:atest, :include_invalid=>true).size.must_equal 1
@db.indexes(:btest, :include_invalid=>true).must_be_empty
end if DB.server_version >= 110000

it "#lock should lock table if inside a transaction" do
@db.transaction{@d.lock('EXCLUSIVE'); @d.insert(:name=>'a')}
end
Expand Down