Skip to content

Commit f27b3a5

Browse files
Lev Kokotovgitbook-bot
authored andcommitted
GITBOOK-57: change request with no subject merged in GitBook
1 parent 82fcbd4 commit f27b3a5

File tree

5 files changed

+259
-31
lines changed

5 files changed

+259
-31
lines changed

pgml-docs/docs/guides/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@
5252
* [Deployment](deployment/README.md)
5353
* [PostgresML Cloud](deployment/postgresml-cloud.md)
5454
* [Self-hosting](deployment/self-hosting/README.md)
55-
* [Replication](deployment/self-hosting/replication.md)
55+
* [Pooler](deployment/self-hosting/pooler.md)
5656
* [Building from Source](deployment/self-hosting/building-from-source.md)
57+
* [Replication](deployment/self-hosting/replication.md)
5758
* [PgCat](pgcat.md)
5859
* [Benchmarks](benchmarks/README.md)
5960
* [PostgresML is 8-40x faster than Python HTTP microservices](benchmarks/postgresml-is-8-40x-faster-than-python-http-microservices.md)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
11
# Building from Source
22

3+
PostgresML is a Postgres extension written in Rust, so it can be built and installed on any system that supported by PostgreSQL and the Rust compiler. If you're planning on using GPU acceleration for Large Language Models or for XGBoost / LightGBM supervised learning, we would recommend you use an operating system well supported by Nvidia drivers and Cuda. Thankfully, that list is pretty large these days, including popular distributions like Ubuntu, Debian, RHEL, Centos, Fedora and OpenSuse.
4+
5+
### Dependencies
6+
7+
PostgresML depends on a few system packages and libraries that should be installed separately in order to successfully compile it from source. The names of the packages vary based on the Linux distribution you're using, but in most cases you should be able to find all of them verbatim in your package manger repositories:
8+
9+
```
10+
cmake
11+
clang
12+
pkg-config
13+
build-essential
14+
git
15+
libclang-dev
16+
libpython3-dev
17+
libssl-dev
18+
libopenblas-dev
19+
postgresql-server-dev-14
20+
lld
21+
```
22+
23+
This guide assumes that you're using PostgreSQL 14, so if your Postgres version is different, replace `14` in `postgresql-server-dev-14` with the correct version of Postgres. PostgresML supports all Postgres versions supported by `pgrx` and the PostgreSQL community (as of this writing, versions 12 through 16).
24+
25+
### Getting the source code
26+
27+
All of our source code is open source and hosted in GitHub. You can download it with git:
28+
29+
```bash
30+
git clone https://github.com/postgresml/postgresml
31+
```
32+
33+
The repository contains the extension, the dashboard, SDKs, and all apps we've written that are powered by PostgresML.
34+
35+
### Installing PostgresML
36+
37+
For a typical deployment in production, you would need to compile and install the extension into your system PostgreSQL installation. PostgresML is using the `pgrx` Rust extension toolkit, so this is pretty easy and straight forward to do.
38+
39+
#### Install pgrx
40+
41+
`pgrx` is open source and available from crates.io. We are currently using the `0.10.0` version. It's important that your `pgrx` version matches what we're using, since there are some hard dependencies between our code and theirs.
42+
43+
To install `pgrx`, simply run:
44+
45+
```
46+
cargo install cargo-pgrx --version "0.10.0"
47+
```
48+
49+
Before using `pgrx`, it needs to be initialized against the installed version of PostgreSQL. In this example, we'll be using the Ubuntu 22.04 default PostgreSQL 14 installation:
50+
51+
```
52+
cargo pgrx init --pg14 /usr/bin/pg_config
53+
```
54+
55+
#### Install the extension
56+
57+
Now that `pgrx` is initialized, you can compile and install the extension:
58+
59+
```
60+
cd pgml-extension && \
61+
cargo pgrx package
62+
```
63+
64+
This will produce a number of artifacts in `target/release/pg14-pgml` which you can then copy to their respective folders in `/usr` using `sudo cp`. At the time of this writing, `pgrx` is working on a command that does this automatically, but it has not been released yet.
65+
66+
Once the files are copied to their respective folders in `/usr`, you need to make sure the `pgml` extension is loaded in `shared_preload_libraries`. We use shared memory to control model versioning and other cool things that make PostgresML "just work". In `/etc/postgresql/14/main/postgresql.conf`, change or add the following line:
67+
68+
```
69+
shared_preload_libraries = 'pgml'
70+
```
71+
72+
Restart Postgres for this change to take effect:
73+
74+
```
75+
sudo service postgresql restart
76+
```
77+
78+
#### Validate the installation
79+
80+
To make sure PostgresML is installed, you can create the extension in a database of your choice:
81+
82+
```
83+
postgresml=# CREATE EXTENSION pgml;
84+
INFO: Python version: 3.10.6 (main, Nov 2 2022, 18:53:38) [GCC 11.3.0]
85+
INFO: Scikit-learn 1.1.3, XGBoost 1.7.1, LightGBM 3.3.3, NumPy 1.23.5
86+
CREATE EXTENSION
87+
```
88+
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Pooler
2+
3+
A pooler is a piece of software that is placed in front of a PostgreSQL cluster in order to load balance client connections and minimize the load placed on the database servers. Clients connect to the pooler, which pretends to be a Postgres database, and the pooler in turn connects to Postgres servers and forward clients' requests in an efficient manner.
4+
5+
### Why use a pooler
6+
7+
Postgres is a process-based database server (as opposed to threads), and each client connection forks the primary process to operate in its own memory space. A fork is generally more expensive than a thread because of extra memory allocation and OS scheduling overhead, but with a properly configured pooler, Postgres achieves a high degree of concurrency at massive scale in production.
8+
9+
#### PostgresML considerations
10+
11+
PostgresML caches machine learning models in the connection process memory space. For XGBoost/LightGBM/Scikit-learn models, which are typically only a few MBs in size, this is not a major concern, but for LLMs like Llama2 and Mistral, which are tens of gigabytes, the system memory and GPU memory usage is considerable. In order to be able to run these models effectively in production, the usage of a pooler running in transaction mode is essential. A pooler will route thousands of clients to the same Postgres server connection, reusing the same cached model, allowing for high concurrency and efficient use of resources.
12+
13+
### Choosing a pooler
14+
15+
The PostgreSQL open source community has developed many poolers over the years: PgBouncer, Odyssey, and PgPool. Each one has its pros and cons, but most of them can scale a PostgresML server effectively. At PostgresML, we developed our own pooler called PgCat, which supports many enterprise-grade features not available elsewhere that we needed to provide a seamless experience using Postgres in production, like load balancing, failover and sharding.
16+
17+
This guide will use PgCat as the pooler of choice.
18+
19+
### Installation
20+
21+
If you have followed our [Self-hosting](./) guide, you can just install PgCat for Ubuntu 22.04 from our APT repository:
22+
23+
```bash
24+
sudo apt install -y pgcat
25+
```
26+
27+
If not, you can easily install it from source.
28+
29+
#### Compiling from source
30+
31+
Download the source code from Github:
32+
33+
```bash
34+
git clone https://github.com/postgresml/pgcat
35+
```
36+
37+
If you don't have it already, install the Rust compiler from rust-lang.org:
38+
39+
```bash
40+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
41+
```
42+
43+
Finally, compile PgCat in release mode and install it into your system folders:
44+
45+
<pre class="language-bash"><code class="lang-bash">cd pgcat &#x26;&#x26; \
46+
cargo build --release &#x26;&#x26; \
47+
<strong>sudo cp target/release/pgcat /usr/local/bin/pgcat &#x26;&#x26; \
48+
</strong>sudo cp pgcat.toml /etc/pgcat.toml.example
49+
</code></pre>
50+
51+
### Configuration
52+
53+
PgCat uses the TOML configuration language and, if installed from APT, will use the configuration file stored in `/etc/pgcat.toml`. If installed from source, you'll have to pass the configuration file path as an argument when launching.
54+
55+
This example will assume that you have a database called `postgresml` with a user `postgresml_user` already configured. You can create and use as many databases and users as you need. That being said, each database/user combination will be a separate connection pool in PgCat and create its own PostgreSQL server connections.
56+
57+
For a primary-only setup used to serve Large Language Models, the pooler configuration is pretty basic:
58+
59+
```toml
60+
[general]
61+
host = "0.0.0.0"
62+
port = 6432
63+
admin_username = "pgcat"
64+
admin_password = "<secure password>"
65+
server_lifetime = 86400000
66+
idle_timeout = 86400000
67+
68+
[pools.postgresml]
69+
pool_mode = "transaction"
70+
71+
[pools.postgresml.shards.0]
72+
servers = [
73+
["<primary hostname or IP address>", 5432, "primary"].
74+
]
75+
database = "postgresml"
76+
77+
[pools.postgresml.users.0]
78+
username = "postgresml_user"
79+
password = "<secure password>"
80+
pool_size = 1
81+
```
82+
83+
Important considerations here are the `pool_size` of only `1` which will create and maintain only one PostgreSQL connection loaded with the LLM. Both `idle_timeout` and `server_lifetime` settings are set to 24 hours, so every 24 hours a new PostgreSQL connection will be created and the old one closed. This may not be desirable since loading a LLM into the GPU can take several seconds. To avoid this, this value can be set to be arbitrarily large, e.g. 100 years. In that case, the connection will basically never be closed.
84+
85+
Having only one server connection is not mandatory. If your hardware allows to load more than one LLM into your GPUs, you can increase the `pool_size` to a larger value. Our dedicated databases currently support up to 256GB GPU-powered LLMs, so we allow considerably more connections than would be otherwise supported by say just a GeForce RTX 4080.
86+
87+
### Running the pooler
88+
89+
Once configured, the pooler is ready to go. If you installed it from our APT repository, you can just run:
90+
91+
```bash
92+
sudo service pgcat start
93+
```
94+
95+
If you compiled it from source, you can run it directly:
96+
97+
```
98+
pgcat /etc/pgcat.toml
99+
```
100+
101+
To validate that the pooler is running correctly, you can connect to it with `psql`:
102+
103+
```bash
104+
PGPASSWORD="<secure password>" psql \
105+
-h "127.0.0.1" \
106+
-p 6432 \
107+
-U postgresml_user \
108+
-d postgresml
109+
```
110+
111+
```
112+
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
113+
Type "help" for help.
114+
115+
postgresml=> SELECT pgml.version();
116+
version
117+
---------
118+
2.7.9
119+
(1 row)
120+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy