Skip to content

Commit

Permalink
Support :unlogged_tables_default Database option on Postgres for maki…
Browse files Browse the repository at this point in the history
…ng created tables unlogged by default

This can improve performance in cases where data integrity is not
important.
  • Loading branch information
jeremyevans committed Mar 13, 2024
1 parent b703aae commit 235a62a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== master

* Support :unlogged_tables_default Database option on Postgres for making created tables unlogged by default (jeremyevans) (#2134)

* Add Dataset#select_prepend for prepending to the current selected columns (jeremyevans) (#2139)

=== 5.78.0 (2024-03-01)
Expand Down
2 changes: 2 additions & 0 deletions doc/opening_databases.rdoc
Expand Up @@ -346,6 +346,8 @@ The following additional options are supported:
separated by commas (for use via a URL: <tt>postgres:///?search_path=schema1,schema2</tt>), or it
can be an array of strings (for use via an option:
<tt>Sequel.postgres(search_path: ['schema1', 'schema2'])</tt>).
:unlogged_tables_default :: Set to true to use UNLOGGED by default for created tables, for potentially better performance
when data integrity is not important.
:use_iso_date_format :: This can be set to false to not force the ISO date format. Sequel forces
it by default to allow for an optimization.

Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/adapters/shared/postgres.rb
Expand Up @@ -1425,7 +1425,7 @@ def create_table_prefix_sql(name, options)
elsif options[:foreign]
raise(Error, "can't provide both :foreign and :unlogged to create_table") if options[:unlogged]
'FOREIGN '
elsif options[:unlogged]
elsif options.fetch(:unlogged){typecast_value_boolean(@opts[:unlogged_tables_default])}
'UNLOGGED '
end

Expand Down
18 changes: 18 additions & 0 deletions spec/core/mock_adapter_spec.rb
Expand Up @@ -529,6 +529,24 @@ def a.method_missing(m, *x) push(*x) end
@db.sqls.must_equal ['CREATE UNLOGGED TABLE "unlogged_dolls" ("name" text)']
end

it "should respect :unlogged_tables_default Database option when creating a table" do
@db.opts[:unlogged_tables_default] = true
@db.create_table(:unlogged_dolls, :unlogged => true){text :name}
@db.sqls.must_equal ['CREATE UNLOGGED TABLE "unlogged_dolls" ("name" text)']
@db.create_table(:unlogged_dolls){text :name}
@db.sqls.must_equal ['CREATE UNLOGGED TABLE "unlogged_dolls" ("name" text)']
@db.create_table(:unlogged_dolls, :unlogged => false){text :name}
@db.sqls.must_equal ['CREATE TABLE "unlogged_dolls" ("name" text)']

@db.opts[:unlogged_tables_default] = 't'
@db.create_table(:unlogged_dolls){text :name}
@db.sqls.must_equal ['CREATE UNLOGGED TABLE "unlogged_dolls" ("name" text)']

@db.opts[:unlogged_tables_default] = 'f'
@db.create_table(:unlogged_dolls){text :name}
@db.sqls.must_equal ['CREATE TABLE "unlogged_dolls" ("name" text)']
end

it "should support spatial indexes" do
@db.alter_table(:posts){add_spatial_index [:geom]}
@db.sqls.must_equal ['CREATE INDEX "posts_geom_index" ON "posts" USING gist ("geom")']
Expand Down

0 comments on commit 235a62a

Please sign in to comment.