    # File lib/active_record/connection_adapters/postgresql_adapter.rb, line 270
          def pk_and_sequence_for(table)
            # First try looking for a sequence with a dependency on the
            # given table's primary key.
            result = query("SELECT attr.attname, name.nspname, seq.relname\nFROM pg_class      seq,\npg_attribute  attr,\npg_depend     dep,\npg_namespace  name,\npg_constraint cons\nWHERE seq.oid           = dep.objid\nAND seq.relnamespace  = name.oid\nAND seq.relkind       = 'S'\nAND attr.attrelid     = dep.refobjid\nAND attr.attnum       = dep.refobjsubid\nAND attr.attrelid     = cons.conrelid\nAND attr.attnum       = cons.conkey[1]\nAND cons.contype      = 'p'\nAND dep.refobjid      = '\#{table}'::regclass\n", 'PK and serial sequence')[0]

            if result.nil? or result.empty?
              # If that fails, try parsing the primary key's default value.
              # Support the 7.x and 8.0 nextval('foo'::text) as well as
              # the 8.1+ nextval('foo'::regclass).
              # TODO: assumes sequence is in same schema as table.
              result = query("SELECT attr.attname, name.nspname, split_part(def.adsrc, '''', 2)\nFROM pg_class       t\nJOIN pg_namespace   name ON (t.relnamespace = name.oid)\nJOIN pg_attribute   attr ON (t.oid = attrelid)\nJOIN pg_attrdef     def  ON (adrelid = attrelid AND adnum = attnum)\nJOIN pg_constraint  cons ON (conrelid = adrelid AND adnum = conkey[1])\nWHERE t.oid = '\#{table}'::regclass\nAND cons.contype = 'p'\nAND def.adsrc ~* 'nextval'\n", 'PK and custom sequence')[0]
            end
            # check for existence of . in sequence name as in public.foo_sequence.  if it does not exist, return unqualified sequence
            # We cannot qualify unqualified sequences, as rails doesn't qualify any table access, using the search path
            [result.first, result.last]
          rescue
            nil
          end