Methods
Public Instance
- analyze
- complex_expression_sql_append
- disable_insert_returning
- empty?
- explain
- for_key_share
- for_no_key_update
- for_share
- full_text_search
- insert
- insert_conflict
- insert_ignore
- insert_select
- insert_select_sql
- join_table
- lock
- merge
- merge_delete_when_not_matched_by_source
- merge_do_nothing_when_matched
- merge_do_nothing_when_not_matched
- merge_do_nothing_when_not_matched_by_source
- merge_insert
- merge_update_when_not_matched_by_source
- overriding_system_value
- overriding_user_value
- supports_cte?
- supports_cte_in_subqueries?
- supports_distinct_on?
- supports_group_cube?
- supports_group_rollup?
- supports_grouping_sets?
- supports_insert_conflict?
- supports_insert_select?
- supports_lateral_subqueries?
- supports_merge?
- supports_modifying_joins?
- supports_nowait?
- supports_regexp?
- supports_returning?
- supports_skip_locked?
- supports_timestamp_timezones?
- supports_window_clause?
- supports_window_function_frame_option?
- supports_window_functions?
- truncate
- with_ties
Protected Instance
Included modules
Constants
EXPLAIN_BOOLEAN_OPTIONS | = | {} | ||
EXPLAIN_NONBOOLEAN_OPTIONS | = | { :serialize => {:none=>"SERIALIZE NONE", :text=>"SERIALIZE TEXT", :binary=>"SERIALIZE BINARY"}.freeze, :format => {:text=>"FORMAT TEXT", :xml=>"FORMAT XML", :json=>"FORMAT JSON", :yaml=>"FORMAT YAML"}.freeze }.freeze | ||
LOCK_MODES | = | ['ACCESS SHARE', 'ROW SHARE', 'ROW EXCLUSIVE', 'SHARE UPDATE EXCLUSIVE', 'SHARE', 'SHARE ROW EXCLUSIVE', 'EXCLUSIVE', 'ACCESS EXCLUSIVE'].each(&:freeze).freeze | ||
NULL | = | LiteralString.new('NULL').freeze |
Public Instance methods
Return the results of an EXPLAIN ANALYZE query as a string
# File lib/sequel/adapters/shared/postgres.rb 2000 def analyze 2001 explain(:analyze=>true) 2002 end
Handle converting the ruby xor operator (^) into the PostgreSQL xor operator (#), and use the ILIKE and NOT ILIKE operators.
# File lib/sequel/adapters/shared/postgres.rb 2007 def complex_expression_sql_append(sql, op, args) 2008 case op 2009 when :^ 2010 j = ' # ' 2011 c = false 2012 args.each do |a| 2013 sql << j if c 2014 literal_append(sql, a) 2015 c ||= true 2016 end 2017 when :ILIKE, :'NOT ILIKE' 2018 sql << '(' 2019 literal_append(sql, args[0]) 2020 sql << ' ' << op.to_s << ' ' 2021 literal_append(sql, args[1]) 2022 sql << ')' 2023 else 2024 super 2025 end 2026 end
Disables automatic use of INSERT … RETURNING. You can still use returning manually to force the use of RETURNING when inserting.
This is designed for cases where INSERT RETURNING cannot be used, such as when you are using partitioning with trigger functions or conditional rules, or when you are using a PostgreSQL version less than 8.2, or a PostgreSQL derivative that does not support returning.
Note that when this method is used, insert will not return the primary key of the inserted row, you will have to get the primary key of the inserted row before inserting via nextval, or after inserting via currval or lastval (making sure to use the same database connection for currval or lastval).
# File lib/sequel/adapters/shared/postgres.rb 2042 def disable_insert_returning 2043 clone(:disable_insert_returning=>true) 2044 end
Always return false when using VALUES
# File lib/sequel/adapters/shared/postgres.rb 2047 def empty? 2048 return false if @opts[:values] 2049 super 2050 end
Return the results of an EXPLAIN query. Boolean options:
:analyze |
Use the ANALYZE option. |
:buffers |
Use the BUFFERS option. |
:costs |
Use the COSTS option. |
:generic_plan |
Use the GENERIC_PLAN option. |
:memory |
Use the MEMORY option. |
:settings |
Use the SETTINGS option. |
:summary |
Use the SUMMARY option. |
:timing |
Use the TIMING option. |
:verbose |
Use the VERBOSE option. |
:wal |
Use the WAL option. |
Non boolean options:
:format |
Use the FORMAT option to change the format of the returned value. Values can be :text, :xml, :json, or :yaml. |
:serialize |
Use the SERIALIZE option to get timing on serialization. Values can be :none, :text, or :binary. |
See the PostgreSQL EXPLAIN documentation for an explanation of what each option does.
In most cases, the return value is a single string. However, using the format: :json
option can result in the return value being an array containing a hash.
# File lib/sequel/adapters/shared/postgres.rb 2080 def explain(opts=OPTS) 2081 rows = clone(:append_sql=>explain_sql_string_origin(opts)).map(:'QUERY PLAN') 2082 2083 if rows.length == 1 2084 rows[0] 2085 elsif rows.all?{|row| String === row} 2086 rows.join("\r\n") 2087 # :nocov: 2088 else 2089 # This branch is unreachable in tests, but it seems better to just return 2090 # all rows than throw in error if this case actually happens. 2091 rows 2092 # :nocov: 2093 end 2094 end
Return a cloned dataset which will use FOR NO KEY UPDATE to lock returned rows. This is generally a better choice than using for_update on PostgreSQL, unless you will be deleting the row or modifying a key column. Supported on PostgreSQL 9.3+.
# File lib/sequel/adapters/shared/postgres.rb 2105 def for_no_key_update 2106 cached_lock_style_dataset(:_for_no_key_update_ds, :no_key_update) 2107 end
Run a full text search on PostgreSQL. By default, searching for the inclusion of any of the terms in any of the cols.
Options:
:headline |
Append a expression to the selected columns aliased to headline that contains an extract of the matched text. |
:language |
The language to use for the search (default: ‘simple’) |
:plain |
Whether a plain search should be used (default: false). In this case, terms should be a single string, and it will do a search where cols contains all of the words in terms. This ignores search operators in terms. |
:phrase |
Similar to :plain, but also adding an ILIKE filter to ensure that returned rows also include the exact phrase used. |
:rank |
Set to true to order by the rank, so that closer matches are returned first. |
:to_tsquery |
Can be set to :plain, :phrase, or :websearch to specify the function to use to convert the terms to a ts_query. |
:tsquery |
Specifies the terms argument is already a valid SQL expression returning a tsquery, and can be used directly in the query. |
:tsvector |
Specifies the cols argument is already a valid SQL expression returning a tsvector, and can be used directly in the query. |
# File lib/sequel/adapters/shared/postgres.rb 2133 def full_text_search(cols, terms, opts = OPTS) 2134 lang = Sequel.cast(opts[:language] || 'simple', :regconfig) 2135 2136 unless opts[:tsvector] 2137 phrase_cols = full_text_string_join(cols) 2138 cols = Sequel.function(:to_tsvector, lang, phrase_cols) 2139 end 2140 2141 unless opts[:tsquery] 2142 phrase_terms = terms.is_a?(Array) ? terms.join(' | ') : terms 2143 2144 query_func = case to_tsquery = opts[:to_tsquery] 2145 when :phrase, :plain 2146 :"#{to_tsquery}to_tsquery" 2147 when :websearch 2148 :"websearch_to_tsquery" 2149 else 2150 (opts[:phrase] || opts[:plain]) ? :plainto_tsquery : :to_tsquery 2151 end 2152 2153 terms = Sequel.function(query_func, lang, phrase_terms) 2154 end 2155 2156 ds = where(Sequel.lit(["", " @@ ", ""], cols, terms)) 2157 2158 if opts[:phrase] 2159 raise Error, "can't use :phrase with either :tsvector or :tsquery arguments to full_text_search together" if opts[:tsvector] || opts[:tsquery] 2160 ds = ds.grep(phrase_cols, "%#{escape_like(phrase_terms)}%", :case_insensitive=>true) 2161 end 2162 2163 if opts[:rank] 2164 ds = ds.reverse{ts_rank_cd(cols, terms)} 2165 end 2166 2167 if opts[:headline] 2168 ds = ds.select_append{ts_headline(lang, phrase_cols, terms).as(:headline)} 2169 end 2170 2171 ds 2172 end
Insert given values into the database.
# File lib/sequel/adapters/shared/postgres.rb 2175 def insert(*values) 2176 if @opts[:returning] 2177 # Already know which columns to return, let the standard code handle it 2178 super 2179 elsif @opts[:sql] || @opts[:disable_insert_returning] 2180 # Raw SQL used or RETURNING disabled, just use the default behavior 2181 # and return nil since sequence is not known. 2182 super 2183 nil 2184 else 2185 # Force the use of RETURNING with the primary key value, 2186 # unless it has been disabled. 2187 returning(insert_pk).insert(*values){|r| return r.values.first} 2188 end 2189 end
Handle uniqueness violations when inserting, by updating the conflicting row, using ON CONFLICT. With no options, uses ON CONFLICT DO NOTHING. Options:
:conflict_where |
The index filter, when using a partial index to determine uniqueness. |
:constraint |
An explicit constraint name, has precendence over :target. |
:target |
The column name or expression to handle uniqueness violations on. |
:update |
A hash of columns and values to set. Uses ON CONFLICT DO UPDATE. |
:update_where |
A WHERE condition to use for the update. |
Examples:
DB[:table].insert_conflict.insert(a: 1, b: 2) # INSERT INTO TABLE (a, b) VALUES (1, 2) # ON CONFLICT DO NOTHING DB[:table].insert_conflict(constraint: :table_a_uidx).insert(a: 1, b: 2) # INSERT INTO TABLE (a, b) VALUES (1, 2) # ON CONFLICT ON CONSTRAINT table_a_uidx DO NOTHING DB[:table].insert_conflict(target: :a).insert(a: 1, b: 2) # INSERT INTO TABLE (a, b) VALUES (1, 2) # ON CONFLICT (a) DO NOTHING DB[:table].insert_conflict(target: :a, conflict_where: {c: true}).insert(a: 1, b: 2) # INSERT INTO TABLE (a, b) VALUES (1, 2) # ON CONFLICT (a) WHERE (c IS TRUE) DO NOTHING DB[:table].insert_conflict(target: :a, update: {b: Sequel[:excluded][:b]}).insert(a: 1, b: 2) # INSERT INTO TABLE (a, b) VALUES (1, 2) # ON CONFLICT (a) DO UPDATE SET b = excluded.b DB[:table].insert_conflict(constraint: :table_a_uidx, update: {b: Sequel[:excluded][:b]}, update_where: {Sequel[:table][:status_id] => 1}).insert(a: 1, b: 2) # INSERT INTO TABLE (a, b) VALUES (1, 2) # ON CONFLICT ON CONSTRAINT table_a_uidx # DO UPDATE SET b = excluded.b WHERE (table.status_id = 1)
# File lib/sequel/adapters/shared/postgres.rb 2226 def insert_conflict(opts=OPTS) 2227 clone(:insert_conflict => opts) 2228 end
Ignore uniqueness/exclusion violations when inserting, using ON CONFLICT DO NOTHING. Exists mostly for compatibility to MySQL’s insert_ignore. Example:
DB[:table].insert_ignore.insert(a: 1, b: 2) # INSERT INTO TABLE (a, b) VALUES (1, 2) # ON CONFLICT DO NOTHING
# File lib/sequel/adapters/shared/postgres.rb 2236 def insert_ignore 2237 insert_conflict 2238 end
Insert a record, returning the record inserted, using RETURNING. Always returns nil without running an INSERT statement if disable_insert_returning
is used. If the query runs but returns no values, returns false.
# File lib/sequel/adapters/shared/postgres.rb 2243 def insert_select(*values) 2244 return unless supports_insert_select? 2245 # Handle case where query does not return a row 2246 server?(:default).with_sql_first(insert_select_sql(*values)) || false 2247 end
The SQL to use for an insert_select
, adds a RETURNING clause to the insert unless the RETURNING clause is already present.
# File lib/sequel/adapters/shared/postgres.rb 2251 def insert_select_sql(*values) 2252 ds = opts[:returning] ? self : returning 2253 ds.insert_sql(*values) 2254 end
Support SQL::AliasedExpression as expr to setup a USING join with a table alias for the USING columns.
# File lib/sequel/adapters/shared/postgres.rb 2258 def join_table(type, table, expr=nil, options=OPTS, &block) 2259 if expr.is_a?(SQL::AliasedExpression) && expr.expression.is_a?(Array) && !expr.expression.empty? && expr.expression.all? 2260 options = options.merge(:join_using=>true) 2261 end 2262 super 2263 end
Locks all tables in the dataset’s FROM clause (but not in JOINs) with the specified mode (e.g. ‘EXCLUSIVE’). If a block is given, starts a new transaction, locks the table, and yields. If a block is not given, just locks the tables. Note that PostgreSQL will probably raise an error if you lock the table outside of an existing transaction. Returns nil.
# File lib/sequel/adapters/shared/postgres.rb 2270 def lock(mode, opts=OPTS) 2271 if defined?(yield) # perform locking inside a transaction and yield to block 2272 @db.transaction(opts){lock(mode, opts); yield} 2273 else 2274 sql = 'LOCK TABLE '.dup 2275 source_list_append(sql, @opts[:from]) 2276 mode = mode.to_s.upcase.strip 2277 unless LOCK_MODES.include?(mode) 2278 raise Error, "Unsupported lock mode: #{mode}" 2279 end 2280 sql << " IN #{mode} MODE" 2281 @db.execute(sql, opts) 2282 end 2283 nil 2284 end
Support MERGE RETURNING on PostgreSQL 17+.
# File lib/sequel/adapters/shared/postgres.rb 2287 def merge(&block) 2288 sql = merge_sql 2289 if uses_returning?(:merge) 2290 returning_fetch_rows(sql, &block) 2291 else 2292 execute_ddl(sql) 2293 end 2294 end
Return a dataset with a WHEN NOT MATCHED BY SOURCE THEN DELETE clause added to the MERGE statement. If a block is passed, treat it as a virtual row and use it as additional conditions for the match.
merge_delete_not_matched_by_source # WHEN NOT MATCHED BY SOURCE THEN DELETE merge_delete_not_matched_by_source{a > 30} # WHEN NOT MATCHED BY SOURCE AND (a > 30) THEN DELETE
# File lib/sequel/adapters/shared/postgres.rb 2305 def merge_delete_when_not_matched_by_source(&block) 2306 _merge_when(:type=>:delete_not_matched_by_source, &block) 2307 end
Return a dataset with a WHEN MATCHED THEN DO NOTHING clause added to the MERGE statement. If a block is passed, treat it as a virtual row and use it as additional conditions for the match.
merge_do_nothing_when_matched # WHEN MATCHED THEN DO NOTHING merge_do_nothing_when_matched{a > 30} # WHEN MATCHED AND (a > 30) THEN DO NOTHING
# File lib/sequel/adapters/shared/postgres.rb 2318 def merge_do_nothing_when_matched(&block) 2319 _merge_when(:type=>:matched, &block) 2320 end
Return a dataset with a WHEN NOT MATCHED THEN DO NOTHING clause added to the MERGE statement. If a block is passed, treat it as a virtual row and use it as additional conditions for the match.
merge_do_nothing_when_not_matched # WHEN NOT MATCHED THEN DO NOTHING merge_do_nothing_when_not_matched{a > 30} # WHEN NOT MATCHED AND (a > 30) THEN DO NOTHING
# File lib/sequel/adapters/shared/postgres.rb 2331 def merge_do_nothing_when_not_matched(&block) 2332 _merge_when(:type=>:not_matched, &block) 2333 end
Return a dataset with a WHEN NOT MATCHED BY SOURCE THEN DO NOTHING clause added to the MERGE BY SOURCE statement. If a block is passed, treat it as a virtual row and use it as additional conditions for the match.
merge_do_nothing_when_not_matched_by_source # WHEN NOT MATCHED BY SOURCE THEN DO NOTHING merge_do_nothing_when_not_matched_by_source{a > 30} # WHEN NOT MATCHED BY SOURCE AND (a > 30) THEN DO NOTHING
# File lib/sequel/adapters/shared/postgres.rb 2344 def merge_do_nothing_when_not_matched_by_source(&block) 2345 _merge_when(:type=>:not_matched_by_source, &block) 2346 end
Support OVERRIDING USER|SYSTEM VALUE for MERGE INSERT.
# File lib/sequel/adapters/shared/postgres.rb 2349 def merge_insert(*values, &block) 2350 h = {:type=>:insert, :values=>values} 2351 if @opts[:override] 2352 h[:override] = insert_override_sql(String.new) 2353 end 2354 _merge_when(h, &block) 2355 end
Return a dataset with a WHEN NOT MATCHED BY SOURCE THEN UPDATE clause added to the MERGE statement. If a block is passed, treat it as a virtual row and use it as additional conditions for the match.
merge_update_not_matched_by_source(i1: Sequel[:i1]+:i2+10, a: Sequel[:a]+:b+20) # WHEN NOT MATCHED BY SOURCE THEN UPDATE SET i1 = (i1 + i2 + 10), a = (a + b + 20) merge_update_not_matched_by_source(i1: :i2){a > 30} # WHEN NOT MATCHED BY SOURCE AND (a > 30) THEN UPDATE SET i1 = i2
# File lib/sequel/adapters/shared/postgres.rb 2366 def merge_update_when_not_matched_by_source(values, &block) 2367 _merge_when(:type=>:update_not_matched_by_source, :values=>values, &block) 2368 end
Use OVERRIDING USER VALUE for INSERT statements, so that identity columns always use the user supplied value, and an error is not raised for identity columns that are GENERATED ALWAYS.
# File lib/sequel/adapters/shared/postgres.rb 2373 def overriding_system_value 2374 clone(:override=>:system) 2375 end
Use OVERRIDING USER VALUE for INSERT statements, so that identity columns always use the sequence value instead of the user supplied value.
# File lib/sequel/adapters/shared/postgres.rb 2379 def overriding_user_value 2380 clone(:override=>:user) 2381 end
# File lib/sequel/adapters/shared/postgres.rb 2383 def supports_cte?(type=:select) 2384 if type == :select 2385 server_version >= 80400 2386 else 2387 server_version >= 90100 2388 end 2389 end
PostgreSQL supports using the WITH clause in subqueries if it supports using WITH at all (i.e. on PostgreSQL 8.4+).
# File lib/sequel/adapters/shared/postgres.rb 2393 def supports_cte_in_subqueries? 2394 supports_cte? 2395 end
DISTINCT ON is a PostgreSQL extension
# File lib/sequel/adapters/shared/postgres.rb 2398 def supports_distinct_on? 2399 true 2400 end
PostgreSQL 9.5+ supports GROUP CUBE
# File lib/sequel/adapters/shared/postgres.rb 2403 def supports_group_cube? 2404 server_version >= 90500 2405 end
PostgreSQL 9.5+ supports GROUP ROLLUP
# File lib/sequel/adapters/shared/postgres.rb 2408 def supports_group_rollup? 2409 server_version >= 90500 2410 end
PostgreSQL 9.5+ supports GROUPING SETS
# File lib/sequel/adapters/shared/postgres.rb 2413 def supports_grouping_sets? 2414 server_version >= 90500 2415 end
PostgreSQL 9.5+ supports the ON CONFLICT clause to INSERT.
# File lib/sequel/adapters/shared/postgres.rb 2423 def supports_insert_conflict? 2424 server_version >= 90500 2425 end
True unless insert returning has been disabled for this dataset.
# File lib/sequel/adapters/shared/postgres.rb 2418 def supports_insert_select? 2419 !@opts[:disable_insert_returning] 2420 end
PostgreSQL 9.3+ supports lateral subqueries
# File lib/sequel/adapters/shared/postgres.rb 2428 def supports_lateral_subqueries? 2429 server_version >= 90300 2430 end
PostgreSQL 15+ supports MERGE.
# File lib/sequel/adapters/shared/postgres.rb 2438 def supports_merge? 2439 server_version >= 150000 2440 end
PostgreSQL supports modifying joined datasets
# File lib/sequel/adapters/shared/postgres.rb 2433 def supports_modifying_joins? 2434 true 2435 end
PostgreSQL supports NOWAIT.
# File lib/sequel/adapters/shared/postgres.rb 2443 def supports_nowait? 2444 true 2445 end
PostgreSQL supports pattern matching via regular expressions
# File lib/sequel/adapters/shared/postgres.rb 2458 def supports_regexp? 2459 true 2460 end
MERGE RETURNING is supported on PostgreSQL 17+. Other RETURNING is supported on all supported PostgreSQL versions.
# File lib/sequel/adapters/shared/postgres.rb 2449 def supports_returning?(type) 2450 if type == :merge 2451 server_version >= 170000 2452 else 2453 true 2454 end 2455 end
PostgreSQL 9.5+ supports SKIP LOCKED.
# File lib/sequel/adapters/shared/postgres.rb 2463 def supports_skip_locked? 2464 server_version >= 90500 2465 end
PostgreSQL supports timezones in literal timestamps
# File lib/sequel/adapters/shared/postgres.rb 2470 def supports_timestamp_timezones? 2471 # SEQUEL6: Remove 2472 true 2473 end
PostgreSQL 8.4+ supports WINDOW clause.
# File lib/sequel/adapters/shared/postgres.rb 2477 def supports_window_clause? 2478 server_version >= 80400 2479 end
Base support added in 8.4, offset supported added in 9.0, GROUPS and EXCLUDE support added in 11.0.
# File lib/sequel/adapters/shared/postgres.rb 2488 def supports_window_function_frame_option?(option) 2489 case option 2490 when :rows, :range 2491 true 2492 when :offset 2493 server_version >= 90000 2494 when :groups, :exclude 2495 server_version >= 110000 2496 else 2497 false 2498 end 2499 end
PostgreSQL 8.4+ supports window functions
# File lib/sequel/adapters/shared/postgres.rb 2482 def supports_window_functions? 2483 server_version >= 80400 2484 end
Truncates the dataset. Returns nil.
Options:
:cascade |
whether to use the CASCADE option, useful when truncating tables with foreign keys. |
:only |
truncate using ONLY, so child tables are unaffected |
:restart |
use RESTART IDENTITY to restart any related sequences |
:only and :restart only work correctly on PostgreSQL 8.4+.
Usage:
DB[:table].truncate # TRUNCATE TABLE "table" DB[:table].truncate(cascade: true, only: true, restart: true) # TRUNCATE TABLE ONLY "table" RESTART IDENTITY CASCADE
# File lib/sequel/adapters/shared/postgres.rb 2517 def truncate(opts = OPTS) 2518 if opts.empty? 2519 super() 2520 else 2521 clone(:truncate_opts=>opts).truncate 2522 end 2523 end
Use WITH TIES when limiting the result set to also include additional rules that have the same results for the order column as the final row. Requires PostgreSQL 13.
# File lib/sequel/adapters/shared/postgres.rb 2528 def with_ties 2529 clone(:limit_with_ties=>true) 2530 end
Protected Instance methods
If returned primary keys are requested, use RETURNING unless already set on the dataset. If RETURNING is already set, use existing returning values. If RETURNING is only set to return a single columns, return an array of just that column. Otherwise, return an array of hashes.
# File lib/sequel/adapters/shared/postgres.rb 2538 def _import(columns, values, opts=OPTS) 2539 if @opts[:returning] 2540 # no transaction: our multi_insert_sql_strategy should guarantee 2541 # that there's only ever a single statement. 2542 sql = multi_insert_sql(columns, values)[0] 2543 returning_fetch_rows(sql).map{|v| v.length == 1 ? v.values.first : v} 2544 elsif opts[:return] == :primary_key 2545 returning(insert_pk)._import(columns, values, opts) 2546 else 2547 super 2548 end 2549 end
# File lib/sequel/adapters/shared/postgres.rb 2551 def to_prepared_statement(type, *a) 2552 if type == :insert && !@opts.has_key?(:returning) 2553 returning(insert_pk).send(:to_prepared_statement, :insert_pk, *a) 2554 else 2555 super 2556 end 2557 end