The PostgreSQL adapter works both with the native C (ruby.scripting.ca/postgres/) and the pure Ruby (available both as gem and from rubyforge.org/frs/?group_id=234&release_id=1944) drivers.
Options:
- :host - Defaults to "localhost".
- :port - Defaults to 5432.
- :username - Defaults to nothing.
- :password - Defaults to nothing.
- :database - The name of the database. No default, must be provided.
- :schema_search_path - An optional schema search path for the connection given as a string of comma-separated schema names. This is backward-compatible with the :schema_order option.
- :encoding - An optional client encoding that is used in a SET client_encoding TO <encoding> call on the connection.
- :min_messages - An optional client min messages that is used in a SET client_min_messages TO <min_messages> call on the connection.
- :allow_concurrency - If true, use async query methods so Ruby threads don‘t deadlock; otherwise, use blocking query methods.
- active?
- adapter_name
- add_column
- begin_db_transaction
- change_column
- change_column_default
- change_column_null
- client_min_messages
- client_min_messages=
- columns
- commit_db_transaction
- create_database
- create_savepoint
- current_database
- disconnect!
- encoding
- escape_bytea
- execute
- indexes
- insert
- new
- outside_transaction?
- postgresql_version
- quote_table_name
- reconnect!
- release_savepoint
- remove_index
- rename_column
- rename_table
- rollback_db_transaction
- rollback_to_savepoint
- schema_search_path
- schema_search_path=
- select_rows
- supports_ddl_transactions?
- supports_insert_with_returning?
- supports_migrations?
- supports_savepoints?
- supports_standard_conforming_strings?
- table_alias_length
- tables
- type_to_sql
- unescape_bytea
- update_sql
| ADAPTER_NAME | = | 'PostgreSQL'.freeze |
| NATIVE_DATABASE_TYPES | = | { :primary_key => "serial primary key".freeze, :string => { :name => "character varying", :limit => 255 }, :text => { :name => "text" }, :integer => { :name => "integer" }, :float => { :name => "float" }, :decimal => { :name => "decimal" }, :datetime => { :name => "timestamp" }, :timestamp => { :name => "timestamp" }, :time => { :name => "time" }, :date => { :name => "date" }, :binary => { :name => "bytea" }, :boolean => { :name => "boolean" } |
Initializes and connects a PostgreSQL adapter.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 206
206: def initialize(connection, logger, connection_parameters, config)
207: super(connection, logger)
208: @connection_parameters, @config = connection_parameters, config
209:
210: connect
211: end
Is this connection alive and ready for queries?
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 214
214: def active?
215: if @connection.respond_to?(:status)
216: @connection.status == PGconn::CONNECTION_OK
217: else
218: # We're asking the driver, not ActiveRecord, so use @connection.query instead of #query
219: @connection.query 'SELECT 1'
220: true
221: end
222: # postgres-pr raises a NoMethodError when querying if no connection is available.
223: rescue PGError, NoMethodError
224: false
225: end
Returns ‘PostgreSQL’ as adapter name for identification purposes.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 201
201: def adapter_name
202: ADAPTER_NAME
203: end
Adds a new column to the named table. See TableDefinition#column for details of the options you can use.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 827
827: def add_column(table_name, column_name, type, options = {})
828: default = options[:default]
829: notnull = options[:null] == false
830:
831: # Add the column.
832: execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}")
833:
834: change_column_default(table_name, column_name, default) if options_include_default?(options)
835: change_column_null(table_name, column_name, false, default) if notnull
836: end
Begins a transaction.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 553
553: def begin_db_transaction
554: execute "BEGIN"
555: end
Changes the column of a table.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 839
839: def change_column(table_name, column_name, type, options = {})
840: quoted_table_name = quote_table_name(table_name)
841:
842: begin
843: execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
844: rescue ActiveRecord::StatementInvalid => e
845: raise e if postgresql_version > 80000
846: # This is PostgreSQL 7.x, so we have to use a more arcane way of doing it.
847: begin
848: begin_db_transaction
849: tmp_column_name = "#{column_name}_ar_tmp"
850: add_column(table_name, tmp_column_name, type, options)
851: execute "UPDATE #{quoted_table_name} SET #{quote_column_name(tmp_column_name)} = CAST(#{quote_column_name(column_name)} AS #{type_to_sql(type, options[:limit], options[:precision], options[:scale])})"
852: remove_column(table_name, column_name)
853: rename_column(table_name, tmp_column_name, column_name)
854: commit_db_transaction
855: rescue
856: rollback_db_transaction
857: end
858: end
859:
860: change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
861: change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
862: end
Changes the default value of a table column.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 865
865: def change_column_default(table_name, column_name, default)
866: execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
867: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 869
869: def change_column_null(table_name, column_name, null, default = nil)
870: unless null || default.nil?
871: execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
872: end
873: execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
874: end
Returns the current client message level.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 733
733: def client_min_messages
734: query('SHOW client_min_messages')[0][0]
735: end
Set the client message level.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 738
738: def client_min_messages=(level)
739: execute("SET client_min_messages TO '#{level}'")
740: end
Returns the list of all column definitions for a table.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 694
694: def columns(table_name, name = nil)
695: # Limit, precision, and scale are all handled by the superclass.
696: column_definitions(table_name).collect do |name, type, default, notnull|
697: PostgreSQLColumn.new(name, default, type, notnull == 'f')
698: end
699: end
Commits a transaction.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 558
558: def commit_db_transaction
559: execute "COMMIT"
560: end
Create a new PostgreSQL database. Options include :owner, :template, :encoding, :tablespace, and :connection_limit (note that MySQL uses :charset while PostgreSQL uses :encoding).
Example:
create_database config[:database], config create_database 'foo_development', :encoding => 'unicode'
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 601
601: def create_database(name, options = {})
602: options = options.reverse_merge(:encoding => "utf8")
603:
604: option_string = options.symbolize_keys.sum do |key, value|
605: case key
606: when :owner
607: " OWNER = \"#{value}\""
608: when :template
609: " TEMPLATE = \"#{value}\""
610: when :encoding
611: " ENCODING = '#{value}'"
612: when :tablespace
613: " TABLESPACE = \"#{value}\""
614: when :connection_limit
615: " CONNECTION LIMIT = #{value}"
616: else
617: ""
618: end
619: end
620:
621: execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
622: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 575
575: def create_savepoint
576: execute("SAVEPOINT #{current_savepoint_name}")
577: end
Returns the current database name.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 702
702: def current_database
703: query('select current_database()')[0][0]
704: end
Close the connection.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 239
239: def disconnect!
240: @connection.close rescue nil
241: end
Returns the current database encoding format.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 707
707: def encoding
708: query("SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database\nWHERE pg_database.datname LIKE '\#{current_database}'\n")[0][0]
709: end
Escapes binary strings for bytea input to the database.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 289
289: def escape_bytea(value)
290: if @connection.respond_to?(:escape_bytea)
291: self.class.instance_eval do
292: define_method(:escape_bytea) do |value|
293: @connection.escape_bytea(value) if value
294: end
295: end
296: elsif PGconn.respond_to?(:escape_bytea)
297: self.class.instance_eval do
298: define_method(:escape_bytea) do |value|
299: PGconn.escape_bytea(value) if value
300: end
301: end
302: else
303: self.class.instance_eval do
304: define_method(:escape_bytea) do |value|
305: if value
306: result = ''
307: value.each_byte { |c| result << sprintf('\\\\%03o', c) }
308: result
309: end
310: end
311: end
312: end
313: escape_bytea(value)
314: end
Executes an SQL statement, returning a PGresult object on success or raising a PGError exception otherwise.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 537
537: def execute(sql, name = nil)
538: log(sql, name) do
539: if @async
540: @connection.async_exec(sql)
541: else
542: @connection.exec(sql)
543: end
544: end
545: end
Returns the list of all indexes for a table.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 653
653: def indexes(table_name, name = nil)
654: schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
655: result = query("SELECT distinct i.relname, d.indisunique, d.indkey, t.oid\nFROM pg_class t, pg_class i, pg_index d\nWHERE i.relkind = 'i'\nAND d.indexrelid = i.oid\nAND d.indisprimary = 'f'\nAND t.oid = d.indrelid\nAND t.relname = '\#{table_name}'\nAND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (\#{schemas}) )\nORDER BY i.relname\n", name)
656:
657:
658: indexes = []
659:
660: indexes = result.map do |row|
661: index_name = row[0]
662: unique = row[1] == 't'
663: indkey = row[2].split(" ")
664: oid = row[3]
665:
666: columns = query("SELECT a.attname, a.attnum\nFROM pg_attribute a\nWHERE a.attrelid = \#{oid}\nAND a.attnum IN (\#{indkey.join(\",\")})\n", "Columns for index #{row[0]} on #{table_name}").inject({}) {|attlist, r| attlist[r[1]] = r[0]; attlist}
667:
668: column_names = indkey.map {|attnum| columns[attnum] }
669: IndexDefinition.new(table_name, index_name, unique, column_names)
670:
671: end
672:
673: indexes
674: end
Executes an INSERT query and returns the new record‘s ID
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 470
470: def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
471: # Extract the table from the insert sql. Yuck.
472: table = sql.split(" ", 4)[2].gsub('"', '')
473:
474: # Try an insert with 'returning id' if available (PG >= 8.2)
475: if supports_insert_with_returning?
476: pk, sequence_name = *pk_and_sequence_for(table) unless pk
477: if pk
478: id = select_value("#{sql} RETURNING #{quote_column_name(pk)}")
479: clear_query_cache
480: return id
481: end
482: end
483:
484: # Otherwise, insert then grab last_insert_id.
485: if insert_id = super
486: insert_id
487: else
488: # If neither pk nor sequence name is given, look them up.
489: unless pk || sequence_name
490: pk, sequence_name = *pk_and_sequence_for(table)
491: end
492:
493: # If a pk is given, fallback to default sequence name.
494: # Don't fetch last insert id for a table without a pk.
495: if pk && sequence_name ||= default_sequence_name(table, pk)
496: last_insert_id(table, sequence_name)
497: end
498: end
499: end
The ruby-pg driver supports inspecting the transaction status, while the ruby-postgres driver does not.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 570
570: def outside_transaction?
571: @connection.transaction_status == PGconn::PQTRANS_IDLE
572: end
Checks the following cases:
- table_name
- "table.name"
- schema_name.table_name
- schema_name."table.name"
- "schema.name".table_name
- "schema.name"."table.name"
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 415
415: def quote_table_name(name)
416: schema, name_part = extract_pg_identifier_from_name(name.to_s)
417:
418: unless name_part
419: quote_column_name(schema)
420: else
421: table_name, name_part = extract_pg_identifier_from_name(name_part)
422: "#{quote_column_name(schema)}.#{quote_column_name(table_name)}"
423: end
424: end
Close then reopen the connection.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 228
228: def reconnect!
229: if @connection.respond_to?(:reset)
230: @connection.reset
231: configure_connection
232: else
233: disconnect!
234: connect
235: end
236: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 583
583: def release_savepoint
584: execute("RELEASE SAVEPOINT #{current_savepoint_name}")
585: end
Drops an index from a table.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 882
882: def remove_index(table_name, options = {})
883: execute "DROP INDEX #{quote_table_name(index_name(table_name, options))}"
884: end
Renames a column in a table.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 877
877: def rename_column(table_name, column_name, new_column_name)
878: execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
879: end
Renames a table.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 821
821: def rename_table(name, new_name)
822: execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
823: end
Aborts a transaction.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 563
563: def rollback_db_transaction
564: execute "ROLLBACK"
565: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 579
579: def rollback_to_savepoint
580: execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
581: end
Returns the active schema search path.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 728
728: def schema_search_path
729: @schema_search_path ||= query('SHOW search_path')[0][0]
730: end
Sets the schema search path to a string of comma-separated schema names. Names beginning with $ have to be quoted (e.g. $user => ’$user’). See: www.postgresql.org/docs/current/static/ddl-schemas.html
This should be not be called manually but set in database.yml.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 720
720: def schema_search_path=(schema_csv)
721: if schema_csv
722: execute "SET search_path TO #{schema_csv}"
723: @schema_search_path = schema_csv
724: end
725: end
Executes a SELECT query and returns an array of rows. Each row is an array of field values.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 465
465: def select_rows(sql, name = nil)
466: select_raw(sql, name).last
467: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 272
272: def supports_ddl_transactions?
273: true
274: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 268
268: def supports_insert_with_returning?
269: postgresql_version >= 80200
270: end
Does PostgreSQL support migrations?
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 248
248: def supports_migrations?
249: true
250: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 276
276: def supports_savepoints?
277: true
278: end
Does PostgreSQL support standard conforming strings?
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 253
253: def supports_standard_conforming_strings?
254: # Temporarily set the client message level above error to prevent unintentional
255: # error messages in the logs when working on a PostgreSQL database server that
256: # does not support standard conforming strings.
257: client_min_messages_old = client_min_messages
258: self.client_min_messages = 'panic'
259:
260: # postgres-pr does not raise an exception when client_min_messages is set higher
261: # than error and "SHOW standard_conforming_strings" fails, but returns an empty
262: # PGresult instead.
263: has_support = query('SHOW standard_conforming_strings')[0][0] rescue false
264: self.client_min_messages = client_min_messages_old
265: has_support
266: end
Returns the configured supported identifier length supported by PostgreSQL, or report the default of 63 on PostgreSQL 7.x.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 282
282: def table_alias_length
283: @table_alias_length ||= (postgresql_version >= 80000 ? query('SHOW max_identifier_length')[0][0].to_i : 63)
284: end
Returns the list of all tables in the schema search path or a specified schema.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 642
642: def tables(name = nil)
643: schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
644: query("SELECT tablename\nFROM pg_tables\nWHERE schemaname IN (\#{schemas})\n", name).map { |row| row[0] }
645: end
Maps logical Rails types to PostgreSQL-specific data types.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 887
887: def type_to_sql(type, limit = nil, precision = nil, scale = nil)
888: return super unless type.to_s == 'integer'
889:
890: case limit
891: when 1..2; 'smallint'
892: when 3..4, nil; 'integer'
893: when 5..8; 'bigint'
894: else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
895: end
896: end
Unescapes bytea output from a database to the binary string it represents. NOTE: This is NOT an inverse of escape_bytea! This is only to be used
on escaped binary output from database drive.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 319
319: def unescape_bytea(value)
320: # In each case, check if the value actually is escaped PostgreSQL bytea output
321: # or an unescaped Active Record attribute that was just written.
322: if PGconn.respond_to?(:unescape_bytea)
323: self.class.instance_eval do
324: define_method(:unescape_bytea) do |value|
325: if value =~ /\\\d{3}/
326: PGconn.unescape_bytea(value)
327: else
328: value
329: end
330: end
331: end
332: else
333: self.class.instance_eval do
334: define_method(:unescape_bytea) do |value|
335: if value =~ /\\\d{3}/
336: result = ''
337: i, max = 0, value.size
338: while i < max
339: char = value[i]
340: if char == ?\\
341: if value[i+1] == ?\\
342: char = ?\\
343: i += 1
344: else
345: char = value[i+1..i+3].oct
346: i += 3
347: end
348: end
349: result << char
350: i += 1
351: end
352: result
353: else
354: value
355: end
356: end
357: end
358: end
359: unescape_bytea(value)
360: end
Executes an UPDATE query and returns the number of affected tuples.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 548
548: def update_sql(sql, name = nil)
549: super.cmd_tuples
550: end
Returns the version of the connected PostgreSQL version.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 935
935: def postgresql_version
936: @postgresql_version ||=
937: if @connection.respond_to?(:server_version)
938: @connection.server_version
939: else
940: # Mimic PGconn.server_version behavior
941: begin
942: query('SELECT version()')[0][0] =~ /PostgreSQL (\d+)\.(\d+)\.(\d+)/
943: ($1.to_i * 10000) + ($2.to_i * 100) + $3.to_i
944: rescue
945: 0
946: end
947: end
948: end