SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
ActiveRecordの
  PostgreSQLアダプタ
 周辺について調べてみた

(株)サイクル・オブ・フィフス
      石田朗雄
はじめに
ActiveRecordのPostgreSQLアダプタについて調べてみて、
はまったところや気になったとこをまとめました。
でもメインはその下のレイヤの話です。
普段Rubyを使っているわけではないので、僕の話にRubyに対
する愛が足りないように聞こえたらごめんなさい。
構成
                    ActiveRecord::Base

                AR::ConnectionAdapter::Base

 AR::CA::PostgreSQLAdapter           AR::CA::他のRDBMS向け

ruby-postgres
                 postgres-pr
                 (pure ruby)
   libpq(C言語)




04:54:22 PM           Ruby-Sapporo 2007-11-17            3
postgres-prとの比較
    ruby-postgres-0.7.1.2006.04.06-mswin32
    postgres-pr-0.4.0
    postgres-prは、postgres.soがあればそっちを呼ぶので、両
    方入っている環境では工夫が必要
  ruby-postgres
  irb(main):002:0> require quot;postgresquot;
  => true

  postgres-pr
  irb(main):003:0> require quot;postgres-pr/postgres-compatquot;
  => true




04:54:22 PM                 Ruby-Sapporo 2007-11-17        4
postgres-prとの比較
  > PGconn.instance_methods(false).sort
  => [quot;async_execquot;, quot;async_queryquot;, quot;client_encodingquot;, quot;closequot;, quot;dbquot;,
  quot;endcopyquot;, quot;errorquot;, quot;execquot;, quot;finishquot;, quot;get_notifyquot;, quot;getlinequot;, quot;hostquot;,
  quot;insert_tablequot;, quot;lo_createquot;, quot;lo_exportquot;, quot;lo_importquot;, quot;lo_openquot;,
  quot;lo_unlinkquot;, quot;locreatequot;, quot;loexportquot;, quot;loimportquot;, quot;loopenquot;, quot;lounlinkquot;,
  quot;on_noticequot;, quot;optionsquot;, quot;portquot;, quot;protocol_versionquot;, quot;putlinequot;, quot;queryquot;,
  quot;resetquot;, quot;select_onequot;, quot;select_valuequot;, quot;select_valuesquot;, quot;server_versionquot;,
  quot;set_client_encodingquot;, quot;statusquot;, quot;tracequot;, quot;transaction_statusquot;, quot;ttyquot;,
  quot;untracequot;, quot;userquot;]


  > PGconn.instance_methods(false).sort
  => [quot;closequot;, quot;dbquot;, quot;execquot;, quot;hostquot;, quot;queryquot;, quot;userquot;]




04:54:22 PM                 Ruby-Sapporo 2007-11-17                           5
接続
  irb(main):013:0> c=PGconn.new(nil, 9999, nil, nil, 'ishida', 'ishida',
  nil)
  PGError: could not connect to server: Connection refused
  (0x0000274D/10061)
          Is the server running on host quot;???quot; and accepting
          TCP/IP connections on port 9999?

              from (irb):13:in `initialize'      postgres-prではhostがnilだと
              from (irb):13:in `new'             portが無視される
              from (irb):13

  irb(main):004:0> c=PGconn.new(nil, 9999, nil, nil, 'ishida', 'ishida',
  nil)
  => #<PGconn:0x2dc3d98 @db=quot;ishidaquot;, @host=nil,
  @conn=#<PostgresPR::Connection:0x2dc3d70
  @params={quot;integer_datetimesquot;=>quot;offquot;, quot;TimeZonequot;=>quot;JST-9quot;,
  quot;server_encodingquot;=>quot;EUC_JPquot;, quot;standard_conforming_stringsquot;=>quot;offquot;,
  quot;DateStylequot;=>quot;ISO, MDYquot;, quot;client_encodingquot;=>quot;EUC_JPquot;,
  quot;session_authorizationquot;=>quot;ishidaquot;, quot;server_versionquot;=>quot;8.1.3quot;,
  quot;is_superuserquot;=>quot;offquot;}, @conn=#<TCPSocket:0x2dc3bf4>>, @user=quot;ishidaquot;>
04:54:22 PM                     Ruby-Sapporo 2007-11-17                    6
ParameterStatus
    PostgreSQLのコンフィグのうち、クライアントの動作に影響
    するものをサーバからクライアントに対して送る
    接続時、SETコマンドによる変更、コンフィグファイルのリ
    ロード等
        server_version
        client_encoding
        DateStyle
        等々
    バージョンが上がると増えることがあるの
    で、PGconn#getparamみたいのが欲しい



04:54:22 PM               Ruby-Sapporo 2007-11-17   7
postgres-prとの比較
  irb(main):004:0> c.client_encoding
  => quot;SQL_ASCIIquot;
  irb(main):005:0> c.exec(quot;set client_encoding to sjisquot;)
  => #<PGresult:0x2dc2358>
  irb(main):006:0> c.client_encoding
  => quot;SJISquot;

   irb(main):010:0>
   c.instance_variable_get(:@conn).instance_variable_get(:@params)
   [quot;client_encodingquot;]
   => quot;SQL_ASCIIquot;
   irb(main):011:0> c.exec(quot;set client_encoding to sjisquot;)
   => #<PGresult:0x2e98cb4 @result=[], @fields=[],
   @res=#<PostgresPR::Connection::Result:0x2e98c50 @cmd_tag=quot;SETquot;,
   @fields=[], @rows=[]>>
   irb(main):012:0>
   c.instance_variable_get(:@conn).instance_variable_get(:@params)
   [quot;client_encodingquot;]
   => quot;SQL_ASCIIquot;                                     postgres-prは
                                                      ParameterStatusの変更を
04:54:22 PM                  Ruby-Sapporo 2007-11-17  拾っていない             8
コイツ、、、動くぞ、、、
  PGconn.translate_results = false if PGconn.respond_to? :translate_results=
  ....
  if @connection.respond_to?(:status)
  ...
  # TODO: postgres-pr doesn't have PGconn#reset.
  if @connection.respond_to?(:reset)

  但し、allow_concurrencyだけは使ってはいけない
     def query(sql, name = nil) #:nodoc:
       log(sql, name) do
         if @async
           @connection.async_query(sql)
         else
           @connection.query(sql)
         end
       end




04:54:22 PM                 Ruby-Sapporo 2007-11-17                            9
PGconn#async_exec
  pgconn_async_exec():postgres.c

       if (!PQsendQuery(conn, RSTRING(str)->ptr)) {
           rb_raise(rb_ePGError, PQerrorMessage(conn));
       }

       cs = PQsocket(conn);                  PQsendQueryで非同期にクエリ
       for(;;) {                             送信した後、結果が返ってくるの
           FD_ZERO(&rset);                   を待っている?
           FD_SET(cs, &rset);
           ret = rb_thread_select(cs + 1, &rset, NULL, NULL, NULL);




04:54:22 PM                  Ruby-Sapporo 2007-11-17                  10
まとめ?
    ruby-postgresとpostgres-prは結構違うけ
    ど、ActiveRecordが違いを吸収している
    今回挙げたものの他にもpostgres-prは、引数のoptionsを無
    視してたり環境変数も無視してたり、ParameterStatusを無
    視するケースがあったり、、、
    素のpostgres-prを使うには覚悟が必要




04:54:22 PM       Ruby-Sapporo 2007-11-17   11
C:rubylibrubygems1.8gemspostgres-pr-0.4.0lib>grep -r TODO *
 postgres-pr/connection.rb:         # TODO
 postgres-pr/connection.rb:         # TODO: use transaction status
 postgres-pr/connection.rb:         # TODO
 postgres-pr/connection.rb:         # TODO
 postgres-pr/message.rb:# TODO
 postgres-pr/message.rb:# TODO
 postgres-pr/message.rb:         # TODO: zero means unspecified. map to nil?
 postgres-pr/message.rb:# TODO: duplicate message-type, split into
 client/servermessages
 postgres-pr/postgres-compat.rb:     # TODO: correct?
 postgres-pr/postgres-compat.rb: # TODO: status, getlength, cmdstatus
 postgres-pr/postgres-compat.rb:     # TODO: correct?
 postgres-pr/postgres-compat.rb:     # TODO: correct?
 postgres-pr/typeconv/TC_conv.rb:     assert_equal [quot;quot;], decode_array(quot;{ }quot;) #
 TODO: Correct?




04:54:22 PM                  Ruby-Sapporo 2007-11-17                             12

Más contenido relacionado

La actualidad más candente

Varnish: Making eZ Publish sites fly
Varnish: Making eZ Publish sites flyVarnish: Making eZ Publish sites fly
Varnish: Making eZ Publish sites flyPeter Keung
 
LogStash - Yes, logging can be awesome
LogStash - Yes, logging can be awesomeLogStash - Yes, logging can be awesome
LogStash - Yes, logging can be awesomeJames Turnbull
 
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injectionStHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injectionStHack
 
Token Based Authentication Systems
Token Based Authentication SystemsToken Based Authentication Systems
Token Based Authentication SystemsHüseyin BABAL
 
Nginx常见应用技术指南(Nginx Tips)
Nginx常见应用技术指南(Nginx Tips)Nginx常见应用技术指南(Nginx Tips)
Nginx常见应用技术指南(Nginx Tips)dreamwing.org
 
5 issues
5 issues5 issues
5 issuesm0use
 
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonbСтажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonbSmartTools
 
To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)Geoffrey Anderson
 
OpenResty TCP 服务代理和动态路由
OpenResty TCP 服务代理和动态路由OpenResty TCP 服务代理和动态路由
OpenResty TCP 服务代理和动态路由Orangle Liu
 
Devsumi2012 攻めの運用の極意
Devsumi2012 攻めの運用の極意Devsumi2012 攻めの運用の極意
Devsumi2012 攻めの運用の極意Ryosuke IWANAGA
 
PyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to ProfilingPyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to ProfilingPerrin Harkins
 
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang MeetupДоклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang MeetupBadoo Development
 
Ajax и будущее Java Script
Ajax и будущее Java ScriptAjax и будущее Java Script
Ajax и будущее Java ScriptConstantin Kichinsky
 
HTTP For the Good or the Bad - FSEC Edition
HTTP For the Good or the Bad - FSEC EditionHTTP For the Good or the Bad - FSEC Edition
HTTP For the Good or the Bad - FSEC EditionXavier Mertens
 
Codified PostgreSQL Schema
Codified PostgreSQL SchemaCodified PostgreSQL Schema
Codified PostgreSQL SchemaSean Chittenden
 
Logstash for SEO: come monitorare i Log del Web Server in realtime
Logstash for SEO: come monitorare i Log del Web Server in realtimeLogstash for SEO: come monitorare i Log del Web Server in realtime
Logstash for SEO: come monitorare i Log del Web Server in realtimeAndrea Cardinale
 
Spark手把手:[e2-spk-s02]
Spark手把手:[e2-spk-s02]Spark手把手:[e2-spk-s02]
Spark手把手:[e2-spk-s02]Erhwen Kuo
 

La actualidad más candente (20)

Varnish: Making eZ Publish sites fly
Varnish: Making eZ Publish sites flyVarnish: Making eZ Publish sites fly
Varnish: Making eZ Publish sites fly
 
Nginx + PHP
Nginx + PHPNginx + PHP
Nginx + PHP
 
LogStash - Yes, logging can be awesome
LogStash - Yes, logging can be awesomeLogStash - Yes, logging can be awesome
LogStash - Yes, logging can be awesome
 
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injectionStHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
 
Token Based Authentication Systems
Token Based Authentication SystemsToken Based Authentication Systems
Token Based Authentication Systems
 
Nginx常见应用技术指南(Nginx Tips)
Nginx常见应用技术指南(Nginx Tips)Nginx常见应用技术指南(Nginx Tips)
Nginx常见应用技术指南(Nginx Tips)
 
5 issues
5 issues5 issues
5 issues
 
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonbСтажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
 
Perl dancer
Perl dancerPerl dancer
Perl dancer
 
To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)
 
OpenResty TCP 服务代理和动态路由
OpenResty TCP 服务代理和动态路由OpenResty TCP 服务代理和动态路由
OpenResty TCP 服务代理和动态路由
 
Devsumi2012 攻めの運用の極意
Devsumi2012 攻めの運用の極意Devsumi2012 攻めの運用の極意
Devsumi2012 攻めの運用の極意
 
PyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to ProfilingPyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to Profiling
 
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang MeetupДоклад Антона Поварова "Go in Badoo" с Golang Meetup
Доклад Антона Поварова "Go in Badoo" с Golang Meetup
 
Ajax и будущее Java Script
Ajax и будущее Java ScriptAjax и будущее Java Script
Ajax и будущее Java Script
 
HTTP For the Good or the Bad - FSEC Edition
HTTP For the Good or the Bad - FSEC EditionHTTP For the Good or the Bad - FSEC Edition
HTTP For the Good or the Bad - FSEC Edition
 
Codified PostgreSQL Schema
Codified PostgreSQL SchemaCodified PostgreSQL Schema
Codified PostgreSQL Schema
 
20070329 Phpconf2007 Training
20070329 Phpconf2007 Training20070329 Phpconf2007 Training
20070329 Phpconf2007 Training
 
Logstash for SEO: come monitorare i Log del Web Server in realtime
Logstash for SEO: come monitorare i Log del Web Server in realtimeLogstash for SEO: come monitorare i Log del Web Server in realtime
Logstash for SEO: come monitorare i Log del Web Server in realtime
 
Spark手把手:[e2-spk-s02]
Spark手把手:[e2-spk-s02]Spark手把手:[e2-spk-s02]
Spark手把手:[e2-spk-s02]
 

Destacado

Ruby Postgres 2009
Ruby Postgres 2009Ruby Postgres 2009
Ruby Postgres 2009Akio Ishida
 
Getting start with knockout.js
Getting start with knockout.jsGetting start with knockout.js
Getting start with knockout.jsAkio Ishida
 
PostgreSQLの範囲型と排他制約
PostgreSQLの範囲型と排他制約PostgreSQLの範囲型と排他制約
PostgreSQLの範囲型と排他制約Akio Ishida
 
Local php-100828 2
Local php-100828 2Local php-100828 2
Local php-100828 2Akio Ishida
 
PostgreSQLで学ぶBoyer-Moore-Horspoolアルゴリズム
PostgreSQLで学ぶBoyer-Moore-HorspoolアルゴリズムPostgreSQLで学ぶBoyer-Moore-Horspoolアルゴリズム
PostgreSQLで学ぶBoyer-Moore-HorspoolアルゴリズムAkio Ishida
 
XIDを周回させてみよう
XIDを周回させてみようXIDを周回させてみよう
XIDを周回させてみようAkio Ishida
 
使いこなそうGUC
使いこなそうGUC使いこなそうGUC
使いこなそうGUCAkio Ishida
 
Rubysapporo Stringsearch
Rubysapporo StringsearchRubysapporo Stringsearch
Rubysapporo StringsearchAkio Ishida
 
phpspecで学ぶLondon School TDD
phpspecで学ぶLondon School TDDphpspecで学ぶLondon School TDD
phpspecで学ぶLondon School TDDAkio Ishida
 
textsearch_jaで全文検索
textsearch_jaで全文検索textsearch_jaで全文検索
textsearch_jaで全文検索Akio Ishida
 
よりよいPHPUnitの実行方法を求めて
よりよいPHPUnitの実行方法を求めてよりよいPHPUnitの実行方法を求めて
よりよいPHPUnitの実行方法を求めてAkio Ishida
 
Prophecyを使ったユニットテスト
Prophecyを使ったユニットテスト Prophecyを使ったユニットテスト
Prophecyを使ったユニットテスト Akio Ishida
 

Destacado (13)

Ruby Postgres 2009
Ruby Postgres 2009Ruby Postgres 2009
Ruby Postgres 2009
 
Getting start with knockout.js
Getting start with knockout.jsGetting start with knockout.js
Getting start with knockout.js
 
PostgreSQLの範囲型と排他制約
PostgreSQLの範囲型と排他制約PostgreSQLの範囲型と排他制約
PostgreSQLの範囲型と排他制約
 
Local php-100828 2
Local php-100828 2Local php-100828 2
Local php-100828 2
 
PostgreSQLで学ぶBoyer-Moore-Horspoolアルゴリズム
PostgreSQLで学ぶBoyer-Moore-HorspoolアルゴリズムPostgreSQLで学ぶBoyer-Moore-Horspoolアルゴリズム
PostgreSQLで学ぶBoyer-Moore-Horspoolアルゴリズム
 
XIDを周回させてみよう
XIDを周回させてみようXIDを周回させてみよう
XIDを周回させてみよう
 
使いこなそうGUC
使いこなそうGUC使いこなそうGUC
使いこなそうGUC
 
Rubysapporo Stringsearch
Rubysapporo StringsearchRubysapporo Stringsearch
Rubysapporo Stringsearch
 
phpspecで学ぶLondon School TDD
phpspecで学ぶLondon School TDDphpspecで学ぶLondon School TDD
phpspecで学ぶLondon School TDD
 
textsearch_jaで全文検索
textsearch_jaで全文検索textsearch_jaで全文検索
textsearch_jaで全文検索
 
よりよいPHPUnitの実行方法を求めて
よりよいPHPUnitの実行方法を求めてよりよいPHPUnitの実行方法を求めて
よりよいPHPUnitの実行方法を求めて
 
Elsevier与Lib2
Elsevier与Lib2Elsevier与Lib2
Elsevier与Lib2
 
Prophecyを使ったユニットテスト
Prophecyを使ったユニットテスト Prophecyを使ったユニットテスト
Prophecyを使ったユニットテスト
 

Similar a Ruby Postgres

How To Create Custom DSLs By PHP
How To Create Custom DSLs By PHPHow To Create Custom DSLs By PHP
How To Create Custom DSLs By PHPAtsuhiro Kubo
 
JSplash - Adobe MAX 2009
JSplash - Adobe MAX 2009JSplash - Adobe MAX 2009
JSplash - Adobe MAX 2009gyuque
 
Shibuya.abc - Gnashで遊ぼう
Shibuya.abc - Gnashで遊ぼうShibuya.abc - Gnashで遊ぼう
Shibuya.abc - Gnashで遊ぼうgyuque
 
ブラウザでMap Reduce風味の並列分散処理
ブラウザでMap Reduce風味の並列分散処理ブラウザでMap Reduce風味の並列分散処理
ブラウザでMap Reduce風味の並列分散処理Shinya Miyazaki
 
20090418 イケテルRails勉強会 第1部Rails編
20090418 イケテルRails勉強会 第1部Rails編20090418 イケテルRails勉強会 第1部Rails編
20090418 イケテルRails勉強会 第1部Rails編mochiko AsTech
 
yusukebe in Yokohama.pm 090909
yusukebe in Yokohama.pm 090909yusukebe in Yokohama.pm 090909
yusukebe in Yokohama.pm 090909Yusuke Wada
 
20090323 Phpstudy
20090323 Phpstudy20090323 Phpstudy
20090323 PhpstudyYusuke Ando
 
20080424 Cdb2008 Postgresql News Bartunov
20080424 Cdb2008 Postgresql News Bartunov20080424 Cdb2008 Postgresql News Bartunov
20080424 Cdb2008 Postgresql News BartunovNikolay Samokhvalov
 
企业级搜索引擎Solr交流
企业级搜索引擎Solr交流企业级搜索引擎Solr交流
企业级搜索引擎Solr交流chuan liang
 
とちぎRuby会議01(原)
とちぎRuby会議01(原)とちぎRuby会議01(原)
とちぎRuby会議01(原)Shin-ichiro HARA
 
Ruby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese VersionRuby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese VersionLibin Pan
 
4200 Kte7.0 Training V1.0
4200 Kte7.0 Training V1.04200 Kte7.0 Training V1.0
4200 Kte7.0 Training V1.0wayneliao
 
樽家昌也 (日本Rubyの会)
樽家昌也 (日本Rubyの会) 樽家昌也 (日本Rubyの会)
樽家昌也 (日本Rubyの会) toRuby
 
技術トレンディセミナー フレームワークとしてのTrac
技術トレンディセミナー フレームワークとしてのTrac技術トレンディセミナー フレームワークとしてのTrac
技術トレンディセミナー フレームワークとしてのTracterada
 
P2P Bug Tracking with SD
P2P Bug Tracking with SDP2P Bug Tracking with SD
P2P Bug Tracking with SDJesse Vincent
 
オブジェクト指向スクリプト言語 Ruby
オブジェクト指向スクリプト言語 Rubyオブジェクト指向スクリプト言語 Ruby
オブジェクト指向スクリプト言語 RubyKitajiro Kitayama
 

Similar a Ruby Postgres (20)

How To Create Custom DSLs By PHP
How To Create Custom DSLs By PHPHow To Create Custom DSLs By PHP
How To Create Custom DSLs By PHP
 
Grails紹介
Grails紹介Grails紹介
Grails紹介
 
JSplash - Adobe MAX 2009
JSplash - Adobe MAX 2009JSplash - Adobe MAX 2009
JSplash - Adobe MAX 2009
 
Revisited
RevisitedRevisited
Revisited
 
What Can Compilers Do for Us?
What Can Compilers Do for Us?What Can Compilers Do for Us?
What Can Compilers Do for Us?
 
Shibuya.abc - Gnashで遊ぼう
Shibuya.abc - Gnashで遊ぼうShibuya.abc - Gnashで遊ぼう
Shibuya.abc - Gnashで遊ぼう
 
ブラウザでMap Reduce風味の並列分散処理
ブラウザでMap Reduce風味の並列分散処理ブラウザでMap Reduce風味の並列分散処理
ブラウザでMap Reduce風味の並列分散処理
 
20090418 イケテルRails勉強会 第1部Rails編
20090418 イケテルRails勉強会 第1部Rails編20090418 イケテルRails勉強会 第1部Rails編
20090418 イケテルRails勉強会 第1部Rails編
 
yusukebe in Yokohama.pm 090909
yusukebe in Yokohama.pm 090909yusukebe in Yokohama.pm 090909
yusukebe in Yokohama.pm 090909
 
20090323 Phpstudy
20090323 Phpstudy20090323 Phpstudy
20090323 Phpstudy
 
20080424 Cdb2008 Postgresql News Bartunov
20080424 Cdb2008 Postgresql News Bartunov20080424 Cdb2008 Postgresql News Bartunov
20080424 Cdb2008 Postgresql News Bartunov
 
object-shapes
object-shapesobject-shapes
object-shapes
 
企业级搜索引擎Solr交流
企业级搜索引擎Solr交流企业级搜索引擎Solr交流
企业级搜索引擎Solr交流
 
とちぎRuby会議01(原)
とちぎRuby会議01(原)とちぎRuby会議01(原)
とちぎRuby会議01(原)
 
Ruby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese VersionRuby on Rails 2.1 What's New Chinese Version
Ruby on Rails 2.1 What's New Chinese Version
 
4200 Kte7.0 Training V1.0
4200 Kte7.0 Training V1.04200 Kte7.0 Training V1.0
4200 Kte7.0 Training V1.0
 
樽家昌也 (日本Rubyの会)
樽家昌也 (日本Rubyの会) 樽家昌也 (日本Rubyの会)
樽家昌也 (日本Rubyの会)
 
技術トレンディセミナー フレームワークとしてのTrac
技術トレンディセミナー フレームワークとしてのTrac技術トレンディセミナー フレームワークとしてのTrac
技術トレンディセミナー フレームワークとしてのTrac
 
P2P Bug Tracking with SD
P2P Bug Tracking with SDP2P Bug Tracking with SD
P2P Bug Tracking with SD
 
オブジェクト指向スクリプト言語 Ruby
オブジェクト指向スクリプト言語 Rubyオブジェクト指向スクリプト言語 Ruby
オブジェクト指向スクリプト言語 Ruby
 

Último

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 

Último (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Ruby Postgres

  • 1. ActiveRecordの PostgreSQLアダプタ 周辺について調べてみた (株)サイクル・オブ・フィフス 石田朗雄
  • 3. 構成 ActiveRecord::Base AR::ConnectionAdapter::Base AR::CA::PostgreSQLAdapter AR::CA::他のRDBMS向け ruby-postgres postgres-pr (pure ruby) libpq(C言語) 04:54:22 PM Ruby-Sapporo 2007-11-17 3
  • 4. postgres-prとの比較 ruby-postgres-0.7.1.2006.04.06-mswin32 postgres-pr-0.4.0 postgres-prは、postgres.soがあればそっちを呼ぶので、両 方入っている環境では工夫が必要 ruby-postgres irb(main):002:0> require quot;postgresquot; => true postgres-pr irb(main):003:0> require quot;postgres-pr/postgres-compatquot; => true 04:54:22 PM Ruby-Sapporo 2007-11-17 4
  • 5. postgres-prとの比較 > PGconn.instance_methods(false).sort => [quot;async_execquot;, quot;async_queryquot;, quot;client_encodingquot;, quot;closequot;, quot;dbquot;, quot;endcopyquot;, quot;errorquot;, quot;execquot;, quot;finishquot;, quot;get_notifyquot;, quot;getlinequot;, quot;hostquot;, quot;insert_tablequot;, quot;lo_createquot;, quot;lo_exportquot;, quot;lo_importquot;, quot;lo_openquot;, quot;lo_unlinkquot;, quot;locreatequot;, quot;loexportquot;, quot;loimportquot;, quot;loopenquot;, quot;lounlinkquot;, quot;on_noticequot;, quot;optionsquot;, quot;portquot;, quot;protocol_versionquot;, quot;putlinequot;, quot;queryquot;, quot;resetquot;, quot;select_onequot;, quot;select_valuequot;, quot;select_valuesquot;, quot;server_versionquot;, quot;set_client_encodingquot;, quot;statusquot;, quot;tracequot;, quot;transaction_statusquot;, quot;ttyquot;, quot;untracequot;, quot;userquot;] > PGconn.instance_methods(false).sort => [quot;closequot;, quot;dbquot;, quot;execquot;, quot;hostquot;, quot;queryquot;, quot;userquot;] 04:54:22 PM Ruby-Sapporo 2007-11-17 5
  • 6. 接続 irb(main):013:0> c=PGconn.new(nil, 9999, nil, nil, 'ishida', 'ishida', nil) PGError: could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host quot;???quot; and accepting TCP/IP connections on port 9999? from (irb):13:in `initialize' postgres-prではhostがnilだと from (irb):13:in `new' portが無視される from (irb):13 irb(main):004:0> c=PGconn.new(nil, 9999, nil, nil, 'ishida', 'ishida', nil) => #<PGconn:0x2dc3d98 @db=quot;ishidaquot;, @host=nil, @conn=#<PostgresPR::Connection:0x2dc3d70 @params={quot;integer_datetimesquot;=>quot;offquot;, quot;TimeZonequot;=>quot;JST-9quot;, quot;server_encodingquot;=>quot;EUC_JPquot;, quot;standard_conforming_stringsquot;=>quot;offquot;, quot;DateStylequot;=>quot;ISO, MDYquot;, quot;client_encodingquot;=>quot;EUC_JPquot;, quot;session_authorizationquot;=>quot;ishidaquot;, quot;server_versionquot;=>quot;8.1.3quot;, quot;is_superuserquot;=>quot;offquot;}, @conn=#<TCPSocket:0x2dc3bf4>>, @user=quot;ishidaquot;> 04:54:22 PM Ruby-Sapporo 2007-11-17 6
  • 7. ParameterStatus PostgreSQLのコンフィグのうち、クライアントの動作に影響 するものをサーバからクライアントに対して送る 接続時、SETコマンドによる変更、コンフィグファイルのリ ロード等 server_version client_encoding DateStyle 等々 バージョンが上がると増えることがあるの で、PGconn#getparamみたいのが欲しい 04:54:22 PM Ruby-Sapporo 2007-11-17 7
  • 8. postgres-prとの比較 irb(main):004:0> c.client_encoding => quot;SQL_ASCIIquot; irb(main):005:0> c.exec(quot;set client_encoding to sjisquot;) => #<PGresult:0x2dc2358> irb(main):006:0> c.client_encoding => quot;SJISquot; irb(main):010:0> c.instance_variable_get(:@conn).instance_variable_get(:@params) [quot;client_encodingquot;] => quot;SQL_ASCIIquot; irb(main):011:0> c.exec(quot;set client_encoding to sjisquot;) => #<PGresult:0x2e98cb4 @result=[], @fields=[], @res=#<PostgresPR::Connection::Result:0x2e98c50 @cmd_tag=quot;SETquot;, @fields=[], @rows=[]>> irb(main):012:0> c.instance_variable_get(:@conn).instance_variable_get(:@params) [quot;client_encodingquot;] => quot;SQL_ASCIIquot; postgres-prは ParameterStatusの変更を 04:54:22 PM Ruby-Sapporo 2007-11-17 拾っていない 8
  • 9. コイツ、、、動くぞ、、、 PGconn.translate_results = false if PGconn.respond_to? :translate_results= .... if @connection.respond_to?(:status) ... # TODO: postgres-pr doesn't have PGconn#reset. if @connection.respond_to?(:reset) 但し、allow_concurrencyだけは使ってはいけない def query(sql, name = nil) #:nodoc: log(sql, name) do if @async @connection.async_query(sql) else @connection.query(sql) end end 04:54:22 PM Ruby-Sapporo 2007-11-17 9
  • 10. PGconn#async_exec pgconn_async_exec():postgres.c if (!PQsendQuery(conn, RSTRING(str)->ptr)) { rb_raise(rb_ePGError, PQerrorMessage(conn)); } cs = PQsocket(conn); PQsendQueryで非同期にクエリ for(;;) { 送信した後、結果が返ってくるの FD_ZERO(&rset); を待っている? FD_SET(cs, &rset); ret = rb_thread_select(cs + 1, &rset, NULL, NULL, NULL); 04:54:22 PM Ruby-Sapporo 2007-11-17 10
  • 11. まとめ? ruby-postgresとpostgres-prは結構違うけ ど、ActiveRecordが違いを吸収している 今回挙げたものの他にもpostgres-prは、引数のoptionsを無 視してたり環境変数も無視してたり、ParameterStatusを無 視するケースがあったり、、、 素のpostgres-prを使うには覚悟が必要 04:54:22 PM Ruby-Sapporo 2007-11-17 11
  • 12. C:rubylibrubygems1.8gemspostgres-pr-0.4.0lib>grep -r TODO * postgres-pr/connection.rb: # TODO postgres-pr/connection.rb: # TODO: use transaction status postgres-pr/connection.rb: # TODO postgres-pr/connection.rb: # TODO postgres-pr/message.rb:# TODO postgres-pr/message.rb:# TODO postgres-pr/message.rb: # TODO: zero means unspecified. map to nil? postgres-pr/message.rb:# TODO: duplicate message-type, split into client/servermessages postgres-pr/postgres-compat.rb: # TODO: correct? postgres-pr/postgres-compat.rb: # TODO: status, getlength, cmdstatus postgres-pr/postgres-compat.rb: # TODO: correct? postgres-pr/postgres-compat.rb: # TODO: correct? postgres-pr/typeconv/TC_conv.rb: assert_equal [quot;quot;], decode_array(quot;{ }quot;) # TODO: Correct? 04:54:22 PM Ruby-Sapporo 2007-11-17 12