# Pg gem 0.10.1 wth Ruby 1.9.2 does not work with method @pg\_conn.exec\_prepared(stmt\_name, parameters)

**URL:** <https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188>\
**Category:** Ruby\
**Created:** [January 24, 2011, 8:34am UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188 "2011-01-24T08:34:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 24, 2011, 8:34am UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/1 "2011-01-24T08:34:36Z")

</div>

Hi

The pg gem 0.10.1 does not seem to work with Ruby 1.9.2 with the method  
@pg\_conn.exec\_prepared(stmt\_name, parameters)

This does work:

C:\Users\Luis\Projects\_sandbox\>irb  
irb(main):001:0\> require ‘rubygems’  
=\> true  
irb(main):002:0\> require ‘pg’  
=\> true  
irb(main):003:0\> conn = PGconn.open(:dbname =\> ‘test’, :user =\>  
‘postgres’, :password =\> ‘’)  
=\> #PGconn:0x2c9a198  
irb(main):004:0\> res = conn.exec(‘SELECT 1 + 2’)  
=\> #PGresult:0x2bc0698  
irb(main):005:0\> res.getvalue(0,0)  
=\> “3”

But this method does _not_ work:

@pg\_conn.exec\_prepared(stmt\_name, parameters)

the resulting error is:

from /usr/lib/ruby/gems/1.9.1/gems/dbd-pg-0.3.9/lib/dbd/pg/  
statement.rb:37:in `execute' from /usr/lib/ruby/gems/1.9.1/gems/dbi-0.4.2/lib/dbi/ base_classes/database.rb:96:in`execute’  
from /usr/lib/ruby/gems/1.9.1/gems/dbi-0.4.2/lib/dbi/  
base\_classes/database.rb:114:in `do' from /usr/lib/ruby/gems/1.9.1/gems/dbi-0.4.2/lib/dbi/handles/ database.rb:106:in`do’  
from /usr/lib/ruby/site\_ruby/1.9.1/odba/storage.rb:176:in  
`create_index' from /usr/lib/ruby/site_ruby/1.9.1/odba/index.rb:226:in`initialize’  
from /usr/lib/ruby/site\_ruby/1.9.1/odba/cache.rb:148:in `new' from /usr/lib/ruby/site_ruby/1.9.1/odba/cache.rb:148:in`block  
in create\_index’  
from /usr/lib/ruby/site\_ruby/1.9.1/odba/storage.rb:558:in  
`call' from /usr/lib/ruby/site_ruby/1.9.1/odba/storage.rb:558:in`block in transaction’  
from /usr/lib/ruby/gems/1.9.1/gems/dbi-0.4.2/lib/dbi/handles/  
database.rb:209:in `transaction' from /usr/lib/ruby/site_ruby/1.9.1/odba/connection_pool.rb: 36:in`block in method\_missing’  
from /usr/lib/ruby/site\_ruby/1.9.1/odba/connection\_pool.rb:  
26:in `next_connection' from /usr/lib/ruby/site_ruby/1.9.1/odba/connection_pool.rb: 35:in`method\_missing’  
from /usr/lib/ruby/site\_ruby/1.9.1/odba/storage.rb:554:in  
`transaction' from /usr/lib/ruby/site_ruby/1.9.1/odba/cache.rb:520:in`transaction’  
from /usr/lib/ruby/site\_ruby/1.9.1/odba/cache.rb:140:in  
`create_index' from /usr/lib/ruby/site_ruby/1.9.1/odba/cache.rb:131:in`block  
in create\_deferred\_indices’  
from /usr/lib/ruby/site\_ruby/1.9.1/odba/cache.rb:125:in `each' from /usr/lib/ruby/site_ruby/1.9.1/odba/cache.rb:125:in`create\_deferred\_indices’  
from /usr/lib/ruby/site\_ruby/1.9.1/odba/cache.rb:437:in  
`setup' from /usr/lib/ruby/site_ruby/1.9.1/oddb/persistence/odba.rb: 35:in`module:ODDB’  
from /usr/lib/ruby/site\_ruby/1.9.1/oddb/persistence/odba.rb:  
28:in `<top (required)>' from /var/www/ramaze.ch.oddb.org/model/init.rb:3:in`require’  
from /var/www/ramaze.ch.oddb.org/model/init.rb:3:in `<top (required)>' from /var/www/ramaze.ch.oddb.org/app.rb:32:in`require’  
from /var/www/ramaze.ch.oddb.org/app.rb:32:in `<top (required)>' from start.rb:7:in`require’  
from start.rb:7:in `’

Any Feedback is more then welcome.

This did work with Ruby 1.8.6 and Ruby 1.9.1

Best  
Zeno

---

<div class="post-metadata">

**Author:** ![zeno\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a587f6/32.png) [@zeno\_d](https://www.ruby-forum.com/u/zeno_d)\
**Post date:** [January 24, 2011, 8:37am UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/2 "2011-01-24T08:37:11Z")

</div>

On Sun, Jan 23, 2011 at 11:34 PM, Zeno D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the resulting error is:
> 
> [snip]

You don’t actually show the exception, only the backtrace. What is the  
actual error?

Ben

---

<div class="post-metadata">

**Author:** ![zeno\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a587f6/32.png) [@zeno\_d](https://www.ruby-forum.com/u/zeno_d)\
**Post date:** [January 24, 2011, 9:11am UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/3 "2011-01-24T09:11:04Z")

</div>

Dear Ben

Thank you for your reply.

That is what we are trying to find out ourself. ODBA our software - at  
start up of our software - checks if the tables of the database are  
there or not. When checking one such table we guess (our traceback) ODBA  
fails at the method

@pg\_conn.exec\_prepared(stmt\_name, parameters)

Normally ODBA would find all the tables and then load the data into the  
cache. But since Ruby 1.9.2 ODBA stops at the above point.

What is the actual difference between dbd-pg and pg?

Best  
Zeno

---

<div class="post-metadata">

**Author:** ![zeno\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a587f6/32.png) [@zeno\_d](https://www.ruby-forum.com/u/zeno_d)\
**Post date:** [January 24, 2011, 9:33am UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/4 "2011-01-24T09:33:54Z")

</div>

Dear Ben

We could trace the error down as far as to this file:

/usr/lib64/ruby/gems/1.9.1/gems/pg-0.10.1/ext/pg.c

and that files source is C. For a full list of steps, please see:

[http://dev.ywesee.com/wiki.php/Masa/20110124-setup-ramaze#Result](http://dev.ywesee.com/wiki.php/Masa/20110124-setup-ramaze#Result)

Best  
Zeno

---

<div class="post-metadata">

**Author:** ![zeno\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a587f6/32.png) [@zeno\_d](https://www.ruby-forum.com/u/zeno_d)\
**Post date:** [January 24, 2011, 9:57am UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/5 "2011-01-24T09:57:48Z")

</div>

Ok, it seems that this again is related to some subtle changes from Ruby  
1.9.1 to Ruby 1.9.2. So we are now digging back into ODBA and comparing  
both processes on Ruby 1.9.1 and Ruby 1.9.2 on Linux.

Freaks me out this inconsistency in Ruby 1.9 itself!

Best  
Zeno

---

<div class="post-metadata">

**Author:** ![zeno\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a587f6/32.png) [@zeno\_d](https://www.ruby-forum.com/u/zeno_d)\
**Post date:** [January 24, 2011, 2:02pm UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/6 "2011-01-24T14:02:00Z")

</div>

Dear Rick

Thank you for your reply.

We are back-tracing now. It is not that simple.

It is due to a difference in Ruby 1.9.2 and Ruby 1.9.1 as we guess.

If you look at this:

[http://dev.ywesee.com/wiki.php/Masa/20110124-setup-ramaze#fork](http://dev.ywesee.com/wiki.php/Masa/20110124-setup-ramaze#fork)

Then you see that Ruby 1.9.1 returns a “true” and Ruby 1.9.2 returns a  
“false” so we need to know where that takes us.

Still working on it. Now we are looking into dbi.

Best  
Zeno

---

<div class="post-metadata">

**Author:** ![zeno\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a587f6/32.png) [@zeno\_d](https://www.ruby-forum.com/u/zeno_d)\
**Post date:** [January 24, 2011, 1:42pm UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/7 "2011-01-24T13:42:54Z")

</div>

On Mon, Jan 24, 2011 at 3:33 AM, Zeno D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dear Ben
> 
> We could trace the error down as far as to this file:
> 
> /usr/lib64/ruby/gems/1.9.1/gems/pg-0.10.1/ext/pg.c
> 
> and that files source is C. For a full list of steps, please see:
> 
> [dev.ywesee.com - Masa/20110124-setup-ramaze](http://dev.ywesee.com/wiki.php/Masa/20110124-setup-ramaze#Result)

Zeno,

Ben asked this:  
On Mon, Jan 24, 2011 at 2:36 AM, Ben B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Sun, Jan 23, 2011 at 11:34 PM, Zeno D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > the resulting error is:
> > 
> > [snip]
> 
> You don’t actually show the exception, only the backtrace. What is the  
> actual error?

Not sure why you broke the threading by the way.

You seemed to miss his question, he was asking for the actual  
exception which was raised, not just the backtrace.

From your link this would appear to be:

/usr/lib64/ruby/gems/1.9.1/gems/dbd-pg-0.3.9/lib/dbd/pg/statement.rb:71:in  
`rescue in execute’: ERROR: relation “oddb\_business\_company\_name”  
already exists (DBI::ProgrammingError)

This doesn’t look like a bug in the pg gem or in Ruby 1.9.2, but with  
the prepared statement you are executing or the state of the database  
when you execute it.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [NameBright - Coming Soon](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![zeno\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a587f6/32.png) [@zeno\_d](https://www.ruby-forum.com/u/zeno_d)\
**Post date:** [January 24, 2011, 3:02pm UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/8 "2011-01-24T15:02:26Z")

</div>

Ok, it seems this is related to dbi (0.4.5) and the way Ruby 1.9.2  
handles the block-Argument-scope vs Ruby 1.9.1. dbi uses this.

see the read read lines here:

[http://dev.ywesee.com/wiki.php/Masa/20110124-setup-ramaze#dbi\_while](http://dev.ywesee.com/wiki.php/Masa/20110124-setup-ramaze#dbi_while)

may be related to this:

[http://mng.iop.kcl.ac.uk/site/node/617](http://mng.iop.kcl.ac.uk/site/node/617)

Best  
Zeno

---

<div class="post-metadata">

**Author:** ![zeno\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a587f6/32.png) [@zeno\_d](https://www.ruby-forum.com/u/zeno_d)\
**Post date:** [January 24, 2011, 4:55pm UTC](https://www.ruby-forum.com/t/pg-gem-0-10-1-wth-ruby-1-9-2-does-not-work-with-method-pg-conn-exec-prepared-stmt-name-parameters/202188/9 "2011-01-24T16:55:00Z")

</div>

Ok just FYI we solved the problem. It again (after the Hash-Iteration  
problem - thank you Chuck R.) was in dbi. What we had to do is force  
the Array in dbi, as so:

/usr/lib64/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi/handles/statement.rb

we changed

#fetched\_rows.push(row)

to

fetched\_rows.push(row.to\_a)

This works for us in Ruby 1.9.1 _and_ Ruby 1.9.2

We do not have a clue so far why this works in Ruby 1.9.2 and in Ruby  
1.9.1 but it does work for us.

Thanks for all your help so far and let us know if you know why this  
is needed for Ruby 1.9.2 to work.

We will also investigate further.

Best  
Zeno
