question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SELECT COUNT() return typeof string

See original GitHub issue

using postgres 9.2 and pg moudle 2.0.0

script like following, the count type is number in 1.1.3, but string in 2.0.0, is it by design?

pgClient.query "SELECT COUNT(*) AS cnt FROM abc;", (err, results) ->
    count = results.rows[0].cnt
    console.log typeof(count)
    pgClient.end()

Thanks Qiang

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:11
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

9reactions
brianccommented, Jun 25, 2013

Unfortunately those are going to return strings now since the return type of the COUNT operator is bigint which, by definition, can exceed the maximum value of an int in JavaScript. I know it’s extremely unlikely you’d ever have more than one hundred billion rows or whatever the actual Number.MAX_VALUE is, but it’s the only way to be correct. We’ve gone back and forth and back and forth on number type conversions, and converting numbers to integers when they can possibly loose precision is the wrong way to do things.

It really sucks it introduces such a large backwards compatibility issue, but that’s why I bumped the major version. I would suggest if you know you’re never going to have more than Number.MAX_VALUE data in your database you provide a custom type parser for the number types you’d like to receive as integers. I’ve created an “example” module for reference here:

https://github.com/brianc/node-pg-parse-float

0reactions
brianccommented, Nov 19, 2013

I think so, yeah. Sometimes I like to leave these kinda issues open as informative. But…github search is a lot better than it used to be so that’s probably a waste now. 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres cast count(*) as integer? - node.js - Stack Overflow
I'm not seeing it: select pg_typeof(count(*)) from cell_per; bigint . · @S-Man is corect. · My guess is age has a declared type...
Read more >
SQL Count(*) return type? | Ars OpenForum - Ars Technica
In Oracle, the fastest way to get a COUNT() is to use an indexed column, i.e. SELECT COUNT(id) FROM staff or suchlike.
Read more >
Return type of [select count() from sObject where Condition ]
Hi guys I am trying to counting total rows in opportunity object using count() but its giving me an errors says Compile Error:...
Read more >
Documentation: 9.5: Aggregate Functions - PostgreSQL
A query like: SELECT count(*) FROM sometable;. will require effort proportional to the size of the table: PostgreSQL will need to scan either...
Read more >
What is the return type of a “count” query against MySQL using ...
What is the return type of a “count” query against MySQL using Java JDBC? ... useSSL=false", "root","123456"); String yourQuery = "SELECT ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found