philihp@pg_openskill

v0.1.0•Created 4 months ago•By philihp

pg_openskill

OpenSkill ratings as PostgreSQL functions. A pg_tle extension on database.dev.

Version 0.1.0 implements the Plackett-Luce model with the default constants. Results match openskill.js 5.0.1 bit for bit. The test suite compares the IEEE-754 bit pattern of every float.

Install

select dbdev.install('philihp@pg_openskill');
create extension "philihp@pg_openskill" version '0.1.0';

This creates the functions in the openskill schema. It requires dbdev and pg_tle.

Usage

-- a new rating: (25, 8.333...)
select openskill.rating();

-- rate a match: teams in finish order
select openskill.rate('[
  [{"mu": 25, "sigma": 8.333333333333334}],
  [{"mu": 25, "sigma": 8.333333333333334}]
]'::jsonb);
-- → [[{"mu": 27.63..., "sigma": 8.06...}], [{"mu": 22.36..., "sigma": 8.06...}]]

Teams can have different sizes (2v1, 4v4v2). The rank argument sets the result of the match. A lower rank is better, and equal ranks are ties.

select openskill.rate(
  '[[{"mu":24.1,"sigma":6.2},{"mu":26.9,"sigma":5.1}], [{"mu":33.3,"sigma":4.4}]]'::jsonb,
  array[2, 1]  -- the solo player won
);

-- for one player per team, use the composite-type wrapper
select openskill.rate_1v1(array[openskill.rating(), openskill.rating()], array[2, 1]);

-- sort players by a conservative skill estimate (mu - 3*sigma)
select openskill.ordinal(24.0, 6.0);          -- → 6.0
select openskill.ordinal(openskill.rating()); -- → 0.0

Rate a table of matches

Start with a table of matches:

create table match (
  id int primary key,
  played_at date not null,
  team_a int[] not null,  -- player ids
  team_b int[] not null,
  winner int not null     -- 1 or 2
);

insert into match values
  (1, '2026-08-01', '{1,2}', '{3,4}', 1),
  (2, '2026-08-02', '{1,3}', '{2,4}', 2),
  (3, '2026-08-03', '{1,4}', '{2,3}', 1);

Each rating depends on the ratings before it, so the matches must apply in time order. This recursive query folds the matches into a ratings map. New players start at openskill.rating(). The last step is the leaderboard, sorted by ordinal():

with recursive ordered_match as (
  select match.*, row_number() over (order by played_at, id) as seq
  from match
),
step as (
  select 0::bigint as seq, '{}'::jsonb as ratings
  union all
  select om.seq, s.ratings || upd.changed
  from step s
  join ordered_match om on om.seq = s.seq + 1
  cross join lateral (
    select openskill.rate(
      jsonb_build_array(
        (select jsonb_agg(coalesce(s.ratings -> p::text, to_jsonb(openskill.rating())) order by ord)
         from unnest(om.team_a) with ordinality as a(p, ord)),
        (select jsonb_agg(coalesce(s.ratings -> p::text, to_jsonb(openskill.rating())) order by ord)
         from unnest(om.team_b) with ordinality as b(p, ord))),
      case when om.winner = 1 then array[1, 2] else array[2, 1] end) as res
  ) rated
  cross join lateral (
    select jsonb_object_agg(p::text, rated.res -> tm -> (ord - 1)::int) as changed
    from (
      select p, ord, 0 as tm from unnest(om.team_a) with ordinality as a(p, ord)
      union all
      select p, ord, 1 from unnest(om.team_b) with ordinality as b(p, ord)
    ) players
  ) upd
)
select key::int as player_id,
       (value ->> 'mu')::float8 as mu,
       (value ->> 'sigma')::float8 as sigma,
       openskill.ordinal((value ->> 'mu')::float8, (value ->> 'sigma')::float8) as skill
from (select ratings from step order by seq desc limit 1) as final,
     jsonb_each(final.ratings)
order by skill desc;
 player_id |         mu         |       sigma       |       skill
-----------+--------------------+-------------------+--------------------
         2 | 27.004038221652657 | 7.878811820428792 | 3.3676027603662817
         1 |  26.92455102195347 | 7.878811820428792 | 3.2881155606670944
         4 | 26.843123794875794 | 7.878811820428792 |  3.206688333589419
         3 |  19.22828696151808 | 7.878811820428792 | -4.408148499768295

For incremental updates, store the ratings in a table instead. Rate each new match with openskill.rate(), and write the result back.

This query runs in CI (test/api.sql).

API

functionreturnsnotes
openskill.rating(mu float8 = null, sigma float8 = null)openskill.ratingdefaults (25, 25/3)
openskill.ordinal(mu float8, sigma float8) / ordinal(rating)float8mu - 3*sigma
openskill.rate(teams jsonb, rank int[] = null)jsonbteams as [[{mu, sigma}, ...], ...], null rank = finish order
openskill.rate_1v1(players openskill.rating[], rank int[] = null)openskill.rating[]one player per team

All functions are IMMUTABLE PARALLEL SAFE.

Notes on precision

Postgres exp() uses the platform libm, which differs from V8 in the last bit for ~10% of inputs. The extension contains a plpgsql port of V8's Math.exp instead.

The reference is V8's source, which x64 Node builds execute exactly. Some arm64 Node builds compile V8 with FMA contraction. Those builds drift from their own source by 1 ulp on ~0.1% of exp inputs. This extension matches the x64 behavior on all platforms.

Development

npm install          # dev dependencies (openskill.js, for fixture generation)
npm run fixtures     # regenerate test/fixtures.json from openskill.js
npm test             # temporary Postgres cluster + bitwise parity + API tests

License

MIT

Install

  1. Install the dbdev CLI
  2. Generate migration:
dbdev add -o ./migrations -s extensions -v 0.1.0 package -n "philihp@pg_openskill"

Downloads

  • 0 all time downloads
  • 0 downloads in the last 30 days
  • 0 downloads in the last 90 days
  • 0 downloads in the last 180 days