Skip to content
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

sql: unique_rowid() returns and javascript #15721

Closed
petermattis opened this issue May 5, 2017 · 20 comments
Closed

sql: unique_rowid() returns and javascript #15721

petermattis opened this issue May 5, 2017 · 20 comments

Comments

@petermattis
Copy link
Collaborator

petermattis commented May 5, 2017

unique_rowid() returns values that cannot be represented exactly by a number in Javascript. For example, 235191684988928001 is a recent value returned by unique_rowid() but in Javascript that is represented as 235191684988928000 (notice the last digit is different). The precision loss can then foul up Javascript application which temporarily stores the ID and then queries using it.

Reported by @mattiasmak. This is arguably a bug in the NodeJS driver.

@tamird
Copy link
Contributor

tamird commented May 7, 2017 via email

@petermattis
Copy link
Collaborator Author

node-pg was mentioned. @mattiasmak do you have a snippet of code you can share with us demonstrating this problem?

@jseldess
Copy link
Contributor

jseldess commented May 8, 2017

@petermattis, is there a workaround we should suggest here? Don't query based on a returned unique_rowid() value in a Javascript application?

@petermattis
Copy link
Collaborator Author

The workaround would be to cast the integer to a string when using a language that doesn't support 64-bit integer types. For example: SELECT CAST(id AS string) FROM my_table.

@benesch
Copy link
Contributor

benesch commented May 8, 2017

Since v3, which is ancient at this point, the node-postgres driver (the de facto standard driver for Node) will return bigints as strings unless you explicitly configure it otherwise (source).

We should probably document this necessity. My guess is you've somehow changed this default, @mattiasmak?

@tamird
Copy link
Contributor

tamird commented May 8, 2017 via email

@jseldess
Copy link
Contributor

jseldess commented May 8, 2017

I don't understand the implications of the last two comments. Here's how I'm explaining the limitation currently. Is it still accurate? If not, how should I change it?

The unique_rowid() function returns values that cannot be represented exactly by numbers in Javascript. For example, a 235191684988928001 value returned by unique_rowid() would get represented as 235191684988928000 in Javascript. Notice that the last digit is different. In such cases, if the Javascript application temporarily stores the value 235191684988928001 and queries using it, it won't match the value that actually got stored.

As a workaround when using JavaScript or another language that doesn't support 64-bit integer types, cast the integer to a string server-side before inspecting or using it, for example:

SELECT CAST (id AS string) FROM my_table;

@tamird
Copy link
Contributor

tamird commented May 11, 2017 via email

@tamird
Copy link
Contributor

tamird commented May 11, 2017

Ah, I see we already have this on known-limitations. We should remove it. It's also the case that this kind of problem will affect any 64-bit integer returned from the database to a Javascript application (but again, only with prehistoric versions of node-pg or with exotic configurations of node-pg), so if we do decide to document this, we should not confine the note to unique_rowid.

@jseldess
Copy link
Contributor

Thanks, @tamird. Asked @sploiselle to remove it as part of cockroachdb/docs#1400.

@benesch
Copy link
Contributor

benesch commented May 12, 2017

Wait wait! I think this is worth documenting. I'll wager there are many node applications that choose to parse int8s as ints instead of strings. (See here for one such discussion.)

I do think we should expand the documentation to include all 64-bit integers, but calling out unique_rowid explicitly isn't so crazy. Auto-incrementing primary keys are exactly where most users will run into this first, if they run into it, because parsing a Postgres auto-incrementing int8 column as a number will work until you hit your 100-billionth insert or whatever, whereas it'll fall over basically immediately with Cockroach.

@jseldess
Copy link
Contributor

Perhaps this just shouldn't be documented as a known limitation, which implies a limitation on the part of CockroachDB, but rather somewhere else, maybe in the SQL-specific FAQs section?

@benesch
Copy link
Contributor

benesch commented May 12, 2017

That's a good point! It's not a known limitation; it's a "gotcha" for folks who were already bending the rules.

@benesch
Copy link
Contributor

benesch commented May 12, 2017

It just occurred to me that our examples-orm repository literally gets this wrong at this very moment. https://reviewable.io/reviews/cockroachdb/examples-orms/20#-Kfiyp_3rHRaWNdsq433

We should document this more loudly, if anything.

@tamird
Copy link
Contributor

tamird commented May 12, 2017 via email

@petermattis petermattis added this to the 1.1 milestone Jun 1, 2017
@petermattis
Copy link
Collaborator Author

@jseldess Have we done anything towards documenting this?

@petermattis
Copy link
Collaborator Author

@jseldess Is this done?

@benesch Was examples-orms fixed?

@jseldess
Copy link
Contributor

It looks like we removed this from known limitations, but we never put it back anywhere else.

I've just opened this docs PR to add it back as a SQL FAQ: cockroachdb/docs#1928.

jseldess pushed a commit to cockroachdb/docs that referenced this issue Sep 27, 2017
jseldess pushed a commit to cockroachdb/docs that referenced this issue Sep 27, 2017
jseldess pushed a commit to cockroachdb/docs that referenced this issue Sep 27, 2017
jseldess pushed a commit to cockroachdb/docs that referenced this issue Oct 3, 2017
@jseldess
Copy link
Contributor

jseldess commented Oct 3, 2017

Docs PR merged: cockroachdb/docs#1928

@petermattis, ok to close this?

@petermattis
Copy link
Collaborator Author

Looks great.

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

No branches or pull requests

4 participants