SELECT pg_try_advisory_lock(1)::integer as gotlock \gset
\if :gotlock

SELECT pg_advisory_lock(42);
        alter table p detach partition p1 concurrently ;
        alter table p detach partition p5 concurrently ;
SELECT pg_advisory_unlock(42);

\set var random(1, 5000)
\sleep :var us

alter table p attach partition p1 for values from (0) to (1);
alter table p attach partition p5 for values from (4) to (5);

select nextval('detaches');

\endif
