pg_bigm

1. Overview

pg_bigm provides bigram-based full-text search capabilities for IvorySQL. It is optimized for Chinese, Japanese, and Korean (CJK) text, enabling fuzzy search and similarity queries.

2. Installation

The IvorySQL installation package already includes the pg_bigm extension. If you installed IvorySQL using the installation package, you typically do not need to manually install pg_bigm. For other installation methods, refer to the source code installation steps below.

The source code installation environment is Ubuntu 24.04 (x86_64), with IvorySQL 5 or later already installed at /usr/ivory-5.

2.1. Source Code Installation

Download the pg_bigm v1.2 source code from https://github.com/pgbigm/pg_bigm/releases/tag/v1.2-20250903.

  • Install pg_bigm

# Extract the source code and enter its directory
cd pg_bigm-1.2-20250903

# Build the code, setting the PG_CONFIG environment variable to the path of pg_config, e.g., /usr/ivory-5/bin/pg_config
make USE_PGXS=1 PG_CONFIG=/usr/ivory-5/bin/pg_config
sudo make USE_PGXS=1 PG_CONFIG=/usr/ivory-5/bin/pg_config install

2.2. Building from Source on Windows

This section was verified on Windows 11 with Visual Studio 2022 Build Tools (MSVC 19.44). The Windows port of IvorySQL is still under review, so the verification used commit 947b50e1 of the Windows test baseline: master plus the open Windows port PRs (IvorySQL #2321, #2332, #2333, #2485, #2530, #2811, #3418, #3419, #3420 and #3421).

Build and install IvorySQL in "Developer PowerShell for VS 2022". Git, Strawberry Perl, winflexbison and Meson (with Ninja) must be on PATH. The verification build also used -Dcassert=true -Dtap_tests=enabled to run the tests.

git clone --branch win/reference https://github.com/jiabin777on-hue/IvorySQL.git
Set-Location IvorySQL
git checkout 947b50e1
meson setup build --prefix=C:\ivorysql -Dauto_features=disabled -Duuid=none
ninja -C build
meson install -C build
Set-Location ..

Windows has no PGXS, so compile the extension directly with MSVC’s cl and link, linking against postgres.lib from the IvorySQL installation. Clone with core.autocrlf=false so that test data files are not converted to CRLF.

$IVY = 'C:\ivorysql'
$INC = "$IVY\include\postgresql\server"
$CFLAGS = @("/nologo", "/c", "/O2", "/MD", "/utf-8", "/std:c11", "/DWIN32", "/DWINDOWS",
    "/D__WINDOWS__", "/D__WIN32__", "/D_CRT_SECURE_NO_DEPRECATE", "/D_CRT_NONSTDC_NO_DEPRECATE",
    "/I.", "/I$INC\port\win32_msvc", "/I$INC\port\win32", "/I$INC", "/I$IVY\include")
git clone -c core.autocrlf=false https://github.com/pgbigm/pg_bigm.git
Set-Location pg_bigm
git checkout 557cc34afa90
cl @CFLAGS bigm_op.c bigm_gin.c
link /nologo /DLL /OUT:pg_bigm.dll bigm_op.obj bigm_gin.obj "$IVY\lib\postgres.lib"
Copy-Item pg_bigm.dll "$IVY\lib\postgresql\"
Copy-Item pg_bigm.control, pg_bigm--*.sql "$IVY\share\postgresql\extension\"

The verified version is pg_bigm 1.2 (commit 557cc34afa90); the source needs no changes and the library does not need to be preloaded.

Results: the extension’s regression tests pg_bigm and pg_bigm_ja pass in PG mode (2/2); pg_bigm_ja also passes in Oracle mode.

Two things to keep in mind in Oracle mode:

  • Oracle’s LIKE has no default escape character, while likequery() escapes %, _ and \ with \. To search for those characters, give the escape character explicitly: WHERE col LIKE likequery('100%') ESCAPE '\'. Without ESCAPE, such searches miss rows.

  • An empty string is NULL, so for example show_bigm('') returns NULL rather than {}.

3. Create Extension

Connect to the database via psql and execute the following commands:

ivorysql=# CREATE EXTENSION pg_bigm;
CREATE EXTENSION

ivorysql=# SELECT * FROM pg_available_extensions WHERE name = 'pg_bigm';
  name   | default_version | installed_version |                             comment
---------+-----------------+-------------------+------------------------------------------------------------------
 pg_bigm | 1.2             | 1.2               | text similarity measurement and index searching based on bigrams
(1 row)

4. Usage

# First, create the index
CREATE TABLE pg_tools (tool text, description text);

INSERT INTO pg_tools VALUES ('pg_hint_plan', 'Tool that allows a user to specify an optimizer HINT to PostgreSQL');
INSERT INTO pg_tools VALUES ('pg_dbms_stats', 'Tool that allows a user to stabilize planner statistics in PostgreSQL');
INSERT INTO pg_tools VALUES ('pg_bigm', 'Tool that provides 2-gram full text search capability in PostgreSQL');
INSERT INTO pg_tools VALUES ('pg_trgm', 'Tool that provides 3-gram full text search capability in PostgreSQL');

CREATE INDEX pg_tools_idx ON pg_tools USING gin (description gin_bigm_ops);

# Execute full-text search
SELECT * FROM pg_tools WHERE description LIKE '%search%';

  tool   |                             description
---------+---------------------------------------------------------------------
 pg_bigm | Tool that provides 2-gram full text search capability in PostgreSQL
 pg_trgm | Tool that provides 3-gram full text search capability in PostgreSQL
(2 rows)