Getting Started with SWI-Prolog RocksDB on Windows 11

Note: Do not reply to this topic; questions, concerns, comments, etc. are to be handled in
Wiki Discussion: Getting Started with SWI-Prolog RocksDB on Windows 11



Getting Started with SWI-Prolog RocksDB on Windows 11

December 24, 2025

A beginner’s guide to installing and using RocksDB with SWI-Prolog on Windows 11.

What is This?

RocksDB is a high-performance database that stores information as key-value pairs. Think of it like a very fast filing cabinet where you can store and retrieve data.

SWI-Prolog is a programming language particularly good at working with logical rules and relationships.

rocks-predicates lets you store Prolog facts (like “John is a person” or “Paris is in France”) in RocksDB so they survive even after you close your program.

Key Benefit: Not Limited by RAM

Unlike regular Prolog which keeps everything in RAM (memory), RocksDB stores data on your hard drive, SSD, or even USB flash drives. This means:

  • No RAM limits: Store millions or billions of facts without running out of memory
  • Persistent storage: Data survives computer restarts and power loss
  • Flexible storage: Use fast SSDs for speed, or USB drives for portability
  • Trade-off: Hard drive access is slower than RAM, but RocksDB is optimized to be very fast (queries in microseconds)

Think of it like the difference between keeping notes in your head (RAM) versus writing them in a notebook (disk). The notebook can hold far more information, though accessing it takes slightly longer.

Why MSYS2?

Currently, RocksDB doesn’t work with native Windows SWI-Prolog. We need MSYS2, which provides a Linux-like environment on Windows where we can compile and run RocksDB.

Prerequisites

  • Windows 11 (this guide is specifically for Windows 11)
  • About 2 GB of free disk space
  • Administrator access to install software
  • About 30-45 minutes of time

Step 1: Install MSYS2

MSYS2 provides the environment we need to run RocksDB.

  1. Download MSYS2

    • Open your web browser
    • Go to: https://www.msys2.org/
    • Click the big download button
    • Save the installer (it’s called something like msys2-x86_64-*.exe)
  2. Run the Installer

    • Double-click the downloaded file
    • Click “Next” through the installer
    • Use the default installation path: C:\msys64
    • Click “Finish”
  3. Update MSYS2

    • After installation, a terminal window opens automatically
    • Type this command and press Enter:
      pacman -Syu
      
    • When it asks “Proceed with installation? [Y/n]”, type Y and press Enter
    • If it says to close the terminal, close it and reopen it from the Start menu (search for “MSYS2 MSYS”)
    • Run the update command again:
      pacman -Syu
      

Step 2: Install Required Tools

Now we’ll install the tools needed to build RocksDB.

  1. Close the MSYS2 MSYS terminal if it’s open

  2. Open MSYS2 MINGW64 (this is important - not the regular MSYS2)

    • Press the Windows key
    • Search for “MSYS2 MINGW64”
    • Click to open it
    • You should see a purple/blue terminal window
  3. Install Git (needed to download code)

    pacman -S git
    
    • When asked “Proceed with installation? [Y/n]”, type Y and press Enter
  4. Install the C++ Compiler

    pacman -S mingw-w64-x86_64-gcc
    
    • Type Y when prompted
  5. Install RocksDB Library

    pacman -S mingw-w64-x86_64-rocksdb
    
    • Type Y when prompted
  6. Install SWI-Prolog for MSYS2

    pacman -S mingw-w64-x86_64-swi-prolog
    
    • Type Y when prompted
  7. Verify SWI-Prolog Installed

    swipl --version
    
    • You should see something like “SWI-Prolog version 10.0.0”

Step 3: Build the RocksDB Interface

Now we need to build the connection between SWI-Prolog and RocksDB.

  1. Download the RocksDB Pack

    cd ~
    git clone https://github.com/JanWielemaker/rocksdb.git
    cd rocksdb
    
  2. Fix Compatibility Issues

    The code needs two small fixes for newer RocksDB. Run these commands:

    sed -i '1044s/.*/               true; } ),  \/\/ random_access_max_buffer_size removed in newer RocksDB/' cpp/rocksdb4pl.cpp
    
    sed -i '1084s/.*/               true; } ),  \/\/ fail_if_options_file_error removed in newer RocksDB/' cpp/rocksdb4pl.cpp
    
  3. Create Output Directory

    mkdir -p lib/x64-win64
    
  4. Compile the Interface

    Copy and paste this entire command (it’s long):

    g++ -shared -D__SWI_PROLOG__ -I/mingw64/lib/swipl/include \
        cpp/rocksdb4pl.cpp /mingw64/lib/librocksdb.a \
        -L/mingw64/lib/swipl/lib/x64-win64 -lswipl \
        -lsnappy -lzstd -lz -llz4 -lbz2 -lshlwapi -lrpcrt4 \
        -o lib/x64-win64/rocksdb4pl.dll
    
    • This will take about 30 seconds to 1 minute
    • If successful, you won’t see any error messages
  5. Install to SWI-Prolog Pack Directory

    mkdir -p ~/.local/share/swi-prolog/pack/rocksdb/lib/x64-win64
    cp lib/x64-win64/rocksdb4pl.dll ~/.local/share/swi-prolog/pack/rocksdb/lib/x64-win64/
    

Step 4: Test Basic RocksDB

Let’s verify RocksDB works!

  1. Start SWI-Prolog

    swipl
    
    • You should see a ?- prompt
  2. Load RocksDB

    ?- use_module(library(rocksdb)).
    
    • Expected result: true.
  3. Test Basic Operations

    Run these commands one at a time:

    ?- rocks_open('test.db', DB, []).
    ?- rocks_put(DB, foo, bar).
    ?- rocks_get(DB, foo, V).
    ?- rocks_close(DB).
    

    Expected results:

    • rocks_open → Shows DB = <rocksdb>(...)
    • rocks_put → Shows true.
    • rocks_get → Shows V = bar.
    • rocks_close → Shows true.
  4. Exit SWI-Prolog

    ?- halt.
    

Step 5: Install rocks-predicates

This gives you a way to store Prolog facts persistently.

  1. Download rocks-predicates

    cd ~
    git clone https://github.com/JanWielemaker/rocks-predicates.git
    cd rocks-predicates
    
  2. Create Database Directory

    mkdir -p dbs
    
  3. Start SWI-Prolog

    swipl
    
  4. Load the Libraries

    ?- use_module(library(rocksdb)).
    ?- consult('rocks_preds.pl').
    
    • You may see a warning about “ensure_directory” - this is normal and safe to ignore
  5. Test Storing Data

    Run these commands:

    ?- rdb_open('dbs/test_preds', DB).
    ?- rdb_assertz('dbs/test_preds', person(john)).
    ?- rdb_assertz('dbs/test_preds', person(mary)).
    ?- rdb_clause('dbs/test_preds', person(X), Body).
    

    Expected results:

    • rdb_open → Shows DB = <rocksdb>(...)
    • Both rdb_assertz → Show true.
    • rdb_clause → Shows:
      X = john,
      Body = true ;
      X = mary,
      Body = true.
      

Common Commands Reference

Opening a Database

?- rdb_open('dbs/my_database', DB).

Storing Facts

?- rdb_assertz('dbs/my_database', parent(tom, bob)).
?- rdb_assertz('dbs/my_database', parent(bob, alice)).

Querying Facts

?- rdb_clause('dbs/my_database', parent(X, Y), Body).

This will show all parent relationships.

Closing a Database

?- rdb_close('dbs/my_database').

Storing Databases on Different Drives

Since RocksDB stores data on disk (not in RAM), you can put databases anywhere:

On Your Main Hard Drive

?- rdb_open('C:/data/my_database', DB).

On an External USB Drive

?- rdb_open('E:/portable_data/my_database', DB).

On a Network Drive

?- rdb_open('Z:/shared/my_database', DB).

Performance Tips:

  • SSD drives: Fastest option for frequent queries
  • Regular hard drives: Slower but fine for occasional access
  • USB flash drives: Portable but slowest; good for backups or moving data between computers
  • Network drives: Convenient for sharing but speed depends on network connection

Troubleshooting

“bash: git: command not found”

Solution: Install git:

pacman -S git

“cannot open output file lib/x64-win64/rocksdb4pl.dll”

Solution: Create the directory first:

mkdir -p lib/x64-win64

“The specified module could not be found”

Solution: Make sure you copied the DLL to the pack directory:

mkdir -p ~/.local/share/swi-prolog/pack/rocksdb/lib/x64-win64
cp lib/x64-win64/rocksdb4pl.dll ~/.local/share/swi-prolog/pack/rocksdb/lib/x64-win64/

Missing rocksdb4pl.dll when running rocks_open

Symptoms:
When running rocks_open('test.db', DB, []), you get an error like:

ERROR: Initialization goal raised exception:
ERROR: The specified module could not be found.
ERROR: Exported procedure rocksdb:rocks_open_/3 is not defined

Cause: The rocksdb4pl.dll file is missing from the expected location.

How to verify the DLL location:
You can check where SWI-Prolog expects to find the DLL:

?- absolute_file_name(foreign(rocksdb4pl), Path, [file_type(executable), access(exist)]).

This will show the expected path, typically:

~/.local/share/swi-prolog/pack/rocksdb/lib/x64-win64/rocksdb4pl.dll

Solution:

  1. Make sure you completed Step 3, substep 5 from the installation instructions:

    mkdir -p ~/.local/share/swi-prolog/pack/rocksdb/lib/x64-win64
    cp lib/x64-win64/rocksdb4pl.dll ~/.local/share/swi-prolog/pack/rocksdb/lib/x64-win64/
    
  2. Verify the DLL exists:

    ls -lh ~/.local/share/swi-prolog/pack/rocksdb/lib/x64-win64/rocksdb4pl.dll
    

“Unknown procedure: rdb_open/2”

Solution: Make sure you loaded both libraries:

?- use_module(library(rocksdb)).
?- consult('rocks_preds.pl').

Wrong Terminal Type

Make sure you’re using MSYS2 MINGW64 (purple/blue terminal), not “MSYS2 MSYS” (yellow terminal).

What’s Next?

Now that you have RocksDB working, you can:

  1. Store large amounts of data - RocksDB can handle millions of facts efficiently
  2. Persist data across sessions - Unlike regular Prolog, your data survives when you close the program
  3. Index your data - Use rdb_index/3 to make queries faster on large datasets
  4. Use external storage - Put databases on USB drives or network drives for portability

Performance Examples

According to the rocks-predicates README (tested on AMD3950X with SSD):

Dataset Facts Load Time Query Time Database Size
WordNet 3.0 821,492 clauses 11.7 sec 10 microseconds 99 MB
Geonames RDF 123 million triples 35 minutes 30 microseconds 5.2 GB

What this means:

  • You can store millions of facts without running out of RAM
  • Queries are still very fast (microseconds)
  • The data is persistently saved on disk

Future: Native Windows Support

Currently, this setup requires MSYS2. There’s a potential path to native Windows support using vcpkg (Microsoft’s C++ package manager). This would eliminate the need for MSYS2 but requires modifying the rocksdb pack.

Getting Help

If you encounter issues:

  1. Check the Troubleshooting section above
  2. Visit the SWI-Prolog Discourse: https://swi-prolog.discourse.group/
  3. Check the rocksdb pack page: "rocksdb" pack for SWI-Prolog
  4. Open an issue on GitHub: GitHub - JanWielemaker/rocksdb: SWI-Prolog interface for RocksDB

Credits

  • RocksDB Pack: Jan Wielemaker
  • rocks-predicates: Jan Wielemaker
  • MSYS2 Integration: mgondan - Discourse forum - msys2
  • This Guide: Based on successful Windows 11 installation (December 2025)

License

This guide is provided as-is for educational purposes. The software packages mentioned have their own licenses:

  • RocksDB: GPL-2.0-only OR Apache-2.0
  • SWI-Prolog: BSD-2-Clause
  • MSYS2: Various open source licenses
1 Like