PostgreSQL Deep Dive Flashcards

Data types: JSON, JSONB, arrays, enums SERIAL, BIGSERIAL, IDENTITY columns RETURNING clause on INSERT/UPDATE Table inheritance and partitioning Extensions: PostGIS, pg_trgm, uuid-ossp Role-based access control Vacuuming, analyze, and autovacuum (31 cards)

1
Q

What are JSON and JSONB data types in PostgreSQL?

A

JSON stores data as text, while JSONB stores it in a binary format that allows indexing and faster querying.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between SERIAL, BIGSERIAL, and IDENTITY columns?

A

SERIAL and BIGSERIAL auto-increment via sequences (legacy), while IDENTITY (introduced in SQL standard) offers better compliance and control.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the RETURNING clause do in INSERT/UPDATE statements?

A

It returns specified columns (like inserted IDs) immediately after the operation, reducing the need for a follow-up SELECT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are arrays in PostgreSQL?

A

Arrays allow you to store multiple values of the same type in a single column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are enums in PostgreSQL?

A

Enums restrict a column to a predefined set of values, improving data consistency and readability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is table inheritance in PostgreSQL?

A

It allows a table to inherit columns and constraints from a parent table, useful for modeling polymorphic data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is partitioning in PostgreSQL?

A

Partitioning splits large tables into smaller pieces for better performance and maintainability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is PostGIS extension?

A

It adds support for geographic objects, enabling spatial queries like distance, overlap, and geometry operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the pg_trgm extension do?

A

It enables similarity-based text searching and indexing, useful for fuzzy matching and full-text search.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the uuid-ossp extension?

A

It provides functions to generate UUIDs, commonly used for unique identifiers in distributed systems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is role-based access control in PostgreSQL?

A

It allows granting permissions to users and groups (roles) to manage data access securely and flexibly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is vacuuming in PostgreSQL?

A

VACUUM reclaims storage by cleaning up dead tuples created by updates/deletes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does ANALYZE do in PostgreSQL?

A

It updates statistics used by the query planner to optimize query execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is autovacuum in PostgreSQL?

A

It’s a background process that automatically performs VACUUM and ANALYZE to maintain performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are advantages of JSONB over JSON?

A

JSONB supports indexing and is faster to query but requires more storage space and CPU for insertions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are best practices for using enums in PostgreSQL?

A

Use for static value sets; avoid if the list changes frequently since modifying enums requires DDL operations.

17
Q

What is a good use case for RETURNING clause?

A

Retrieving the auto-generated ID or timestamp after inserting a row, in a single atomic operation.

18
Q

What is a trade-off of table inheritance?

A

Inheritance doesn’t always play well with constraints or indexes, and parent queries may not hit child tables unless specified.

19
Q

What is the benefit of partitioning?

A

Improves performance for large datasets by reducing scan size and enabling parallel query execution.

20
Q

What are monitoring tools for PostgreSQL performance?

A

Use pg_stat_activity, pg_stat_user_tables, auto_explain, and third-party tools like pgBadger or pgHero.

21
Q

What is a real-world tradeoff of using UUIDs over SERIAL?

A

UUIDs offer global uniqueness and better security but are larger and slower to index than integer-based SERIALs.

22
Q

What is a potential gotcha with JSONB?

A

It’s not human-readable like JSON, and frequent updates can lead to bloat if not vacuumed properly.

23
Q

What’s a gotcha when using autovacuum?

A

It might not run frequently enough for high-update tables, causing bloat and degraded performance.

24
Q

What’s a best practice when using extensions like pg_trgm?

A

Only enable when needed due to extra storage and potential performance cost; monitor usage carefully.

25
What’s a common interview question around VACUUM?
Explain the difference between VACUUM, ANALYZE, and autovacuum, and how they impact performance.
26
What’s an architectural implication of using JSONB?
It enables schema-less designs in a relational DB but can blur the line between relational and NoSQL approaches.
27
What’s a use case for PostGIS?
Applications that require spatial data operations, like geolocation, route optimization, or proximity-based services.
28
What’s an advantage of RETURNING clause in system design?
Reduces network round trips and ensures atomicity between insert/update and read.
29
What’s the impact of using arrays in PostgreSQL?
They allow compact data modeling but can complicate queries and indexing.
30
What’s the downside of overusing extensions?
Increased maintenance complexity and risk of incompatibilities during upgrades or migrations.
31
What’s a best practice for roles in PostgreSQL?
Use roles to abstract permission sets and avoid directly assigning privileges to individual users.