PostgreSQL Interview Questions
Tags: postgres, career, beginners, database
Published: 2021-04-08 20:22:02

If you’re interviewing for a position in which you’ll have to work with a PostgreSQL database; there are some fundamental things that you should know. In this article, I have prepared some important PostgreSQL interview questions and answers which will help you succeed in your interview.

See if you can read through the list below and answer every question. If you can - you are going to crush this interview! If not, read on below for the answers and explanations.

#### Question List
1. What are the languages which PostgreSQL supports?
2. What are the main features of PostgreSQL?
3. What is a non-clustered index?
4. How can you store binary data?
5. Explain what a Function is
6. How can we change the column data type in SQL?
7. Explain Write-Ahead Logging
8. What is multi-version concurrency control?
9. What are tokens?
10. Explain how to Enable Debugging
11. What is the purpose of Array_To_String in PostgreSQL?
12. What is a CTID?

## What are the languages which PostgreSQL supports? 
It supports a language of its own, PL/pgSQL and it supports internal procedural languages - Python, Perl, and Tcl.

## What are the main features of PostgreSQL?
* MVCC (Multi Version Concurrency Control), and procedural languages
* Flexible API and data validation
* Support and extensibility for SQL
We covered more of the key features of [Postgres compared to MySQL in a previous blog post](https://arctype.com/blog/mysqlvspostgres/). 

## What is a non-clustered index?
A non-clustered index is a type of index where the order of the rows does not match the order of the actual data.

## How can you store binary data in PostgreSQL?
PostgreSQL provides two distinct methods for storing binary data:
* The first is by storing the binary data in a table using the data type `bytea`.
* The second method is by using the Large Object feature, which stores the binary data in a separate table in a special format and refers to that table by storing a value of type `oid` in your table. 
The `bytea` data type is not well suited for storing very large amounts of data, while the Large Object method for storing binary data is better suited for storing very large values. 

## Explain functions in PostgreSQL
Functions in PostgreSQL are also known as stored procedures. They are used to store commands, declarations, assignments, etc. This makes it easy to perform operations that would generally take thousands of lines of code to write. 
PostgreSQL functions can be created in several languages such as SQL, PL/pgSQL, C, Python, etc.
 
## How can we change the column data type in SQL?
Columns data-type in PostgreSQL are changed using the `ALTER TABLE` statement combined with the `ALTER COLUMN` statement.
```sql
ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE new_data_type
```

## Explain Write-Ahead Logging
This feature provides a log of a database in case of a database crash by logging changes before any changes or updates are made to the database.

## What is multi-version concurrency control in PostgreSQL?
It is a method commonly used to provide concurrent access to the database, and in programming languages to implement transactional memory. It avoids unnecessary locking of the database - removing the time lag for the user to log into the database.

## What are tokens?
Answer: Tokens are the building blocks of any line of source code. A token can be a keyword, an identifier, a quoted identifier, a constant, etc. Tokens which are keywords consist of predefined SQL commands, while identifiers are used to represent variable names like columns and tables.

## Explain how to enable debugging in PostgreSQL
This command is used for enabling the compilation of all libraries and applications. This process slows down the system and at the same time increases the binary file size. It can be turned on by [installing the pldbgapi extension](https://gist.github.com/eneldoserrata/6633963a1dbb63001b2e3c4a6a5e9bba) and running the command:
```
CREATE EXTENSION pldbgapi
```
## What is the purpose of `Array_To_String` in PostgreSQL?
The `Array_To_String` function concatenates array elements to a string using a `provide` delimiter.

## What is a CTID?
This is a field that exists in every PostgreSQL table. It is a hidden and unique record for each table in PostgreSQL that easily denotes the location of a tuple and is known to identify certain physical rows according to their block and offset positions within a particular table. A logical row’s ctid changes when it is updated, so the ctid cannot be used as a long term identifier.

### Benefits of studying Database Systems
Most interviews will not focus on specific technologies too much. But if you list familiarity with Postgres you can expect to get some questions about how Postgres works and its features. Asking and [answering questions on Discord servers](https://discord.gg/FMZejv5eF3) dedicated to SQL and data is a good idea too. Of course the best way to learn Postgres is to use it and other databases in practice to build projects and apps.