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.
- What are the languages which PostgreSQL supports?
- What are the main features of PostgreSQL?
- What is a non-clustered index?
- How can you store binary data?
- Explain what a Function is
- How can we change the column data type in SQL?
- Explain Write-Ahead Logging
- What is multi-version concurrency control?
- What are tokens?
- Explain how to Enable Debugging
- What is the purpose of
- 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.
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
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.
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?
Column data types in PostgreSQL are changed using the
ALTER TABLE statement combined with the
ALTER COLUMN statement.
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?
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 and running the command:
CREATE EXTENSION pldbgapi
What is the purpose of
Array_To_String in PostgreSQL?
Array_To_String function concatenates array elements to a string using a delimiter like an underscore.
SELECT array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*');
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 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.