Skip to content

Commit 4b33474

Browse files
committed
Ensure that unsigned 4-byte OIDs are able to squeeze into the signed
4-byte int accepted by the two-argument form of pg_try_advisory_lock() we are using. Fixes #30. Thanks to Mark Steben and Greg Sabino Mullane for the report and diagnosis.
1 parent 649e72c commit 4b33474

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

bin/pg_repack.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,8 +1359,8 @@ repack_one_table(const repack_table *table, const char *orderby)
13591359
/* Release advisory lock on table. */
13601360
params[0] = REPACK_LOCK_PREFIX_STR;
13611361
params[1] = buffer;
1362-
res = pgut_execute(connection, "SELECT pg_advisory_unlock($1, $2)",
1363-
2, params);
1362+
res = pgut_execute(connection, "SELECT pg_advisory_unlock($1, -2147483648 + $2)",
1363+
2, params);
13641364
ret = true;
13651365

13661366
cleanup:
@@ -1525,8 +1525,13 @@ static bool advisory_lock(PGconn *conn, const char *relid)
15251525
params[0] = REPACK_LOCK_PREFIX_STR;
15261526
params[1] = relid;
15271527

1528-
res = pgut_execute(conn, "SELECT pg_try_advisory_lock($1, $2)",
1529-
2, params);
1528+
/* For the 2-argument form of pg_try_advisory_lock, we need to
1529+
* pass in two signed 4-byte integers. But a table OID is an
1530+
* *unsigned* 4-byte integer. Add -2147483648 to that OID to make
1531+
* it fit reliably into signed int space.
1532+
*/
1533+
res = pgut_execute(conn, "SELECT pg_try_advisory_lock($1, -2147483648 + $2)",
1534+
2, params);
15301535

15311536
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
15321537
elog(ERROR, "%s", PQerrorMessage(connection));

0 commit comments

Comments
 (0)