Skip to content

Possible bug with handling bigints? #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Developerarif2 opened this issue Aug 22, 2023 · 0 comments
Open

Possible bug with handling bigints? #198

Developerarif2 opened this issue Aug 22, 2023 · 0 comments

Comments

@Developerarif2
Copy link

I'm admittedly new to Node.js and Postgres, so perhaps there is an environment issue that I am simply unaware of. Further, I'm a stranger to the project and am not sure if this is the appropriate place to file something like this. If I'm putting this in the wrong spot, my apologies, and I'd be grateful if you could direct me to where I ought to go.

I'm working with a dataset that uses bigints. It seems like there's an issue where node-postgres loses the lowest two bits when the MSB of a bigint is set. Here's an example:

mydb=# select version();
PostgreSQL 9.1.4 on x86_64-apple-darwin, compiled by i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3), 64-bit
(1 row)
mydb=# create table bigints ( number bigint );
CREATE TABLE
mydb=# insert into bigints (number) values (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12), (13), (14), (15), (25227580722380800+0), (25227580722380800+1), (25227580722380800+2), (25227580722380800+3), (25227580722380800+4), (25227580722380800+5), (25227580722380800+6), (25227580722380800+7), (25227580722380800+8), (25227580722380800+9), (25227580722380800+10), (25227580722380800+11), (25227580722380800+12), (25227580722380800+13), (25227580722380800+14), (25227580722380800+15);
INSERT 0 32
mydb=# select * from bigints;
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
25227580722380800
25227580722380801
25227580722380802
25227580722380803
25227580722380804
25227580722380805
25227580722380806
25227580722380807
25227580722380808
25227580722380809
25227580722380810
25227580722380811
25227580722380812
25227580722380813
25227580722380814
25227580722380815
(32 rows)

So far, so good. Let's try it in node.js using node-postgres:

var pg = require('pg');
function retrieve(req, res)
{
var conString = "tcp://postgres:mypassword@localhost/mydb";
var client = new pg.Client(conString);
client.connect(function(error)
{
if(error != null)
{
logger.error("Error connecting: " + error);
} else
{
client.query("SELECT * FROM bigints", function(error, result)
{
rowOutput = [];
for(var i = 0; i < result.rows.length; i++)
{
rowOutput.push(result.rows[i].number.toString(2));
}

                    res.send(rowOutput.join("\n"));
                });
        }
    });

}
$ curl http://localhost:3000/
0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111
1011001101000000101100111011011100000000000000000000000
1011001101000000101100111011011100000000000000000000000
1011001101000000101100111011011100000000000000000000000
1011001101000000101100111011011100000000000000000000100
1011001101000000101100111011011100000000000000000000100
1011001101000000101100111011011100000000000000000000100
1011001101000000101100111011011100000000000000000001000
1011001101000000101100111011011100000000000000000001000
1011001101000000101100111011011100000000000000000001000
1011001101000000101100111011011100000000000000000001000
1011001101000000101100111011011100000000000000000001000
1011001101000000101100111011011100000000000000000001100
1011001101000000101100111011011100000000000000000001100
1011001101000000101100111011011100000000000000000001100
1011001101000000101100111011011100000000000000000010000
1011001101000000101100111011011100000000000000000010000

Notice that for the small values, the least significant bits are fine; but for the big values, the two LSBs are always 0. I see the same behavior happen in other tables, using both select *, as well as select column_name, and INSERT...RETURNING. I'm using node-postgres v0.8.1.

For the sake of completeness, I tried again using Python and psycopg2:

#!/usr/bin/python

import psycopg2;

conn = psycopg2.connect("dbname=mydb user=postgres password=mypassword")
cur = conn.cursor()

cur.execute("SELECT * FROM bigints;")
for record in cur:
print record
$ /tmp/bigint-test.py
(0L,)
(1L,)
(2L,)
(3L,)
(4L,)
(5L,)
(6L,)
(7L,)
(8L,)
(9L,)
(10L,)
(11L,)
(12L,)
(13L,)
(14L,)
(15L,)
(25227580722380800L,)
(25227580722380801L,)
(25227580722380802L,)
(25227580722380803L,)
(25227580722380804L,)
(25227580722380805L,)
(25227580722380806L,)
(25227580722380807L,)
(25227580722380808L,)
(25227580722380809L,)
(25227580722380810L,)
(25227580722380811L,)
(25227580722380812L,)
(25227580722380813L,)
(25227580722380814L,)
(25227580722380815L,)

The output here matches the correct values, suggesting that this issue is specific to node-postgres. Any guidance would be tremendously appreciated. Thanks!

P.S.: Here's a summary of all the software versions I'm using.

PostgreSQL 9.1.4
node.js v0.8.4
node-postgres v0.8.1 (installed via npm install pg)
OS X Lion 10.8 Build 12A269
Darwin 12.0.0
For completeness' sake:

Python 2.7.2 (used for comparison test above)
psycopg2 2.4.5 (used for comparison test above)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant