Postgres DBA Interview Questions
Postgres DBA Interview Questions
Once a connection is established, the client process can send a query to the backend process it’s
connected to. The backend process parses the query, creates an execution plan, executes the plan,
and returns the retrieved rows to the client by transmitting them over the established connection.
8. Let’s say your work_mem is 4MB. However your query is doing heavy sorting, for which
work_mem required might be more than 4MB. So will the query execution be successful?
Yes it will be successful, Because it utilized the work_mem fully, it started using temp files.
Disadvantage is , it causes bloating. Also it needs more data to keep multiple versions of the
data. If your database doing lot of DML activities, then postgres need to keep all the old
transaction records also(updated or deleted) . And maintenance activity like vaccum need to be
done remove the dead tuples.
ADMINISTRATION:
1. What is the default port in postgres?
Default is 5432.
2. What is the default block size in postgres? Can we set different block size?
3. What is a tablespace?
A tablespace is used to map a logical name to a physical location on disk. In the simplest word,
we can understand tablespace as a location on the disk where all database objects like table and
indexes are stored.
It is also important to note that the name of the tablespace must not start with pg_, as these are
reserved for the system tablespaces.
4. What are the tablespaces created by default after installing postgres cluster?
Pg_global – > PGDATA/global – > used for cluster wide table and system catalog
Pg_default – > PGDATA/base directory – > it stores databases and relations
Whenever any user creates any table or index, it will be created under pg_default and this is the
default_tablespace setting.
However you can change the default_tablespace setting using below one.
alter system set default_tablespace=ts_postgres;
6. What are the default databases created after setting up postgres cluster?
postgres=# select datname from pg_database;
datname
———–
postgres
template1
template0
7. What is the significance of template0 and template1 databases? What the difference
between these two?
Whenever we create a new database , it creates is using the template1 database. And if you
modify anything in template1 database like creating new extension in template1 database, then
these changes will be reflected to other created databases.
Template0 – > No changes are allowed to this database. If you messed up your template1
database, then you can revert them by creating a new one from template0.
In the below screenshot, you can see for template1, datallowconn is true, But template0 it is
false.
8. What are common database object names in postgres compare to industry terms.
A schema is a collection of database objects. But a user is used to connect to postgres cluster. A
single user can be used to connect to any database in the cluster (provider permission is give).
However schema is local to that particular database.
Below diagram should clear your doubts
|-------------------------------------------|---|
| PostgreSQL instance | |
|-------------------------------------------| U |
| Database 1 | Database 2 | S |
|---------------------|---------------------| E |
| Schema 1 | Schema 2 | Schema 1 | Schema 2 | R |
|----------|----------|----------|----------| S |
| t1,t2,t3 | t1,t2,t3 | t1,t2,t3 | t1,t2,t3 | |
-------------------------------------------------
10. Difference between role and user in postgres? Can we convert a role to user?
Role and user are almost same in postgres, only difference is , a role cannot login , But a user
can. We can say like a user is role with login privilege.
And yes we can convert the role to a user.
alter role <role_name> nologin;
11. Difference mode to stop a postgres cluster using pg_ctl?
Postgres has 3 shutdown modes.
smart –> It is the normal mode . i.e shutdown will wait for all existing connection to be
terminated And it might take a lot of time. And no new connections will be allowed
during this.
fast -> It terminates all existing sessions and performs checkpoint . It is comparably
quick. But if there is lot of trnsactions that need to written to disk, then checkpoint might
take lot of time.
immediate– > It will abort the instance. I.e it will terminate all existing session and
shutdown the instance without performing checkpoint. It is the quickest . But upon
instance startup , recovery will happen.
So if you have lot of pending transactions in your database, then the best way is perform
checkpoint manually and then stop the server.
postgress#CHECKPOINT; – > Run as super user
enterprisedb$ pg_ctl stop -m fast
12. Can you give comparison of shutdown modes between oracle and postgres?
ORACLE POSTGRES
shutdown normal smart
shutdown immediate fast
shutdown abort immediate
2. what is the maximum file size of table or index in postgres? Can we increase that ?
Max size is 1GB. If a table size is big, then it can spread across multiple files.
4. Is there a way in which we can rebuild/reorg a table online to release free space?
As we know vacuum full is used to rebuild table and it releases free space to operating system.
However this method, puts an exclusive lock on the table.
So we can use pg_repack extension to rebuild a table online.
5. While dropping a postgres database i am getting error? What might be the issue?
If you want to drop a database , then you need to fire the command, after connecting to a
different database in the same postgres cluster with superuser privilege.
5. Between postgres and nosql database like mongodb , which one is better?
MAINTENANCE:
1. What is vacuuming in postgres?
Whenever tuples are deleted or becomes obsolete due to update, then they are not removed
physicallly. These tuples are known as dead tuples. So vacuuming can be used to clear those
dead tuples and release the space.
Vacuum(with full)
Vacuum with full option, will reclaim more space and release the space to operating
system.
However we need additional storage , as it will rewrite the entire data to new disk file
without any extra space.
Also it will put exlusive lock on the table for which vaccuming is in progress.
This method is much slower.
12. Let’s says autovacuuming already running in background and You again ran the
vaccum command manually? What will happen?
In this case, postgres will terminate the autovacuum worker process and give priority to the
manual vacuum command. i.e autovacuuming will never block the normal manual database
operations.
Method 2:
You can run below query.
select * from pg_stat_replication;
5. I have a master and slave setup with streaming replication. Now I want to break the
replication and open the standby server for some testing purpose and once the testing is
done, the server again need to be a standby of that primary with replication enabled. How
can i do that?
pg_rewind.
MONITORING:
BACKUP & RECOVERY:
5. Difference between pg_dump and pg_dumpall?
Both are logical backup utilities.
pg_dump -> Used for taking backup of databases, schema,table .backup format is like sql, tar file
or directory
pg_dumpall -> Used for taking backup of complete cluster. backup format is sql .
4. Does create index puts lock on the table? If yes , then any workaround to avoid this
locking?
Yes create index command puts exclusive lock on the table . So you can
add CONCURRENTLY keyword, which will avoid the exclusive lock on the table.