Skip to content

Instantly share code, notes, and snippets.

@akorotkov
Created February 2, 2020 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akorotkov/fce8ec80e3b0bf113b68a82fe41294a3 to your computer and use it in GitHub Desktop.
Save akorotkov/fce8ec80e3b0bf113b68a82fe41294a3 to your computer and use it in GitHub Desktop.
Fastpath lock collision generator
CREATE OR REPLACE FUNCTION int4_to_bytea(int8) RETURNS bytea AS
$BODY$
SELECT set_byte(' '::bytea, 0, ($1 % 256)::int4) ||
set_byte(' '::bytea, 0, (($1 / 256) % 256)::int4) ||
set_byte(' '::bytea, 0, (($1 / 65536) % 256)::int4) ||
set_byte(' '::bytea, 0, (($1 / 16777216) % 256)::int4);
$BODY$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION int2_to_bytea(int4) RETURNS bytea AS
$BODY$
SELECT set_byte(' '::bytea, 0, $1 % 256) ||
set_byte(' '::bytea, 0, ($1 / 256) % 256);
$BODY$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION hashbytea(bytea) RETURNS int8 AS $$hashvarlena$$ LANGUAGE internal;
CREATE OR REPLACE FUNCTION rellocktaghash(regclass) RETURNS int4 AS
$BODY$
SELECT (hashbytea(
int4_to_bytea((SELECT oid FROM pg_database WHERE datname = current_database())::int8) ||
int4_to_bytea($1::int8) ||
int4_to_bytea(0) ||
int2_to_bytea(0) ||
int2_to_bytea(256)) % 1024)::int4;
$BODY$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION make_collision() RETURNS void AS
$BODY$
BEGIN
LOOP
CREATE TABLE collision (i int4);
IF rellocktaghash('collision'::regclass) = rellocktaghash('pgbench_accounts'::regclass) THEN
EXIT;
END IF;
DROP TABLE collision;
END LOOP;
END
$BODY$
LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment