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)
What are JSON and JSONB data types in PostgreSQL?
JSON stores data as text, while JSONB stores it in a binary format that allows indexing and faster querying.
What is the difference between SERIAL, BIGSERIAL, and IDENTITY columns?
SERIAL and BIGSERIAL auto-increment via sequences (legacy), while IDENTITY (introduced in SQL standard) offers better compliance and control.
What does the RETURNING clause do in INSERT/UPDATE statements?
It returns specified columns (like inserted IDs) immediately after the operation, reducing the need for a follow-up SELECT.
What are arrays in PostgreSQL?
Arrays allow you to store multiple values of the same type in a single column.
What are enums in PostgreSQL?
Enums restrict a column to a predefined set of values, improving data consistency and readability.
What is table inheritance in PostgreSQL?
It allows a table to inherit columns and constraints from a parent table, useful for modeling polymorphic data.
What is partitioning in PostgreSQL?
Partitioning splits large tables into smaller pieces for better performance and maintainability.
What is PostGIS extension?
It adds support for geographic objects, enabling spatial queries like distance, overlap, and geometry operations.
What does the pg_trgm extension do?
It enables similarity-based text searching and indexing, useful for fuzzy matching and full-text search.
What is the uuid-ossp extension?
It provides functions to generate UUIDs, commonly used for unique identifiers in distributed systems.
What is role-based access control in PostgreSQL?
It allows granting permissions to users and groups (roles) to manage data access securely and flexibly.
What is vacuuming in PostgreSQL?
VACUUM reclaims storage by cleaning up dead tuples created by updates/deletes.
What does ANALYZE do in PostgreSQL?
It updates statistics used by the query planner to optimize query execution.
What is autovacuum in PostgreSQL?
It’s a background process that automatically performs VACUUM and ANALYZE to maintain performance.
What are advantages of JSONB over JSON?
JSONB supports indexing and is faster to query but requires more storage space and CPU for insertions.
What are best practices for using enums in PostgreSQL?
Use for static value sets; avoid if the list changes frequently since modifying enums requires DDL operations.
What is a good use case for RETURNING clause?
Retrieving the auto-generated ID or timestamp after inserting a row, in a single atomic operation.
What is a trade-off of table inheritance?
Inheritance doesn’t always play well with constraints or indexes, and parent queries may not hit child tables unless specified.
What is the benefit of partitioning?
Improves performance for large datasets by reducing scan size and enabling parallel query execution.
What are monitoring tools for PostgreSQL performance?
Use pg_stat_activity, pg_stat_user_tables, auto_explain, and third-party tools like pgBadger or pgHero.
What is a real-world tradeoff of using UUIDs over SERIAL?
UUIDs offer global uniqueness and better security but are larger and slower to index than integer-based SERIALs.
What is a potential gotcha with JSONB?
It’s not human-readable like JSON, and frequent updates can lead to bloat if not vacuumed properly.
What’s a gotcha when using autovacuum?
It might not run frequently enough for high-update tables, causing bloat and degraded performance.
What’s a best practice when using extensions like pg_trgm?
Only enable when needed due to extra storage and potential performance cost; monitor usage carefully.