SlideShare uma empresa Scribd logo
1 de 34
Baixar para ler offline
© 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~

Mais conteúdo relacionado

Mais procurados

Out of the Box Replication in Postgres 9.4(pgconfsf)
Out of the Box Replication in Postgres 9.4(pgconfsf)Out of the Box Replication in Postgres 9.4(pgconfsf)
Out of the Box Replication in Postgres 9.4(pgconfsf)
Denish Patel
 
0628阙宏宇
0628阙宏宇0628阙宏宇
0628阙宏宇
zhu02
 
A Node.JS bag of goodies for analyzing Web Traffic
A Node.JS bag of goodies for analyzing Web TrafficA Node.JS bag of goodies for analyzing Web Traffic
A Node.JS bag of goodies for analyzing Web Traffic
Philip Tellis
 
Как Web-акселератор акселерирует ваш сайт / Александр Крижановский (Tempesta ...
Как Web-акселератор акселерирует ваш сайт / Александр Крижановский (Tempesta ...Как Web-акселератор акселерирует ваш сайт / Александр Крижановский (Tempesta ...
Как Web-акселератор акселерирует ваш сайт / Александр Крижановский (Tempesta ...
Ontico
 
CUDA by Example : CUDA C on Multiple GPUs : Notes
CUDA by Example : CUDA C on Multiple GPUs : NotesCUDA by Example : CUDA C on Multiple GPUs : Notes
CUDA by Example : CUDA C on Multiple GPUs : Notes
Subhajit Sahu
 

Mais procurados (20)

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

Destaque

Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres OpenRobert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
PostgresOpen
 
Academic Position in Quantum Chemistry of Electronically Excited States
Academic Position in Quantum Chemistry of Electronically Excited StatesAcademic Position in Quantum Chemistry of Electronically Excited States
Academic Position in Quantum Chemistry of Electronically Excited States
xrqtchemistry
 
David Keeney - SQL Database Server Requests from the Browser @ Postgres Open
David Keeney - SQL Database Server Requests from the Browser @ Postgres OpenDavid Keeney - SQL Database Server Requests from the Browser @ Postgres Open
David Keeney - SQL Database Server Requests from the Browser @ Postgres Open
PostgresOpen
 
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
Ryan Jarvinen Open Shift Talk @ Postgres Open 2013
PostgresOpen
 
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres OpenKeith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
Keith Fiske - When PostgreSQL Can't, You Can @ Postgres Open
PostgresOpen
 
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
Gurjeet Singh - How Postgres is Different From (Better Tha) Your RDBMS @ Post...
PostgresOpen
 
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres OpenKevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
Kevin Kempter - PostgreSQL Backup and Recovery Methods @ Postgres Open
PostgresOpen
 
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres OpenBruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
Bruce Momjian - Inside PostgreSQL Shared Memory @ Postgres Open
PostgresOpen
 
Resumen Ejecutivo . Teens 2010 castellano
Resumen Ejecutivo . Teens 2010 castellanoResumen Ejecutivo . Teens 2010 castellano
Resumen Ejecutivo . Teens 2010 castellano
Digital Pymes
 
Keith Paskett - Postgres on ZFS @ Postgres Open
Keith Paskett - Postgres on ZFS @ Postgres OpenKeith Paskett - Postgres on ZFS @ Postgres Open
Keith Paskett - Postgres on ZFS @ Postgres Open
PostgresOpen
 

Destaque (20)

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

Semelhante a Michael Paquier - Taking advantage of custom bgworkers @ Postgres Open

Going live with BommandBox and docker Into The Box 2018
Going live with BommandBox and docker Into The Box 2018Going live with BommandBox and docker Into The Box 2018
Going live with BommandBox and docker Into The Box 2018
Ortus Solutions, Corp
 
Plny12 galera-cluster-best-practices
Plny12 galera-cluster-best-practicesPlny12 galera-cluster-best-practices
Plny12 galera-cluster-best-practices
Dimas Prasetyo
 
Grabbing the PostgreSQL Elephant by the Trunk
Grabbing the PostgreSQL Elephant by the TrunkGrabbing the PostgreSQL Elephant by the Trunk
Grabbing the PostgreSQL Elephant by the Trunk
Harold Giménez
 
Out of the Box Replication in Postgres 9.4(PgConfUS)
Out of the Box Replication in Postgres 9.4(PgConfUS)Out of the Box Replication in Postgres 9.4(PgConfUS)
Out of the Box Replication in Postgres 9.4(PgConfUS)
Denish Patel
 

Semelhante a Michael Paquier - Taking advantage of custom bgworkers @ Postgres Open (20)

Practical Operation Automation with StackStorm
Practical Operation Automation with StackStormPractical Operation Automation with StackStorm
Practical Operation Automation with StackStorm
 
Overview of Postgres Utility Processes
Overview of Postgres Utility ProcessesOverview of Postgres Utility Processes
Overview of Postgres Utility Processes
 
Into The Box 2018 Going live with commandbox and docker
Into The Box 2018 Going live with commandbox and dockerInto The Box 2018 Going live with commandbox and docker
Into The Box 2018 Going live with commandbox and docker
 
Going live with BommandBox and docker Into The Box 2018
Going live with BommandBox and docker Into The Box 2018Going live with BommandBox and docker Into The Box 2018
Going live with BommandBox and docker Into The Box 2018
 
手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務
 
Plny12 galera-cluster-best-practices
Plny12 galera-cluster-best-practicesPlny12 galera-cluster-best-practices
Plny12 galera-cluster-best-practices
 
Grabbing the PostgreSQL Elephant by the Trunk
Grabbing the PostgreSQL Elephant by the TrunkGrabbing the PostgreSQL Elephant by the Trunk
Grabbing the PostgreSQL Elephant by the Trunk
 
Developing with the Go client for Apache Kafka
Developing with the Go client for Apache KafkaDeveloping with the Go client for Apache Kafka
Developing with the Go client for Apache Kafka
 
Real-Time Query for Data Guard
Real-Time Query for Data Guard Real-Time Query for Data Guard
Real-Time Query for Data Guard
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
EuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears TrainingEuroPython 2013 - Python3 TurboGears Training
EuroPython 2013 - Python3 TurboGears Training
 
What's New in spring-security-core 2.0
What's New in spring-security-core 2.0What's New in spring-security-core 2.0
What's New in spring-security-core 2.0
 
How the HotSpot and Graal JVMs execute Java Code
How the HotSpot and Graal JVMs execute Java CodeHow the HotSpot and Graal JVMs execute Java Code
How the HotSpot and Graal JVMs execute Java Code
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
Out of the box replication in postgres 9.4(pg confus)
Out of the box replication in postgres 9.4(pg confus)Out of the box replication in postgres 9.4(pg confus)
Out of the box replication in postgres 9.4(pg confus)
 
Out of the Box Replication in Postgres 9.4(PgConfUS)
Out of the Box Replication in Postgres 9.4(PgConfUS)Out of the Box Replication in Postgres 9.4(PgConfUS)
Out of the Box Replication in Postgres 9.4(PgConfUS)
 
Using NGINX as an Effective and Highly Available Content Cache
Using NGINX as an Effective and Highly Available Content CacheUsing NGINX as an Effective and Highly Available Content Cache
Using NGINX as an Effective and Highly Available Content Cache
 
Joget Workflow v6 Training Slides - 20 - Basic System Administration
Joget Workflow v6 Training Slides - 20 - Basic System AdministrationJoget Workflow v6 Training Slides - 20 - Basic System Administration
Joget Workflow v6 Training Slides - 20 - Basic System Administration
 
Mastering Grails 3 Plugins - G3 Summit 2016
Mastering Grails 3 Plugins - G3 Summit 2016Mastering Grails 3 Plugins - G3 Summit 2016
Mastering Grails 3 Plugins - G3 Summit 2016
 
Oracle to Postgres Migration - part 2
Oracle to Postgres Migration - part 2Oracle to Postgres Migration - part 2
Oracle to Postgres Migration - part 2
 

Mais de PostgresOpen

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

Mais de PostgresOpen (10)

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

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

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~