### Home Classes Methods

### In Files

- lib/active_record/connection_adapters/postgresql_adapter.rb

### Parent

ActiveRecord::ConnectionAdapters::AbstractAdapter

### Namespace

- MODULE ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils
- CLASS ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::StatementPool
- CLASS ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition

### Methods

- ::new
- #active?
- #adapter_name
- #add_column
- #begin_db_transaction
- #change_column
- #change_column_default
- #change_column_null
- #clear_cache!
- #client_min_messages
- #client_min_messages=
- #columns
- #commit_db_transaction
- #create_database
- #create_savepoint
- #current_database
- #current_schema
- #disconnect!
- #encoding
- #escape_bytea
- #exec_delete
- #exec_query
- #exec_update
- #execute
- #explain
- #index_name_length
- #indexes
- #insert_sql
- #outside_transaction?
- #postgresql_version
- #primary_key
- #quote_table_name
- #reconnect!
- #release_savepoint
- #rename_column
- #rename_index
- #rename_table
- #reset!
- #rollback_db_transaction
- #rollback_to_savepoint
- #schema_exists?
- #schema_search_path
- #schema_search_path=
- #select_rows
- #serial_sequence
- #session_auth=
- #set_standard_conforming_strings
- #sql_for_insert
- #substitute_at
- #supports_ddl_transactions?
- #supports_explain?
- #supports_index_sort_order?
- #supports_insert_with_returning?
- #supports_migrations?
- #supports_savepoints?
- #supports_statement_cache?
- #table_alias_length
- #table_exists?
- #tables
- #translate_exception
- #type_cast
- #type_to_sql
- #unescape_bytea
- #update_sql

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- ActiveRecord
- ActiveRecord::ActiveRecordError
- ActiveRecord::AdapterNotFound
- ActiveRecord::AdapterNotSpecified
- ActiveRecord::Aggregations
- ActiveRecord::Aggregations::ClassMethods
- ActiveRecord::AssociationTypeMismatch
- ActiveRecord::Associations
- ActiveRecord::Associations::ClassMethods
- ActiveRecord::AttributeAssignment
- ActiveRecord::AttributeAssignment::ClassMethods
- ActiveRecord::AttributeAssignmentError
- ActiveRecord::AttributeMethods
- ActiveRecord::AttributeMethods::BeforeTypeCast
- ActiveRecord::AttributeMethods::ClassMethods
- ActiveRecord::AttributeMethods::DeprecatedUnderscoreRead
- ActiveRecord::AttributeMethods::DeprecatedUnderscoreRead::ClassMethods
- ActiveRecord::AttributeMethods::Dirty
- ActiveRecord::AttributeMethods::PrimaryKey
- ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods
- ActiveRecord::AttributeMethods::Query
- ActiveRecord::AttributeMethods::Read
- ActiveRecord::AttributeMethods::Read::ClassMethods
- ActiveRecord::AttributeMethods::Serialization
- ActiveRecord::AttributeMethods::Serialization::Attribute
- ActiveRecord::AttributeMethods::Serialization::ClassMethods
- ActiveRecord::AttributeMethods::TimeZoneConversion
- ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods
- ActiveRecord::AttributeMethods::Write
- ActiveRecord::AttributeMethods::Write::ClassMethods
- ActiveRecord::AutosaveAssociation
- ActiveRecord::AutosaveAssociation::ClassMethods
- ActiveRecord::Base
- ActiveRecord::Batches
- ActiveRecord::Calculations
- ActiveRecord::Callbacks
- ActiveRecord::Coders
- ActiveRecord::ConfigurationError
- ActiveRecord::ConnectionAdapters
- ActiveRecord::ConnectionAdapters::AbstractAdapter
- ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- ActiveRecord::ConnectionAdapters::ConnectionManagement
- ActiveRecord::ConnectionAdapters::ConnectionPool
- ActiveRecord::ConnectionAdapters::DatabaseLimits
- ActiveRecord::ConnectionAdapters::DatabaseStatements
- ActiveRecord::ConnectionAdapters::Mysql2Adapter
- ActiveRecord::ConnectionAdapters::MysqlAdapter
- ActiveRecord::ConnectionAdapters::MysqlAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils
- ActiveRecord::ConnectionAdapters::PostgreSQLColumn
- ActiveRecord::ConnectionAdapters::QueryCache
- ActiveRecord::ConnectionAdapters::Quoting
- ActiveRecord::ConnectionAdapters::SQLiteAdapter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::ExplainPrettyPrinter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::Version
- ActiveRecord::ConnectionAdapters::SchemaCache
- ActiveRecord::ConnectionAdapters::SchemaStatements
- ActiveRecord::ConnectionAdapters::StatementPool
- ActiveRecord::ConnectionAdapters::Table
- ActiveRecord::ConnectionAdapters::TableDefinition
- ActiveRecord::ConnectionNotEstablished
- ActiveRecord::ConnectionTimeoutError
- ActiveRecord::CounterCache
- ActiveRecord::DangerousAttributeError
- ActiveRecord::Delegation
- ActiveRecord::DynamicFinderMatch
- ActiveRecord::DynamicMatchers
- ActiveRecord::DynamicScopeMatch
- ActiveRecord::Explain
- ActiveRecord::FinderMethods
- ActiveRecord::Fixtures
- ActiveRecord::Fixtures::File
- ActiveRecord::Generators
- ActiveRecord::Generators::Migration
- ActiveRecord::Generators::MigrationGenerator
- ActiveRecord::Generators::ModelGenerator
- ActiveRecord::Generators::ObserverGenerator
- ActiveRecord::Generators::SessionMigrationGenerator
- ActiveRecord::IdentityMap
- ActiveRecord::IdentityMap::Middleware
- ActiveRecord::Inheritance
- ActiveRecord::Inheritance::ClassMethods
- ActiveRecord::Integration
- ActiveRecord::InvalidForeignKey
- ActiveRecord::IrreversibleMigration
- ActiveRecord::Locking
- ActiveRecord::Locking::Optimistic
- ActiveRecord::Locking::Optimistic::ClassMethods
- ActiveRecord::Locking::Pessimistic
- ActiveRecord::LogSubscriber
- ActiveRecord::Migration
- ActiveRecord::Migration::CommandRecorder
- ActiveRecord::MigrationProxy
- ActiveRecord::ModelSchema
- ActiveRecord::ModelSchema::ClassMethods
- ActiveRecord::MultiparameterAssignmentErrors
- ActiveRecord::NestedAttributes
- ActiveRecord::NestedAttributes::ClassMethods
- ActiveRecord::NestedAttributes::TooManyRecords
- ActiveRecord::Observer
- ActiveRecord::Persistence
- ActiveRecord::Persistence::ClassMethods
- ActiveRecord::PreparedStatementInvalid
- ActiveRecord::QueryCache
- ActiveRecord::QueryCache::ClassMethods
- ActiveRecord::QueryMethods
- ActiveRecord::Querying
- ActiveRecord::Railtie
- ActiveRecord::Railties
- ActiveRecord::Railties::ControllerRuntime
- ActiveRecord::Railties::ControllerRuntime::ClassMethods
- ActiveRecord::ReadOnlyRecord
- ActiveRecord::ReadonlyAttributes
- ActiveRecord::ReadonlyAttributes::ClassMethods
- ActiveRecord::RecordInvalid
- ActiveRecord::RecordNotFound
- ActiveRecord::RecordNotSaved
- ActiveRecord::RecordNotUnique
- ActiveRecord::Reflection
- ActiveRecord::Reflection::ClassMethods
- ActiveRecord::Reflection::MacroReflection
- ActiveRecord::Result
- ActiveRecord::Rollback
- ActiveRecord::Sanitization
- ActiveRecord::Sanitization::ClassMethods
- ActiveRecord::Schema
- ActiveRecord::Scoping
- ActiveRecord::Scoping::ClassMethods
- ActiveRecord::Scoping::Default
- ActiveRecord::Scoping::Default::ClassMethods
- ActiveRecord::Scoping::Named
- ActiveRecord::Scoping::Named::ClassMethods
- ActiveRecord::Serialization
- ActiveRecord::SerializationTypeMismatch
- ActiveRecord::SessionStore
- ActiveRecord::SessionStore::Session
- ActiveRecord::SessionStore::SqlBypass
- ActiveRecord::SpawnMethods
- ActiveRecord::StaleObjectError
- ActiveRecord::StatementInvalid
- ActiveRecord::Store
- ActiveRecord::Store::ClassMethods
- ActiveRecord::TestFixtures
- ActiveRecord::TestFixtures::ClassMethods
- ActiveRecord::ThrowResult
- ActiveRecord::Timestamp
- ActiveRecord::Transactions
- ActiveRecord::Transactions::ClassMethods
- ActiveRecord::Translation
- ActiveRecord::UnknownAttributeError
- ActiveRecord::UnknownPrimaryKey
- ActiveRecord::Validations
- ActiveRecord::Validations::AssociatedValidator
- ActiveRecord::Validations::ClassMethods
- ActiveRecord::Validations::UniquenessValidator
- ActiveRecord::WrappedDatabaseException
- Mysql
- Mysql::Result
- Mysql::Stmt
- Mysql::Time
- ArJdbcMySQL
- ArJdbcMySQL::Error

No matching classes.

# ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

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.

### Constants

ADAPTER_NAME
FOREIGN_KEY_VIOLATION

  See www.postgresql.org/docs/9.1/static/errcodes-appendix.html

NATIVE_DATABASE_TYPES
UNIQUE_VIOLATION

### Public Class Methods

new(connection, logger, connection_parameters, config) click to toggle source

Initializes and connects a PostgreSQL adapter.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 309
    def initialize(connection, logger, connection_parameters, config)
      super(connection, logger)

      if config.fetch(:prepared_statements) { true }
        @visitor = Arel::Visitors::PostgreSQL.new self
      else
        @visitor = BindSubstitution.new self
      end

      @connection_parameters, @config = connection_parameters, config

      # @local_tz is initialized as nil to avoid warnings when connect tries to use it
      @local_tz = nil
      @table_alias_length = nil

      connect
      @statements = StatementPool.new @connection,
                                      config.fetch(:statement_limit) { 1000 }

      if postgresql_version < 80200
        raise "Your version of PostgreSQL (#{postgresql_version}) is too old, please upgrade!"
      end

      @local_tz = execute('SHOW TIME ZONE', 'SCHEMA').first["TimeZone"]
    end

### Public Instance Methods

active?() click to toggle source

Is this connection alive and ready for queries?

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 341
    def active?
      @connection.query 'SELECT 1'
      true
    rescue PGError
      false
    end

adapter_name() click to toggle source

Returns 'PostgreSQL' as adapter name for identification purposes.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 238
    def adapter_name
      ADAPTER_NAME
    end

add_column(table_name, column_name, type, options = {}) click to toggle source

Adds a new column to the named table. See TableDefinition#column for details of the options you can use.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1017
    def add_column(table_name, column_name, type, options = {})
      clear_cache!
      add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
      add_column_options!(add_column_sql, options)

      execute add_column_sql
    end

begin_db_transaction() click to toggle source

Begins a transaction.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 698
    def begin_db_transaction
      execute "BEGIN"
    end

change_column(table_name, column_name, type, options = {}) click to toggle source

Changes the column of a table.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1026
    def change_column(table_name, column_name, type, options = {})
      clear_cache!
      quoted_table_name = quote_table_name(table_name)

      execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"

      change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
      change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
    end

change_column_default(table_name, column_name, default) click to toggle source

Changes the default value of a table column.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1037
    def change_column_default(table_name, column_name, default)
      clear_cache!
      execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
    end

change_column_null(table_name, column_name, null, default = nil) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1042
    def change_column_null(table_name, column_name, null, default = nil)
      clear_cache!
      unless null || default.nil?
        execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
      end
      execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
    end

clear_cache!() click to toggle source

Clears the prepared statements cache.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 336
    def clear_cache!
      @statements.clear
    end

client_min_messages() click to toggle source

Returns the current client message level.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 896
    def client_min_messages
      query('SHOW client_min_messages', 'SCHEMA')[0][0]
    end

client_min_messages=(level) click to toggle source

Set the client message level.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 901
    def client_min_messages=(level)
      execute("SET client_min_messages TO '#{level}'", 'SCHEMA')
    end

columns(table_name, name = nil) click to toggle source

Returns the list of all column definitions for a table.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 853
    def columns(table_name, name = nil)
      # Limit, precision, and scale are all handled by the superclass.
      column_definitions(table_name).collect do |column_name, type, default, notnull|
        PostgreSQLColumn.new(column_name, default, type, notnull == 'f')
      end
    end

commit_db_transaction() click to toggle source

Commits a transaction.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 703
    def commit_db_transaction
      execute "COMMIT"
    end

create_database(name, options = {}) click to toggle source

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'

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 744
    def create_database(name, options = {})
      options = options.reverse_merge(:encoding => "utf8")

      option_string = options.symbolize_keys.sum do |key, value|
        case key
        when :owner
          " OWNER = \"#{value}\""
        when :template
          " TEMPLATE = \"#{value}\""
        when :encoding
          " ENCODING = '#{value}'"
        when :tablespace
          " TABLESPACE = \"#{value}\""
        when :connection_limit
          " CONNECTION LIMIT = #{value}"
        else
          ""
        end
      end

      execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
    end

create_savepoint() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 716
    def create_savepoint
      execute("SAVEPOINT #{current_savepoint_name}")
    end

current_database() click to toggle source

Returns the current database name.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 861
    def current_database
      query('select current_database()', 'SCHEMA')[0][0]
    end

current_schema() click to toggle source

Returns the current schema name.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 866
    def current_schema
      query('SELECT current_schema', 'SCHEMA')[0][0]
    end

disconnect!() click to toggle source

Disconnects from the database if already connected. Otherwise, this method does nothing.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 363
    def disconnect!
      clear_cache!
      @connection.close rescue nil
    end

encoding() click to toggle source

Returns the current database encoding format.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 871
    def encoding
      query(          SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database          WHERE pg_database.datname LIKE '#{current_database}', 'SCHEMA')[0][0]
    end

escape_bytea(value) click to toggle source

Escapes binary strings for bytea input to the database.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 416
    def escape_bytea(value)
      @connection.escape_bytea(value) if value
    end

exec_delete(sql, name = 'SQL', binds = []) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 669
    def exec_delete(sql, name = 'SQL', binds = [])
      log(sql, name, binds) do
        result = binds.empty? ? exec_no_cache(sql, binds) :
                                exec_cache(sql, binds)
        affected = result.cmd_tuples
        result.clear
        affected
      end
    end

Also aliased as: exec_update
exec_query(sql, name = 'SQL', binds = []) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 658
    def exec_query(sql, name = 'SQL', binds = [])
      log(sql, name, binds) do
        result = binds.empty? ? exec_no_cache(sql, binds) :
                                exec_cache(sql, binds)

        ret = ActiveRecord::Result.new(result.fields, result_as_array(result))
        result.clear
        return ret
      end
    end

exec_update(sql, name = 'SQL', binds = []) click to toggle source
Alias for: exec_delete
execute(sql, name = nil) click to toggle source

Executes an SQL statement, returning a PGresult object on success or raising a PGError exception otherwise.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 648
    def execute(sql, name = nil)
      log(sql, name) do
        @connection.async_exec(sql)
      end
    end

explain(arel, binds = []) click to toggle source

DATABASE STATEMENTS ======================================

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 532
    def explain(arel, binds = [])
      sql = "EXPLAIN #{to_sql(arel, binds)}"
      ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds))
    end

index_name_length() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1064
    def index_name_length
      63
    end

indexes(table_name, name = nil) click to toggle source

Returns an array of indexes for the given table.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 814
    def indexes(table_name, name = nil)
       result = query(           SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid           FROM pg_class t           INNER JOIN pg_index d ON t.oid = d.indrelid           INNER JOIN pg_class i ON d.indexrelid = i.oid           WHERE i.relkind = 'i'             AND d.indisprimary = 'f'             AND t.relname = '#{table_name}'             AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )          ORDER BY i.relname, 'SCHEMA')

      result.map do |row|
        index_name = row[0]
        unique = row[1] == 't'
        indkey = row[2].split(" ")
        inddef = row[3]
        oid = row[4]

        columns = Hash[query(          SELECT a.attnum, a.attname          FROM pg_attribute a          WHERE a.attrelid = #{oid}          AND a.attnum IN (#{indkey.join(",")}), "SCHEMA")]

        column_names = columns.values_at(*indkey).compact

        # add info on sort order for columns (only desc order is explicitly specified, asc is the default)
        desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
        orders = desc_order_columns.any? ? Hash[desc_order_columns.map {|order_column| [order_column, :desc]}] : {}

        column_names.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names, [], orders)
      end.compact
    end

insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) click to toggle source

Executes an INSERT query and returns the new record's ID

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 581
    def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
      unless pk
        # Extract the table from the insert sql. Yuck.
        table_ref = extract_table_ref_from_insert_sql(sql)
        pk = primary_key(table_ref) if table_ref
      end

      if pk
        select_value("#{sql} RETURNING #{quote_column_name(pk)}")
      else
        super
      end
    end

outside_transaction?() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 712
    def outside_transaction?
      @connection.transaction_status == PGconn::PQTRANS_IDLE
    end

primary_key(table) click to toggle source

Returns just a table's primary key

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 987
    def primary_key(table)
      row = exec_query(          SELECT attr.attname          FROM pg_attribute attr          INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]          WHERE cons.contype = 'p'            AND cons.conrelid = '#{quote_table_name(table)}'::regclass, 'SCHEMA').rows.first

      row && row.first
    end

quote_table_name(name) click to toggle source

Checks the following cases:

- table_name

- "table.name"

- schema_name.table_name

- schema_name."table.name"

- "schema.name".table_name

- "schema.name"."table.name"

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 481
    def quote_table_name(name)
      schema, name_part = extract_pg_identifier_from_name(name.to_s)

      unless name_part
        quote_column_name(schema)
      else
        table_name, name_part = extract_pg_identifier_from_name(name_part)
        "#{quote_column_name(schema)}.#{quote_column_name(table_name)}"
      end
    end

reconnect!() click to toggle source

Close then reopen the connection.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 349
    def reconnect!
      clear_cache!
      @connection.reset
      @open_transactions = 0
      configure_connection
    end

release_savepoint() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 724
    def release_savepoint
      execute("RELEASE SAVEPOINT #{current_savepoint_name}")
    end

rename_column(table_name, column_name, new_column_name) click to toggle source

Renames a column in a table.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1051
    def rename_column(table_name, column_name, new_column_name)
      clear_cache!
      execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
    end

rename_index(table_name, old_name, new_name) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1060
    def rename_index(table_name, old_name, new_name)
      execute "ALTER INDEX #{quote_column_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
    end

rename_table(name, new_name) click to toggle source

Renames a table. Also renames a table's primary key sequence if the sequence name matches the Active Record default.

Example:

    rename_table('octopuses', 'octopi')

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1005
    def rename_table(name, new_name)
      clear_cache!
      execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
      pk, seq = pk_and_sequence_for(new_name)
      if seq == "#{name}_#{pk}_seq"
        new_seq = "#{new_name}_#{pk}_seq"
        execute "ALTER TABLE #{quote_table_name(seq)} RENAME TO #{quote_table_name(new_seq)}"
      end
    end

reset!() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 356
    def reset!
      clear_cache!
      super
    end

rollback_db_transaction() click to toggle source

Aborts a transaction.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 708
    def rollback_db_transaction
      execute "ROLLBACK"
    end

rollback_to_savepoint() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 720
    def rollback_to_savepoint
      execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
    end

schema_exists?(name) click to toggle source

Returns true if schema exists.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 805
    def schema_exists?(name)
      exec_query(          SELECT COUNT(*)          FROM pg_namespace          WHERE nspname = '#{name}', 'SCHEMA').rows.first[0].to_i > 0
    end

schema_search_path() click to toggle source

Returns the active schema search path.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 891
    def schema_search_path
      @schema_search_path ||= query('SHOW search_path', 'SCHEMA')[0][0]
    end

schema_search_path=(schema_csv) click to toggle source

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.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 883
    def schema_search_path=(schema_csv)
      if schema_csv
        execute("SET search_path TO #{schema_csv}", 'SCHEMA')
        @schema_search_path = schema_csv
      end
    end

select_rows(sql, name = nil) click to toggle source

Executes a SELECT query and returns an array of rows. Each row is an array of field values.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 576
    def select_rows(sql, name = nil)
      select_raw(sql, name).last
    end

serial_sequence(table, column) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 912
    def serial_sequence(table, column)
      result = exec_query(          SELECT pg_get_serial_sequence('#{table}', '#{column}'), 'SCHEMA')
      result.rows.first.first
    end

session_auth=(user) click to toggle source

Set the authorized user for this session

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 508
    def session_auth=(user)
      clear_cache!
      exec_query "SET SESSION AUTHORIZATION #{user}"
    end

set_standard_conforming_strings() click to toggle source

Enable standard-conforming strings if available.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 383
    def set_standard_conforming_strings
      old, self.client_min_messages = client_min_messages, 'panic'
      execute('SET standard_conforming_strings = on', 'SCHEMA') rescue nil
    ensure
      self.client_min_messages = old
    end

sql_for_insert(sql, pk, id_value, sequence_name, binds) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 680
    def sql_for_insert(sql, pk, id_value, sequence_name, binds)
      unless pk
        # Extract the table from the insert sql. Yuck.
        table_ref = extract_table_ref_from_insert_sql(sql)
        pk = primary_key(table_ref) if table_ref
      end

      sql = "#{sql} RETURNING #{quote_column_name(pk)}" if pk

      [sql, binds]
    end

substitute_at(column, index) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 654
    def substitute_at(column, index)
      Arel::Nodes::BindParam.new "$#{index + 1}"
    end

supports_ddl_transactions?() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 394
    def supports_ddl_transactions?
      true
    end

supports_explain?() click to toggle source

Returns true.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 404
    def supports_explain?
      true
    end

supports_index_sort_order?() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 248
    def supports_index_sort_order?
      true
    end

supports_insert_with_returning?() click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 390
    def supports_insert_with_returning?
      true
    end

supports_migrations?() click to toggle source

Returns true, since this connection adapter supports migrations.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 373
    def supports_migrations?
      true
    end

supports_savepoints?() click to toggle source

Returns true, since this connection adapter supports savepoints.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 399
    def supports_savepoints?
      true
    end

supports_statement_cache?() click to toggle source

Returns `true`, since this connection adapter supports prepared statement caching.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 244
    def supports_statement_cache?
      true
    end

table_alias_length() click to toggle source

Returns the configured supported identifier length supported by PostgreSQL

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 409
    def table_alias_length
      @table_alias_length ||= query('SHOW max_identifier_length')[0][0].to_i
    end

table_exists?(name) click to toggle source

Returns true if table exists. If the schema is not specified as part of `name` then it will only find tables within the current schema search path (regardless of permissions to access tables in other schemas)

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 787
    def table_exists?(name)
      schema, table = Utils.extract_schema_and_table(name.to_s)
      return false unless table

      binds = [[nil, table]]
      binds << [nil, schema] if schema

      exec_query(            SELECT COUNT(*)            FROM pg_class c            LEFT JOIN pg_namespace n ON n.oid = c.relnamespace            WHERE c.relkind in ('v','r')            AND c.relname = '#{table.gsub(/(^"|"$)/,'')}'            AND n.nspname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'}, 'SCHEMA').rows.first[0].to_i > 0
    end

tables(name = nil) click to toggle source

Returns the list of all tables in the schema search path or a specified schema.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 776
    def tables(name = nil)
      query(          SELECT tablename          FROM pg_tables          WHERE schemaname = ANY (current_schemas(false)), 'SCHEMA').map { |row| row[0] }
    end

type_cast(value, column) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 456
    def type_cast(value, column)
      return super unless column

      case value
      when String
        return super unless 'bytea' == column.sql_type
        { :value => value, :format => 1 }
      else
        super
      end
    end

type_to_sql(type, limit = nil, precision = nil, scale = nil) click to toggle source

Maps logical Rails types to PostgreSQL-specific data types.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1069
    def type_to_sql(type, limit = nil, precision = nil, scale = nil)
      case type.to_s
      when 'binary'
        # PostgreSQL doesn't support limits on binary (bytea) columns.
        # The hard limit is 1Gb, because of a 32-bit size field, and TOAST.
        case limit
        when nil, 0..0x3fffffff; super(type)
        else raise(ActiveRecordError, "No binary type has byte size #{limit}.")
        end
      when 'text'
        # PostgreSQL doesn't support limits on text columns.
        # The hard limit is 1Gb, according to section 8.3 in the manual.
        case limit
        when nil, 0..0x3fffffff; super(type)
        else raise(ActiveRecordError, "The limit on text can be at most 1GB - 1byte.")
        end
      when 'integer'
        return 'integer' unless limit

        case limit
          when 1, 2; 'smallint'
          when 3, 4; 'integer'
          when 5..8; 'bigint'
          else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
        end
      else
        super
      end
    end

unescape_bytea(value) click to toggle source

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.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 423
    def unescape_bytea(value)
      @connection.unescape_bytea(value) if value
    end

update_sql(sql, name = nil) click to toggle source

Executes an UPDATE query and returns the number of affected tuples.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 693
    def update_sql(sql, name = nil)
      super.cmd_tuples
    end

### Protected Instance Methods

postgresql_version() click to toggle source

Returns the version of the connected PostgreSQL server.

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1138
    def postgresql_version
      @connection.server_version
    end

translate_exception(exception, message) click to toggle source

    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 1146
    def translate_exception(exception, message)
      return exception unless exception.respond_to?(:result)

      case exception.result.try(:error_field, PGresult::PG_DIAG_SQLSTATE)
      when UNIQUE_VIOLATION
        RecordNotUnique.new(message, exception)
      when FOREIGN_KEY_VIOLATION
        InvalidForeignKey.new(message, exception)
      else
        super
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.