@@ -126,6 +126,9 @@ const char *PROGRAM_VERSION = "unknown";
126
126
" AND granted = false AND relation = %u"\
127
127
" AND mode = 'AccessExclusiveLock' AND pid <> pg_backend_pid()"
128
128
129
+ /* Will be used as a unique prefix for advisory locks. */
130
+ #define REPACK_LOCK_PREFIX_STR "16185446"
131
+
129
132
/*
130
133
* per-table information
131
134
*/
@@ -1300,8 +1303,10 @@ repack_one_table(const repack_table *table, const char *orderby)
1300
1303
}
1301
1304
1302
1305
/* Release advisory lock on table. */
1303
- res = pgut_execute (connection , "SELECT pg_advisory_unlock($1::bigint)" ,
1304
- 1 , params );
1306
+ params [0 ] = REPACK_LOCK_PREFIX_STR ;
1307
+ params [1 ] = buffer ;
1308
+ res = pgut_execute (connection , "SELECT pg_advisory_unlock($1, $2)" ,
1309
+ 2 , params );
1305
1310
ret = true;
1306
1311
1307
1312
cleanup :
@@ -1452,26 +1457,22 @@ lock_access_share(PGconn *conn, Oid relid, const char *target_name)
1452
1457
}
1453
1458
1454
1459
1455
- /* XXX: Make sure that repack_one_table() also obtains an advisory
1456
- * lock on the table, so that we can't have a table-wide repack running
1457
- * along with an indexes-only repack. Also, since advisory locks are
1458
- * 8 bytes wide and OIDs are only 4 bytes, consider using our own prefix
1459
- * rather than just the table OID, to avoid inadvertent conflict with
1460
- * other applications using advisory locks.
1461
- */
1462
-
1463
1460
/* Obtain an advisory lock on the table's OID, to make sure no other
1464
1461
* pg_repack is working on the table. This is not so much a concern with
1465
1462
* full-table repacks, but mainly so that index-only repacks don't interfere
1466
1463
* with each other or a full-table repack.
1467
1464
*/
1468
1465
static bool advisory_lock (PGconn * conn , const char * relid )
1469
1466
{
1470
- PGresult * res = NULL ;
1471
- bool ret = false;
1467
+ PGresult * res = NULL ;
1468
+ bool ret = false;
1469
+ const char * params [2 ];
1470
+
1471
+ params [0 ] = REPACK_LOCK_PREFIX_STR ;
1472
+ params [1 ] = relid ;
1472
1473
1473
- res = pgut_execute (conn , "SELECT pg_try_advisory_lock($1::bigint )" ,
1474
- 1 , & relid );
1474
+ res = pgut_execute (conn , "SELECT pg_try_advisory_lock($1, $2 )" ,
1475
+ 2 , params );
1475
1476
1476
1477
if (PQresultStatus (res ) != PGRES_TUPLES_OK ) {
1477
1478
elog (ERROR , "%s" , PQerrorMessage (connection ));
@@ -1790,7 +1791,7 @@ repack_all_indexes(char *errbuf, size_t errsize)
1790
1791
* table.
1791
1792
*/
1792
1793
if (!advisory_lock (connection , utoa (getoid (res , 0 , 3 ), buffer ))) {
1793
- snprintf (errbuf , errsize , "Unable to obtain advisory lock on %s " ,
1794
+ snprintf (errbuf , errsize , "Unable to obtain advisory lock on \"%s\" " ,
1794
1795
table_name );
1795
1796
goto cleanup ;
1796
1797
}
0 commit comments