Hello
Our Rails 3.2 app which relies on AR for session storage and OmniAuth spradicly raises the following exception:
A ActiveRecord::StatementInvalid occurred in channels#show:
PG::Error: ERROR: relation "sessions" does not exist
LINE 4: WHERE a.attrelid = '"sessions"'::regclass
^
[...]
However, I can execute the above SELECT query without any problem in "rails db". And the exception occurs on the homepage which sees many hits, yet onle one or two exceptions per day are risen. Could this be a ruby-pg issue?
$ irb --prompt xmp ~/temp/sessions_query.rbrequire 'pg'==>truequery = <<-END_SQL
SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnullFROM pg_attribute a LEFT JOIN pg_attrdef dON a.attrelid = d.adrelid AND a.attnum = d.adnumWHERE a.attrelid = '"sessions"'::regclassAND a.attnum > 0 AND NOT a.attisdroppedORDER BY a.attnum
END_SQL==>"SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull\nFROM pg_attribute a LEFT JOIN pg_attrdef d\nON a.attrelid = d.adrelid AND a.attnum = d.adnum\nWHERE a.attrelid = '\"sessions\"'::regclass\nAND a.attnum > 0 AND NOT a.attisdropped\nORDER BY a.attnum\n"c = PG.connect( dbname: 'test' )==>#<PG::Connection:0x007fa7410516b8>c.exec( "DROP TABLE IF EXISTS sessions" )NOTICE: table "sessions" does not exist, skipping==>#<PG::Result:0x007fa74103f738>c.exec( query )
PG::Error: ERROR: relation "sessions" does not existLINE 4: WHERE a.attrelid = '"sessions"'::regclass^
from /Users/mgranger/temp/sessions_query.rb:12:in `exec'from /Users/mgranger/temp/sessions_query.rb:12from /Users/mgranger/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'c.exec( "CREATE TABLE sessions ( id serial )" )NOTICE: CREATE TABLE will create implicit sequence "sessions_id_seq" for serial column "sessions.id"==>#<PG::Result:0x007fa741020590>c.exec( query )==>#<PG::Result:0x007fa7419bae20>
So somehow your sessions table is getting deleted, a database handle is pointed to the wrong database or schema, or something is connecting as a user that can't see the sessions table.
--
You received this message because you are subscribed to the Google Groups "ruby-pg" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ruby-pg/-/ksfsGxl8I2AJ.
To post to this group, send email to rub...@googlegroups.com.
To unsubscribe from this group, send email to ruby-pg+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby-pg?hl=en.
If you're app is multithreaded this can happen if you share the same connection among threads. Is that the case?
I wasn't hypothesizing about a cause; I can't diagnose what your particular application is doing with its database handles. My point was that the error you're seeing comes from the database server itself, so your application code or one of the frameworks on which it depends is doing something to cause the 'sessions' table not to be visible. I was offering guesses as to what the problem might be. If the PostgreSQL server responds with an error code, ruby-pg will raise it as an exception. As such, without further evidence to the contrary, it appears to me that your issue isn't an issue in ruby-pg.
That said, there are a number of things you can do from the driver to help diagnose the problem. You can enable query tracing:
conn.trace( $stderr ) # Log to STDERR
or:
conn.trace( File.open('pgtrace.log','a') )
You can increase the verbosity of error messages from the backend:
conn.set_error_verbosity( PG::PQERRORS_VERBOSE )
You can log the PID of the connection on the server to track which requests are using which connection handles:
$stderr.puts "PGPID: %s" % [ conn.backend_pid ]
You can set up a notice processor to log server notices, and then increase the verbosity of them:
c.set_notice_receiver {|pgres| logfile.puts(pgres.error_message) }
c.exec( "set client_min_messages to debug5" )
--
Michael Granger <g...@FaerieMUD.org>
Rubymage, Architect, Believer
The FaerieMUD Consortium <http://FaerieMUD.org/>
I found the source of these requests: Passenger's "passenger_pre_start" directive (Nginx).
Stacktrace, SQL string and error message match together, which otherwise could signalize a threading issue. The only idea I have, is that the schema search path is changed somewhere in the application, so that the next request fails.
Regards,
Lars
Thanks for the debugging hints, I'll try to find out where exactly the error originates.
--
You received this message because you are subscribed to the Google Groups "ruby-pg" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ruby-pg/-/LZxOIlcLVPYJ.
Ah, glad you were able to track it down. Was there some reference to this problem that you found, or did you just do detective work to find it? Future readers might be helped if you gave more details.
Hi Sven,
great you were able to solve the issue! And thank you for sharing your solution!
Regards,
Lars