postgresql_adapter.rb 30.1 KB
Newer Older
1
# Make sure we're using pg high enough for type casts and Ruby 2.2+ compatibility
2 3
gem "pg", "~> 0.18"
require "pg"
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18
require_relative "abstract_adapter"
require_relative "statement_pool"
require_relative "postgresql/column"
require_relative "postgresql/database_statements"
require_relative "postgresql/explain_pretty_printer"
require_relative "postgresql/oid"
require_relative "postgresql/quoting"
require_relative "postgresql/referential_integrity"
require_relative "postgresql/schema_creation"
require_relative "postgresql/schema_definitions"
require_relative "postgresql/schema_dumper"
require_relative "postgresql/schema_statements"
require_relative "postgresql/type_metadata"
require_relative "postgresql/utils"
19

D
Initial  
David Heinemeier Hansson 已提交
20
module ActiveRecord
21
  module ConnectionHandling # :nodoc:
D
Initial  
David Heinemeier Hansson 已提交
22
    # Establishes a connection to the database that's used by all Active Record objects
23
    def postgresql_connection(config)
24
      conn_params = config.symbolize_keys
D
Initial  
David Heinemeier Hansson 已提交
25

26
      conn_params.delete_if { |_, v| v.nil? }
27 28 29 30

      # Map ActiveRecords param names to PGs.
      conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
      conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]
D
Initial  
David Heinemeier Hansson 已提交
31

32 33
      # Forward only valid config params to PG::Connection.connect.
      valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
34
      conn_params.slice!(*valid_conn_param_keys)
35

36
      # The postgres drivers don't allow the creation of an unconnected PG::Connection object,
37
      # so just pass a nil connection object for the time being.
38
      ConnectionAdapters::PostgreSQLAdapter.new(nil, logger, conn_params, config)
39 40
    end
  end
41

42
  module ConnectionAdapters
43
    # The PostgreSQL adapter works with the native C (https://bitbucket.org/ged/ruby-pg) driver.
44 45 46
    #
    # Options:
    #
47 48
    # * <tt>:host</tt> - Defaults to a Unix-domain socket in /tmp. On machines without Unix-domain sockets,
    #   the default is to connect to localhost.
P
Pratik Naik 已提交
49
    # * <tt>:port</tt> - Defaults to 5432.
50 51 52
    # * <tt>:username</tt> - Defaults to be the same as the operating system name of the user running the application.
    # * <tt>:password</tt> - Password to be used if the server demands password authentication.
    # * <tt>:database</tt> - Defaults to be the same as the user name.
53
    # * <tt>:schema_search_path</tt> - An optional schema search path for the connection given
54
    #   as a string of comma-separated schema names. This is backward-compatible with the <tt>:schema_order</tt> option.
55
    # * <tt>:encoding</tt> - An optional client encoding that is used in a <tt>SET client_encoding TO
56
    #   <encoding></tt> call on the connection.
57
    # * <tt>:min_messages</tt> - An optional client min messages that is used in a
58
    #   <tt>SET client_min_messages TO <min_messages></tt> call on the connection.
59 60
    # * <tt>:variables</tt> - An optional hash of additional parameters that
    #   will be used in <tt>SET SESSION key = val</tt> calls on the connection.
61
    # * <tt>:insert_returning</tt> - An optional boolean to control the use of <tt>RETURNING</tt> for <tt>INSERT</tt> statements
62
    #   defaults to true.
63 64
    #
    # Any further options are used as connection parameters to libpq. See
65
    # http://www.postgresql.org/docs/current/static/libpq-connect.html for the
66 67 68
    # list of parameters.
    #
    # In addition, default connection parameters of libpq can be set per environment variables.
69
    # See http://www.postgresql.org/docs/current/static/libpq-envars.html .
70
    class PostgreSQLAdapter < AbstractAdapter
71
      ADAPTER_NAME = "PostgreSQL".freeze
72 73

      NATIVE_DATABASE_TYPES = {
74
        primary_key: "bigserial primary key",
75
        string:      { name: "character varying" },
76 77 78 79 80 81 82
        text:        { name: "text" },
        integer:     { name: "integer" },
        float:       { name: "float" },
        decimal:     { name: "decimal" },
        datetime:    { name: "timestamp" },
        time:        { name: "time" },
        date:        { name: "date" },
B
bUg 已提交
83 84 85 86 87 88
        daterange:   { name: "daterange" },
        numrange:    { name: "numrange" },
        tsrange:     { name: "tsrange" },
        tstzrange:   { name: "tstzrange" },
        int4range:   { name: "int4range" },
        int8range:   { name: "int8range" },
89 90 91 92 93 94 95 96
        binary:      { name: "bytea" },
        boolean:     { name: "boolean" },
        xml:         { name: "xml" },
        tsvector:    { name: "tsvector" },
        hstore:      { name: "hstore" },
        inet:        { name: "inet" },
        cidr:        { name: "cidr" },
        macaddr:     { name: "macaddr" },
97
        uuid:        { name: "uuid" },
98
        json:        { name: "json" },
99
        jsonb:       { name: "jsonb" },
100
        ltree:       { name: "ltree" },
101
        citext:      { name: "citext" },
S
Sean Griffin 已提交
102
        point:       { name: "point" },
103 104 105 106 107 108
        line:        { name: "line" },
        lseg:        { name: "lseg" },
        box:         { name: "box" },
        path:        { name: "path" },
        polygon:     { name: "polygon" },
        circle:      { name: "circle" },
S
Sean Griffin 已提交
109 110
        bit:         { name: "bit" },
        bit_varying: { name: "bit varying" },
111
        money:       { name: "money" },
112
        interval:    { name: "interval" },
113
        oid:         { name: "oid" },
114 115
      }

116 117 118 119
      OID = PostgreSQL::OID #:nodoc:

      include PostgreSQL::Quoting
      include PostgreSQL::ReferentialIntegrity
120
      include PostgreSQL::SchemaStatements
121
      include PostgreSQL::DatabaseStatements
122
      include PostgreSQL::ColumnDumper
123

124 125 126 127
      def supports_index_sort_order?
        true
      end

128 129 130 131
      def supports_partial_index?
        true
      end

132 133 134 135
      def supports_expression_index?
        true
      end

136 137 138 139
      def supports_transaction_isolation?
        true
      end

140 141 142 143
      def supports_foreign_keys?
        true
      end

144 145 146 147
      def supports_views?
        true
      end

148 149 150 151
      def supports_datetime_with_precision?
        true
      end

152 153 154 155
      def supports_json?
        postgresql_version >= 90200
      end

156 157 158 159
      def supports_comments?
        true
      end

160 161 162 163
      def supports_savepoints?
        true
      end

164
      def index_algorithms
165
        { concurrently: "CONCURRENTLY" }
166 167
      end

168 169
      class StatementPool < ConnectionAdapters::StatementPool
        def initialize(connection, max)
170 171
          super(max)
          @connection = connection
172 173 174 175 176 177 178 179
          @counter = 0
        end

        def next_key
          "a#{@counter + 1}"
        end

        def []=(sql, key)
180
          super.tap { @counter += 1 }
181 182
        end

183
        private
184

185 186 187 188 189
          def dealloc(key)
            @connection.query "DEALLOCATE #{key}" if connection_active?
          end

          def connection_active?
190 191
            @connection.status == PG::CONNECTION_OK
          rescue PG::Error
192 193
            false
          end
194 195
      end

196 197
      # Initializes and connects a PostgreSQL adapter.
      def initialize(connection, logger, connection_parameters, config)
198
        super(connection, logger, config)
199

200
        @connection_parameters = connection_parameters
201

202 203
        # @local_tz is initialized as nil to avoid warnings when connect tries to use it
        @local_tz = nil
204
        @max_identifier_length = nil
205

206
        connect
207
        add_pg_encoders
208
        @statements = StatementPool.new @connection,
V
Vipul A M 已提交
209
                                        self.class.type_cast_config_to_integer(config[:statement_limit])
210

211
        if postgresql_version < 90100
212
          raise "Your version of PostgreSQL (#{postgresql_version}) is too old. Active Record supports PostgreSQL >= 9.1."
213 214
        end

215 216
        add_pg_decoders

217
        @type_map = Type::HashLookupTypeMap.new
218
        initialize_type_map(type_map)
219
        @local_tz = execute("SHOW TIME ZONE", "SCHEMA").first["TimeZone"]
220
        @use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
221 222
      end

X
Xavier Noria 已提交
223
      # Clears the prepared statements cache.
224
      def clear_cache!
225 226 227
        @lock.synchronize do
          @statements.clear
        end
228 229
      end

230 231 232 233
      def truncate(table_name, name = nil)
        exec_query "TRUNCATE TABLE #{quote_table_name(table_name)}", name, []
      end

234 235
      # Is this connection alive and ready for queries?
      def active?
236 237 238
        @lock.synchronize do
          @connection.query "SELECT 1"
        end
239
        true
240
      rescue PG::Error
241
        false
242 243 244 245
      end

      # Close then reopen the connection.
      def reconnect!
246 247 248 249 250
        @lock.synchronize do
          super
          @connection.reset
          configure_connection
        end
251
      end
252

253
      def reset!
254 255 256 257 258 259 260 261
        @lock.synchronize do
          clear_cache!
          reset_transaction
          unless @connection.transaction_status == ::PG::PQTRANS_IDLE
            @connection.query "ROLLBACK"
          end
          @connection.query "DISCARD ALL"
          configure_connection
262
        end
263 264
      end

265 266
      # Disconnects from the database if already connected. Otherwise, this
      # method does nothing.
267
      def disconnect!
268 269 270 271
        @lock.synchronize do
          super
          @connection.close rescue nil
        end
272
      end
273

274
      def native_database_types #:nodoc:
275
        NATIVE_DATABASE_TYPES
276
      end
277

278
      def set_standard_conforming_strings
279
        execute("SET standard_conforming_strings = on", "SCHEMA")
280 281
      end

282 283 284
      def supports_ddl_transactions?
        true
      end
285

286 287 288 289
      def supports_advisory_locks?
        true
      end

290 291 292 293
      def supports_explain?
        true
      end

294
      def supports_extensions?
295
        true
296 297
      end

A
Andrew White 已提交
298
      def supports_ranges?
299
        # Range datatypes weren't introduced until PostgreSQL 9.2
A
Andrew White 已提交
300 301 302
        postgresql_version >= 90200
      end

303 304 305 306
      def supports_materialized_views?
        postgresql_version >= 90300
      end

307 308 309 310
      def supports_pgcrypto_uuid?
        postgresql_version >= 90400
      end

311 312 313
      def get_advisory_lock(lock_id) # :nodoc:
        unless lock_id.is_a?(Integer) && lock_id.bit_length <= 63
          raise(ArgumentError, "Postgres requires advisory lock ids to be a signed 64 bit integer")
314
        end
315
        select_value("SELECT pg_try_advisory_lock(#{lock_id});")
316 317
      end

318 319 320
      def release_advisory_lock(lock_id) # :nodoc:
        unless lock_id.is_a?(Integer) && lock_id.bit_length <= 63
          raise(ArgumentError, "Postgres requires advisory lock ids to be a signed 64 bit integer")
321
        end
322
        select_value("SELECT pg_advisory_unlock(#{lock_id})")
323 324
      end

325
      def enable_extension(name)
326
        exec_query("CREATE EXTENSION IF NOT EXISTS \"#{name}\"").tap {
327 328
          reload_type_map
        }
329 330 331
      end

      def disable_extension(name)
332
        exec_query("DROP EXTENSION IF EXISTS \"#{name}\" CASCADE").tap {
333 334
          reload_type_map
        }
335 336 337
      end

      def extension_enabled?(name)
R
Rafael Mendonça França 已提交
338
        if supports_extensions?
339
          res = exec_query "SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '#{name}' AND installed_version IS NOT NULL) as enabled",
340
            "SCHEMA"
341
          res.cast_values.first
342
        end
343 344
      end

345 346
      def extensions
        if supports_extensions?
347
          exec_query("SELECT extname from pg_extension", "SCHEMA").cast_values
348
        else
349
          super
350 351 352
        end
      end

353
      # Returns the configured supported identifier length supported by PostgreSQL
354
      def table_alias_length
355
        @max_identifier_length ||= select_value("SHOW max_identifier_length", "SCHEMA").to_i
356
      end
357
      alias index_name_length table_alias_length
358

359 360
      # Set the authorized user for this session
      def session_auth=(user)
361
        clear_cache!
A
Aaron Patterson 已提交
362
        exec_query "SET SESSION AUTHORIZATION #{user}"
363 364
      end

365 366
      def use_insert_returning?
        @use_insert_returning
367 368
      end

369
      def update_table_definition(table_name, base) #:nodoc:
370
        PostgreSQL::Table.new(table_name, base)
371 372
      end

373 374 375 376 377 378 379 380 381 382
      def column_name_for_operation(operation, node) # :nodoc:
        OPERATION_ALIASES.fetch(operation) { operation.downcase }
      end

      OPERATION_ALIASES = { # :nodoc:
        "maximum" => "max",
        "minimum" => "min",
        "average" => "avg",
      }

D
Derek Prior 已提交
383 384 385 386
      # Returns the version of the connected PostgreSQL server.
      def postgresql_version
        @connection.server_version
      end
387

388 389 390 391
      def default_index_type?(index) # :nodoc:
        index.using == :btree || super
      end

392
      private
393

394
        # See http://www.postgresql.org/docs/current/static/errcodes-appendix.html
395
        VALUE_LIMIT_VIOLATION = "22001"
396
        NUMERIC_VALUE_OUT_OF_RANGE = "22003"
397
        NOT_NULL_VIOLATION    = "23502"
398 399
        FOREIGN_KEY_VIOLATION = "23503"
        UNIQUE_VIOLATION      = "23505"
400
        SERIALIZATION_FAILURE = "40001"
401
        DEADLOCK_DETECTED     = "40P01"
402

403
        def translate_exception(exception, message)
404 405
          return exception unless exception.respond_to?(:result)

406
          case exception.result.try(:error_field, PG::PG_DIAG_SQLSTATE)
407
          when UNIQUE_VIOLATION
408
            RecordNotUnique.new(message)
409
          when FOREIGN_KEY_VIOLATION
410
            InvalidForeignKey.new(message)
411 412
          when VALUE_LIMIT_VIOLATION
            ValueTooLong.new(message)
413 414
          when NUMERIC_VALUE_OUT_OF_RANGE
            RangeError.new(message)
415 416
          when NOT_NULL_VIOLATION
            NotNullViolation.new(message)
417
          when SERIALIZATION_FAILURE
418
            SerializationFailure.new(message)
419
          when DEADLOCK_DETECTED
420
            Deadlocked.new(message)
421 422 423 424 425
          else
            super
          end
        end

426
        def get_oid_type(oid, fmod, column_name, sql_type = "".freeze)
427
          if !type_map.key?(oid)
428
            load_additional_types(type_map, [oid])
429 430
          end

431 432
          type_map.fetch(oid, fmod, sql_type) {
            warn "unknown OID #{oid}: failed to recognize type of '#{column_name}'. It will be treated as String."
433
            Type.default_value.tap do |cast_type|
434 435 436
              type_map.register_type(oid, cast_type)
            end
          }
437 438
        end

A
Akira Matsuda 已提交
439
        def initialize_type_map(m)
440 441 442
          register_class_with_limit m, "int2", Type::Integer
          register_class_with_limit m, "int4", Type::Integer
          register_class_with_limit m, "int8", Type::Integer
443
          m.register_type "oid", OID::Oid.new
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
          m.register_type "float4", Type::Float.new
          m.alias_type "float8", "float4"
          m.register_type "text", Type::Text.new
          register_class_with_limit m, "varchar", Type::String
          m.alias_type "char", "varchar"
          m.alias_type "name", "varchar"
          m.alias_type "bpchar", "varchar"
          m.register_type "bool", Type::Boolean.new
          register_class_with_limit m, "bit", OID::Bit
          register_class_with_limit m, "varbit", OID::BitVarying
          m.alias_type "timestamptz", "timestamp"
          m.register_type "date", Type::Date.new

          m.register_type "money", OID::Money.new
          m.register_type "bytea", OID::Bytea.new
          m.register_type "point", OID::Point.new
          m.register_type "hstore", OID::Hstore.new
461
          m.register_type "json", Type::Json.new
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
          m.register_type "jsonb", OID::Jsonb.new
          m.register_type "cidr", OID::Cidr.new
          m.register_type "inet", OID::Inet.new
          m.register_type "uuid", OID::Uuid.new
          m.register_type "xml", OID::Xml.new
          m.register_type "tsvector", OID::SpecializedString.new(:tsvector)
          m.register_type "macaddr", OID::SpecializedString.new(:macaddr)
          m.register_type "citext", OID::SpecializedString.new(:citext)
          m.register_type "ltree", OID::SpecializedString.new(:ltree)
          m.register_type "line", OID::SpecializedString.new(:line)
          m.register_type "lseg", OID::SpecializedString.new(:lseg)
          m.register_type "box", OID::SpecializedString.new(:box)
          m.register_type "path", OID::SpecializedString.new(:path)
          m.register_type "polygon", OID::SpecializedString.new(:polygon)
          m.register_type "circle", OID::SpecializedString.new(:circle)
477

478 479 480 481
          m.register_type "interval" do |_, _, sql_type|
            precision = extract_precision(sql_type)
            OID::SpecializedString.new(:interval, precision: precision)
          end
482

483 484
          register_class_with_precision m, "time", Type::Time
          register_class_with_precision m, "timestamp", OID::DateTime
S
Sean Griffin 已提交
485

486
          m.register_type "numeric" do |_, fmod, sql_type|
S
Sean Griffin 已提交
487
            precision = extract_precision(sql_type)
S
Sean Griffin 已提交
488
            scale = extract_scale(sql_type)
S
Sean Griffin 已提交
489

490 491 492 493 494 495 496 497
            # The type for the numeric depends on the width of the field,
            # so we'll do something special here.
            #
            # When dealing with decimal columns:
            #
            # places after decimal  = fmod - 4 & 0xffff
            # places before decimal = (fmod - 4) >> 16 & 0xffff
            if fmod && (fmod - 4 & 0xffff).zero?
498 499
              # FIXME: Remove this class, and the second argument to
              # lookups on PG
500 501 502 503
              Type::DecimalWithoutScale.new(precision: precision)
            else
              OID::Decimal.new(precision: precision, scale: scale)
            end
S
Sean Griffin 已提交
504 505
          end

506 507 508
          load_additional_types(m)
        end

A
Akira Matsuda 已提交
509
        def extract_limit(sql_type)
S
Sean Griffin 已提交
510
          case sql_type
511 512 513 514 515 516
          when /^bigint/i, /^int8/i
            8
          when /^smallint/i
            2
          else
            super
S
Sean Griffin 已提交
517 518 519
          end
        end

520
        # Extracts the value from a PostgreSQL column default definition.
A
Akira Matsuda 已提交
521
        def extract_value_from_default(default)
522
          case default
523
            # Quoted types
524
          when /\A[\(B]?'(.*)'.*::"?([\w. ]+)"?(?:\[\])?\z/m
525
            # The default 'now'::date is CURRENT_DATE
526 527 528 529 530
            if $1 == "now".freeze && $2 == "date".freeze
              nil
            else
              $1.gsub("''".freeze, "'".freeze)
            end
531
            # Boolean types
532 533
          when "true".freeze, "false".freeze
            default
534
            # Numeric types
535 536
          when /\A\(?(-?\d+(\.\d*)?)\)?(::bigint)?\z/
            $1
537
            # Object identifier types
538 539
          when /\A-?\d+\z/
            $1
540 541 542
          else
            # Anything else is blank, some user type, or some function
            # and we can't know the value of that, so return nil.
543
            nil
544 545 546
          end
        end

A
Akira Matsuda 已提交
547
        def extract_default_function(default_value, default)
548 549 550
          default if has_default_function?(default_value, default)
        end

A
Akira Matsuda 已提交
551
        def has_default_function?(default_value, default)
552
          !default_value && %r{\w+\(.*\)|\(.*\)::\w+|CURRENT_DATE|CURRENT_TIMESTAMP}.match?(default)
553 554
        end

A
Akira Matsuda 已提交
555
        def load_additional_types(type_map, oids = nil)
556 557
          initializer = OID::TypeMapInitializer.new(type_map)

558
          if supports_ranges?
559
            query = <<-SQL
560
              SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype
561 562 563 564
              FROM pg_type as t
              LEFT JOIN pg_range as r ON oid = rngtypid
            SQL
          else
565
            query = <<-SQL
566
              SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, t.typtype, t.typbasetype
567 568 569
              FROM pg_type as t
            SQL
          end
570 571 572

          if oids
            query += "WHERE t.oid::integer IN (%s)" % oids.join(", ")
573 574
          else
            query += initializer.query_conditions_for_initial_load(type_map)
575 576
          end

577
          execute_and_clear(query, "SCHEMA", []) do |records|
578 579
            initializer.run(records)
          end
580 581
        end

582
        FEATURE_NOT_SUPPORTED = "0A000" #:nodoc:
583

584 585 586 587 588 589 590 591
        def execute_and_clear(sql, name, binds, prepare: false)
          if without_prepared_statement?(binds)
            result = exec_no_cache(sql, name, [])
          elsif !prepare
            result = exec_no_cache(sql, name, binds)
          else
            result = exec_cache(sql, name, binds)
          end
592 593 594 595 596
          ret = yield result
          result.clear
          ret
        end

597
        def exec_no_cache(sql, name, binds)
598
          type_casted_binds = type_casted_binds(binds)
599 600 601 602 603
          log(sql, name, binds, type_casted_binds) do
            ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
              @connection.async_exec(sql, type_casted_binds)
            end
          end
604
        end
605

606
        def exec_cache(sql, name, binds)
607
          stmt_key = prepare_statement(sql)
608
          type_casted_binds = type_casted_binds(binds)
609

610
          log(sql, name, binds, type_casted_binds, stmt_key) do
611 612 613
            ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
              @connection.exec_prepared(stmt_key, type_casted_binds)
            end
614 615
          end
        rescue ActiveRecord::StatementInvalid => e
616
          raise unless is_cached_plan_failure?(e)
617

618 619 620 621 622
          # Nothing we can do if we are in a transaction because all commands
          # will raise InFailedSQLTransaction
          if in_transaction?
            raise ActiveRecord::PreparedStatementCacheExpired.new(e.cause.message)
          else
623 624 625 626
            @lock.synchronize do
              # outside of transactions we can simply flush this query and retry
              @statements.delete sql_key(sql)
            end
627
            retry
628 629 630
          end
        end

631 632 633 634 635 636 637 638 639
        # Annoyingly, the code for prepared statements whose return value may
        # have changed is FEATURE_NOT_SUPPORTED.
        #
        # This covers various different error types so we need to do additional
        # work to classify the exception definitively as a
        # ActiveRecord::PreparedStatementCacheExpired
        #
        # Check here for more details:
        # http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
640
        CACHED_PLAN_HEURISTIC = "cached plan must not change result type".freeze
641 642
        def is_cached_plan_failure?(e)
          pgerror = e.cause
643
          code = pgerror.result.result_error_field(PG::PG_DIAG_SQLSTATE)
644 645 646 647 648 649 650 651 652
          code == FEATURE_NOT_SUPPORTED && pgerror.message.include?(CACHED_PLAN_HEURISTIC)
        rescue
          false
        end

        def in_transaction?
          open_transactions > 0
        end

653 654 655 656 657 658 659 660 661
        # Returns the statement identifier for the client side cache
        # of statements
        def sql_key(sql)
          "#{schema_search_path}-#{sql}"
        end

        # Prepare the statement if it hasn't been prepared, return
        # the statement key.
        def prepare_statement(sql)
662 663 664 665 666 667 668 669 670 671 672 673
          @lock.synchronize do
            sql_key = sql_key(sql)
            unless @statements.key? sql_key
              nextkey = @statements.next_key
              begin
                @connection.prepare nextkey, sql
              rescue => e
                raise translate_exception_class(e, sql)
              end
              # Clear the queue
              @connection.get_last_result
              @statements[sql_key] = nextkey
674
            end
675
            @statements[sql_key]
676 677
          end
        end
678

679 680 681
        # Connects to a PostgreSQL server and sets up the adapter depending on the
        # connected server's characteristics.
        def connect
682
          @connection = PG.connect(@connection_parameters)
683
          configure_connection
684 685
        rescue ::PG::Error => error
          if error.message.include?("does not exist")
686
            raise ActiveRecord::NoDatabaseError
687
          else
688
            raise
689
          end
690 691
        end

692
        # Configures the encoding, verbosity, schema search path, and time zone of the connection.
693
        # This is called by #connect and should not be called manually.
694 695
        def configure_connection
          if @config[:encoding]
696
            @connection.set_client_encoding(@config[:encoding])
697
          end
698
          self.client_min_messages = @config[:min_messages] || "warning"
699
          self.schema_search_path = @config[:schema_search_path] || @config[:schema_order]
700

701
          # Use standard-conforming strings so we don't have to do the E'...' dance.
702 703
          set_standard_conforming_strings

704
          # If using Active Record's time zone support configure the connection to return
705
          # TIMESTAMP WITH ZONE types in UTC.
706
          # (SET TIME ZONE does not use an equals sign like other SET variables)
707
          if ActiveRecord::Base.default_timezone == :utc
708
            execute("SET time zone 'UTC'", "SCHEMA")
709
          elsif @local_tz
710
            execute("SET time zone '#{@local_tz}'", "SCHEMA")
711
          end
712 713

          # SET statements from :variables config hash
714
          # http://www.postgresql.org/docs/current/static/sql-set.html
715 716
          variables = @config[:variables] || {}
          variables.map do |k, v|
717
            if v == ":default" || v == :default
718
              # Sets the value to the global or compile default
719
              execute("SET SESSION #{k} TO DEFAULT", "SCHEMA")
720
            elsif !v.nil?
721
              execute("SET SESSION #{k} TO #{quote(v)}", "SCHEMA")
722 723
            end
          end
724 725
        end

726
        # Returns the list of a table's column names, data types, and default values.
727 728
        #
        # The underlying query is roughly:
729
        #  SELECT column.name, column.type, default.value, column.comment
730 731 732 733 734 735 736 737 738 739 740 741 742 743
        #    FROM column LEFT JOIN default
        #      ON column.table_id = default.table_id
        #     AND column.num = default.column_num
        #   WHERE column.table_id = get_table_id('table_name')
        #     AND column.num > 0
        #     AND NOT column.is_dropped
        #   ORDER BY column.num
        #
        # If the table name is not prefixed with a schema, the database will
        # take the first match from the schema search path.
        #
        # Query implementation notes:
        #  - format_type includes the column size constraint, e.g. varchar(50)
        #  - ::regclass is a function that gives the id for a table name
A
Akira Matsuda 已提交
744
        def column_definitions(table_name)
745
          query(<<-end_sql, "SCHEMA")
746
              SELECT a.attname, format_type(a.atttypid, a.atttypmod),
747
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
748 749 750 751 752
                     c.collname, col_description(a.attrelid, a.attnum) AS comment
                FROM pg_attribute a
                LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                LEFT JOIN pg_type t ON a.atttypid = t.oid
                LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
R
Ryuta Kamizono 已提交
753
               WHERE a.attrelid = #{quote(quote_table_name(table_name))}::regclass
754 755
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
756
          end_sql
D
Initial  
David Heinemeier Hansson 已提交
757
        end
758

A
Akira Matsuda 已提交
759
        def extract_table_ref_from_insert_sql(sql)
760
          sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
761 762 763
          $1.strip if $1
        end

764 765
        def arel_visitor
          Arel::Visitors::PostgreSQL.new(self)
766
        end
S
Sean Griffin 已提交
767 768 769 770 771 772

        def can_perform_case_insensitive_comparison_for?(column)
          @case_insensitive_cache ||= {}
          @case_insensitive_cache[column.sql_type] ||= begin
            sql = <<-end_sql
              SELECT exists(
773 774 775 776
                SELECT * FROM pg_proc
                WHERE proname = 'lower'
                  AND proargtypes = ARRAY[#{quote column.sql_type}::regtype]::oidvector
              ) OR exists(
S
Sean Griffin 已提交
777 778
                SELECT * FROM pg_proc
                INNER JOIN pg_cast
N
nanaya 已提交
779
                  ON ARRAY[casttarget]::oidvector = proargtypes
S
Sean Griffin 已提交
780
                WHERE proname = 'lower'
N
nanaya 已提交
781
                  AND castsource = #{quote column.sql_type}::regtype
S
Sean Griffin 已提交
782 783 784
              )
            end_sql
            execute_and_clear(sql, "SCHEMA", []) do |result|
785
              result.getvalue(0, 0)
S
Sean Griffin 已提交
786 787 788
            end
          end
        end
789

790 791 792 793 794 795 796 797
        def add_pg_encoders
          map = PG::TypeMapByClass.new
          map[Integer] = PG::TextEncoder::Integer.new
          map[TrueClass] = PG::TextEncoder::Boolean.new
          map[FalseClass] = PG::TextEncoder::Boolean.new
          @connection.type_map_for_queries = map
        end

798 799
        def add_pg_decoders
          coders_by_name = {
800 801 802 803 804 805 806
            "int2" => PG::TextDecoder::Integer,
            "int4" => PG::TextDecoder::Integer,
            "int8" => PG::TextDecoder::Integer,
            "oid" => PG::TextDecoder::Integer,
            "float4" => PG::TextDecoder::Float,
            "float8" => PG::TextDecoder::Float,
            "bool" => PG::TextDecoder::Boolean,
807
          }
808 809
          known_coder_types = coders_by_name.keys.map { |n| quote(n) }
          query = <<-SQL % known_coder_types.join(", ")
810
            SELECT t.oid, t.typname
811
            FROM pg_type as t
812
            WHERE t.typname IN (%s)
813 814 815
          SQL
          coders = execute_and_clear(query, "SCHEMA", []) do |result|
            result
816
              .map { |row| construct_coder(row, coders_by_name[row["typname"]]) }
817 818 819 820 821 822 823 824 825 826
              .compact
          end

          map = PG::TypeMapByOid.new
          coders.each { |coder| map.add_coder(coder) }
          @connection.type_map_for_results = map
        end

        def construct_coder(row, coder_class)
          return unless coder_class
827
          coder_class.new(oid: row["oid"].to_i, name: row["typname"])
828
        end
829 830 831 832 833 834 835

        ActiveRecord::Type.add_modifier({ array: true }, OID::Array, adapter: :postgresql)
        ActiveRecord::Type.add_modifier({ range: true }, OID::Range, adapter: :postgresql)
        ActiveRecord::Type.register(:bit, OID::Bit, adapter: :postgresql)
        ActiveRecord::Type.register(:bit_varying, OID::BitVarying, adapter: :postgresql)
        ActiveRecord::Type.register(:binary, OID::Bytea, adapter: :postgresql)
        ActiveRecord::Type.register(:cidr, OID::Cidr, adapter: :postgresql)
Y
yuuji.yaginuma 已提交
836
        ActiveRecord::Type.register(:datetime, OID::DateTime, adapter: :postgresql)
837 838 839 840 841 842
        ActiveRecord::Type.register(:decimal, OID::Decimal, adapter: :postgresql)
        ActiveRecord::Type.register(:enum, OID::Enum, adapter: :postgresql)
        ActiveRecord::Type.register(:hstore, OID::Hstore, adapter: :postgresql)
        ActiveRecord::Type.register(:inet, OID::Inet, adapter: :postgresql)
        ActiveRecord::Type.register(:jsonb, OID::Jsonb, adapter: :postgresql)
        ActiveRecord::Type.register(:money, OID::Money, adapter: :postgresql)
843 844
        ActiveRecord::Type.register(:point, OID::Point, adapter: :postgresql)
        ActiveRecord::Type.register(:legacy_point, OID::LegacyPoint, adapter: :postgresql)
845 846 847
        ActiveRecord::Type.register(:uuid, OID::Uuid, adapter: :postgresql)
        ActiveRecord::Type.register(:vector, OID::Vector, adapter: :postgresql)
        ActiveRecord::Type.register(:xml, OID::Xml, adapter: :postgresql)
D
Initial  
David Heinemeier Hansson 已提交
848 849 850
    end
  end
end