Skip to content

Eliminates the possibilities of counter overflows. - #554

Merged
Jason Petersen (jasonmp85) merged 2 commits into
citusdata:masterfrom
amosbird:i64_affected_tuples
Jun 8, 2016
Merged

Jason Petersen (jasonmp85) merged 2 commits into
citusdata:masterfrom
amosbird:i64_affected_tuples

Conversation

@amosbird

Copy link
Copy Markdown
Contributor

This patch uses scanint8 instead of pg_atoi to make sure the affected
tuples counter never gets overflow.

@jasonmp85

Copy link
Copy Markdown
Contributor

The ultimate destination for this value is the es_processed field of an EState struct, which is declared as uint32. Since this field is also heavily used in "normal" PostgreSQL operations (see ProcessQuery), which use snprintf with %u and es_processed to set the command tag, I can't envision a scenario where the number of rows processed ever exceeds a uint32.

Is there a particular reason you coded up this fix? It seems weird to go through the trouble of changing this type to int64 only to truncate it to uint32 as soon as it's returned.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do go ahead with this review, the %ld should be INT64_FORMAT and not a literal %ld.

@amosbird Amos Bird (amosbird) Jun 1, 2016 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I wasn't aware of that. Thanks.

@jasonmp85

Copy link
Copy Markdown
Contributor

The documentation for PQcmdTuples doesn't actually state the width of its return value, so if 32-bit overflows, there's no contract that 64-bit wouldn't also overflow.

Practically, the actual code that generates the command tag either emits 0 as the update count or uses the same uint32 es_processed field as we do with a %u format string.

With all that in mind, the original code does have the problem that it returns an int32, potentially truncating to +231 when it is ultimately assigned to a uint32.

Maybe the best solution would be to:

  • Change ExecuteDistributedModify to return uint32
  • Update ExecuteDistributedModify's internals to parse using a int64
  • Add an Assert that this int64 value is within uint32's limits

Does that all make sense? Am I missing something?

@amosbird

Amos Bird (amosbird) commented Jun 1, 2016 •

Copy link
Copy Markdown
Contributor Author

Hmm. I was referring to the master branch of postgres where the es_processed field of an EState struct is declared as uint64. This patch still suffers the possibility of overflow you just mentioned 😜 . scanint8 would throw if the integer is out of range.

@jasonmp85

Copy link
Copy Markdown
Contributor

Hmm. I was referring to the master branch of postgres where the es_processed field of an EState struct is declared as uint64.

Heh. When I was reading this I thought to myself maybe there's some change in master I don't know about to widen this field, since it seems weird it's only uint32. Guess I should have checked. Sorry! Now I understand we'll (possibly?) want this for the future.

This patch still suffers the possibility of overflow you just mentioned 😜 . scanint8 would throw if the integer is out of range.

Well, I'd like to have have a function check and throw rather than have a silent truncation, wrap, etc. So this is still an improvement. Still, it'd be nice to scan as a uint64 somehow (possibly read as int128, check bounds, then cast to uint64)?

It sounds like this is a little more tricky than I initially thought. I'll bring it up with a teammate and get back to you (for instance, now I'm wondering about a scenario where a 9.5 master is talking to 9.6 workers, and to what degree we need to gracefully support that, warn, or fail).

Thanks for the explanation.

@jasonmp85

Copy link
Copy Markdown
Contributor

Hey Amos Bird (@amosbird)… sorry for the delay. After talking this over, we're OK with this approach:

  • Read the value as a int64
  • Verify that the result is not negative
  • Modify the enclosing function to return uint64 using a cast
  • (Optional) Add an #if (PG_VERSION_NUM < 90600) in RouterExecutorRun to ensure the return value isn't over PG_UINT32_MAX only in PostgreSQL versions prior to 9.6 (using an Assert).

This doesn't handle values greater than PG_INT64_MAX, but that number is already so astronomical as to be a big deal; besides, the int8 function would throw an error for them anyways, so we'd notice.

With the above, that's a :shipit: (ship it), so if you want to do those little updates and get back to me we can merge (you'll need to rebase).

This patch uses scanint8 instead of pg_atoi to make sure the affected
tuples counter never gets overflow.
@amosbird

Copy link
Copy Markdown
Contributor Author

Hi Jason Petersen (@jasonmp85) , I've updated the patch hopefully as you described 😄

@jasonmp85

Copy link
Copy Markdown
Contributor

This looks great! I'm going to fix up some of the formatting with our style tool and I'll get this merged in.

@jasonmp85
Jason Petersen (jasonmp85) merged commit fd1b088 into citusdata:master Jun 8, 2016
@jasonmp85

Copy link
Copy Markdown
Contributor

Thanks for your contributions!

@amosbird
Amos Bird (amosbird) deleted the i64_affected_tuples branch June 9, 2016 04:13
@amosbird

Copy link
Copy Markdown
Contributor Author

Jason Petersen (@jasonmp85) Oh, I've used the citus_indent to format the patch. It seems I didn't get it right.

@jasonmp85

Copy link
Copy Markdown
Contributor

Amos Bird (@amosbird) no problem; it wasn't stuff citus_indent would have caught, but rather minor things like starting block comments with a blank line, making sure single-line comments are lowercase. Silly things like that, so don't worry.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants