0% found this document useful (0 votes)
349 views14 pages

Postgrre

The document outlines the daily, weekly, monthly and yearly responsibilities of a PostgreSQL database administrator. Daily responsibilities include backups, monitoring database and operating system alerts and logs, SQL tuning, tablespace management, replication, security monitoring and schema/code changes. Weekly responsibilities include full backups, cloning non-production databases and patching/upgrades. Monthly responsibilities include more cloning, monitoring growth trends and practicing recovery scenarios. Quarterly and yearly responsibilities include database upgrades, hardware evaluation and attending conferences.

Uploaded by

k2sh07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
349 views14 pages

Postgrre

The document outlines the daily, weekly, monthly and yearly responsibilities of a PostgreSQL database administrator. Daily responsibilities include backups, monitoring database and operating system alerts and logs, SQL tuning, tablespace management, replication, security monitoring and schema/code changes. Weekly responsibilities include full backups, cloning non-production databases and patching/upgrades. Monthly responsibilities include more cloning, monitoring growth trends and practicing recovery scenarios. Quarterly and yearly responsibilities include database upgrades, hardware evaluation and attending conferences.

Uploaded by

k2sh07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

1.what are your responsibilities as a PostgreSQL database administrator?

Daily

Backups—these are usually incremental or cumulative, weekly fulls, and logs are
archived and e-mailed to DBA upon failure
Database Alert Logs—such as ERROR,FATAL automatic notifications through e-mail,
pagers
Operating System File space, CPU and I/O statistics—depends on system admin support
SQL Tuning Sets—Top 5 to 10 SQL statements
Tablespace growth—Extension, Partition Management, Temporary Tablespace.
Replication—Log Shipping/Application in Synch
Alert logs—intrusion detection, removal of unused accounts
New account creation—should be at least partially automated
Personnel security changes—At least 24 hours notice
Migrate schema and code changes or ad hoc SQL updates
Large table growth, coalescing tablespace
Keeping a log of daily changes to the database—publishing it for certain IT staff

Weekly
Backups—usually full
Cloning for non-production databases—automated or scripted
Tablespace growth—daily rolled up to weekly
Patches/Upgrades—Milestone updates

Monthly
Cloning for non-production databases—automated or scripted
Monitoring tablespace growth—weekly rolled up to monthly
Trends and forecasts—CPU utilization, I/O stats, logons
Password changes on production—sys, system,schema.
Practicing recovery scenarios

Quarterly
Database upgrades
Monitoring tablespace growth—monthly rolled up to yearly

Yearly
Tablespace growth—yearly report
Trends and forecast rollups
Attend postgresql conferences—regional or national pgconf groups
PostgreSQL upgrades with planned downtime.
Software licensing and warranty renewals
Hardware evaluation and replacement
SSL Certificate renewals.

(1) Can you briefly tell me the features of PostgreSQL?

- Object-Relational Database Management System : PostgreSQL Database is an


ORDBMS where it implements object-oriented features such as user-defined types,
inheritance, and polymorphism is called an object-relational database management
system (ORDBMS).

- PostgreSQL is an open-source database management system and no License and a


Large Community.

- Extensibility : One of the most important features of PostgreSQL is that it


can be extended. You can add new data types, new functions and operators, and even
new procedural and client languages. There are many contributed packages available
on the Internet.
- Scalability, High-Availability Redundancy : PostgreSQL includes streaming and
logical replication built-in to the core package of the software. You may also able
to setup a synchronous replication for PostgreSQL to have more high-availability
cluster, while making a stand by node process your read queries.

(2) What is the difference between 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.
Commit and Checkpoint

(3) Can you tell me the stages involved in statement processing?

Here is the short description of the stages of query processing.

- Parse Stage: The parser stage checks the query transmitted by the application
program for correct syntax and creates a query tree.
- The rewrite system: The rewrite system takes the query tree created by the
parser stage and looks for any rules (stored in the system catalogs) to apply to
the query tree. It performs the transformations given in the rule bodies.
- The planner/optimizer: The planner/optimizer takes the (rewritten) query tree
and creates a query plan that will be the input to the executor.
- The executor: The executor recursively steps through the plan tree and
retrieves rows in the way represented by the plan.

(4) How can you get the help commands in PostgreSQL?


\h or \?

(5) How do you enable alert logging in PostgreSQL?

By setting the below parameter


log_destination = 'stderr'
logging_collector = on
log_directory = 'log'
log_filename = 'alert_postgresql.log'

(6) How often checkpoint occurs in PostgreSQL?

A checkpoint occurs in the following cases:

When the administrator runs the CHECKPOINT statement.


With the interval specified in parameter checkpoint_timeout(default 300 seconds)
Amount of data written to the WAL has reached a parameter max_wal_size (default:
1GB).
At the start of an online backup
At the execution of pg_start_backup function
At the execution of a pg_basebackup command
At the shutdown of an instance(Except for the pg_ctl stop -m immediate command
execution)
At the time of database configuration such as CREATE DATABASE / DROP DATABASE
statement

(7) What is max_wal_size?


It is the parameter I set to limit the maximum disk space that my wal files should
take.

(8) What is work_mem?


This is the amount of memory reserved for either a single sort or hash table
operation in a query and it is controlled by work_mem database parameter. A sort
operation could be one of an ORDER BY, DISTINCT or Merge join and a hash table
operation could be due to a hash-join, hash based aggregation or an IN subquery.

(9) How do you set work_mem locally?


set local work_mem=’3GB’;

(10) What is maintenance_work_mem?


This is the maximum amount of memory allocation of RAM consumed for maintenance
operations. A maintenance operation could be one of the VACUUM, CREATE INDEX or
adding a FOREIGN KEY to a table. The setting is controlled by the
maintenance_work_mem database parameter.

(11) What are the different ways you can install PostgreSQL?
PostgreSQL installation can be done in 3 ways.
source code installation
yum installation
rpm based installation

(12) What are the different RPMs you need to install PostgreSQL?

postgresql-libs The postgresql-libs package provides the essential shared


libraries for any PostgreSQL client program or interface. You will need to install
this package to use any other PostgreSQL package or any clients that need to
connect to a PostgreSQL server.
postgresql If you want to manipulate a PostgreSQL database on a local or remote
PostgreSQL server, you need this package. You also need to install this package if
you’re installing the postgresql-server package.
postgresql-contrib The postgresql-contrib package contains contributed
packages that are included in the PostgreSQL distribution.
postgresql-devel The postgresql-devel package contains the header files and
libraries needed to compile C or C++ applications which will directly interact with
a PostgreSQL database management server and the ecpg Embedded C Postgres
preprocessor. You need to install this package if you want to develop applications
which will interact with a PostgreSQL server.
postgresql-docs The postgresql-docs package includes the SGML source for the
documentation as well as the documentation in PDF format and some extra
documentation. Install this package if you want to help with the PostgreSQL
documentation project, or if you want to generate printed documentation.
postgresql-server The postgresql-server package includes the programs needed to
create and run a PostgreSQL server, which will in turn allow you to create and
maintain PostgreSQL databases. You should install postgresql-server if you want to
create and maintain your own PostgreSQL databases and/or your own PostgreSQL
server. You also need to install the postgresql package and its requirements.
postgresql-tcl The postgresql-tcl package contains the Pgtcl client library and
its documentation.
postgresql-jdbc The postgresql-jdbc package includes the .jar files needed for
Java programs to access a PostgreSQL database.
postgresql-plperl The postgresql-plperl package contains the the PL/Perl procedural
language for the backend. PL/pgSQL is part of the core server package.
postgresql-pltcl The postgresql-plperl package contains PL/Tcl procedural language
for the backend. PL/pgSQL is part of the core server package.
Important rpm’s are marked in bold

(13) What are the different shutdown modes you have?


Shutdown modes are:
smart : quit after all clients have disconnected
fast : quit directly, with proper shutdown (default)
immediate : quit without complete shutdown; will lead to recovery on restart

(14) Give me any 5 important tools you use in your day to day administration

pg_ctl : controls the postgres cluster


pg_basebackup : take physical backup
pg_upgrade : upgrade your cluster
pg_restore : import logical backup
pg_dump : used for logical backup or exporting

(15) Can you give me few important files and folders in your data directory?

PG_VERSION - A file containing the major version number of PostgreSQL


base - Subdirectory containing per-database subdirectories
current_logfiles - File recording the log file(s) currently written to by the
logging collector
global - Subdirectory containing cluster-wide tables, such as pg_database
pg_commit_ts - Subdirectory containing transaction commit timestamp data
pg_dynshmem - Subdirectory containing files used by the dynamic shared memory
subsystem
pg_logical - Subdirectory containing status data for logical decoding
pg_multixact - Subdirectory containing multitransaction status data (used for
shared row locks)
pg_notify - Subdirectory containing LISTEN/NOTIFY status data
pg_replslot - Subdirectory containing replication slot data
pg_serial - Subdirectory containing information about committed serializable
transactions
pg_snapshots - Subdirectory containing exported snapshots
pg_stat - Subdirectory containing permanent files for the statistics subsystem
pg_stat_tmp - Subdirectory containing temporary files for the statistics subsystem
pg_subtrans - Subdirectory containing subtransaction status data
pg_tblspc - Subdirectory containing symbolic links to tablespaces
pg_twophase - Subdirectory containing state files for prepared transactions
pg_wal - Subdirectory containing WAL (Write Ahead Log) files
pg_xact - Subdirectory containing transaction commit status data
postgresql.auto.conf - A file used for storing configuration parameters that are
set by ALTER SYSTEM
postmaster.opts - A file recording the command-line options the server was last
started with
postmaster.pid - A lock file recording the current postmaster process ID (PID),
cluster data directory path, postmaster start timestamp, port number, Unix-domain
socket directory path (empty on Windows), first valid listen_address (IP address or
*, or empty if not listening on TCP), and shared memory segment ID (this file is
not present after server shutdown)

(16) Can you tell me something about default schemas in PostgreSQL?


- Information_schema consists of a standardized set of views that contain
information about the objects in the database. These views get system information
from the system catalog tables in a standardized way.
- Pg_catalog contains the system catalog tables, built-in data types, functions,
and operators. It is always part of the schema search path, even if it is not
explicitly named in the search path.
- Pg_toast stores large objects such as records that exceed the page size. This
schema is used internally by the Greenplum Database system.
- Every database has a default schema named public. If you do not create any
schemas, objects are created in the public schema.
(17) Are there any role attributes you have in your environment?
CONNECTION LIMIT connlimit - If role can log in, this specifies how many concurrent
connections the role can make. -1 (the default) means no limit.

CREATEROLE | NOCREATEROLE - Determines if the role is allowed to create and manage


other roles. NOCREATEROLE is the default.

(18) What happens when you lose any of these files?


Deleted pg_control - In order to recover the pg_control file, restore the
pg_control file from a backup, and then re-create the WAL file with specifying the
-x option to pg_resetxlog command. If the instance was terminated abnormally,
specify -f option also at the same time. Instance cannot start in case that
pg_control file is only restored.

Deleted WAL file - If an instance is successful, regardless of the case of abnormal


termination, if the entire WAL file has been deleted, Instance cannot start under
the condition. Execute pg_resetxlog command to re-create the WAL file. In case of
immediately after an abnormal instance termination, specify -f option to
pg_resetxlog command to create WAL files forcibly

Behavior on data file deletion - Instance started successfully. Error (ERROR


category) occurred on the deleted table when accessing with the SELECT statement.
Instance or session are not affected.

Logs are not output at the time of instance startup. At the time of SELECT
statement execution, following log has been output.

Behavior of Visibility Map (VM) / Free Space Map (FSM) file deletion - Error does
not occur on VM files, FSM file deletion, and SQL statement for the target table
succeeds. These files are re-created on the next VACUUM.

Behavior at the time of pg_filenode.map file deletion - When pg_filenode.map file


is deleted, it becomes impossible to mapping of the system catalog and the file
name, and then cannot use the database.

Behavior at the time of PG_VERSION file deletion - When PG_VERSION file is deleted,
the directory cannot be recognized as the directory for PostgreSQL database.

(19) Can you tell me high-level steps involved in configuring replication in


PostgreSQL?
Configure password less connectivity between primary and standby
create replication user.
make necessary changes in primary postgresql.conf file and pg_hba file.
take full backup of primary and import in standby.
create recovery.conf file and start standby.

(20) Can you tell me the contents of recovery.conf file?


standby_mode = ‘on’
primary_conninfo = ‘user=replica password=replicauser@ host=192.168.1.9 port=5432
sslmode=disable sslcompression=1’
restore_command = ‘cp /u01/archives/%f %p’
trigger_file = ‘/tmp/postgresql.trigger.5432’
can you tell me the changes you made in your postgresql.conf file?
listen_addresses = ‘*’
**wal_level = hot_standby
archive_mode = on
archive_command = ‘cp %p /u02/New_PostgreData/archivedlogs/%f’
archive_timeout = 5min
**max_wal_senders = 3
wal_keep_segments = 8
**hot_standby = on

(21) What is the switchover process?


create a trigger file in standby and rewind your primary database and create
recovey.conf file in new standby(old primary)

(22) What is a replication slot?


Replication slot is a mechanism in Postgresql to ensure wal files are retained in
primary if they are not applied in standby.

(23) what is logical replication?


It is a mechanism to replicate logical objects like tables, databases in
PostgreSQL.

(24) What is vacuuming in PostgreSQL?


PostgreSQL’s VACUUM command has to process each table on a regular basis for
several reasons:

To recover or reuse disk space occupied by updated or deleted rows.


To update data statistics used by the PostgreSQL query planner.
To update the visibility map, which speeds up index-only scans.
To protect against loss of very old data due to transaction ID wraparound or
multixact ID wraparound.
There are two variants of VACUUM: standard VACUUM and VACUUM FULL.

VACUUM FULL can reclaim more disk space but runs much more slowly.

Also, the standard form of VACUUM can run in parallel with production database
operations. (Commands such as SELECT, INSERT, UPDATE, and DELETE will continue to
function normally, though you will not be able to modify the definition of a table
with commands such as ALTER TABLE while it is being vacuumed.)

VACUUM FULL requires exclusive lock on the table it is working on, and therefore
cannot be done in parallel with other use of the table.

VACUUM creates a substantial amount of I/O traffic, which can cause poor
performance for other active sessions.

recover or reuse disk space : If you have a table whose entire contents are deleted
on a periodic basis, consider doing it with TRUNCATE rather than using DELETE
followed by VACUUM.

TRUNCATE removes the entire content of the table immediately, without requiring a
subsequent VACUUM or VACUUM FULL to reclaim the now-unused disk space. The
disadvantage is that strict MVCC semantics are violated.

Update data statistics : The PostgreSQL query planner relies on statistical


information about the contents of tables in order to generate good plans for
queries. These statistics are gathered by the ANALYZE command, which can be invoked
by itself or as an optional step in VACUUM. It is important to have reasonably
accurate statistics, otherwise poor choices of plans might degrade database
performance

The autovacuum daemon, if enabled, will automatically issue ANALYZE commands


whenever the content of a table has changed sufficiently.

(25) what is free space map?


Free Space Map (FSM) is a file to manage the volume of the free space in each page
of the table file. It manages each page included in the file of the table at a
single byte. The name of the FSM file is “{RELFILENODE}_fsm”. By referring to this
file, it will be able to find the storage location of a tuple at a high speed.

(26) can you tell me any important parameters in vacuum?

vacuum_freeze_min_age governs whether or not a tuple will be frozen while vacuum is


already looking at a page to see if it has dead tuples that can be cleaned up.
Tuples older than vacuum_freeze_min_age will be frozen in this case

Last there is autovacuum_max_freeze_age. If the last time the table was scanned
completely for unfrozen rows was more than this many transactions ago, autovacuum
will start an anti-wraparound vacuum on the table. The default is 200m

vacuum_freeze_table_age (integer)

VACUUM performs a whole-table scan if the table’s pg_class.relfrozenxid field has


reached the age specified by this setting. The default is 150 million transactions

vacuum_freeze_min_age (integer) and vacuum_multixact_freeze_min_age

Specifies the cutoff age (in transactions) that VACUUM should use to decide whether
to freeze row versions while scanning a table. The default is 50 million
transactions.

vacuum_freeze_table_age (integer) and vacuum_multixact_freeze_table_age

VACUUM performs a whole-table scan if the table’s pg_class.relfrozenxid field has


reached the age specified by this setting. The default is 150 million transactions.

autovacuum_freeze_max_age (integer) and autovacuum_multixact_freeze_max_age

Specifies the maximum age (in transactions) that a table’s pg_class.relfrozenxid


field can attain before a VACUUM operation is forced to prevent transaction ID
wraparound within the table.

(27) How do you get the explain plan in PostgreSQL?

explain query;

(28) what are the things you get when you run explain plan?

Estimated start-up cost (0.00). This is the time expended before the output phase
can begin, e.g., time to do the sorting in a sort node.
Estimated total cost (201.00). This is stated on the assumption that the plan node
is run to completion, i.e., all available rows are retrieved. In practice a node’s
parent node might stop short of reading all available rows (see the LIMIT example
below).
Estimated number of rows output by this plan node (10000). Again, the node is
assumed to be run to completion.
Estimated average width of rows (49) output by this plan node (in bytes).
Node: the operation that my PostgreSQL is performing.

(29) How can you avoid unnecessary locking of a database?


Ans. We can use MVCC (Multi-version concurrency control) to avoid unnecessary
locking of a database.

(30) Which are the methods PostgreSQL provides to create a new database?
Ans. PostgreSQL provides the following methods to create a new database:

Using CREATE DATABASE, an SQL command


Using created a command-line executable

(31) How do you delete the database in PostgreSQL?


Ans. We can delete the database by using any one of the below options:

Using DROP DATABASE, an SQL command


Using dropdb a command-line executable

Are there any disadvantages with PostgreSQL?


Ans. Yes. There are a few disadvantages. Some of these include the following:

It is slower than MySQL on the performance front.


It doesn’t have the support of a good number of open source applications when
compared to MySQL.
Since it focuses more on compatibility, changes made to improve the speed
need more work.

What is the purpose of table space in PostgreSQL?


Ans. It is a location in the disk. In this, PostgreSQL stores the data files, which
contain indices and tables, etc.

What does Write-Ahead Logging do?


Ans. The Write-Ahead Logging enhances database reliability by logging changes
before any changes or updates are made to the database

How is security ensured in PostgreSQL?


Ans. PostgreSQL uses SSL connections to encrypt client or server communications so
that security will be ensured.

Which are the commands used to control transactions in PostgreSQL?


Ans. The commands used to control transactions in PostgreSQL are BEGIN TRANSACTION,
COMMIT, and ROLLBACK.

What do you need to do to update statistics in PostgreSQL?


Ans. To update statistics in PostgreSQL, we need to use a special function called a
vacuum.

What is the disadvantage with the DROP TABLE command in deleting complete data from
an existing table?
Ans. Though the DROP TABLE command has the ability to delete complete data from an
existing table, the disadvantage with it is - it removes complete table structure
from the database. Due to this, we need to re-create a table to store data.

How can you delete complete data from an existing table?


Ans. We can delete complete data from an existing table using the PostgreSQL
TRUNCATE TABLE command.

How to select first 10 records in PostgreSQL?


select * from users order by name desc LIMIT 0, 10

What is a CTID of PostgreSQL?


In PostgreSQL, the CTID field is one that exists in each and every PostgreSQL
table, and it is unique for all records inside a table, which is used to denote the
location of the tuples.

How to take the backup of PostgreSQL?


Here are the steps to back up a single database in PostgreSQL. We shall be using
the pg_dump tool as it dumps out the content of all the objects in the database to
a single file.

Step 1: First of all, navigate to the PostgreSQL BIN folder:

C:\>cd C:\Program Files\PostgreSQL\9.2\bin

Step 2: Now, we shall execute the pg_dump program and use the following method to
back up the examples database to the example.tar file to the c:\pgbackup\ folder.

pg_dump -U postgres -W -F t examples > c:\pgbackup\examples.tar

Step 3: Here is your backed up database

c:\pgbackup\dvdrental.tar

How to check status of PostgreSQL server running or not?


The simplest way to check whether your server in PostgreSQL is running or not:

ps auxwww | grep postgres

How to change user's password in PostgreSQL?


Follow these steps to carefully change the user password Postgres in Laravel:
Step 1: Make yourself the “Postgres” system user (through the root user, sudo or
via SSH public key verification)
Step 2: Connect to the local server using “PSQL”
Step 3: Type this meta-command of PSQL \password

Which is better MySQL or PostgreSQL?


In terms of learning databases, MySQL is perfect. It is the first choice for web-
based projects merely requiring a database for transactions and nothing else. But,
PostgreSQL is better in terms of functionalities and performance. It is more used
in the execution of complex queries, data warehousing, and data analysis.

What is the Maximum size for a database in PostgreSQL?


PostgreSQL has no maximum database size, so users can put unlimited data into it.
But, the table, row, and field size are limited. The row and indexes are also
universal for its users.

PostgreSQL usually stores its desk facts in chunks of 8KB. The quantity of these
blocks is confined to a 32-bit signed integer, giving the most desk dimension of
16TB.

What is the maximum size of the table in PostgreSQL?


32 TB is the maximum size of the table in PostgreSQL

What does a cluster index do?


Cluster index sorts table data rows based on their key values.

How are the stats updated in PostgreSQL?


It is not that hard as it seems. To get your statistics updated in PostgreSQL a
special function called explicit ‘vacuum’ call is made. The method to do is to
create a Vacuum where the option of Analyze is used to update statistics in
Postgresql

VACUUM ANALYZE;

is the syntax.
What are the tokens?
Token are the building blocks of any source code. They are known to comprise many
of the special character symbols. These can be regarded as constant, quoted
identifiers, other identifiers, and keywords. Tokens which are keywords consist of
pre-defined SQL commands and meanings. Identifiers are used to represent variable
names like columns, tables etc.

What is A Ctid?
CTIDs is a field, which exists in every PostgreSQL table and is known to identify
specific physical rows according to their block and offset positions within a
particular table. They are used by index entries to point to physical rows. It is
unique for each record in the table and easily denotes the location of a tuple. A
logical row’s CTID changes when it is updated, so the CTID cannot be used as a
long-term row identifier. However, it is sometimes useful to identify a row within
a transaction when no competing update is expected.

What is the option in PostgreSQL to check rows that are affected in a previous part
of the transaction?
The SQL standard is defined by four levels of transaction isolation basically
regarding three phenomena. The three phenomenon must be prevented between
concurrent transactions. The unwanted phenomenon is
-Phantom read: a transaction that re-executes a query, returning a set of
rows that satisfy a search condition and then finds that the set of rows that have
been satisfying the condition has changed due to other recently committed
transaction.
-Non-repeatable read: a transaction that re-reads the data that it has
previously read then finds that data has already been modified by another
transaction.
-Dirty reads: a transaction when reads data that is written by a concurrent
uncommitted transaction is the dirty read.

What are the indices of PostgreSQL?


These are inbuilt functions or methods like GIST Indices, hash table and B-tree
which can be used by the user to scan the index in a backward manner. Users can
also define their indices of PostgreSQL.

What is base directory in PostgreSQL?


The base directory in PostgreSQL (data_dir/base) is the folder is where PostgreSQL
stores all the data you have inserted in your databases. It contains all the sub-
directories which are used by a database in your clusters.

What is the process of splitting a large tale into smaller pieces called in
postgreSQL?
It is called table partitioning.

=========================================

The pg_hba.conf File


The pg_hba.conf file controls which IP addresses and users can connect to the
database. Furthermore, it dictates the authentication protocol that the client must
follow. Changes to the file require at least a reload to take effect. A typical
pg_hba.conf looks like

# TYPE DATABASE USER ADDRESS METHOD


host all all 127.0.0.1/32 ident
host all all ::1/128 trust
host all all 192.168.54.0/24 md5
hostssl all all 0.0.0.0/0 md5

For each connection request, pg_hba.conf is checked from the top down. As soon as a
rule granting access is encountered, a connection is allowed and the server reads
no further in the file. As soon as a rule rejecting access is encountered, the
connection is denied and the server reads no further in the file. If the end of the
file is reached without any matching rules, the connection is denied. A common
mistake people make is to put the rules in the wrong order.

For example, if you added 0.0.0.0/0 reject before 127.0.0.1/32 trust, local users
won’t be able to connect, even though a rule is in place allowing them to.
New in version 10 is the pg_hba_file_rules system view that lists all the contents
of the pg_hba.conf file.

“I edited my pg_hba.conf and now my server is broken.”

This error is generally caused by typos or by adding an unavailable authentication


scheme. When the postgres service can’t parse pg_hba.conf, it blocks all access
just to be safe. Sometimes, it won’t even start up.

Managing Connections

Cancelling the query, terminating the connection, or both will put an end to the
offending query. There are few scenarios where you should cancel all active update
queries: before backing up the database and before restoring the database.

To cancel running queries and terminate connections, follow these steps:

1. Retrieve a listing of recent connections and process IDs (PIDs):


SELECT * FROM pg_stat_activity;

pg_stat_activity is a view that lists the last query running on each connection,
the connected user (usename), the database (datname) in use, and the start times of
the queries. Review the list to identify the PIDs of connections you wish to
terminate.

2. Cancel active queries on a connection with PID 1234:


SELECT pg_cancel_backend(1234);

This does not terminate the connection itself, though.

3. Terminate the connection:


SELECT pg_terminate_backend(1234);

You may need to take the additional step of terminating the client connection. This
is especially important prior to a database restore. If you don’t terminate the
connection, the client may immediately reconnect after restore and run the
offending query anew. If you did not already cancel the queries on the connection,
terminating the connection will cancel all of its queries.

pg_terminate_backend and pg_cancel_backend act on only one connection at a time,


you can kill multiple connections by wrapping them in a SELECT. For example, let’s
suppose you want to kill all connections belonging to a role with a single blow.
Run this SQL command:

SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE usename = 'some_role';

You can set certain operational parameters at the server, database, user, session,
or function level. Any queries that exceed the parameter will automatically be
cancelled by the server. Setting a parameter to 0 disables the parameter:

deadlock_timeout

This is the amount of time a deadlocked query should wait before giving up. This
defaults to 1000 ms. If your application performs a lot of updates, you may want to
increase this value to minimize contention.

Instead of relying on this setting, you can include a NOWAIT clause in your update
SQL:
SELECT FOR UPDATE NOWAIT ... .

The query will be automatically cancelled upon encountering a deadlock.

In PostgreSQL 9.5, you have another choice: SELECT FOR UPDATE SKIP LOCKED will skip
over locked rows.

statement_timeout
This is the amount of time a query can run before it is forced to cancel. This
defaults to 0, meaning no time limit. If you have long-running functions that you
want cancelled if they exceed a certain time, set this value in the definition of
the function rather than globally. Cancelling a function cancels the query and the
transaction that’s calling it.

lock_timeout
This is the amount of time a query should wait for a lock before giving up, and is
most applicable to update queries. Before data updates, the query must obtain an
exclusive lock on affected records. The default is 0, meaning that the query will
wait infinitely. This setting is generally used at the function or session level.
lock_timeout should be lower than statement_timeout, otherwise statement_timeout
will always occur first, making lock_timeout irrelevant.

idle_in_transaction_session_timeout
This is the amount of time a transaction can stay in an idle state before it is
terminated. This defaults to 0, meaning it can stay alive infinitely. This setting
is new in PostgreSQL 9.6. It’s useful for preventing queries from holding on to
locks on data indefinitely or eating up a connection.

=================

Can you tell me about the Postmaster process?

• Postmaster is the master process called postgres


• Listens on 1-and-only-1 tcp port, default port is 5432.
• Receives client connection request

How do you stop and start cluster?


pg_ctl command

Can you briefly tell me about the 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

•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

Give me any two incomplete recovery parameters?

recovery_target = ‘immediate’ — consistent state has been reached


recovery_target_lsn= ‘0/3019838’ — select pg_current_wal_lsn();
recovery_target_time=2019-10-26 19:34:55
recovery_target_xid= ‘634’ — select txid_current();

What is a tablespace?

Tablespaces allow database administrators to have multiple file systems per machine
and decide how to best use physical storage to store database objects. They are
named locations within a filespace in which you can create objects. Tablespaces
allow you to assign different storage for frequently and infrequently used database
objects or to control the I/O performance on certain database objects. For example,
place frequently-used tables on file systems that use high performance solid-state
drives (SSD), and place other tables on standard hard drives.

Pg_default tablespace corresponds to the “base” directory in the database cluster.


With an external tablespace is created a symbolic link is created in the
{PGDATA}/pg_tblspc directory. File name of the symbolic link corresponds to the oid
column of pg_tablespace catalog.

What are the different isolation levels you have in Postgresql?


- Read uncommitted : This is the lowest isolation level. In this level, dirty reads
are allowed, so one transaction may see not-yet-committed changes made by other
transactions.
- Read Committed : This is the default isolation level in PostgreSQL.

When a transaction uses this isolation level, a SELECT query (without a FOR
UPDATE/SHARE clause) sees only data committed before the query began; it never sees
either uncommitted data or changes committed during query execution by concurrent
transactions.

In this isolation level, a lock-based concurrency control DBMS implementation keeps


write locks (acquired on selected data) until the end of the transaction, but read
locks are released as soon as the SELECT operation is performed (so the non-
repeatable reads phenomenon can occur in this isolation level).

- Repeatable reads : In this isolation level, a lock-based concurrency control DBMS


implementation keeps read and write locks (acquired on selected data) until the end
of the transaction. However, range-locks are not managed, so phantom reads can
occur.

Violation in SERIALIZABLE may lead to below error.

ERROR: could not serialize access due to concurrent update


Serializable

This is the highest isolation level.

With a lock-based concurrency control DBMS implementation, serializability requires


read and write locks (acquired on selected data) to be released at the end of the
transaction. Also range-locks must be acquired when a SELECT query uses a ranged
WHERE clause, especially to avoid the phantom reads phenomenon.

How do you set the isolation level in PostgreSQL?


SET TRANSACTION transaction_mode… command.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy