CREATE OR REPLACE FUNCTION microsharding_advisory_lock_() RETURNS void
LANGUAGE plpgsql
SET search_path FROM CURRENT
AS $$
BEGIN
  -- Acquires an advisory lock on metadata changes. Uses random, but static
  -- constant to avoid updating the function concurrently when moving multiple
  -- shards simultaneously ("tuple concurrently updated").
  -- https://stackoverflow.com/a/44101303
  PERFORM pg_advisory_xact_lock(8099492402791936623); -- "pg-micro" string as int8
END;
$$;

COMMENT ON FUNCTION microsharding_advisory_lock_()
  IS 'Acquires an advisory lock on metadata changes. Uses random, but static '
     'constant to avoid updating the shards metadata concurrently (both '
     '"tuple concurrently updated" and multiple clients calling to e.g. '
     'microshard_ensure_active().';
