SlideShare a Scribd company logo
© 2013 VMware Inc. All rights reserved
Taking advantage of custom bgworkers
Postgres Open 2013, Chicago, 2013/09/16
Michael Paquier, Member of Technical Staff – PostgreSQL
2 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
About your lecturer
§  Michael Paquier
•  Working on Postgres for VMware
•  Postgres community hacker and blogger
•  Based in Tokyo
§  Contacts:
•  Twitter: @michaelpq
•  Blog: http://michael.otacoo.com
•  Email: mpaquier@vmware.com, michael@otacoo.com
3 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Agenda
§  Introduction to background workers
§  Implementation basics
§  Good habits and examples
§  What next?
4 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Introduction to background workers
5 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Bgworker?
§  Plug-in infrastructure introduced in PostgreSQL 9.3
§  Child process of postmaster
•  Similar to a normal backend
•  Control by postmaster, lives and dies with postmaster
•  Signal control centralized under postmaster
•  Assumed to run continuously as a daemon process
§  Run customized code
•  User-defined code of shared library loaded by PG core server
•  Code loaded at server start
§  Set of APIs for process-related plug-ins
•  Customizable
•  Extensible
•  Adaptable
•  Dangerous
6 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Features
§  Several options
•  Access to databases
•  Access to shared memory
•  Serial transactions
§  User-defined parameters
§  Some control for start/stop/restart of process
§  Not necessarily visible in pg_stat_*
§  Process listed with suffix bgworker: + $WORKER_NAME as name
$ ps -o pid= -o command= -p `pgrep -f ”worker name"`
$PID postgres: bgworker: worker name
7 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Development APIs
§  All in bgworker.h
§  Main management structure
§  Other functions
•  RegisterBackgroundWorker, register bgworker at load phase
•  BackgroundWorkerBlockSignals/BackgroundWorkerUnblockSignals
•  BackgroundWorkerInitializeConnection, connect to a wanted database
•  Only to catalogs if database name is NULL
typedef struct BackgroundWorker
{
char bgw_name[BGW_MAXLEN];
int bgw_flags;
BgWorkerStartTime bgw_start_time;
int bgw_restart_time;
bgworker_main_type bgw_main;
Datum bgw_main_arg;
} BackgroundWorker;
8 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Development APIs (2)
§  Flags - bgw_flags
•  BGWORKER_SHMEM_ACCESS
•  BGWORKER_BACKEND_DATABASE_CONNECTION
§  Start moment – bgw_start
•  BgWorkerStart_PostmasterStart
•  BgWorkerStart_ConsistentState
•  BgWorkerStart_RecoveryFinished
§  Restart time in seconds - bgw_restart_time
•  BGW_DEFAULT_RESTART_INTERVAL, 60s by default
•  BGW_NEVER_RESTART
•  Effective for *crashes*
§  Documentation
•  http://www.postgresql.org/docs/9.3/static/bgworker.html
9 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Implementation basics
10 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
“Hello World” class example
§  With most basic implementation
•  Print once “Hello World”, then exit
•  But this is not funny…
§  Instead => output “Hello World” every 10s to the server logs
$ tail -n3 pg_log/postgresql-*.log | grep "Hello"
Process: 12642, timestamp: 2013-08-19 12:50:32.159 JSTLOG: Hello World!
Process: 12642, timestamp: 2013-08-19 12:50:42.169 JSTLOG: Hello World!
Process: 12642, timestamp: 2013-08-19 12:50:52.172 JSTLOG: Hello World!
$ ps -o pid= -o command= -p `pgrep -f "hello world"`
12642 postgres: bgworker: hello world
LOG: registering background worker "hello world"
LOG: loaded library "hello_world"
11 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Example: Hello World (1)
§  Headers!
/* Minimum set of headers */
#include "postgres.h”
#include "postmaster/bgworker.h”
#include "storage/ipc.h”
#include "storage/latch.h”
#include "storage/proc.h”
#include "fmgr.h”
/* Essential for shared libs! */
PG_MODULE_MAGIC;
/* Entry point of library loading */
void _PG_init(void);
/* Signal handling */
static volatile sig_atomic_t got_sigterm = false;
12 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Example: Hello World (2)
§  Initialization with _PG_init()
void
_PG_init(void)
{
BackgroundWorker worker;
worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
worker.bgw_main = hello_main;
snprintf(worker.bgw_name, BGW_MAXLEN, "hello world");
worker.bgw_restart_time = BGW_NEVER_RESTART;
worker.bgw_main_arg = (Datum) 0;
RegisterBackgroundWorker(&worker);
}
13 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Example: Hello World (3)
§  Main loop
static void
hello_main(Datum main_arg)
{
pqsignal(SIGTERM, hello_sigterm);
BackgroundWorkerUnblockSignals();
while (!got_sigterm)
{
int rc;
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
10000L);
ResetLatch(&MyProc->procLatch);
if (rc & WL_POSTMASTER_DEATH)
proc_exit(1);
elog(LOG, "Hello World!"); /* Say Hello to the world */
}
proc_exit(0);
}
14 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Example: Hello World (4)
§  SIGTERM handler
§  Makefile
static void hello_sigterm(SIGNAL_ARGS)
{
int save_errno = errno;
got_sigterm = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
errno = save_errno;
}
MODULES = hello_world
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) –pgxs)
include $(PGXS)
15 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Example: Hello World – Conclusion
§  Good things
•  Postmaster death correctly managed
•  Management of SIGTERM
•  Use of a Latch
§  And not-so-good things
•  Avoid shared memory connection if possible
•  Might lead to memory corruption
•  Use a private latch
•  Avoid database connection if not necessary
•  Management of SIGHUP
16 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Just don’t forget that (1)
§  Consistency with existing backend code
•  Don’t reinvent the wheel!
§  Reload parameters
•  Handling of SIGHUP and ProcessConfigFile important!
•  Postmaster sends signal to workers, but workers should handle it properly
§  Test your code before putting it in production, especially if…
•  bgworker interacts with the OS/database
•  Access to shared memory used
§  Security
•  That’s C!
•  Door to security holes
•  Ports opened on a bgworker
•  Interactions with other components
•  Easy to break server…
17 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Just don’t forget that (2)
§  Use a private latch if possible
§  Limit access to shared memory
•  Flag BGWORKER_SHMEM_ACCESS
•  Don’t play with security
§  Limit access to database
•  Flag BGWORKER_SHMEM_ACCESS |
BGWORKER_BACKEND_DATABASE_CONNECTION
§  Do NOT use pg_usleep, does not stop at postmaster death
§  Load with _PG_init() and PG_MODULE_MAGIC to enable it!
§  Headers necessary to survive
#include "postgres.h”
#include "postmaster/bgworker.h”
#include "storage/ipc.h”
#include "fmgr.h”
18 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Just don’t forget that (3)
§  No physical limit of bgworkers allowed
•  MaxBackends calculated from number of registered workers
•  Lot of bgworkers = risk of OOM on standby
•  Be sure to not have an extravagant number of workers
•  Fixed in 9.4~ with max_worker_processes
§  Code loading
•  Set shared_preload_libraries in postgresql.conf
•  Entry point is _PG_init()
•  Register your worker
§  Set signal functions, then unblock signals
pqsignal(SIGHUP, my_worker_sighup);
pqsignal(SIGTERM, my_worker_sigterm);
BackgroundWorkerUnblockSignals();
19 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Just don’t forget that (4)
§  One last thing… Limitations for one-time tasks
•  Workers designed to always restart, like daemons
•  Possible to combine NEVER_RESTART with exit code != 0 for definite stop,
not that intuitive
•  Cannot start workers at will, always at server startup
•  When stopped like that, can never be restarted
20 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Good habits and examples
21 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
What should do a bgworker?
§  Like a daemon process
•  Interact with external components for an interval of time
•  Monitor activity inside and outside server
•  Check slave status (trigger an email if late on replay?)
§  Like a Postgres backend
•  Run transactions, queries and interact with databases
•  Receive, proceed signal
•  Proceed signals
•  Use existing infrastructure of server
•  Run statistics
•  Other things not listed here
22 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Custom parameters
§  Loaded in _PG_init
§  Advice for name convention
•  $WORKER_NAME.$PARAMETER_NAME
•  Not mandatory though… Feel free to mess up everything
void DefineCustomIntVariable(
const char *name,
const char *short_desc,
const char *long_desc,
int *valueAddr,
int bootValue,
int minValue,
int maxValue,
GucContext context,
int flags,
GucIntCheckHook check_hook,
GucIntAssignHook assign_hook,
GucShowHook show_hook);
§  Separate config file?
§  Same control granularity as
server
• APIs in guc.h
• Int, float, bool, enum, string
• Type: sighup, postmaster
23 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Timestamps
§  Timestamps in transactions
•  Set in postgres.c, not in worker code!
•  Calls to SetCurrentStatementStartTimestamp()
•  *Before* transaction start
•  And *Before* extra query execution
/* Start transaction */
SetCurrentStatementStartTimestamp()
StartTransactionCommand();
/* Run queries (not necessary for 1st one in transaction) */
SetCurrentStatementStartTimestamp()
[… Run queries …]
24 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Statistics
§  Mainly calls to pgstat_report_activity
•  STATE_RUNNING with query string before running query
•  STATE_IDLE when transaction commits
•  Activity mainly reported in pg_stat_activity
§  Advantage of reporting stats
•  Good for maintenance processes
•  Check if process is not stuck
•  For database processes only
§  When not necessary?
•  Utility workers (no direct interaction with server)
•  Cloud apps, users have no idea of what is running for them here
§  APIs of pgstat.h
25 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Transaction flow
§  All the APIs of xact.c
/* Start transaction */
SetCurrentStatementStartTimestamp()
StartTransactionCommand();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
/* Run queries */
SetCurrentStatementStartTimestamp()
pgstat_report_activity(STATE_RUNNING, $QUERY)
[… Run queries …]
/* Finish */
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
pgstat_report_activity(STATE_IDLE, NULL);
26 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Execute queries (1)
§  With SPI, common facility of all Postgres modules and core
§  Functions in executor/spi.h
•  SPI_connect to initialize facility
•  SPI_finish to clean up
•  SPI_execute to run query
•  SPI_getbinval to fetch tuple values
§  Prepared queries
•  SPI_prepare to prepare a query
•  SPI_execute_plan to execute this plan
•  etc.
§  Use and abuse of StringInfo and StringInfoData for query strings J
27 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Execute queries (2)
§  Common way of fetching tuple results
/* Execute query */
ret = SPI_execute(“SELECT intval, strval FROM table”,
true, 0);
if (ret != SPI_OK_SELECT)
elog(FATAL, ”Fatal hit…”);
/* Fetch data */
for (i = 0; i < SPI_processed; i++)
{
intValue = DatumGetInt32(
SPI_getbinval(SPI_tuptable->vals[i],
SPI_tuptable->tupdesc,
1, &isnull));
strValue = DatumGetCString(
SPI_getbinval(SPI_tuptable->vals[i],
SPI_tuptable->tupdesc,
2, &isnull));
}
28 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Example - kill automatically idle connections
§  Use of the following things
•  Custom parameters
•  Timestamps
•  Transaction
•  SPI calls
§  Query used by worker process
§  Interval customizable with parameter
•  Name: kill_idle.max_idle_time
•  Default: 5s, Max value: 1h
SELECT pid, datname, usename,
pg_terminate_backend(pid) as status
FROM pg_stat_activity
WHERE now() - state_change > interval ’$INTERVAL s' AND
pid != pg_backend_pid();
29 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Next example, cut automatically idle connections
§  Worker process
§  Disconnection activity in logs
§  Statistic activity
$ ps -o pid= -o command= -p `pgrep -f "kill_idle"`
23460 postgres: bgworker: kill_idle
$ tail -n 2 postgresql-*.log | grep Disconnected
LOG: Disconnected idle connection: PID 23564 mpaquier/mpaquier/none
LOG: Disconnected idle connection: PID 23584 postgres/mpaquier/none
postgres=# SELECT datname, usename, substring(query, 1, 38)
FROM pg_stat_activity WHERE pid = 23460;
datname | usename | substring
----------+----------+----------------------------------------
postgres | mpaquier | SELECT pid, pg_terminate_backend(pid)
(1 row)
30 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
More material?
§  Documentation
•  http://www.postgresql.org/docs/9.3/static/bgworker.html
§  Bgworker modules popping around
•  Mongres:
•  Get MongoDB queries and pass them to Postgres
•  https://github.com/umitanuki/mongres
•  contrib/worker_spi
•  All the basics in one module
•  9.4~ stuff also included on master
•  A future pg_cron?
•  Examples of today and more => pg_workers
•  https://github.com/michaelpq/pg_workers
•  kill_idle https://github.com/michaelpq/pg_workers/tree/master/kill_idle
•  hello_world https://github.com/michaelpq/pg_workers/tree/master/hello_world
•  Under PostgreSQL license
31 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
What next?
32 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Bgworkers, and now?
§  With stable 9.3 APIs, wide adoption expected
§  Many possibilities
•  Statistics-related processes
•  Maintenance, cron tasks
•  Reindex automatically invalid indexes
•  Kill inactive connections after certain duration (pg_stat_activity +
pg_terminate_backend) combo
§  HA agents, Pacemaker, Corosync, watchdogs
§  Health checker
•  Disk control: Stop server if free disk space <= X%
•  Automatic update of parameter depending on environment (cloud-related)
§  License checker: Ideal for Postgres server controlled in cloud?
33 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Bgworkers, and in core?
§  Dynamic bgworkers – new sets of APIs in 9.4~
•  Infrastructure for parallel query processing
•  Backward compatible with 9.3
•  Start/stop/restart at will
•  Main worker function loaded externally
•  No need of static loading
•  Not adapted for daemon processes
•  Dynamic area of shared memory for communication between backends
•  Parallel sequential scan
•  Parallel sort
•  Transaction snapshots
34 Copyright (c) 2013 VMware, Inc. All Rights Reserved.
Thanks!
Questions?
Other VMware sessions:
“My experience with embedded PostgresSQL”
Tuesday 2:30PM~
“A comparison of PostgreSQL encryption options”
Wednesday 1:30PM~

More Related Content

ODP
GUC Tutorial Package (9.0)
PDF
7 Ways To Crash Postgres
ODP
Shootout at the PAAS Corral
ODP
Shootout at the AWS Corral
PDF
ApacheConNA 2015: What's new in Apache httpd 2.4
KEY
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PDF
Rails Caching Secrets from the Edge
PDF
Design & Performance - Steve Souders at Fastly Altitude 2015
GUC Tutorial Package (9.0)
7 Ways To Crash Postgres
Shootout at the PAAS Corral
Shootout at the AWS Corral
ApacheConNA 2015: What's new in Apache httpd 2.4
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
Rails Caching Secrets from the Edge
Design & Performance - Steve Souders at Fastly Altitude 2015

What's hot (20)

PDF
The Accidental DBA
PDF
Out of the box replication in postgres 9.4
PDF
Out of the Box Replication in Postgres 9.4(pgconfsf)
PPT
A web perf dashboard up & running in 90 minutes presentation
ODP
PPTX
Solving anything in VCL
PDF
vert.x 소개 및 개발 실습
PDF
ApacheConNA 2015: Apache httpd 2.4 Reverse Proxy
PDF
ApacheCon 2014 - What's New in Apache httpd 2.4
PPT
0628阙宏宇
PDF
A Node.JS bag of goodies for analyzing Web Traffic
PDF
Challenges when building high profile editorial sites
PDF
Backend, app e internet das coisas com NodeJS no Google Cloud Platform
KEY
Scaling Django
PDF
Choosing a Javascript Framework
ODP
Nagios Conference 2014 - Troy Lea - Monitoring VMware Virtualization Using vMA
PDF
[1C1]Service Workers
PDF
DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술
ODP
Как Web-акселератор акселерирует ваш сайт / Александр Крижановский (Tempesta ...
PDF
CUDA by Example : CUDA C on Multiple GPUs : Notes
The Accidental DBA
Out of the box replication in postgres 9.4
Out of the Box Replication in Postgres 9.4(pgconfsf)
A web perf dashboard up & running in 90 minutes presentation
Solving anything in VCL
vert.x 소개 및 개발 실습
ApacheConNA 2015: Apache httpd 2.4 Reverse Proxy
ApacheCon 2014 - What's New in Apache httpd 2.4
0628阙宏宇
A Node.JS bag of goodies for analyzing Web Traffic
Challenges when building high profile editorial sites
Backend, app e internet das coisas com NodeJS no Google Cloud Platform
Scaling Django
Choosing a Javascript Framework
Nagios Conference 2014 - Troy Lea - Monitoring VMware Virtualization Using vMA
[1C1]Service Workers
DEVIEW - 오픈소스를 활용한 분산아키텍처 구현기술
Как Web-акселератор акселерирует ваш сайт / Александр Крижановский (Tempesta ...
CUDA by Example : CUDA C on Multiple GPUs : Notes
Ad

Viewers also liked (20)

PDF
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
PDF
Academic Position in Quantum Chemistry of Electronically Excited States
DOCX
Cátedra de cultura ciudadana
PDF
Annual Report 13.14
PDF
Organización y funcionamiento - CEIP (2010)
PDF
Grow Your Business with Email Marketing Chelmsford
PDF
Harmonic drive hpn brochure
PPT
Presentacion del ceat 2005
PDF
HTML 5: The Future of the Web
PDF
Agua para unesco
PPT
Addressing gaps in clinically useful evidence on potential drug-drug interact...
PDF
FICHA TÉCNICA ELECTROESTIMULADOR COMPEX SP 4.0
PPTX
David Keeney - SQL Database Server Requests from the Browser @ Postgres Open
PDF
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
PDF
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
PDF
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
PDF
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
PDF
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
PDF
Resumen Ejecutivo . Teens 2010 castellano
PDF
Keith Paskett - Postgres on ZFS @ Postgres Open
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
Academic Position in Quantum Chemistry of Electronically Excited States
Cátedra de cultura ciudadana
Annual Report 13.14
Organización y funcionamiento - CEIP (2010)
Grow Your Business with Email Marketing Chelmsford
Harmonic drive hpn brochure
Presentacion del ceat 2005
HTML 5: The Future of the Web
Agua para unesco
Addressing gaps in clinically useful evidence on potential drug-drug interact...
FICHA TÉCNICA ELECTROESTIMULADOR COMPEX SP 4.0
David Keeney - SQL Database Server Requests from the Browser @ Postgres Open
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Resumen Ejecutivo . Teens 2010 castellano
Keith Paskett - Postgres on ZFS @ Postgres Open
Ad

Similar to Michael Paquier - Taking advantage of custom bgworkers @ Postgres Open (20)

PDF
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PDF
Gearman - Northeast PHP 2012
PDF
Gearman: A Job Server made for Scale
PDF
Introduction to PostgreSQL for System Administrators
PDF
he-dieu-hanh_david-mazieres_l18-virtual-machines - [cuuduongthancong.com].pdf
PDF
44CON 2013 - Hack the Gibson - Exploiting Supercomputers - John Fitzpatrick &...
PPTX
Postgresql Database Administration Basic - Day1
PDF
9.6_Course Material-Postgresql_002.pdf
PDF
The Linux Block Layer - Built for Fast Storage
PPTX
What you need to know for postgresql operation
PPT
1. Von Neumann + Booting Sequence + System Calls.ppt
PDF
Tips on High Performance Server Programming
PDF
Nine Circles of Inferno or Explaining the PostgreSQL Vacuum
PPT
Threads Advance in System Administration with Linux
PDF
PostgreSQL and RAM usage
PDF
Faster PHP apps using Queues and Workers
PDF
Trivadis TechEvent 2017 PostgreSQL für die (Orakel) DBA by Ludovico Caldara
PDF
Linux Process Manager The Internals Of Scheduling Interrupts And Signals John...
PPT
Linux introduction
PPTX
Gearman & PHP
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
Gearman - Northeast PHP 2012
Gearman: A Job Server made for Scale
Introduction to PostgreSQL for System Administrators
he-dieu-hanh_david-mazieres_l18-virtual-machines - [cuuduongthancong.com].pdf
44CON 2013 - Hack the Gibson - Exploiting Supercomputers - John Fitzpatrick &...
Postgresql Database Administration Basic - Day1
9.6_Course Material-Postgresql_002.pdf
The Linux Block Layer - Built for Fast Storage
What you need to know for postgresql operation
1. Von Neumann + Booting Sequence + System Calls.ppt
Tips on High Performance Server Programming
Nine Circles of Inferno or Explaining the PostgreSQL Vacuum
Threads Advance in System Administration with Linux
PostgreSQL and RAM usage
Faster PHP apps using Queues and Workers
Trivadis TechEvent 2017 PostgreSQL für die (Orakel) DBA by Ludovico Caldara
Linux Process Manager The Internals Of Scheduling Interrupts And Signals John...
Linux introduction
Gearman & PHP

More from PostgresOpen (10)

PDF
Craig Kerstiens - Scalable Uniques in Postgres @ Postgres Open
PDF
Henrietta Dombrovskaya - A New Approach to Resolve Object-Relational Impedanc...
PDF
Steve Singer - Managing PostgreSQL with Puppet @ Postgres Open
PDF
John Melesky - Federating Queries Using Postgres FDW @ Postgres Open
PDF
Koichi Suzuki - Postgres-XC Dynamic Cluster Management @ Postgres Open
PDF
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
PDF
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
PDF
Michael Bayer Introduction to SQLAlchemy @ Postgres Open
PDF
Robert Haas Query Planning Gone Wrong Presentation @ Postgres Open
PDF
Andrew Dunstan 9.3 JSON Presentation @ Postgres Open 2013
Craig Kerstiens - Scalable Uniques in Postgres @ Postgres Open
Henrietta Dombrovskaya - A New Approach to Resolve Object-Relational Impedanc...
Steve Singer - Managing PostgreSQL with Puppet @ Postgres Open
John Melesky - Federating Queries Using Postgres FDW @ Postgres Open
Koichi Suzuki - Postgres-XC Dynamic Cluster Management @ Postgres Open
Selena Deckelmann - Sane Schema Management with Alembic and SQLAlchemy @ Pos...
Kevin Kempter PostgreSQL Backup and Recovery Methods @ Postgres Open
Michael Bayer Introduction to SQLAlchemy @ Postgres Open
Robert Haas Query Planning Gone Wrong Presentation @ Postgres Open
Andrew Dunstan 9.3 JSON Presentation @ Postgres Open 2013

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Cloud computing and distributed systems.
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Machine learning based COVID-19 study performance prediction
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Network Security Unit 5.pdf for BCA BBA.
Programs and apps: productivity, graphics, security and other tools
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Cloud computing and distributed systems.
Chapter 3 Spatial Domain Image Processing.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology
Unlocking AI with Model Context Protocol (MCP)
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation_ Review paper, used for researhc scholars
sap open course for s4hana steps from ECC to s4
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Weekly Chronicles - August'25 Week I
The AUB Centre for AI in Media Proposal.docx
Spectral efficient network and resource selection model in 5G networks
Machine learning based COVID-19 study performance prediction
Diabetes mellitus diagnosis method based random forest with bat algorithm
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Michael Paquier - Taking advantage of custom bgworkers @ Postgres Open

  • 1. © 2013 VMware Inc. All rights reserved Taking advantage of custom bgworkers Postgres Open 2013, Chicago, 2013/09/16 Michael Paquier, Member of Technical Staff – PostgreSQL
  • 2. 2 Copyright (c) 2013 VMware, Inc. All Rights Reserved. About your lecturer §  Michael Paquier •  Working on Postgres for VMware •  Postgres community hacker and blogger •  Based in Tokyo §  Contacts: •  Twitter: @michaelpq •  Blog: http://michael.otacoo.com •  Email: mpaquier@vmware.com, michael@otacoo.com
  • 3. 3 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Agenda §  Introduction to background workers §  Implementation basics §  Good habits and examples §  What next?
  • 4. 4 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Introduction to background workers
  • 5. 5 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Bgworker? §  Plug-in infrastructure introduced in PostgreSQL 9.3 §  Child process of postmaster •  Similar to a normal backend •  Control by postmaster, lives and dies with postmaster •  Signal control centralized under postmaster •  Assumed to run continuously as a daemon process §  Run customized code •  User-defined code of shared library loaded by PG core server •  Code loaded at server start §  Set of APIs for process-related plug-ins •  Customizable •  Extensible •  Adaptable •  Dangerous
  • 6. 6 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Features §  Several options •  Access to databases •  Access to shared memory •  Serial transactions §  User-defined parameters §  Some control for start/stop/restart of process §  Not necessarily visible in pg_stat_* §  Process listed with suffix bgworker: + $WORKER_NAME as name $ ps -o pid= -o command= -p `pgrep -f ”worker name"` $PID postgres: bgworker: worker name
  • 7. 7 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Development APIs §  All in bgworker.h §  Main management structure §  Other functions •  RegisterBackgroundWorker, register bgworker at load phase •  BackgroundWorkerBlockSignals/BackgroundWorkerUnblockSignals •  BackgroundWorkerInitializeConnection, connect to a wanted database •  Only to catalogs if database name is NULL typedef struct BackgroundWorker { char bgw_name[BGW_MAXLEN]; int bgw_flags; BgWorkerStartTime bgw_start_time; int bgw_restart_time; bgworker_main_type bgw_main; Datum bgw_main_arg; } BackgroundWorker;
  • 8. 8 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Development APIs (2) §  Flags - bgw_flags •  BGWORKER_SHMEM_ACCESS •  BGWORKER_BACKEND_DATABASE_CONNECTION §  Start moment – bgw_start •  BgWorkerStart_PostmasterStart •  BgWorkerStart_ConsistentState •  BgWorkerStart_RecoveryFinished §  Restart time in seconds - bgw_restart_time •  BGW_DEFAULT_RESTART_INTERVAL, 60s by default •  BGW_NEVER_RESTART •  Effective for *crashes* §  Documentation •  http://www.postgresql.org/docs/9.3/static/bgworker.html
  • 9. 9 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Implementation basics
  • 10. 10 Copyright (c) 2013 VMware, Inc. All Rights Reserved. “Hello World” class example §  With most basic implementation •  Print once “Hello World”, then exit •  But this is not funny… §  Instead => output “Hello World” every 10s to the server logs $ tail -n3 pg_log/postgresql-*.log | grep "Hello" Process: 12642, timestamp: 2013-08-19 12:50:32.159 JSTLOG: Hello World! Process: 12642, timestamp: 2013-08-19 12:50:42.169 JSTLOG: Hello World! Process: 12642, timestamp: 2013-08-19 12:50:52.172 JSTLOG: Hello World! $ ps -o pid= -o command= -p `pgrep -f "hello world"` 12642 postgres: bgworker: hello world LOG: registering background worker "hello world" LOG: loaded library "hello_world"
  • 11. 11 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Example: Hello World (1) §  Headers! /* Minimum set of headers */ #include "postgres.h” #include "postmaster/bgworker.h” #include "storage/ipc.h” #include "storage/latch.h” #include "storage/proc.h” #include "fmgr.h” /* Essential for shared libs! */ PG_MODULE_MAGIC; /* Entry point of library loading */ void _PG_init(void); /* Signal handling */ static volatile sig_atomic_t got_sigterm = false;
  • 12. 12 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Example: Hello World (2) §  Initialization with _PG_init() void _PG_init(void) { BackgroundWorker worker; worker.bgw_flags = BGWORKER_SHMEM_ACCESS; worker.bgw_start_time = BgWorkerStart_RecoveryFinished; worker.bgw_main = hello_main; snprintf(worker.bgw_name, BGW_MAXLEN, "hello world"); worker.bgw_restart_time = BGW_NEVER_RESTART; worker.bgw_main_arg = (Datum) 0; RegisterBackgroundWorker(&worker); }
  • 13. 13 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Example: Hello World (3) §  Main loop static void hello_main(Datum main_arg) { pqsignal(SIGTERM, hello_sigterm); BackgroundWorkerUnblockSignals(); while (!got_sigterm) { int rc; rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, 10000L); ResetLatch(&MyProc->procLatch); if (rc & WL_POSTMASTER_DEATH) proc_exit(1); elog(LOG, "Hello World!"); /* Say Hello to the world */ } proc_exit(0); }
  • 14. 14 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Example: Hello World (4) §  SIGTERM handler §  Makefile static void hello_sigterm(SIGNAL_ARGS) { int save_errno = errno; got_sigterm = true; if (MyProc) SetLatch(&MyProc->procLatch); errno = save_errno; } MODULES = hello_world PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) –pgxs) include $(PGXS)
  • 15. 15 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Example: Hello World – Conclusion §  Good things •  Postmaster death correctly managed •  Management of SIGTERM •  Use of a Latch §  And not-so-good things •  Avoid shared memory connection if possible •  Might lead to memory corruption •  Use a private latch •  Avoid database connection if not necessary •  Management of SIGHUP
  • 16. 16 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Just don’t forget that (1) §  Consistency with existing backend code •  Don’t reinvent the wheel! §  Reload parameters •  Handling of SIGHUP and ProcessConfigFile important! •  Postmaster sends signal to workers, but workers should handle it properly §  Test your code before putting it in production, especially if… •  bgworker interacts with the OS/database •  Access to shared memory used §  Security •  That’s C! •  Door to security holes •  Ports opened on a bgworker •  Interactions with other components •  Easy to break server…
  • 17. 17 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Just don’t forget that (2) §  Use a private latch if possible §  Limit access to shared memory •  Flag BGWORKER_SHMEM_ACCESS •  Don’t play with security §  Limit access to database •  Flag BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION §  Do NOT use pg_usleep, does not stop at postmaster death §  Load with _PG_init() and PG_MODULE_MAGIC to enable it! §  Headers necessary to survive #include "postgres.h” #include "postmaster/bgworker.h” #include "storage/ipc.h” #include "fmgr.h”
  • 18. 18 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Just don’t forget that (3) §  No physical limit of bgworkers allowed •  MaxBackends calculated from number of registered workers •  Lot of bgworkers = risk of OOM on standby •  Be sure to not have an extravagant number of workers •  Fixed in 9.4~ with max_worker_processes §  Code loading •  Set shared_preload_libraries in postgresql.conf •  Entry point is _PG_init() •  Register your worker §  Set signal functions, then unblock signals pqsignal(SIGHUP, my_worker_sighup); pqsignal(SIGTERM, my_worker_sigterm); BackgroundWorkerUnblockSignals();
  • 19. 19 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Just don’t forget that (4) §  One last thing… Limitations for one-time tasks •  Workers designed to always restart, like daemons •  Possible to combine NEVER_RESTART with exit code != 0 for definite stop, not that intuitive •  Cannot start workers at will, always at server startup •  When stopped like that, can never be restarted
  • 20. 20 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Good habits and examples
  • 21. 21 Copyright (c) 2013 VMware, Inc. All Rights Reserved. What should do a bgworker? §  Like a daemon process •  Interact with external components for an interval of time •  Monitor activity inside and outside server •  Check slave status (trigger an email if late on replay?) §  Like a Postgres backend •  Run transactions, queries and interact with databases •  Receive, proceed signal •  Proceed signals •  Use existing infrastructure of server •  Run statistics •  Other things not listed here
  • 22. 22 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Custom parameters §  Loaded in _PG_init §  Advice for name convention •  $WORKER_NAME.$PARAMETER_NAME •  Not mandatory though… Feel free to mess up everything void DefineCustomIntVariable( const char *name, const char *short_desc, const char *long_desc, int *valueAddr, int bootValue, int minValue, int maxValue, GucContext context, int flags, GucIntCheckHook check_hook, GucIntAssignHook assign_hook, GucShowHook show_hook); §  Separate config file? §  Same control granularity as server • APIs in guc.h • Int, float, bool, enum, string • Type: sighup, postmaster
  • 23. 23 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Timestamps §  Timestamps in transactions •  Set in postgres.c, not in worker code! •  Calls to SetCurrentStatementStartTimestamp() •  *Before* transaction start •  And *Before* extra query execution /* Start transaction */ SetCurrentStatementStartTimestamp() StartTransactionCommand(); /* Run queries (not necessary for 1st one in transaction) */ SetCurrentStatementStartTimestamp() [… Run queries …]
  • 24. 24 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Statistics §  Mainly calls to pgstat_report_activity •  STATE_RUNNING with query string before running query •  STATE_IDLE when transaction commits •  Activity mainly reported in pg_stat_activity §  Advantage of reporting stats •  Good for maintenance processes •  Check if process is not stuck •  For database processes only §  When not necessary? •  Utility workers (no direct interaction with server) •  Cloud apps, users have no idea of what is running for them here §  APIs of pgstat.h
  • 25. 25 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Transaction flow §  All the APIs of xact.c /* Start transaction */ SetCurrentStatementStartTimestamp() StartTransactionCommand(); SPI_connect(); PushActiveSnapshot(GetTransactionSnapshot()); /* Run queries */ SetCurrentStatementStartTimestamp() pgstat_report_activity(STATE_RUNNING, $QUERY) [… Run queries …] /* Finish */ SPI_finish(); PopActiveSnapshot(); CommitTransactionCommand(); pgstat_report_activity(STATE_IDLE, NULL);
  • 26. 26 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Execute queries (1) §  With SPI, common facility of all Postgres modules and core §  Functions in executor/spi.h •  SPI_connect to initialize facility •  SPI_finish to clean up •  SPI_execute to run query •  SPI_getbinval to fetch tuple values §  Prepared queries •  SPI_prepare to prepare a query •  SPI_execute_plan to execute this plan •  etc. §  Use and abuse of StringInfo and StringInfoData for query strings J
  • 27. 27 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Execute queries (2) §  Common way of fetching tuple results /* Execute query */ ret = SPI_execute(“SELECT intval, strval FROM table”, true, 0); if (ret != SPI_OK_SELECT) elog(FATAL, ”Fatal hit…”); /* Fetch data */ for (i = 0; i < SPI_processed; i++) { intValue = DatumGetInt32( SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull)); strValue = DatumGetCString( SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2, &isnull)); }
  • 28. 28 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Example - kill automatically idle connections §  Use of the following things •  Custom parameters •  Timestamps •  Transaction •  SPI calls §  Query used by worker process §  Interval customizable with parameter •  Name: kill_idle.max_idle_time •  Default: 5s, Max value: 1h SELECT pid, datname, usename, pg_terminate_backend(pid) as status FROM pg_stat_activity WHERE now() - state_change > interval ’$INTERVAL s' AND pid != pg_backend_pid();
  • 29. 29 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Next example, cut automatically idle connections §  Worker process §  Disconnection activity in logs §  Statistic activity $ ps -o pid= -o command= -p `pgrep -f "kill_idle"` 23460 postgres: bgworker: kill_idle $ tail -n 2 postgresql-*.log | grep Disconnected LOG: Disconnected idle connection: PID 23564 mpaquier/mpaquier/none LOG: Disconnected idle connection: PID 23584 postgres/mpaquier/none postgres=# SELECT datname, usename, substring(query, 1, 38) FROM pg_stat_activity WHERE pid = 23460; datname | usename | substring ----------+----------+---------------------------------------- postgres | mpaquier | SELECT pid, pg_terminate_backend(pid) (1 row)
  • 30. 30 Copyright (c) 2013 VMware, Inc. All Rights Reserved. More material? §  Documentation •  http://www.postgresql.org/docs/9.3/static/bgworker.html §  Bgworker modules popping around •  Mongres: •  Get MongoDB queries and pass them to Postgres •  https://github.com/umitanuki/mongres •  contrib/worker_spi •  All the basics in one module •  9.4~ stuff also included on master •  A future pg_cron? •  Examples of today and more => pg_workers •  https://github.com/michaelpq/pg_workers •  kill_idle https://github.com/michaelpq/pg_workers/tree/master/kill_idle •  hello_world https://github.com/michaelpq/pg_workers/tree/master/hello_world •  Under PostgreSQL license
  • 31. 31 Copyright (c) 2013 VMware, Inc. All Rights Reserved. What next?
  • 32. 32 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Bgworkers, and now? §  With stable 9.3 APIs, wide adoption expected §  Many possibilities •  Statistics-related processes •  Maintenance, cron tasks •  Reindex automatically invalid indexes •  Kill inactive connections after certain duration (pg_stat_activity + pg_terminate_backend) combo §  HA agents, Pacemaker, Corosync, watchdogs §  Health checker •  Disk control: Stop server if free disk space <= X% •  Automatic update of parameter depending on environment (cloud-related) §  License checker: Ideal for Postgres server controlled in cloud?
  • 33. 33 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Bgworkers, and in core? §  Dynamic bgworkers – new sets of APIs in 9.4~ •  Infrastructure for parallel query processing •  Backward compatible with 9.3 •  Start/stop/restart at will •  Main worker function loaded externally •  No need of static loading •  Not adapted for daemon processes •  Dynamic area of shared memory for communication between backends •  Parallel sequential scan •  Parallel sort •  Transaction snapshots
  • 34. 34 Copyright (c) 2013 VMware, Inc. All Rights Reserved. Thanks! Questions? Other VMware sessions: “My experience with embedded PostgresSQL” Tuesday 2:30PM~ “A comparison of PostgreSQL encryption options” Wednesday 1:30PM~