Skip to content
no0p edited this page Oct 16, 2014 · 3 revisions

Pgsampler is an open source background worker. Collaboration is welcome.

This short document is an overview of the structure of the background worker.

Overview

Pgsampler is a big loop. Each time it loops it executes functions which collect metrics. The metrics it collects depend on the relevant <a href="https://github.com/no0p/pgsampler/wiki/Sampling-Rates>sampling rate variable. It outputs the collected data to either a csv file or a socket connection depending on the output mode configuration.

Periodically the background worker will stop itself, relying on the postmaster for automatic restart. This is due to the limitation of a single SPI connection per background worker process life cycle.

Starting up

The start up process is located in pgsampler.c. This is where GUCs and signal handlers are registered, and some small state data is placed in shared memory.

One key piece of information stored in shared memory is pgsampler_state->next_db which stores the database the background worker will connect to the next time it starts.

Also in the startup process is a call to ensure_valid_environment() which ensures that it is safe for the background worker to run. Is it a supported Postgresql version? Can files be written to safely? Can the network destination be resolved? Etc. If these are not true, the background worker will exist and not restart.

Main loop

The main looping takes place in pgsampler.c. For each cycle, the program calls the collect_and_send_metrics function in lib/control_loop.c. This function executes command functions located in /lib/commands/ depending on whether they are due to be collected.

Executing a command

The command functions all return a string in the current local memory context. This string data can be sent directly to a network receiver or is parsed into csv format and written to a file.

Commands tend to fall into two categories: those that collect data from relations and those that call C functions.

SQL commands typically create a string representing the query, and pass this to exec_to_command(). For more information on creating custom commands please see the custom metric guide.

Results of commands

The results are then passed to a function appropriate for the output_mode. These functions are implemented in /lib/output. This ultimately will write the data to a file or a socket.

Building

The bgworker uses the standard global make file.

Clone this wiki locally