From 235a62a53d954f7fb1e133655d6732215040c96c Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 13 Mar 2024 15:52:41 -0700 Subject: [PATCH] Support :unlogged_tables_default Database option on Postgres for making created tables unlogged by default This can improve performance in cases where data integrity is not important. --- CHANGELOG | 2 ++ doc/opening_databases.rdoc | 2 ++ lib/sequel/adapters/shared/postgres.rb | 2 +- spec/core/mock_adapter_spec.rb | 18 ++++++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 6925ed69b..943d4ec18 100644 --- a/CHANGELOG +++ b/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) diff --git a/doc/opening_databases.rdoc b/doc/opening_databases.rdoc index 68449523b..656ff6d5e 100644 --- a/doc/opening_databases.rdoc +++ b/doc/opening_databases.rdoc @@ -346,6 +346,8 @@ The following additional options are supported: separated by commas (for use via a URL: postgres:///?search_path=schema1,schema2), or it can be an array of strings (for use via an option: Sequel.postgres(search_path: ['schema1', 'schema2'])). +: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. diff --git a/lib/sequel/adapters/shared/postgres.rb b/lib/sequel/adapters/shared/postgres.rb index 251af3735..ee118c5cc 100644 --- a/lib/sequel/adapters/shared/postgres.rb +++ b/lib/sequel/adapters/shared/postgres.rb @@ -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 diff --git a/spec/core/mock_adapter_spec.rb b/spec/core/mock_adapter_spec.rb index 0affe9368..0cab029fb 100644 --- a/spec/core/mock_adapter_spec.rb +++ b/spec/core/mock_adapter_spec.rb @@ -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")']