Skip to content

Commit 15a7a92

Browse files
committed
Add include_invalid option for DB#indexes in PostgreSQL
1 parent 309ad4c commit 15a7a92

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/postgresql.rdoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ handle locking:
553553
# INSERT INTO "table" ("id") VALUES (2) RETURNING NULL;
554554
# COMMIT;
555555

556+
== PostgreSQL-specific reflection Support
557+
558+
The Database#indexes method supports the options :include_partial and :include_invalid.
559+
These options allow partial and invalid indexes to be included in the results, respectively.
560+
By default, Sequel excludes both partial and invalid indexes.
561+
556562
== Extended Error Info (<tt>postgres/pg only</tt>)
557563

558564
If you run a query that raises a Sequel::DatabaseError, you can pass the exception object to

lib/sequel/adapters/shared/postgres.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ def indexes(table, opts=OPTS)
674674
m = output_identifier_meth
675675
cond = {Sequel[:tab][:oid]=>regclass_oid(table, opts)}
676676
cond[:indpred] = nil unless opts[:include_partial]
677+
cond[:indisvalid] = true unless opts[:include_invalid]
677678

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

spec/adapters/postgres_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,13 @@ def before_create
15761576
@db.indexes(:test, :include_partial=>true)[:tnv_partial].must_equal(:columns=>[:name, :value], :unique=>false, :deferrable=>nil)
15771577
end
15781578

1579+
it "should support parsing invalid indexes with :include_invalid option" do
1580+
@db.add_index :test, [:name, :value], :name=>:tnv_invalid
1581+
@db.run("UPDATE pg_index SET indisvalid = false WHERE indexrelid = 'tnv_invalid'::regclass;")
1582+
@db.indexes(:test)[:tnv_invalid].must_be_nil
1583+
@db.indexes(:test, :include_invalid=>true)[:tnv_invalid].must_equal(:columns=>[:name, :value], :unique=>false, :deferrable=>nil)
1584+
end
1585+
15791586
it "should support creating indexes concurrently" do
15801587
@db.add_index :test, [:name, :value], :concurrently=>true, :name=>'tnv0'
15811588
end

0 commit comments

Comments
 (0)