SlideShare ist ein Scribd-Unternehmen logo
1 von 93
Downloaden Sie, um offline zu lesen
PostgreSQL - Advanced Administration Training
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 1
Course Agenda
• Introduction
• System Architecture
• Installation and Database
Clusters
• Configuration
• Creating and Managing
Databases
• User Tools - CUI and GUI
• Security
• SQL Primer
• Backup, Recovery and PITR
• Routine Maintenance Tasks
• Data Dictionary
• Loading and Moving Data
• SQL Tuning
• Performance Tuning •
Streaming Replication •
Connection Pooling using
pgpool-II
• Table Partitioning
• Extensions
• Monitoring
• Upgrading Best Practices
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 2
Module 1
Introduction
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 3
Objectives
• In this module we will cover:
− PostgreSQL
− Features of PostgreSQL
− General Database Limits
− Common Database Object Names
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 4
Facts about PostgreSQL
• The world‟s most advanced open source
database • Designed for extensibility and
customization • ANSI/ISO compliant SQL support
• Actively developed for more than 20 years
− University Postgres (1986-1993)
− Postgres95 (1994-1995)
− PostgreSQL (1996-current)
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 5
PostgreSQL Community
• Community mailing lists
www.Postgresql.org/community/lists
• Support Forums
http://forums.enterprisedb.com/forums/list.page
• Commercial SLAs
www.enterprisedb.com/products-services-training/subscriptions
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 6
PostgreSQL Lineage
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 7
Major Features of PostgreSQL
• Portable:
− Written in ANSI C
− Supports Windows, Linux, Mac OS/X and major UNIX platforms • Reliable:
− ACID Compliant
− Supports Transactions
− Supports Save points
− Uses Write Ahead Logging (WAL)
• Scalable:
− Uses Multi-version Concurrency Control
− Supports Table Partitioning
− Supports Tablespaces
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 8
Major Features of PostgreSQL (continued)
• Secure:
− Employs Host-Based Access Control
− Provides Object-Level Permissions
− Supports Logging
− SSL 36
• Recovery and Availability:
− Streaming Replication
− Replication Slots
− Supports Hot-Backup, pg_basebackup
− Point-in-Time Recovery
• Advanced:
− Supports Triggers and Functions
− Supports Custom Procedural Languages PL/pgSQL, PL/Perl, PL/TCL,
PL/PHP, PL/Java
− Upgrade using pg_upgrade
− Unlogged Tables
− Materialized Views
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 9
Write Ahead Logging : REDO and Archiving
• PostgreSQL uses Write-Ahead Logging (WAL) as its approach to
transaction logging.
• WAL's central concept is that changes to data files (where tables and
indexes reside) must be written only after those changes have been logged,
that is, when log records describing the changes have been flushed to
permanent storage.
• No need to flush data pages to disk on every transaction commit.
• In the event of a crash we will be able to recover the database using the
logs
• This is roll-forward recovery, also
known as REDO
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 10
Rollback or Undo
• Dynamic allocation of disk space is used for the storage and processing of records within
tables.
• The files that represent the table grow as the table grows.
• It also grows with transactions that are performed against it.
• The data is stored within the file that represents the table.
• When deletes and updates are performed on a table, the file that represents the object
will contain the previous data
• This space gets reused, but need to force recovery of used space. (Using Vaccum)
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 11
General Database Limits
Limit
Maximum Database Size
Value
Unlimited
Maximum Table Size 32 TB
Maximum Row Size 1.6 TB
Maximum Field Size 1 GB
Maximum Rows per Table Unlimited
Maximum Columns per Table 250-1600 (Depending on Column types)
Maximum Indexes per Table Unlimited
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 12
Common Database Object Names
Industry Term
Table or Index
PostgreSQL Term
Relation
Row Tuple
Column Attribute
Data Block Page (when block is on disk)
Page Buffer (when block is in memory)
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 13
Summary
• In this module we covered:
− PostgreSQL
− Features of PostgreSQL
− General Database Limits
− Common Database Object Names
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 14
Module 2
System Architecture
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 15
Module Objectives
• Architectural Summary
• Process and Memory Architecture
• Utility Processes
• Connection Request-Response
• Disk Read Buffering Disk •
Write Buffering
• Background Writer Cleaning Scan
• Commit and Checkpoint
• Statement Processing
• Physical Database Architecture
• Data Directory Layout
• Installation Directory Layout
• Page Layout
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 16
Architectural Summary
• PostgreSQL uses processes, not threads •
Postmaster process acts as supervisor • Several
utility processes perform background work
− postmaster starts them, restarts them if they die
• One backend process per user session −
postmaster listens for new connections
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 17
Process and Memory Architecture
Postmaster Shared Memory
Shared Buffers WAL Buffers
Process Array
BGWRITER
CHECKPOINTER
AUTO --
VACUUM
WAL Writer
STATS
CO
LL
EC
TO
R
AR
CH
IV
ER
LOG WRITER
Data
Data
Files
Files
Error Log
Files
WAL
WAL
Segment
s Segments
Archived
Archive
d WAL
WAL
18
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 18
Utility Processes
• Background writer
− Writes updated data blocks to disk
• WAL writer
− Flushes write-ahead log to disk
• Checkpointer process
− Automatically performs a checkpoint based on config parameters
• Autovacuum launcher
− Starts Autovacuum workers as needed
• Autovacuum workers
− Recover free space for reuse
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 19
More Utility Process
• Logging collector
− Routes log messages to syslog, eventlog, or log files
• Stats collector
− Collects usage statistics by relation and block
• Archiver
− Archives write-ahead log files
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 20
Postmaster as Listener
• Postmaster is the master process called postgres
• Listens on 1-and-only-1 tcp port • Receives client connection request
21
Client request a
connection
Postmaster
Shared Memory
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 21
Disk Read Buffering
• PostgreSQL buffer cache
(shared_buffers) reduces
physical reads from
storage.
• Read the block once, then
examine it many times in
cache.
postgres
postgres
postgres
Shared (data) Buffers
Stable Database
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 22
Disk Write Buffering
• Blocks are written to disk
only when needed:
− To make room for new
blocks
− At checkpoint time
postgres
postgres
postgres Shared (data) Buffers
CHECKPOINT
Stable Database
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 23
Background Writer Cleaning Scan
• Background writer scan
attempts to ensure an
adequate supply of clean
buffers.
• bgwriter writes dirty blocks to
storage as needed.
postgres db2 get db cfg for sample |
grep -i log postgres
postgres Shared
(data) Buffers
BGWRITER
Stable Database
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 24
Write Ahead Logging
• Backend process that writes
data to Write Ahead Logging
(WAL) buffers.
postgres
postgres
postgres
• Flush WAL buffers
periodically (WAL
writer), on commit, or
when buffers are full.
• Group commit.
Shared (data) Buffers
Stable
Database
WAL
Buffer
Transaction
Log
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 25
Transaction Log Archiving
• Archiver spawns a task to
copy pg_xlog log files to
archive location when full.
postgres
postgres
postgres
WAL
ARCHIVE COMMAND
Shared (data)
Buffers
Stable Database
Buffer
Transaction
Log
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 26
Commit and Checkpoint
• Before Commit
− Uncommitted updates are in memory.
• After Commit
− Committed updates written from shared memory to WAL log file (on disk).
• After Checkpoint
− Modified data pages are written from shared memory to the data files.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 27
Statement Processing
• Check Syntax
• Call Traffic Cop
• Identify Query Type
Optimize
• Execute Query based on
• Command Processor if
needed
• Break Query in Tokens
Pars e
• Planner generate Plan •
Uses Database Statistics •
Query Cost Calculation •
Choose best plan
query plan
Execute
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 28
Database Cluster (Instance) Data Directory
Layout Data
Global Base pg_tblsc pg_xlog pg_log
Status
Directories
Configuration
FilesPostmaster Info Files
Cluster wide database
objects
Contains Databases
Symbolic link to tablespaces
Write ahead logs
Error logs
pg_clog, pg_multiexact, pg_snapshots, pg_stat, pg_subtrans,pg_notify,
pg_serial, pg_replslot, pg_logical, pg_dynshmem
postgresql.conf, pg_hba.conf, pg_ident.conf
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 29
Installation Directory Layout
• bin – Binary files
• include – Header files
• lib – Library files
• doc – Help files for different components, contrib modules and extensions
• share – Extensions and sample config files
• scripts – Some useful script files
• stackbuilder – installation directory for Stackbuilder
/opt/PostgresPlus/9.4AS
bin
include
lib
doc
share
scripts
stackbuilderplus
pg_env.sh,
uninstall-* files 30
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 30
Data File Storage Internals
• 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 tablespace/database combination gets one or
more files, in 1GB chunks.
• Additional files used to hold auxiliary information (free space map, visibility
map).
• Each file name is a number (see pg_class.relfilenode).
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 31
Free Space and Visibility Map Files
•Each Relation has a free space map
− Stores information about free space available in the relation − File named as
filenode number plus the suffix _fsm
•Tables also have a visibility map
− Track which pages are known to have no dead tuples − Stored in a fork with the
suffix _vm
32
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 32
Sample - Data Directory
Layout 14307
Database OID
Base
14312
Data
pg_tblsc
16650
/storage
Tablespace OID
14297 14300
14405 14498
14300 14301
/pg_tab
14307 16651
16700 16701
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 33
Lab Exercise
1. Write a command to display a list of all PostgreSQL related processes on
your operating system
2. Write a SQL statement to find the data file name and location for
„customers‟ table
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 34
Module Summary
• Architectural Summary
• Shared Memory
• Inter-processes Communication
• Statement Processing
• Utility Processes
• Disk Read Buffering Disk
• Write Buffering
• Background Writer Cleaning Scan
• Commit and Checkpoint •
Physical Database Architecture •
Data Directory Layout
• Installation Directory Layout
• Page Layout
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 35
Module 3
Installation
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 36
Module Objectives
• OS User and Permissions
• Installation Options
• Installation of PostgreSQL
• StackBuilder
• Setting Environmental Variables
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 37
OS User and Permissions
• PostgreSQL runs as a daemon (Unix / Linux) or service (Windows).
• Installation requires superuser/admin access.
• All PostgreSQL processes and data files must be owned by a user in the
OS.
− OS user is un-related to database user accounts.
− For security reasons, the OS user must not be root or an administrative account.
• SELinux Permissions
− SELinux must be set to permissive mode on systems with SELinux.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 38
Supported Platforms
• PostgreSQL is available for Windows, Linux and Solaris systems
• Windows:
− Windows Server 2008 R1 (32 and 64 bit)
− Windows Server 2008 R2 (64 bit)
− Windows 2012 (64-bit)
• Linux platforms:
− CentOS 6.x and 7.x (64-bit)
− Red Hat Enterprise Linux 6.x and 7.x (64-bit)
− Ubuntu 14.04 LTS
− SLES 11.x
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 39
Supported Locales
• PostgreSQL has been tested and certified for the following locales:
− en_US United States English
− zh_HK Traditional Chinese with Hong Kong SCS
− zh_TW Traditional Chinese for Taiwan
− zh_CN Simplified Chinese
− ja_JP Japanese
− ko_KR Korean
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 40
Text Mode Installation
• Run the installer in text mode:
# ./postgresql.9.4-linux.run –mode text
•Choose the language using a number[1-5]
Please select the installation language
[1] English - English
[2] Japanese - 日本語
[3] Simplified Chinese - 简体中文
[4] Traditional Chinese - 繁体中文
[5] Korean - 한국어
Please choose an option [1] :
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 41
Text Mode Installation Steps
• Installer will prompt for different steps:
− License Agreement
− User Authentication
− Installation Directory
− Components
− Data and WAL Directory
− Configuration Mode
− Database superuser password and Locale
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 42
StackBuilder
• Simplifies the process of downloading and installing modules
• StackBuilder requires Internet access
• Run stackbuilder using the StackBuilder menu option from the Postgresql
9.4 menu
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 43
Setting Environmental Variables
• Setting environment variables is very important for trouble free
startup/shutdown of the database server.
− PATH –should point correct bin directory.
− PGDATA –should point correct data cluster directory.
− PGPORT –should point correct port on which database cluster is running.
− Edit .bash_profile to set the variables
− In Windows set these variables using my computer properties page.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 44
Module Summary
• OS User and Permissions
• Installation Options
• Installation of PostgreSQL
• StackBuilder
• Setting Environmental Variables
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 45
Module 4
Database Clusters
© 2015 EnterpriseDB Corporation. All rights reserved. 46
Module Objectives
• Database Clusters
• Creating a Database Cluster
• Starting and Stopping the Server (pg_ctl)
• Connect to the Server Using psql
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 47
Database Clusters
• A Database Cluster is a collection of databases managed by a single
server instance.
• Database Clusters are comprised of:
− Data directory
− Port
• Default databases are created named:
− template0
− template1
− postgres
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 48
Creating a Database Cluster
• Choose the data directory location for new cluster.
• Initialize the database cluster storage area (data directory) using initdb
utility.
• initdb will create the data directory if it doesn‟t exist.
• You must have permissions on the parent directory so that initdb can
create the data directory.
• Data directory can be created manually using superuser access and
ownership can be given to postgres user.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 49
Example - initdb
• In
the above example database system will be owned by user “postgres.” •
postgres OS user must also own the server process.
• postgres user is the database superuser.
• The default server config file will be created in /edbstore named
postgresql.conf.
• -W is used to force initdb to prompt for the superuser password.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 50
Starting a Database Cluster
• Choose a unique port for postmaster
• Change the port in postgresql.conf
• pg_ctl utility can be used to start a cluster
• Syntax
− pg_ctl start [options]
− Options
-D location of the database storage area
-l write (or append) server log to FILENAME
-w wait until operation completes
-t seconds to wait when using -w option
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 51
Connecting to a Database Cluster
• The psql and pgAdmin-III clients can be used for connections
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 52
Reload a Database Cluster
• Some configuration parameter changes do not require a restart
• Changes can be reloaded using the pg_ctl utility • Changes
can also be reloaded using pg_reload_conf() • Syntax
pg_ctl reload [options]
-D location of the database storage area
-s only print errors, no informational messages
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 53
Stopping a Database Cluster
• pg_ctl can be used to stop a database cluster
• pg_ctl supports three modes of shutdown
− smartquit after all clients have disconnected
− Fast (default)quit directly, with proper shutdown (default)
− immediatequit without complete shutdown; will lead to recovery • Syntax
− pg_ctl stop [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 54
View Cluster Control Information
• pg_controldata can be used to view the control information for a
database cluster
• Syntax:
− pg_controldata [DATADIR]
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 55
Module Summary
• Database Clusters
• Creating a Database Cluster
• Starting and Stopping the Server (pg_ctl)
• Connect to the Server Using psql
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 56
Lab Exercise - 1
1. A new website is to be developed for an online music store. −
Create a new cluster edbdata with ownership of postgres user.
− Start your edbdata cluster
− Stop your edbdata cluster with fast mode.
− Reload your cluster with pg_ctl utility and using
pg_reload_conf() function.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 57
Module 5
Configuration
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 58
Objectives
• In this module we will cover:
− Server Configuration File
− Setting Server Parameters
− Connection Settings
− Security and Authentication Settings
− Memory Settings
− Query Planner Settings -> Performance tuning day4
− Log Management
− Background Writer Settings
− Statement Behavior -> rountine maintenance day3 −
Autovacuum Settings -> rountine maintenance day3
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 59
Server Configuration File
• There are many configuration parameters that effect the behavior of the
database system.
• Server config file postgresql.conf stores all the server
parameters. • All parameter names are case-insensitive.
• Some parameters require restart.
• Query to list of all parameters.
− SELECT name,setting FROM pg_settings;
• Query to list all parameters requiring server restart.
− SELECT name FROM pg_settings WHERE context = 'postmaster'; •
One way to set these parameters is to edit the file postgresql.conf,
which is normally kept in the data directory.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 60
Setting Server Parameters
• Some parameters can be changed per session using the SET command.
• Some parameters can be changed at the user level using ALTER USER.
• Some parameters can be changed at the database level using ALTER
DATABASE.
• The SHOW command can be used to see settings.
• pg_settings catalog table lists settings information.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 61
PostgreSQL ALTER SYSTEM
• ALTER SYSTEM command:
− Available starting with PostgreSQL 9.4
− Edits cluster settings without editing postgresql.conf
− Writes the setting to a file called postgresql.auto.conf
• Example:
− ALTER SYSTEM SET work_mem=20480;
• postgresql.auto.conf is always read last during server reload/restarts
• Reset a parameter change -
− ALTER SYSTEM SET work_mem = DEFAULT;
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 62
Connection Settings
• The following parameters in the postgresql.conf file control the connection
settings:
− listen_addresses (string)
− Specifies the TCP/IP address(es) on which the server is to listen for connections − port (integer)
− The TCP port the server listens on; 5432 by default
− max_connections (integer)
− Determines the maximum number of concurrent connections to the database server −
superuser_reserved_connections (integer)
− Determines the number of connection “slots” that are reserved for connections by Advanced Server
superusers
63
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 63
Memory Settings
• The following parameters in postgresql.conf control the memory settings:
− shared_buffers (integer)
− Sets the amount of memory the database server uses for shared memory buffers
− temp_buffers (integer)
− Sets the maximum number of temporary buffers used by each database session
− work_mem (default 4MB)
− Specifies the amount of memory to be used by internal sort operations and hash tables before
switching to temporary disk files
− autovacuum_work_mem
− Specifies the maximum amount of memory to be used by each autovacuum worker process
− maintenance_work_mem (default 64MB)
− Specifies the maximum amount of memory to be used in maintenance operations, such as
CREATE INDEX, and ALTER TABLE ADD FOREIGN KEY
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 64
Write Ahead Logs Settings
• wal_level (default: minimal): Determines how much information is written
to the WAL. Other values are archive, hot_standby and logical. • fsync
(default on): Turn this off to make your database much faster – and silently
cause arbitrary corruption in case of a system crash – limited applicability.
• wal_buffers (default: -1, autotune): The amount of memory used in shared
memory for WAL data. The default setting of -1 selects a size equal to
1/32nd (about 3%) of shared_buffers.
• checkpoint_segments (default 3): Maximum number of 16MB WAL file
segments between checkpoints. Default is too small!
• checkpoint_timeout (default 5 minutes): Maximum time between
checkpoints.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 65
Logging – Where to Log
• log_destination: Valid values are combinations of stderr, csvlog,
syslog, and eventlog.
• logging_collector: Enables advanced logging features. csvlog requires
logging_collector.
• log_directory: Directory where log files are written. Default pg_log.
• log_filename: Format of log file name (e.g. postgresql-%Y-%m-
%d_%H%M%S.log).
• log_rotation_age: Automatically rotate logs after this much time.
Requires logging_collector.
• log_rotation_size: Automatically rotate logs when they get this big.
Requires logging_collector.
66
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 66
Logging - When To Log
• client_min_messages (default NOTICE). Messages of this severity level
or above are sent to the client.
• log_min_messages (default WARNING). Messages of this severity level
or above are sent to the server.
• log_min_error_statement (default ERROR). When a message of this
severity or higher is written to the server log, the statement that caused it is
logged along with it.
• log_min_duration_statement (default -1, disabled): When a statement
runs for at least this long, it is written to the server log, with its duration.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 67
Logging - What To Log
• log_connections (default off): Log successful connections to the server log.
• log_disconnections (default off): Log some information each time a session disconnects,
including the duration of the session.
• log_error_verbosity (default “default”): Can also select “terse” or “verbose”.
• log_duration (default off): Log duration of each statement.
• log_line_prefix: Additional details to log with each line.
• log_statement (default none): Legal values are none, ddl, mod (DDL and all other data
modifying statements), or all.
• log_temp_files (default -1): Log temporary files of this size or larger, in kilobytes.
• log_checkpoints (default off): Causes checkpoints and restart points to be logged in the
server log.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 68
Lab Exercise
1. Open psql and write a statement to change work_mem to 10MB. This
change must persist across server restarts
2. Open psql and write a statement to change work_mem to 20MB for the
current session
3. Open psql and write a statement to change work_mem to 1 MB for the
postgres user
4. Write a query to list all parameters requiring a server restart
5. Take a backup of the postgresql.conf file
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 69
Lab Exercise
6. Open the configuration file for your Postgres database cluster and make
the following changes
− Maximum allowed connections to 50
− Authentication time to 10 mins
− Shared buffers to 256 MB
− work_mem to 10 MB
− wal_buffers to 8MB
7. Restart the server and verify the changes made in previous step
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 70
Summary
• In this module we covered:
− Setting PostgresPlus Advanced Server Parameters
− Access Control
− Connection Settings
− Security and Authentication Settings
− Memory Settings
− Query Planner Settings
− Log Management
− Background Writer Settings
− Statement Behaviour
− Autovacuum Settings
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 71
Module 6
PSQL
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 72
Objectives
• In this module we will cover:
− Client Interface psql
− Connecting to a Database
− psql Meta Commands
− Executing SQL Commands
− Advance Features
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 73
PSQL
• psql is the name of PostgreSQL PSQL's executable.
• It enables you to type in queries interactively, issue them to PostgreSQL,
and see the query results.
− psql [option...] [dbname [username]]
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 74
Connecting to a Database
• In order to connect to a database you need to know the name of your
target database, the host name and port number of the server and what
user name you want to connect as.
• Command line options, namely -d, -h, -p, and -U respectively.
• Environment variables PGDATABASE, PGHOST,
PGPORT and PGUSER to appropriate values.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 75
Conventions
• psql has it's own set of commands, all of which start with a backslash ().
These are in no way related to SQL commands, which operate in the
server. psql commands only affect psql.
• 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
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 76
On Startup...
• psql will execute commands from $HOME/.psqlrc, unless option -X is
specified.
• -f FILENAME will execute the commands in FILENAME, then exit • -c
COMMAND will execute COMMAND (SQL or internal) and then exit •
--help will display all the startup options, then exit
• --version will display version info and then exit
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 77
Entering Commands
• psql uses the command line editing capabilities that are available in the
native OS. Generally, this means
• Up and Down arrows cycle through command history
− on UNIX, there is tab completion for various things, such as SQL commands and to a more
limited degree, table and field names
− disabled with -n
• s will show the command history
• s FILENAME will save the command history
• e will edit the query buffer and then execute it
• e FILENAME will edit FILENAME and then execute it
• w FILENAME will save the query buffer to FILENAME
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 78
Controlling Output
• There are numerous ways to control output.
• The most important are:
− -o FILENAME or o FILENAME will send query output
(excluding STDERR) to FILENAME (which may be a
pipe) − g FILENAME executes the query buffer,
sending output to FILENAME (may be a pipe)
− -q runs quietly.
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 79
Advance Features: Variables
• psql provides variable substitution •
Variables are simply name/value pairs • Use
set meta command to set a variable
postgres=# set city Edmonton
postgres =# echo :city
Edmonton
• Use unset to delete a variable
postgres =# unset city
© Copyright EnterpriseDB Corporation, 2015. All rights reserved. 80

Weitere ähnliche Inhalte

Ähnlich wie 9.6_Course Material-Postgresql_002.pdf

Less01 db architecture
Less01 db architectureLess01 db architecture
Less01 db architecture
Imran Ali
 
COUG_AAbate_Oracle_Database_12c_New_Features
COUG_AAbate_Oracle_Database_12c_New_FeaturesCOUG_AAbate_Oracle_Database_12c_New_Features
COUG_AAbate_Oracle_Database_12c_New_Features
Alfredo Abate
 

Ähnlich wie 9.6_Course Material-Postgresql_002.pdf (20)

Exploring the Oracle Database Architecture.ppt
Exploring the Oracle Database Architecture.pptExploring the Oracle Database Architecture.ppt
Exploring the Oracle Database Architecture.ppt
 
exploring-the-oracle-database-architecture.ppt
exploring-the-oracle-database-architecture.pptexploring-the-oracle-database-architecture.ppt
exploring-the-oracle-database-architecture.ppt
 
Less01 db architecture
Less01 db architectureLess01 db architecture
Less01 db architecture
 
Migrate from Oracle to Aurora PostgreSQL: Best Practices, Design Patterns, & ...
Migrate from Oracle to Aurora PostgreSQL: Best Practices, Design Patterns, & ...Migrate from Oracle to Aurora PostgreSQL: Best Practices, Design Patterns, & ...
Migrate from Oracle to Aurora PostgreSQL: Best Practices, Design Patterns, & ...
 
Oracle DBA
Oracle DBAOracle DBA
Oracle DBA
 
Technical Introduction to PostgreSQL and PPAS
Technical Introduction to PostgreSQL and PPASTechnical Introduction to PostgreSQL and PPAS
Technical Introduction to PostgreSQL and PPAS
 
MySQL 5.7: Focus on InnoDB
MySQL 5.7: Focus on InnoDBMySQL 5.7: Focus on InnoDB
MySQL 5.7: Focus on InnoDB
 
MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015
 
Greenplum Architecture
Greenplum ArchitectureGreenplum Architecture
Greenplum Architecture
 
Powering GIS Application with PostgreSQL and Postgres Plus
Powering GIS Application with PostgreSQL and Postgres Plus Powering GIS Application with PostgreSQL and Postgres Plus
Powering GIS Application with PostgreSQL and Postgres Plus
 
Enterprise PostgreSQL - EDB's answer to conventional Databases
Enterprise PostgreSQL - EDB's answer to conventional DatabasesEnterprise PostgreSQL - EDB's answer to conventional Databases
Enterprise PostgreSQL - EDB's answer to conventional Databases
 
Oracle database 12c introduction- Satyendra Pasalapudi
Oracle database 12c introduction- Satyendra PasalapudiOracle database 12c introduction- Satyendra Pasalapudi
Oracle database 12c introduction- Satyendra Pasalapudi
 
An AMIS Overview of Oracle database 12c (12.1)
An AMIS Overview of Oracle database 12c (12.1)An AMIS Overview of Oracle database 12c (12.1)
An AMIS Overview of Oracle database 12c (12.1)
 
COUG_AAbate_Oracle_Database_12c_New_Features
COUG_AAbate_Oracle_Database_12c_New_FeaturesCOUG_AAbate_Oracle_Database_12c_New_Features
COUG_AAbate_Oracle_Database_12c_New_Features
 
History of Oracle and Databases
History of Oracle and DatabasesHistory of Oracle and Databases
History of Oracle and Databases
 
What's New in MySQL 5.7
What's New in MySQL 5.7What's New in MySQL 5.7
What's New in MySQL 5.7
 
An AMIS overview of database 12c
An AMIS overview of database 12cAn AMIS overview of database 12c
An AMIS overview of database 12c
 
MySQL 5.7 what's new
MySQL 5.7 what's newMySQL 5.7 what's new
MySQL 5.7 what's new
 
Less01_Architecture.ppt
Less01_Architecture.pptLess01_Architecture.ppt
Less01_Architecture.ppt
 
Extreme replication at IOUG Collaborate 15
Extreme replication at IOUG Collaborate 15Extreme replication at IOUG Collaborate 15
Extreme replication at IOUG Collaborate 15
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 

Kürzlich hochgeladen (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

9.6_Course Material-Postgresql_002.pdf

  • 1. PostgreSQL - Advanced Administration Training © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 1 Course Agenda • Introduction • System Architecture • Installation and Database Clusters • Configuration • Creating and Managing Databases • User Tools - CUI and GUI
  • 2. • Security • SQL Primer • Backup, Recovery and PITR • Routine Maintenance Tasks • Data Dictionary • Loading and Moving Data • SQL Tuning • Performance Tuning • Streaming Replication • Connection Pooling using pgpool-II • Table Partitioning • Extensions • Monitoring • Upgrading Best Practices © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 2
  • 4. Introduction © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 3 Objectives • In this module we will cover: − PostgreSQL − Features of PostgreSQL − General Database Limits − Common Database Object Names
  • 5. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 4 Facts about PostgreSQL • The world‟s most advanced open source database • Designed for extensibility and customization • ANSI/ISO compliant SQL support • Actively developed for more than 20 years
  • 6. − University Postgres (1986-1993) − Postgres95 (1994-1995) − PostgreSQL (1996-current) © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 5 PostgreSQL Community • Community mailing lists www.Postgresql.org/community/lists • Support Forums http://forums.enterprisedb.com/forums/list.page • Commercial SLAs
  • 7. www.enterprisedb.com/products-services-training/subscriptions © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 6 PostgreSQL Lineage
  • 8. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 7 Major Features of PostgreSQL
  • 9. • Portable: − Written in ANSI C − Supports Windows, Linux, Mac OS/X and major UNIX platforms • Reliable: − ACID Compliant − Supports Transactions − Supports Save points − Uses Write Ahead Logging (WAL) • Scalable: − Uses Multi-version Concurrency Control − Supports Table Partitioning − Supports Tablespaces © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 8 Major Features of PostgreSQL (continued)
  • 10. • Secure: − Employs Host-Based Access Control − Provides Object-Level Permissions − Supports Logging − SSL 36 • Recovery and Availability: − Streaming Replication − Replication Slots − Supports Hot-Backup, pg_basebackup − Point-in-Time Recovery • Advanced: − Supports Triggers and Functions − Supports Custom Procedural Languages PL/pgSQL, PL/Perl, PL/TCL, PL/PHP, PL/Java − Upgrade using pg_upgrade − Unlogged Tables − Materialized Views © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 9 Write Ahead Logging : REDO and Archiving
  • 11. • PostgreSQL uses Write-Ahead Logging (WAL) as its approach to transaction logging. • WAL's central concept is that changes to data files (where tables and indexes reside) must be written only after those changes have been logged, that is, when log records describing the changes have been flushed to permanent storage. • No need to flush data pages to disk on every transaction commit. • In the event of a crash we will be able to recover the database using the logs • This is roll-forward recovery, also
  • 12. known as REDO © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 10 Rollback or Undo • Dynamic allocation of disk space is used for the storage and processing of records within tables. • The files that represent the table grow as the table grows. • It also grows with transactions that are performed against it. • The data is stored within the file that represents the table. • When deletes and updates are performed on a table, the file that represents the object will contain the previous data
  • 13. • This space gets reused, but need to force recovery of used space. (Using Vaccum) © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 11 General Database Limits Limit Maximum Database Size Value Unlimited Maximum Table Size 32 TB Maximum Row Size 1.6 TB Maximum Field Size 1 GB Maximum Rows per Table Unlimited Maximum Columns per Table 250-1600 (Depending on Column types) Maximum Indexes per Table Unlimited
  • 14. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 12 Common Database Object Names Industry Term Table or Index PostgreSQL Term Relation Row Tuple Column Attribute Data Block Page (when block is on disk) Page Buffer (when block is in memory) © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 13 Summary
  • 15. • In this module we covered: − PostgreSQL − Features of PostgreSQL − General Database Limits − Common Database Object Names © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 14
  • 17. System Architecture © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 15 Module Objectives • Architectural Summary • Process and Memory Architecture • Utility Processes • Connection Request-Response • Disk Read Buffering Disk • Write Buffering • Background Writer Cleaning Scan • Commit and Checkpoint • Statement Processing • Physical Database Architecture • Data Directory Layout • Installation Directory Layout • Page Layout
  • 18. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 16 Architectural Summary • PostgreSQL uses processes, not threads • Postmaster process acts as supervisor • Several utility processes perform background work − postmaster starts them, restarts them if they die • One backend process per user session − postmaster listens for new connections
  • 19. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 17 Process and Memory Architecture
  • 20. Postmaster Shared Memory Shared Buffers WAL Buffers Process Array BGWRITER CHECKPOINTER AUTO -- VACUUM WAL Writer STATS CO LL EC TO R AR CH IV ER LOG WRITER Data Data Files Files Error Log Files WAL WAL Segment s Segments Archived Archive d WAL WAL
  • 21. 18 © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 18 Utility Processes • Background writer − Writes updated data blocks to disk • WAL writer − Flushes write-ahead log to disk • Checkpointer process − Automatically performs a checkpoint based on config parameters • Autovacuum launcher
  • 22. − Starts Autovacuum workers as needed • Autovacuum workers − Recover free space for reuse © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 19 More Utility Process • Logging collector − Routes log messages to syslog, eventlog, or log files • Stats collector − Collects usage statistics by relation and block • Archiver − Archives write-ahead log files
  • 23. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 20 Postmaster as Listener • Postmaster is the master process called postgres • Listens on 1-and-only-1 tcp port • Receives client connection request
  • 25. Shared Memory © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 21 Disk Read Buffering • PostgreSQL buffer cache (shared_buffers) reduces physical reads from storage. • Read the block once, then examine it many times in cache. postgres postgres postgres
  • 26. Shared (data) Buffers Stable Database © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 22 Disk Write Buffering • Blocks are written to disk only when needed: − To make room for new blocks − At checkpoint time
  • 27. postgres postgres postgres Shared (data) Buffers CHECKPOINT Stable Database
  • 28. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 23 Background Writer Cleaning Scan • Background writer scan attempts to ensure an adequate supply of clean buffers. • bgwriter writes dirty blocks to storage as needed. postgres db2 get db cfg for sample | grep -i log postgres postgres Shared (data) Buffers BGWRITER Stable Database
  • 29. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 24 Write Ahead Logging • Backend process that writes data to Write Ahead Logging (WAL) buffers. postgres postgres postgres • Flush WAL buffers periodically (WAL writer), on commit, or when buffers are full. • Group commit.
  • 30. Shared (data) Buffers Stable Database WAL Buffer Transaction Log © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 25 Transaction Log Archiving • Archiver spawns a task to
  • 31. copy pg_xlog log files to archive location when full. postgres postgres postgres WAL ARCHIVE COMMAND Shared (data) Buffers
  • 32. Stable Database Buffer Transaction Log © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 26 Commit and Checkpoint
  • 33. • Before Commit − Uncommitted updates are in memory. • After Commit − Committed updates written from shared memory to WAL log file (on disk). • After Checkpoint − Modified data pages are written from shared memory to the data files. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 27 Statement Processing
  • 34. • Check Syntax • Call Traffic Cop • Identify Query Type Optimize • Execute Query based on • Command Processor if needed • Break Query in Tokens
  • 35. Pars e • Planner generate Plan • Uses Database Statistics • Query Cost Calculation • Choose best plan query plan Execute © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 28 Database Cluster (Instance) Data Directory
  • 36. Layout Data Global Base pg_tblsc pg_xlog pg_log Status Directories Configuration FilesPostmaster Info Files Cluster wide database objects Contains Databases Symbolic link to tablespaces Write ahead logs Error logs pg_clog, pg_multiexact, pg_snapshots, pg_stat, pg_subtrans,pg_notify, pg_serial, pg_replslot, pg_logical, pg_dynshmem postgresql.conf, pg_hba.conf, pg_ident.conf
  • 37. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 29 Installation Directory Layout • bin – Binary files • include – Header files • lib – Library files • doc – Help files for different components, contrib modules and extensions • share – Extensions and sample config files • scripts – Some useful script files • stackbuilder – installation directory for Stackbuilder /opt/PostgresPlus/9.4AS bin include
  • 38. lib doc share scripts stackbuilderplus pg_env.sh, uninstall-* files 30 © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 30 Data File Storage Internals • File-per-table, file-per-index.
  • 39. • A table-space is a directory. • Each database that uses that table-space gets a subdirectory. • Each relation using that tablespace/database combination gets one or more files, in 1GB chunks. • Additional files used to hold auxiliary information (free space map, visibility map). • Each file name is a number (see pg_class.relfilenode). © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 31 Free Space and Visibility Map Files •Each Relation has a free space map
  • 40. − Stores information about free space available in the relation − File named as filenode number plus the suffix _fsm •Tables also have a visibility map − Track which pages are known to have no dead tuples − Stored in a fork with the suffix _vm 32 © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 32 Sample - Data Directory Layout 14307 Database OID
  • 42. 14300 14301 /pg_tab 14307 16651 16700 16701 © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 33 Lab Exercise
  • 43. 1. Write a command to display a list of all PostgreSQL related processes on your operating system 2. Write a SQL statement to find the data file name and location for „customers‟ table © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 34 Module Summary
  • 44. • Architectural Summary • Shared Memory • Inter-processes Communication • Statement Processing • Utility Processes • Disk Read Buffering Disk • Write Buffering • Background Writer Cleaning Scan • Commit and Checkpoint • Physical Database Architecture • Data Directory Layout • Installation Directory Layout • Page Layout
  • 45. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 35
  • 47. Installation © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 36 Module Objectives • OS User and Permissions • Installation Options • Installation of PostgreSQL • StackBuilder • Setting Environmental Variables
  • 48. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 37 OS User and Permissions • PostgreSQL runs as a daemon (Unix / Linux) or service (Windows). • Installation requires superuser/admin access. • All PostgreSQL processes and data files must be owned by a user in the OS. − OS user is un-related to database user accounts.
  • 49. − For security reasons, the OS user must not be root or an administrative account. • SELinux Permissions − SELinux must be set to permissive mode on systems with SELinux. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 38 Supported Platforms • PostgreSQL is available for Windows, Linux and Solaris systems • Windows: − Windows Server 2008 R1 (32 and 64 bit) − Windows Server 2008 R2 (64 bit) − Windows 2012 (64-bit) • Linux platforms:
  • 50. − CentOS 6.x and 7.x (64-bit) − Red Hat Enterprise Linux 6.x and 7.x (64-bit) − Ubuntu 14.04 LTS − SLES 11.x © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 39 Supported Locales • PostgreSQL has been tested and certified for the following locales: − en_US United States English − zh_HK Traditional Chinese with Hong Kong SCS − zh_TW Traditional Chinese for Taiwan − zh_CN Simplified Chinese − ja_JP Japanese − ko_KR Korean
  • 51. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 40 Text Mode Installation • Run the installer in text mode: # ./postgresql.9.4-linux.run –mode text •Choose the language using a number[1-5] Please select the installation language
  • 52. [1] English - English [2] Japanese - 日本語 [3] Simplified Chinese - 简体中文 [4] Traditional Chinese - 繁体中文 [5] Korean - 한국어 Please choose an option [1] : © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 41 Text Mode Installation Steps • Installer will prompt for different steps: − License Agreement − User Authentication − Installation Directory − Components − Data and WAL Directory
  • 53. − Configuration Mode − Database superuser password and Locale © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 42 StackBuilder • Simplifies the process of downloading and installing modules • StackBuilder requires Internet access • Run stackbuilder using the StackBuilder menu option from the Postgresql 9.4 menu
  • 54. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 43 Setting Environmental Variables • Setting environment variables is very important for trouble free startup/shutdown of the database server. − PATH –should point correct bin directory.
  • 55. − PGDATA –should point correct data cluster directory. − PGPORT –should point correct port on which database cluster is running. − Edit .bash_profile to set the variables − In Windows set these variables using my computer properties page. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 44 Module Summary • OS User and Permissions • Installation Options
  • 56. • Installation of PostgreSQL • StackBuilder • Setting Environmental Variables © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 45
  • 57.
  • 58. Module 4 Database Clusters © 2015 EnterpriseDB Corporation. All rights reserved. 46 Module Objectives • Database Clusters • Creating a Database Cluster • Starting and Stopping the Server (pg_ctl) • Connect to the Server Using psql
  • 59. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 47 Database Clusters • A Database Cluster is a collection of databases managed by a single server instance. • Database Clusters are comprised of: − Data directory − Port
  • 60. • Default databases are created named: − template0 − template1 − postgres © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 48 Creating a Database Cluster • Choose the data directory location for new cluster. • Initialize the database cluster storage area (data directory) using initdb utility. • initdb will create the data directory if it doesn‟t exist.
  • 61. • You must have permissions on the parent directory so that initdb can create the data directory. • Data directory can be created manually using superuser access and ownership can be given to postgres user. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 49 Example - initdb • In
  • 62. the above example database system will be owned by user “postgres.” • postgres OS user must also own the server process. • postgres user is the database superuser. • The default server config file will be created in /edbstore named postgresql.conf. • -W is used to force initdb to prompt for the superuser password. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 50 Starting a Database Cluster • Choose a unique port for postmaster
  • 63. • Change the port in postgresql.conf • pg_ctl utility can be used to start a cluster • Syntax − pg_ctl start [options] − Options -D location of the database storage area -l write (or append) server log to FILENAME -w wait until operation completes -t seconds to wait when using -w option © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 51 Connecting to a Database Cluster • The psql and pgAdmin-III clients can be used for connections
  • 64. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 52 Reload a Database Cluster • Some configuration parameter changes do not require a restart • Changes can be reloaded using the pg_ctl utility • Changes can also be reloaded using pg_reload_conf() • Syntax pg_ctl reload [options]
  • 65. -D location of the database storage area -s only print errors, no informational messages © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 53 Stopping a Database Cluster • pg_ctl can be used to stop a database cluster • pg_ctl supports three modes of shutdown − smartquit after all clients have disconnected − Fast (default)quit directly, with proper shutdown (default) − immediatequit without complete shutdown; will lead to recovery • Syntax
  • 66. − pg_ctl stop [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 54 View Cluster Control Information • pg_controldata can be used to view the control information for a database cluster • Syntax: − pg_controldata [DATADIR]
  • 67. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 55 Module Summary • Database Clusters • Creating a Database Cluster • Starting and Stopping the Server (pg_ctl)
  • 68. • Connect to the Server Using psql © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 56 Lab Exercise - 1 1. A new website is to be developed for an online music store. − Create a new cluster edbdata with ownership of postgres user. − Start your edbdata cluster − Stop your edbdata cluster with fast mode.
  • 69. − Reload your cluster with pg_ctl utility and using pg_reload_conf() function. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 57
  • 71. Configuration © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 58 Objectives • In this module we will cover: − Server Configuration File − Setting Server Parameters − Connection Settings − Security and Authentication Settings − Memory Settings − Query Planner Settings -> Performance tuning day4 − Log Management − Background Writer Settings − Statement Behavior -> rountine maintenance day3 −
  • 72. Autovacuum Settings -> rountine maintenance day3 © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 59 Server Configuration File • There are many configuration parameters that effect the behavior of the database system. • Server config file postgresql.conf stores all the server parameters. • All parameter names are case-insensitive. • Some parameters require restart. • Query to list of all parameters. − SELECT name,setting FROM pg_settings; • Query to list all parameters requiring server restart. − SELECT name FROM pg_settings WHERE context = 'postmaster'; •
  • 73. One way to set these parameters is to edit the file postgresql.conf, which is normally kept in the data directory. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 60 Setting Server Parameters • Some parameters can be changed per session using the SET command. • Some parameters can be changed at the user level using ALTER USER. • Some parameters can be changed at the database level using ALTER DATABASE. • The SHOW command can be used to see settings. • pg_settings catalog table lists settings information.
  • 74. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 61 PostgreSQL ALTER SYSTEM • ALTER SYSTEM command: − Available starting with PostgreSQL 9.4 − Edits cluster settings without editing postgresql.conf − Writes the setting to a file called postgresql.auto.conf • Example: − ALTER SYSTEM SET work_mem=20480;
  • 75. • postgresql.auto.conf is always read last during server reload/restarts • Reset a parameter change - − ALTER SYSTEM SET work_mem = DEFAULT; © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 62 Connection Settings • The following parameters in the postgresql.conf file control the connection settings: − listen_addresses (string) − Specifies the TCP/IP address(es) on which the server is to listen for connections − port (integer) − The TCP port the server listens on; 5432 by default − max_connections (integer) − Determines the maximum number of concurrent connections to the database server −
  • 76. superuser_reserved_connections (integer) − Determines the number of connection “slots” that are reserved for connections by Advanced Server superusers 63 © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 63 Memory Settings • The following parameters in postgresql.conf control the memory settings: − shared_buffers (integer) − Sets the amount of memory the database server uses for shared memory buffers − temp_buffers (integer) − Sets the maximum number of temporary buffers used by each database session − work_mem (default 4MB) − Specifies the amount of memory to be used by internal sort operations and hash tables before switching to temporary disk files − autovacuum_work_mem − Specifies the maximum amount of memory to be used by each autovacuum worker process
  • 77. − maintenance_work_mem (default 64MB) − Specifies the maximum amount of memory to be used in maintenance operations, such as CREATE INDEX, and ALTER TABLE ADD FOREIGN KEY © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 64 Write Ahead Logs Settings • wal_level (default: minimal): Determines how much information is written to the WAL. Other values are archive, hot_standby and logical. • fsync (default on): Turn this off to make your database much faster – and silently cause arbitrary corruption in case of a system crash – limited applicability. • wal_buffers (default: -1, autotune): The amount of memory used in shared memory for WAL data. The default setting of -1 selects a size equal to 1/32nd (about 3%) of shared_buffers.
  • 78. • checkpoint_segments (default 3): Maximum number of 16MB WAL file segments between checkpoints. Default is too small! • checkpoint_timeout (default 5 minutes): Maximum time between checkpoints. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 65 Logging – Where to Log • log_destination: Valid values are combinations of stderr, csvlog, syslog, and eventlog. • logging_collector: Enables advanced logging features. csvlog requires logging_collector. • log_directory: Directory where log files are written. Default pg_log.
  • 79. • log_filename: Format of log file name (e.g. postgresql-%Y-%m- %d_%H%M%S.log). • log_rotation_age: Automatically rotate logs after this much time. Requires logging_collector. • log_rotation_size: Automatically rotate logs when they get this big. Requires logging_collector. 66 © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 66 Logging - When To Log • client_min_messages (default NOTICE). Messages of this severity level or above are sent to the client. • log_min_messages (default WARNING). Messages of this severity level or above are sent to the server.
  • 80. • log_min_error_statement (default ERROR). When a message of this severity or higher is written to the server log, the statement that caused it is logged along with it. • log_min_duration_statement (default -1, disabled): When a statement runs for at least this long, it is written to the server log, with its duration. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 67 Logging - What To Log • log_connections (default off): Log successful connections to the server log. • log_disconnections (default off): Log some information each time a session disconnects, including the duration of the session. • log_error_verbosity (default “default”): Can also select “terse” or “verbose”.
  • 81. • log_duration (default off): Log duration of each statement. • log_line_prefix: Additional details to log with each line. • log_statement (default none): Legal values are none, ddl, mod (DDL and all other data modifying statements), or all. • log_temp_files (default -1): Log temporary files of this size or larger, in kilobytes. • log_checkpoints (default off): Causes checkpoints and restart points to be logged in the server log. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 68 Lab Exercise 1. Open psql and write a statement to change work_mem to 10MB. This change must persist across server restarts 2. Open psql and write a statement to change work_mem to 20MB for the current session
  • 82. 3. Open psql and write a statement to change work_mem to 1 MB for the postgres user 4. Write a query to list all parameters requiring a server restart 5. Take a backup of the postgresql.conf file © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 69 Lab Exercise 6. Open the configuration file for your Postgres database cluster and make the following changes − Maximum allowed connections to 50 − Authentication time to 10 mins
  • 83. − Shared buffers to 256 MB − work_mem to 10 MB − wal_buffers to 8MB 7. Restart the server and verify the changes made in previous step © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 70 Summary • In this module we covered: − Setting PostgresPlus Advanced Server Parameters − Access Control − Connection Settings
  • 84. − Security and Authentication Settings − Memory Settings − Query Planner Settings − Log Management − Background Writer Settings − Statement Behaviour − Autovacuum Settings © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 71 Module 6
  • 85. PSQL © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 72 Objectives • In this module we will cover: − Client Interface psql − Connecting to a Database − psql Meta Commands − Executing SQL Commands − Advance Features
  • 86. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 73 PSQL • psql is the name of PostgreSQL PSQL's executable. • It enables you to type in queries interactively, issue them to PostgreSQL, and see the query results. − psql [option...] [dbname [username]]
  • 87. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 74 Connecting to a Database • In order to connect to a database you need to know the name of your target database, the host name and port number of the server and what user name you want to connect as. • Command line options, namely -d, -h, -p, and -U respectively. • Environment variables PGDATABASE, PGHOST,
  • 88. PGPORT and PGUSER to appropriate values. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 75 Conventions • psql has it's own set of commands, all of which start with a backslash (). These are in no way related to SQL commands, which operate in the server. psql commands only affect psql. • Some commands accept a pattern. This pattern is a modified regex. Key points:
  • 89. − * and ? are wildcards − Double-quotes are used to specify an exact name, ignoring all special characters and preserving case © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 76 On Startup... • psql will execute commands from $HOME/.psqlrc, unless option -X is specified. • -f FILENAME will execute the commands in FILENAME, then exit • -c COMMAND will execute COMMAND (SQL or internal) and then exit •
  • 90. --help will display all the startup options, then exit • --version will display version info and then exit © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 77 Entering Commands • psql uses the command line editing capabilities that are available in the native OS. Generally, this means • Up and Down arrows cycle through command history − on UNIX, there is tab completion for various things, such as SQL commands and to a more limited degree, table and field names − disabled with -n
  • 91. • s will show the command history • s FILENAME will save the command history • e will edit the query buffer and then execute it • e FILENAME will edit FILENAME and then execute it • w FILENAME will save the query buffer to FILENAME © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 78 Controlling Output • There are numerous ways to control output. • The most important are: − -o FILENAME or o FILENAME will send query output (excluding STDERR) to FILENAME (which may be a
  • 92. pipe) − g FILENAME executes the query buffer, sending output to FILENAME (may be a pipe) − -q runs quietly. © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 79 Advance Features: Variables • psql provides variable substitution • Variables are simply name/value pairs • Use set meta command to set a variable postgres=# set city Edmonton
  • 93. postgres =# echo :city Edmonton • Use unset to delete a variable postgres =# unset city © Copyright EnterpriseDB Corporation, 2015. All rights reserved. 80