You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
-`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:
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).
[Link to the extension](https://marketplace.visualstudio.com/items?itemName=ash-blade.postgresql-hacker-helper).
12
12
13
13
This is the only extension I recommend installing, because there are no alternatives to it.
14
14
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
56
56
57
57
## `launch.json`
58
58
59
+
> [Link to file on GitHub](https://github.com/ashenBlade/postgres-dev-helper/tree/master/misc/vscode/launch.json)
60
+
59
61
File `.vscode/launch.json` describes debug session configuration: name, debugger, path to binary/pid to attach, launch args, etc...
60
62
When we are talking about PostgreSQL you should remember that it has multi-process architecture, not multi-threaded, this defines how we start debugging.
61
63
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
113
115
114
116
We can pass it directly using flags, but a better idea would be to use environment variables, because different binaries can use different flags.
115
117
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.
117
119
118
120
For example configuration for `pg_ctl` would be:
119
121
@@ -125,7 +127,7 @@ For example configuration for `pg_ctl` would be:
@@ -145,12 +147,188 @@ Here we are debugging `pg_ctl status` command (see `"args"`) and pass `PGDATA` e
145
147
146
148
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.
147
149
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:
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
+

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:
0 commit comments