Skip to content

Commit fa7c32a

Browse files
committed
docs: add postgresql and vs code setup pages with example scripts/files
1 parent 4dd809b commit fa7c32a

File tree

13 files changed

+855
-9
lines changed

13 files changed

+855
-9
lines changed

docs/dev_scripts.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Development scripts
2+
3+
Most development operations can be effectively optimized using shell scripts. This page describes various scripts that can help in database development and debugging.
4+
5+
During development/debugging you will be working with specific database version and installation, because you do not want to mess everything up with different versions.
6+
7+
We will use the following architecture:
8+
9+
- `./` - project root with all source files
10+
- `./build` - binary installation directory: `bin`, `share`, `lib`, `include`
11+
- `./data` - directory with database installation (`PGDATA`)
12+
- `./scripts` - special development scripts (this section describes them)
13+
- `build.sh` - running `configure` script and building database
14+
- `run.sh` - managing database installation: create, start, stop, etc...
15+
- `clean.sh` - cleaning files: database, build artifacts, etc...
16+
- `env.sh` - generated file with exported environmental variables
17+
18+
Each file contains it's own `--help` flag which show available options and usage examples.
19+
20+
> Scripts can be found in [GitHub repository](https://github.com/ashenBlade/postgres-dev-helper/tree/master/misc/scripts).
21+
22+
## `build.sh`
23+
24+
In previous section we discussed how the database is compiled, so this script will do that - run `configure` and build database.
25+
26+
But that's not all. Usually you will work with single server, no multi-server cluster. So it would be more convenient to just run `pg_ctl start` or `initdb` that will work with the specified installation or just type `psql` to quickly connect to server.
27+
28+
So, after `configure` script has executed we will create env file, that will store PostgreSQL special environmental variables.
29+
30+
Taking into an account default values for database/user/password/etc... we can generate env file in the following way:
31+
32+
```sh
33+
PGINSTDIR="$(pwd)/build"
34+
./configure --prefix="$PGINSTDIR" # ...
35+
36+
# shell env file
37+
SHENVFILE="$(pwd)/scripts/env.sh"
38+
cat <<EOF >"$SHENVFILE"
39+
export PGINSTDIR="$PGINSTDIR"
40+
export PGDATA="$(pwd)/data"
41+
export PGHOST="localhost"
42+
export PGPORT="5432"
43+
export PGUSER="postgres"
44+
export PGDATABASE="postgres"
45+
export PATH="$PGINSTDIR/bin:\$PATH"
46+
LD_LIBRARY_PATH="\${LD_LIBRARY_PATH:-''}"
47+
export LD_LIBRARY_PATH="\$PGINSTDIR/lib:\$LD_LIBRARY_PATH"
48+
EOF
49+
```
50+
51+
Using this file you can just source it and then use all binaries inside database installation path that will work with specific database installation.
52+
53+
```bash
54+
source ./scripts/env.sh
55+
56+
# These commands use binaries under "./build/bin"
57+
# and work with database in "./data"
58+
initdb
59+
pg_ctl start
60+
```
61+
62+
## `run.sh`
63+
64+
There are 4 main operations with database:
65+
66+
- create
67+
- start
68+
- connect using psql
69+
- stop
70+
71+
Script `run.sh` will support all these operations and will be just a wrapper around: `initdb`, `pg_ctl` and `psql`.
72+
73+
## `clean.sh`
74+
75+
As the name implies, this script is responsible for cleaning.
76+
77+
It cleans:
78+
79+
- build artifacts
80+
- installation files
81+
- created database
82+
83+
There is no magic in this file.
23.9 KB
Loading

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This is a VS Code extension to assist PostgreSQL Hackers - source code developer
55
## Table of contents
66

77
- [Configuration](./configuration.md)
8+
- [PostgreSQL setup](./postgresql_setup.md)
9+
- [Development scripts](./dev_scripts.md)
810
- [VS Code setup](./vscode_setup.md)
911
- Tutorials:
1012
- [Create extension](./create_extension.md)

docs/postgresql_setup.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# PostgreSQL setup
2+
3+
## Building from source code
4+
5+
PostgreSQL build is 2 staged: run `configure` script and then `make`.
6+
7+
### `configure`
8+
9+
PostgreSQL uses autoconf to setup special `pg_config.h` header file. It contains lots of macros describing target environment, compiler capabilities, etc...
10+
11+
To create this file you first run `./configure` script and pass various flags.
12+
The set of flags differs between versions, but for development we can outline required:
13+
14+
```bash
15+
$ ./configure --prefix=$(pwd)/build \
16+
--enable-debug \
17+
--enable-cassert \
18+
--enable-tap-tests \
19+
--enable-depend \
20+
CFLAGS="-O0 -g3"
21+
```
22+
23+
What we used:
24+
25+
- `--prefix=$(pwd)/build` - tells to install all binaries inside current working directory, thus you can safely work with multiple versions of PostgreSQL without disrupting the work of others
26+
- `--enable-debug` - add debug symbols to result binary. This is required for debugging and variable exploring.
27+
- `--enable-cassert` - enable `Assert` macros that check state consistency. Otherwise it is easy to violate internal state.
28+
- `--enable-tap-tests` - enable using of TAP-tests using Perl. For this you may need to install Perl on your system.
29+
- `--enable-depend` - enable tracking of changed files in your system, so you will rebuild only required files, without rebuilding full project.
30+
- `CFLAGS="-O0 -g3"` - tell the compiler to:
31+
- `-O0` - use lowest optimization level (so we can see all variables)
32+
- `-g3` - include as much debug symbols as we can
33+
34+
> Prefer using `-g3` level if you are using PostgreSQL Hacker Helper extension, because it allows to use macro definitions and other features that can significantly improve debug experience.
35+
36+
### `make`
37+
38+
The next step is actual building the sources. This is done much easier - just run `make`.
39+
40+
To speed up compilation you can provide number of parallel threads by `-j [THREADS]` flag (or omit to use all available - `make -j`).
41+
42+
When the building is done run `make install`, so all binaries will be installed in `build` directory (directory that we specified as `configure` step).

docs/vscode_setup.md

Lines changed: 187 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Here is shown VS Code setup for PostgreSQL debugging.
88

99
This is the main extension we are talking about. It significantly simplifies development and debugging of source code.
1010

11-
[Link](https://marketplace.visualstudio.com/items?itemName=ash-blade.postgresql-hacker-helper).
11+
[Link to the extension](https://marketplace.visualstudio.com/items?itemName=ash-blade.postgresql-hacker-helper).
1212

1313
This is the only extension I recommend installing, because there are no alternatives to it.
1414
For the further extensions you are free to choose that suit you - no restrictions, just suggestions.
@@ -56,6 +56,8 @@ Moreover, you may have multiple different versions of PostgreSQL installed on yo
5656

5757
## `launch.json`
5858

59+
> [Link to file on GitHub](https://github.com/ashenBlade/postgres-dev-helper/tree/master/misc/vscode/launch.json)
60+
5961
File `.vscode/launch.json` describes debug session configuration: name, debugger, path to binary/pid to attach, launch args, etc...
6062
When we are talking about PostgreSQL you should remember that it has multi-process architecture, not multi-threaded, this defines how we start debugging.
6163
Next, typical configurations for different uses cases will be presented.
@@ -113,7 +115,7 @@ They are separate binaries, so you can launch them directly, but usually they in
113115

114116
We can pass it directly using flags, but a better idea would be to use environment variables, because different binaries can use different flags.
115117

116-
All frontend utilities are located in own `src/bin/UTILITY_NAME` directory and after building each directory contains it's binary.
118+
After installation all frontend utilities will be located in `PGINSTDIR` - directory specified during database setup, so all you have to do is just pick required binary in it.
117119

118120
For example configuration for `pg_ctl` would be:
119121

@@ -125,7 +127,7 @@ For example configuration for `pg_ctl` would be:
125127
"name": "pg_ctl",
126128
"type": "cppdbg",
127129
"request": "launch",
128-
"program": "${workspaceFolder}/src/bin/pg_ctl/pg_ctl",
130+
"program": "${workspaceFolder}/build/bin/pg_ctl",
129131
"cwd": "${workspaceFolder}",
130132
"args": [
131133
"status"
@@ -145,12 +147,188 @@ Here we are debugging `pg_ctl status` command (see `"args"`) and pass `PGDATA` e
145147

146148
The value of it can be any, but in the example I suppose that for development purposes your installation in `data` directory in the repository itself.
147149

148-
> A better idea than passing environment variables would be to pass environmental variable *file*.
149-
> It have 2 benefits against manual specifying:
150-
>
151-
> 1. This file can be automatically generated during database creation
152-
> 2. If you have configuration for multiple binaries, then you do not have to enter the same parameters - just pass this env file.
150+
A better idea than passing environment variables would be to pass environmental variable *file*.
151+
It have 2 benefits against manual specifying:
152+
153+
1. This file can be automatically generated during database creation
154+
2. If you have configuration for multiple binaries, then you do not have to enter the same parameters - just pass this env file.
155+
156+
On [Development scripts](./dev_scripts.md#buildsh) page are shown useful development scripts, one of them (`build.sh`) will create special `.env` file, which contains all generated environment variables, so you can just specify this file:
157+
158+
```json
159+
{
160+
"version": "0.2.0",
161+
"configurations": [
162+
{
163+
"name": "pg_ctl",
164+
"type": "cppdbg",
165+
"request": "launch",
166+
"program": "${workspaceFolder}/build/bin/pg_ctl",
167+
"cwd": "${workspaceFolder}",
168+
"args": [
169+
"status"
170+
],
171+
"envFile": "${workspaceFolder}/scripts/.env"
172+
}
173+
]
174+
}
175+
```
176+
177+
### CoreDump
178+
179+
Sometimes SEGFAULT happens or `Assert` test fails. In such cases a coredump will be created (you can enable them using `ulimit -c unlimited` shell command from root).
180+
181+
C/C++ extension has special configuration for debugging CoreDump:
182+
183+
```json
184+
{
185+
"version": "0.2.0",
186+
"configurations": [
187+
{
188+
"name": "pg_ctl",
189+
"type": "cppdbg",
190+
"request": "launch",
191+
"program": "${workspaceFolder}/src/backend/postgres",
192+
"cwd": "${workspaceFolder}",
193+
"coreDumpPath": "${input:coreDumpPath}"
194+
}
195+
],
196+
"inputs": [
197+
{
198+
"id": "coreDumpPath",
199+
"type": "promptString",
200+
"description": "Enter path to CoreDump"
201+
}
202+
]
203+
}
204+
```
205+
206+
To start debugging core dump you have to enter path to core dump file in prompt.
207+
208+
> PostgreSQL Hacker Helper also works with core dumps.
153209
154210
## `tasks.json`
155211

156-
TODO
212+
> [Link to file on GitHub](https://github.com/ashenBlade/postgres-dev-helper/tree/master/misc/vscode/tasks.json)
213+
214+
Another useful functionality of VS Code are Tasks. In short, Task - is a named command, which can be any shell script.
215+
216+
The schema of task entry is very simple:
217+
218+
```json
219+
{
220+
"label": "Name of task",
221+
"detail": "Description of a task",
222+
"command": "path/to/command",
223+
"args": [
224+
"additional",
225+
"args",
226+
"to",
227+
"command"
228+
],
229+
}
230+
```
231+
232+
On [Development scripts](./dev_scripts.md) page we have defined some useful scripts and now it's time to integrate them into IDE.
233+
234+
This file can be very large, so here we define the most common and necessary.
235+
236+
### Build
237+
238+
To build we can use `build.sh` script. It accepts `--build` flag, that starts building.
239+
But it also accepts number of threads to use and we will use it.
240+
241+
```json
242+
{
243+
"version": "2.0.0",
244+
"tasks": [
245+
{
246+
"label": "Build",
247+
"command": "${workspaceFolder}/scripts/build.sh",
248+
"args": [
249+
"--build",
250+
"-j",
251+
"${input:threads}"
252+
],
253+
"detail": "Run build and install DB with all contribs",
254+
"group": {
255+
"kind": "build",
256+
"isDefault": true
257+
}
258+
}
259+
],
260+
"inputs": [
261+
{
262+
"id": "threads",
263+
"type": "pickString",
264+
"options": [ "1", "2", "3", "4", "5", "6", "7", "8" ],
265+
"default": "8",
266+
"description": "Number of threads to use"
267+
}
268+
]
269+
}
270+
```
271+
272+
As you can see this task runs our `build.sh` script and passes number of threads to it.
273+
And number of threads is prompted interactively using quick-pick - you can see example on screenshot below.
274+
275+
![Quick pick with number of threads](./img/vscode_setup/build_task_threads_number.png)
276+
277+
More than that, we also added a `"group"` member. What did it give us? Now we can run build just by using shortcut `Ctrl + Shift + B`. And that's it - build is running!
278+
279+
### Running PSQL
280+
281+
Next step after compilation is a database creation and connecting to it. For this task we have `run.sh` script which manages database instance.
282+
283+
It may seem annoying to constantly run the same task sequence: `Init database` -> `Run database` -> `Run PSQL`. So we will combine all 3 commands into single task.
284+
285+
```json
286+
{
287+
"label": "Run psql",
288+
"detail": "Run psql",
289+
"command": "${workspaceFolder}/scripts/run.sh",
290+
"args": [
291+
"--init-db",
292+
"--run-db",
293+
"--psql"
294+
],
295+
"isBackground": true,
296+
}
297+
```
298+
299+
Now just after database compilation has completed you just types `psql` in `Run task` prompt and clicks required task. That's it - you are running PSQL and can send queries!
300+
301+
### Start terminal in database environment
302+
303+
Complex tasks are not that simple, so sometimes you have to do everything manually. For such kind of work you must start a new terminal session using `env.sh` script defined earlier. It will setup environment variables in such way, so you will be working with compiled binaries and database instance.
304+
305+
```json
306+
{
307+
"label": "Run terminal",
308+
"detail": "Run terminal with imported environment variables specific to environment",
309+
"presentation": {
310+
"echo": true,
311+
"reveal": "always",
312+
"focus": true,
313+
"panel": "shared",
314+
"showReuseMessage": false,
315+
"clear": false
316+
},
317+
"command": "/usr/bin/sh",
318+
"args": [
319+
"-c",
320+
". ${workspaceFolder}/scripts/env.sh; $SHELL"
321+
],
322+
"isBackground": true
323+
},
324+
```
325+
326+
After launching this task a new shell with all PG env variables will be created.
327+
328+
### Other scripts
329+
330+
In `tasks.json` file you can find more predefined tasks for VS Code. They include:
331+
332+
- Running tests (using `Ctrl + Shift + T` shortcut)
333+
- Managing database instance
334+
- Cleaning files

0 commit comments

Comments
 (0)