Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have built an app that queries Google Analytics for the last 7 days of data. Everything works locally. On Heroku, the process runs smoothly until it tries to get data for today's date. I then get the following error:

2012-10-29T02:32:02+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::Error: ERROR:   numeric field overflow
2012-10-29T02:32:02+00:00 app[web.1]: DETAIL:  A field with precision 8, scale 2 must round to an absolute value less than 10^6.

I have tried to figure out which variable it's not happy with but I don't know right now. I am assuming it's something related to date or time.

Any thoughts or ideas would be great :)

-- update ---

ActiveRecord::Schema.define(:version => 20121014153338) do
  create_table "analytics", :force => true do |t|
    t.string   "site"
    t.integer  "visits"
    t.date     "start_date"
    t.date     "end_date"
    t.decimal  "revenue_per_transaction", :precision => 8, :scale => 2
    t.integer  "transactions"
    t.decimal  "item_quantity",           :precision => 8, :scale => 2
    t.integer  "goal_starts"
    t.integer  "goal_completes"
    t.decimal  "goal_conversion",         :precision => 8, :scale => 2
    t.datetime "created_at",                                            :null => false
    t.datetime "updated_at",                                            :null => false
    t.decimal  "goal_abandon",            :precision => 8, :scale => 2
    t.decimal  "revenue",                 :precision => 8, :scale => 2
    t.string   "source"

You have a numeric field with typmod numeric(8,2) and you're trying to store a value greater than 999999.99 in it. See the PostgreSQL manual on NUMERIC for information on numeric scale and precision, which are the qualifiers shown after the type in parentheses.

This earlier question appears to cover the same issue with Rails, showing the Rails model and how the scale and precision are assigned.

NUMERIC isn't a date/time field, it's a number field.

Demo of the issue:

regress=> SELECT  NUMERIC(8,2) '999999.99';
  numeric  
-----------
 999999.99
(1 row)
regress=> SELECT  NUMERIC(8,2) '1000000.00';
ERROR:  numeric field overflow
DETAIL:  A field with precision 8, scale 2 must round to an absolute value less than 10^6.

It's a pity that Pg doesn't tell you what field this is when it is a field. It's difficult for it to do so, though, because it doesn't usually know which value is going to go into which field when it's parsing string literals. Enable log_statement = 'all' in postgresql.conf, ALTER USER ... SET, ALTER DATABASE ... SET, or per-session with SET log_statement = 'all' then re-test and examine the query logs.

Also look at the table definitions with \dt in psql to see what might have the type numeric(8,2) and could be causing the problem.

As for why it works locally: Is the local DB PostgreSQL? Some Rails users seem to have a very odd setup where they use SQLite locally, and PostgreSQL on Heroku. This is a recipe for chaos and deployment problems. Use the same database in development and testing. If it is PostgreSQL locally, is it the same version?

Thanks for your help! So this could actually be total hits to the site. What migration would I want to run to allow PG to accommodate that number? – Richard Burton Oct 29, 2012 at 2:52 @RichardBurton Hard to say since you haven't shown your original schema definitions / rails model. I'm not much good with the Ruby/Rails side anyway; I'd use Rails' execute in the migration to run the raw SQL ALTER TABLE whatever_table ALTER COLUMN the_problem_column TYPE NUMERIC, making it an unbounded numeric. There might be some properly Rails-y way I don't know. – Craig Ringer Oct 29, 2012 at 2:56 Thanks a lot -- I just added the schema so I guess it's one of those three variables with :precision => 8, :scale => 2 – Richard Burton Oct 29, 2012 at 3:11 @RichardBurton As I said, I'm not much into the Rails side, so I'm not sure how/if data type change migrations work. I'd recommend asking a new ruby-on-rails tagged question for that and link back to this one for context. – Craig Ringer Oct 29, 2012 at 4:05

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.