EPAS Essentials v15
EPAS Essentials v15
Course Agenda
• Introduction and Architectural • Creating and Managing Database
Overview Objects
• System Architecture • Database Security
• EDB Postgres Advanced Server • Monitoring and Admin Tools Overview
Installation • SQL Primer
• User Tools - Command Line Interfaces • Backup and Recovery
• Database Clusters • Routine Maintenance Tasks
• Database Configuration • Data Loading
• Data Dictionary • Data Replication and High Availability
• EDB Portfolio
• Facts about PostgreSQL and EDB Postgres Advanced Server
• Major Features
• EDB Postgres Advanced Server Database Features
• General Database Limits
• Common Database Object Names
• EDB continues to be committed • SQL compatible with Postgres, • SQL compatible with Oracle,
to advancing features in extended for stringent availability reduces effort to migrate
collaboration with the broader and advanced replication needs applications and data to
community • Transparent Data Encryption Postgres
• Formerly known as • Transparent Data Encryption
2ndQPostgres • Additional value-add enterprise
features
Replication Enhancements
EDB Postgres Extended Server
PostgreSQL
● Replication Enhancements
○ Enables EDB Postgres Distributed functionality such as:
■ Group Commit, Commit at Most Once, and Eager all-node synchronous replication
■ Timestamp-based Snapshots
■ Estimates for Replication Catch-up times
■ Selective Backup of a Single Database
■ Hold back freezing to assist resolution of UPDATE/DELETE conflicts
■ Multi-node PITR
■ Application Assessment
Oracle Compatibility ● Oracle Compatibility - Compatibility for schemas, data types, indexes, users, roles,
partitioning, packages, views, PL/SQL triggers, stored procedures, functions, and utilities
● Additional Security - Password policy management, session tag auditing, data redaction, SQL
Additional Security injection protection, and procedural language code obfuscation
● Developer Productivity - Over 200 pre-packaged utility functions, user-defined object types,
Developer Productivity autonomous transactions, nested tables, synonyms, advanced queueing
● DBA Productivity - Throttle CPU and I/O at the process level, over 55 extended catalog views
DBA Productivity to profile all the objects and processing that occurs in the database
Advanced Security ✔️ ✔️
Advanced SQL ✔️ ✔️
Advanced Performance ✔️ ✔️
Resource Manager ✔️ ✔️
Oracle Compatibility ✔️
• Reliable:
• ACID Compliant
• Supports Transactions and Savepoints
• Uses Write Ahead Logging (WAL)
• Scalable:
• Uses Multi-version Concurrency Control
• Table Partitioning and Tablespaces
• Parallel Sequential Scans, DDL(Table and Index Creation)
• Advanced:
• Supports Triggers, Functions and Procedures
• Supports Custom Procedural Languages
• Upgrade using pg_upgrade
• Unlogged Tables and Materialized Views
• Just-in-Time (JIT) Compilation
Row Tuple
Column Attribute
• EDB Portfolio
• Facts about PostgreSQL and EDB Postgres Advanced Server
• Major Features
• EDB Postgres Advanced Server Database Features
• General Database Limits
• Common Database Object Names
Shared Memory
Shared Buffers WAL Buffers Process Array
AUTOVACUUM WRITER
• WAL writer
• Flushes write-ahead log to disk
• Checkpointer
• Automatically performs a checkpoint based on config parameters
• Autovacuum launcher
• Starts Autovacuum workers as needed
• Autovacuum workers
• Recover free space for reuse
• Stats collector
• Collects usage statistics by relation and block
• Archiver
• Archives write-ahead log files
• Dbms_aq launcher
• Collects information for queueing functionality of advanced server
Shared Memory
25
25 © Copyright EnterpriseDB Corporation. All rights reserved.
User Backend Process
26
26 © Copyright EnterpriseDB Corporation. All rights reserved.
Respond to Client
called postgres
• Callback to client
work_mem
cache (shared_buffers)
reduces OS reads Shared (data) Buffers
Shared Memory
• Read the block once,
then examine it many
times in cache Stable Databases
• At checkpoint time
CHECKPOINT
Stable Databases
Transaction Log
• Before commit
• Uncommitted updates are in memory
• After commit
• WAL buffers are written to the disk (write-ahead log file) and shared
buffers are marked as committed
• After checkpoint
• Modified data pages are written from shared memory to the data files
• bin – Programs
• share – Shared data
• include – Header files
• lib or lib64 – Libraries
36 © Copyright EnterpriseDB Corporation. All rights reserved.
Database Cluster Data Directory Layout
Data
• File-per-table, file-per-index
• A table-space is a directory
• Each database that uses that table-space gets a subdirectory
• Each relation using that table-space/database combination gets one
or more files, in 1GB chunks
• Additional files are used to hold auxiliary information (free space
map, visibility map)
• Each file name is a number (see pg_class.relfilenode)
14300
14307
14405
Database OID
base
14498
14312
Data
pg_tblsc
16650
14299
Tablespace OID
/storage/pg_tab
14301
14307 16700
16701
16651
8K
Tuple
• Deployment Options
• OS User and Permissions
• Package Installation
• Installation of EDB Postgres Advanced Server
• Setting Environmental Variables
export PATH
export PGDATA=/var/lib/edb/as15/data/
Logoff and Login
export PGUSER=enterprisedb
export PGPORT=5444
export PGDATABASE=enterprisedb
• -d <Database Name>
• -h <Hostname>
• -p <Database Port>
• -U <Database Username>
Environmental Variables
• psql has its own set of commands, all of which start with
a backslash (\).
• Some commands accept a pattern. This pattern is a
modified regex. Key points:
• * and ? are wildcards
• Double-quotes are used to specify an exact name, ignoring all
special characters and preserving case
• Example:
=# \set AUTOCOMMIT off
• Once AUTOCOMMIT is set to off use COMMIT/ROLLBACK to complete the
running transaction
• \d[(i|s|t|v|b|S)][+] [pattern]
• List of objects (indexes, sequences, tables, views, tablespaces and
dictionaries)
• \d[+] [pattern]
• Describe structure details of an object
• \l[ist][+]
• Lists of databases in a database cluster
• \dn+ [pattern]
• Lists schemas (namespaces)
• + adds permissions and description to output
• \df[+] [pattern]
• Lists functions
• + adds owner, language, source code and description to
output
• You can also use yum package manager for installing EDB*Plus
• EnterpriseDB Repos must be installed prior to installing EDB*Plus
packages
• Login as root or sudo user and run:
• yum install -y edb-edbplus
• Example:
• Introduction to psql
• Connecting to Database
• psql Command Line Parameters
• psql Meta-Commands
• Conditional and Information Commands
• EDB*Plus
• Installing and Starting EDB*Plus
• EDB*Plus Commands
1. Connect to a database using psql 9. Do the same thing, just saving data, not the
2. Switch databases column headers
3. Describe the customers table 10. Create a script via another method, and
4. Describe the customers table including execute from psql
description 11. Turn on the expanded table formatting mode
5. List all databases 12. Lists tables, views and sequences with their
6. List all schemas associated access privileges
7. List all tablespaces 13. Which meta command displays the SQL text
8. Execute a sql statement, saving the output to for a function?
a file 14. View the current working directory
• Database Clusters
• Creating a Database Cluster
• Starting and Stopping the Server (pg_ctl)
• Connecting to the Server Using psql
• If the data directory is not specified, the environment variable PGDATA is used
Transparent Data Encryption (TDE) can be used Following initdb command options can be used
to encrypt data files, WAL and temporary files to enable TDE:
-y, --data-encryption
--copy-key-from=<file>
--key-wrap-command=<command>
--key-unwrap-command=<command>
--no-key-wrap
• Syntax:
$ pg_ctl stop [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-
MODE]
• Example: [enterprisedb@Base ~]$ pg_ctl -D /edbstore/ stop
waiting for server to shut down.... done
server stopped
• Database Clusters
• Creating a Database Cluster
• Starting and Stopping the Server (pg_ctl)
• Connecting to the Server Using psql
• One way to set these parameters is to edit the file postgresql.conf, which is normally
kept in the data directory
maintenance_ autovacuum
shared_buffers temp_buffers work_mem temp_file_limit
work_mem _work_mem
Amount of Amount of
Amount of Amount of
Size of shared memory used memory used Amount of disk
memory used memory used by
buffer pool for a caching sorting and space used for
for maintenance autovacuum
cluster temporary hashing temporary files
commands worker
tables operations
Server Session
logging_collector Enables logger process to capture stderr and csv logging messages
log_autovacuum_min_duration Logs any Autovacuum activity running for at least this long
log_disconnections Log some information each time a session disconnects, including the duration of the session
log_checkpoints Causes checkpoints and restart points to be logged in the server log
log_lock_waits Log information if a session is waits longer then deadlock_timeout to acquire a lock
log_error_verbosity How detailed the logged message is. Can be set to default, terse or verbose
log_line_prefix Additional details to log with each line. Default is '%m [%p] ‘ which logs a timestamp and the process ID
log_statement Legal values are none, ddl, mod (DDL and all other data-modifying statements), or all
• search_path - This parameter specifies the order in which schemas are searched.
The default value for this parameter is "$user", public
• default_tablespace - Name of the tablespace in which objects are created by
default
• temp_tablespaces - Tablespaces name(s) in which temporary objects are created
• statement_timeout - Postgres will abort any statement that takes over the
specified number of milliseconds A value of zero (the default) turns this off
• idle_in_transaction_session_timeout – Terminates any session with an open
transaction that has been idle for longer than the specified duration in
milliseconds
126 © Copyright EnterpriseDB Corporation. All rights reserved.
Parallel Query Scan Settings
• Advanced Server supports parallel execution of read-only queries
• Can be enabled and configured by using configuration parameters
• max_parallel_workers_per_gather (default 2): Enables parallel query scan
• parallel_tuple_cost (default 0.1): Estimated cost of transferring one tuple from a parallel worker
process to another process
• parallel_setup_cost (default 1000): Estimates cost of launching parallel worker processes
• min_parallel_table_scan_size (default 8MB): Sets minimum amount of table data that must be
scanned in order for a parallel scan
• min_parallel_index_scan_size (default 512 KB): Sets the minimum amount of index data that must
be scanned in order for a parallel scan
• force_parallel_mode (default off): Useful when testing parallel query scan even when there is no
performance benefit
• \dS in psql prompt will give you the list of pg_* tables and
views
• This list is from pg_catalog schema
pg_conf_load_time() pg_jit_available()
pg_ls_logdir() • Returns the name, size, and last modified time of each file in the log directory
pg_ls_waldir() • Returns the name, size, and last modified time of each file in the WAL directory
• Object Hierarchy
• Users and Roles
• Tablespaces
• Databases
• Access Control
• Creating Schemas
• Schema Search Path
Users/Groups
Database Tablespaces
(Roles)
Indexes
Tablespace A
Database Instance
Fast Storage
Transactional Tables
Historical Tables
Tablespace B
Slow Storage
Seldom Used Partition
PGDATA/global directory
Database Instance pg_global
Tablespace
Cluster-wide tables and catalog objects
PGDATA/base directory
pg_default
Tablespace
Databases, schemas and other objects
TABLESPACE command
• The tablespace directory must Directory(Database
pg_tblspc
Catalogue Version)
be existing with permissions
• Syntax: Database Directory
Symbolic
Link(Tablespace
for each Database
• CREATE TABLESPACE OID)
tablespace_name [ OWNER
user_name ]
LOCATION 'directory‘; Database
Objects(Files)
(1 row)
edb=# show temp_tablespaces;
temp_tablespaces
------------------
(1 row)
Note: If PATH is not set you can execute psql command from the bin directory
of postgres installation
• Cluster level
• Granted to a user during CREATE or later using ALTER USER
• These privileges are granted by superuser
• Object Level
• Granted to user using GRANT command
• These privileges allow a user to perform particular actions on a database
object, such as tables, views, or sequence
• Can be granted by owner, superuser or someone who has been given
permission to grant privileges (WITH GRANT OPTION)
Tables Views
USER
Domains
This statement will find the first employee table from the schemas listed in the search path
Database
Cluster
Owner
Users/Groups
Database Tablespaces
(Roles)
Event
Table View Sequence Functions
Triggers
• Object Hierarchy
• Users and Roles
• Tablespaces
• Databases
• Access Control
• Creating Schemas
• Schema Search Path
200
200 © Copyright EnterpriseDB Corporation. All rights reserved.
Data Security Requirements
• Access Control
Prevent
• Authentication and Authorization
• Data Control
• Views, Row Level Security,
Encryptions
• User/Password
Database • Connect Privilege
• Schema Permissions
Postmaster
Client
IP: 10.8.99.30
User: appuser1
• Syntax:
CREATE POLICY name ON table_name
[ AS { PERMISSIVE | RESTRICTIVE } ]
[ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
[ TO{ role_name | PUBLIC | CURRENT_USER | SESSION_USER}[,...] ]
[ USING ( using_expression ) ]
[ WITH CHECK ( check_expression ) ]
1. You are working as an EDB Postgres Advanced Server DBA. Your server
box has 2 network cards with ip addresses 192.168.30.10 and
10.4.2.10. 192.168.30.10 is used for the internal LAN and 10.4.2.10 is
used by the web server to connect users from an external network.
Your server should accept TCP/IP connections both from internal and
external users.
• Configure your server to accept connections from external and internal
networks.
2. Configure your server so that the new developer can connect from their
machine
Manage from one Optimize database Monitor system Integrate with other
interface performance health tools
One place to visualize In-depth diagnostics for Built-in dashboards and APIs and webhooks to
and manage everything database reports and customizable alert fetch data, send alerts,
tuning thresholds and manage servers
HTTPD
Monitoring Data
PEM Agent PEM Agent
PEM Storage
(Backend Database: pem) Monitoring Data PEM Agent
• Expand Databases
Schemas Tables
• Right-click on a table
• Select View Data
Filter/Sort Data
ALTER TABLE ALTER TABLE [IF EXISTS] [ONLY] name [*] action [,…]
Table: clients
Column: res_city
Data Type: city
274 © Copyright EnterpriseDB Corporation. All rights reserved.
Types of JOINS
Type Description
LEFT OUTER JOIN Returns all matching rows and rows from left-hand table even if there is no
corresponding row in the joined table
RIGHT OUTER JOIN Returns all matching rows and rows from right-hand table even if there is no
corresponding row in the joined table
FULL OUTER JOIN Returns all matching as well as not matching rows from both tables
CROSS JOIN Returns all rows of both tables with Cartesian product on number of rows
COSTS [ boolean ]
Loop etc.
SETTINGS [ boolean ]
• Join Type, Join Order BUFFERS [ boolean ]
TIMING [ boolean ]
SUMMARY [ boolean ]
• Example
postgres=# EXPLAIN SELECT * FROM emp;
QUERY PLAN
------------------------------------------------------
Seq Scan on emp (cost=0.00..1.14 rows=14 width=145)
Hash
GIN
GIST
SP-GiST Indexes
Index on Expressions
• Example:
EDB*Plus
EDB*Loader
EDB Postgres
Advanced
Server
EDB*Wrap
Oracle DBA
DRITA
• Table partitioning can be used to break one logically large table into
smaller physical pieces
• EDB Postgres Advanced Server supports Oracle compatible syntax for
table partitioning with support for:
• List Partitioning
• Range Partitioning
• Hash Partitioning
• Sub Partitioning
• Interval Partitioning
1. The staff in the HR department wants to hide some of the data in the
EMP table. They want a view called EMPVU based on the employee
numbers, employee names, and department numbers from the EMP
table. They want the heading for the employee name to be EMPLOYEE.
2. Confirm that the view works. Display the contents of the EMPVU view.
3. Using your EMPVU view, write a query for the SALES department to
display all employee names and department numbers.
1. You need a sequence that can be used with the primary key column of
the dept table. The sequence should start at 60 and have a maximum
value of 200. Have your sequence increment by 10. Name the
sequence dept_id_seq.
2. To test your sequence, write a script to insert two rows in the dept
table.
Logical Backups
Physical Backups
• The split command allows you to split the output into smaller files:
$ pg_dump dbname | split -b 1m - filename
-d <database name> - Connect to the specified database. Also restores to this database if -C option is omitted
-C - Create the database named in the dump file and restore directly into it
-v - Verbose option
• The database server must be shut down or in backup mode in order to get a usable backup
• File system backups only work for complete backup and restoration of an entire database
cluster
• Two types of File system backup
• Offline backups
• Online backups
Online Backups
• Modify postgresql.conf
wal_level = replica
archive_command = 'cp -i %p /users/enterprisedb/archive/%f‘
archive_mode = on
max_wal_senders = 3
wal_keep_size = 512
• Backup Command:
$ pg_basebackup [options] ..
• To create a base backup of the server at localhost and store it in the local directory
/usr/local/edb/backup
$ pg_basebackup -h localhost -D /usr/local/edb/backup
Postgres servers
• Standard connection to
Postgres for management,
coordination and monitoring
• Standard replication Barman
pg_basebackup and
pg_receivewal S3/Azure
http://docs.pgbarman.org/
325 © Copyright EnterpriseDB Corporation. All rights reserved.
Barman - Features
• Remote backup and restore with rsync and the PostgreSQL protocol
• Support for file level incremental backups with rsync
• Retention policy support
• WAL Archive Compression with gzip, bzip2, or pigz
• Backup data verification
• Backup with RPO=0 using a synchronous physical streaming
replication connection
• Rate limiting
https://www.pgbarman.org/about/
Feature comparison
Capability Added value Barman pgBackRest Pg_basebackup
• VACUUM FULL
• More aggressive algorithm compared to VACUUM
• Compacts tables by writing a complete new version of the table file with no dead space
• Takes more time
• Requires extra disk space for the new copy of the table, until the operation completes
• Direct
Log File
Parallel
Data File EDB*Loader
•
• Use the following command to invoke EBD*Loader from the command line:
edbldr [-d DBNAME] [-p PORT] [-c "CONNECTION_STRING"]
userid={dbuser[/dbpass]|/} direct={true|false} parallel={true|false}
control=control_file_name log=log_file_name
errors=num_errors
skip_index_maintenance={true|false}
skip=num_skips bad=bad_file_name parfile=par_file_name
freeze={true|false}
handle_conflicts={true|false}
1. Unload the emp table from the edbuser schema to a csv file, with column headers
2. Create a copyemp table with the same structure as the emp table
3. Load the csv file (from step 1) into the copyemp table
WAL stream
Reports
Primary database
Replica
database
• Synchronous Replication
• A 2-safe replication method offering zero data loss
• Transaction must apply changes to primary and
synchronously replicated replicas using two-phase commit
actions
• User gets a commit message after confirmation from both
primary and replica
• This will introduce a delay in committing transactions
-R option
• creates a default copy of standby.signal file
• Add primary server connection info to postgresql.auto.conf
max_standby_streaming_delay •Duration for which replica has to wait during query conflicts
wal_receiver_create_temp_slot •Authorize WAL receiver process to be able to create a temporary replication slot
• pg_stat_replication
• Show connected replicas and their status on the primary
• pg_stat_subscription
• Shows the status of subscription when using logical replication
• pg_stat_wal_receiver
• Shows the WAL receiver process status on Replica
• Recovery information functions:
• pg_is_in_recovery()
• pg_current_wal_lsn
• pg_last_wal_receive_lsn
• pg_last_xact_replay_timestamp()
• Execute:
=# SELECT * FROM pg_stat_replication;
• Find lag (bytes):
=# SELECT pg_wal_lsn_diff(sent_lsn, replay_lsn) FROM
pg_stat_replication;
• Find lag (seconds):
=# SELECT CASE WHEN pg_last_wal_receive_lsn() =
pg_last_wal_replay_lsn()
THEN 0 ELSE
EXTRACT (EPOCH FROM now() -pg_last_xact_replay_timestamp())
END AS stream_delay;
Multi-Master
Synchronous or Flexible Always-ON
Row Level
Asynchronous Deployment DDL Replication DDL and Row Filters
Consistency
Replication for Architectures
Postgres
Configurable Conflict-free
Database Rolling
Parallel Apply Auto Partitioning Column-level Replicated Data
Upgrades
Conflict Resolution Types (CRDTs)
Monitor health databases and Meet your SLAs by switching Switchover on demand to move
identify failures quickly over to the most recent standby the primary to standby for
maintenance
file
Replication
Streaming
4. Start EFM
5. Add nodes to EFM cluster Primary Replica - 2
EFM Agent
6. Monitor the EFM and database EFM Agent
servers Replica - 1
EFM Agent
Streaming Replication
Streaming Replication
Replica - 2
Replica - 1 Primary
[email protected]
www.enterprisedb.com