1. Oracle like Active Session History in PostgreSQL: Why?, How?
and Use cases
Bertrand Drouvot
Expert Database Engineer
bertrand.drouvot@digora.com
2. About me
2
• Oracle DBA since about 20 years (with attraction for opensource)
• OCP
• RAC certified Expert
• Exadata certified implementation specialist
• Kubernetes Administrator
• Blogger since 2012
• Oracle ACE
• Oak table member
• @bertranddrouvot
• BasketBall fan
5. Is it enough?
5
• Yes, if you don’t want to have an history of the session
activity
• Yes, if you don’t want to link sessions activity to queryid
• Yes, if you don’t want to see more than the top level SQL
(in case PL/pgSQL is used)
• Yes, if you don’t want to be able to diagnose historical
blocking activity in the past
6. If not enough? One solution
6
• Sample the pg_stat_activity view and store the
results in a table
• Not really efficient
• Still missing the link with pg_stat_statements
• Still showing only the top level sql
7. If still not enough? Another Solution: Write an extension
7
• PostgreSQL is designed to be easily extensible
• Extensions loaded into the database can function
just like features that are built in
• The contrib/ directory shipped with the source
code contains several extensions
• We can write our own extension
8. If still not enough? Write an extension and make use
of Hooks
8
• Interrupt, and modify the behaviour
• Not well known because not documented,
need to see some examples
9. If still not enough? Write an extension to get
9
A sampling of pg_stat_activity, providing more information:
10. If still not enough? Write an extension to get
10
A sampling of pg_stat_activity implemented as in-memory ring buffer
where samples are written with given (configurable) period. Therefore, user
can see some number of recent samples depending on history size
(configurable).
11. Let’s go through the pgsentinel one
11
• Implemented as a background worker
• That samples the data in a configurable way:
12. Let’s go through the pgsentinel one
12
• By executing a simple query
• Through SPI (Server Programming Interface)
13. Let’s go through the pgsentinel one
13
• Store the results into an array of structure