3

What does the "%%" in this statement mean?

SELECT nextval(seq_name) %% 1024 INTO seq_id;

And why does Postgres say, when I use it?

operator does not exist: bigint %% integer
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
outrunthewolf
  • 125
  • 2
  • 12
  • 2
    It's either a typo for the modulus operator (%), or it's an operator defined by an extension you have not installed. – Ben Grimm Jan 26 '15 at 21:08

1 Answers1

6

Most probably, it's an artifact from translating dynamic SQL with format(), which requires to double % characters. Should be the modulo operator % if translated correctly, which also makes sense for a sharding solution or similar. Effectively, you get numbers cycling from 0 to 1023.

I suspect it's the same as we already dealt with here:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Believe this came from the code sample in a blog post from Instagram on their generation of unique ids. Ran into the same problem. https://instagram-engineering.com/sharding-ids-at-instagram-1cf5a71e5a5c – What Would Be Cool Sep 04 '18 at 21:46