Hello Mutsumara-san,

#3. Documentation
I think modulo operator explanation should put at last at the doc line.
Because the others are more frequently used.

So I like patch3 which is simple and practical.

Ok.

If you agree or reply my comment, I will mark ready for commiter.

Please find attached v4, which is v3 plus an improved documentation
which is clearer about the sign of the remainder.

--
Fabien.
diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c
index 2f7d80e..a815ad3 100644
--- a/contrib/pgbench/pgbench.c
+++ b/contrib/pgbench/pgbench.c
@@ -1574,6 +1574,22 @@ top:
 					}
 					snprintf(res, sizeof(res), INT64_FORMAT, ope1 / ope2);
 				}
+				else if (strcmp(argv[3], "%") == 0)
+				{
+					int64_t remainder;
+					if (ope2 == 0)
+					{
+						fprintf(stderr, "%s: division by zero in modulo\n", argv[0]);
+						st->ecnt++;
+						return true;
+					}
+					/* divisor signed remainder */
+					remainder = ope1 % ope2;
+					if ((ope2 < 0 && remainder > 0) ||
+						(ope2 > 0 && remainder < 0))
+						remainder += ope2;
+					snprintf(res, sizeof(res), INT64_FORMAT, remainder);
+				}
 				else
 				{
 					fprintf(stderr, "%s: unsupported operator %s\n", argv[0], argv[3]);
diff --git a/doc/src/sgml/pgbench.sgml b/doc/src/sgml/pgbench.sgml
index b479105..66ec622 100644
--- a/doc/src/sgml/pgbench.sgml
+++ b/doc/src/sgml/pgbench.sgml
@@ -735,7 +735,9 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
       Each <replaceable>operand</> is either an integer constant or a
       <literal>:</><replaceable>variablename</> reference to a variable
       having an integer value.  The <replaceable>operator</> can be
-      <literal>+</>, <literal>-</>, <literal>*</>, or <literal>/</>.
+      <literal>+</>, <literal>-</>, <literal>*</>, <literal>%</> or <literal>/</>.
+      The modulo operation (<literal>%</>) is based on the floored division, so
+      that the remainder keeps the sign of the divisor.
      </para>
 
      <para>
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to