Re: Searchable chess positions in a Postgress DB

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Ondrej Ivanič <ondrej(dot)ivanic(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, Sidney Cadot <sidney(at)jigsaw(dot)nl>
Subject: Re: Searchable chess positions in a Postgress DB
Date: 2012-04-12 13:55:26
Message-ID: CAHyXU0yvuZHCrYW4SYGbXwahbWzE6GXT_kzE6ZTQs1vf6ALzGw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2012/4/11 Ondrej Ivanič <ondrej(dot)ivanic(at)gmail(dot)com>:
> Hi,
>
> On 11 April 2012 17:15, Sidney Cadot <sidney(at)jigsaw(dot)nl> wrote:
>> I have written code to extract these positions, and now I want to put
>> them into a Postgres database. Specifically, I want to do this in a
>> way that allows *fast* lookups of positions, e.g. "give me all
>> positions that have a White King on c4 and either a Black Bishop or
>> White Knight on f7".
>
> I would try to use single table with 16 columns like:
> white_pawn char(2)[] -- like {'c1', 'd3', ... }, max 8 elements
> white_rook char(2)[] -- max 2 elements
> white_bishop char(2)[] -- max 2 elements
> white_knight char(2)[] -- max 2 elements
> white_queen char(2)
> white_king char(2)
> black_pawn_1 char(2)[]
> ...
> black_king char(2)
>
> and each column; char(2) and char(2)[] should have btree and GiST
> index respectively. The query should looks like this:
> select * from positions where white_king = 'c4' and (white_bishop &&
> ARRAY['f7'] or white_knight && ARRAY['f7'])
>
> Another alternative might be to use hstore (and GiST index):
> http://www.postgresql.org/docs/9.1/static/hstore.html

yeah -- if you want fast searching of positions (or even games) using
phrases you should immediately be thinking GIST. GIST can optimize
quals such as 'A contains B' or 'A overlaps B'. This is a
non-trival but interesting project and I highly encourage you to give
it a go if you're so inclined. Before banging on the schema, I'd
start thinking about to organize the position into a type such that
you can engineer GIST operations.

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-04-12 13:55:58 Re: non-static LIKE patterns
Previous Message Gavin Flower 2012-04-12 12:35:20 Fwd: Re: Searchable chess positions in a Postgress DB