Exploring Query Parameters, String Validations, and the Internals of PostgreSQL

Jaeyeol Lee

Hatched by Jaeyeol Lee

Sep 29, 2023

4 min read

0

Exploring Query Parameters, String Validations, and the Internals of PostgreSQL

Introduction:
In this article, we will delve into two different topics - query parameters and string validations in FastAPI, as well as the internals of PostgreSQL. While seemingly unrelated, these topics share commonalities in terms of their importance in software development and database management. By understanding how to effectively work with query parameters and validate strings in FastAPI, and gaining insights into the internals of PostgreSQL, developers can enhance their skills and make informed decisions when working with these technologies.

Query Parameters and String Validations in FastAPI:
FastAPI provides a convenient way to declare and validate parameters in API routes. By explicitly declaring required parameters and setting default values, developers can ensure that their code functions as expected. When a required parameter is not provided, both the editor and Python itself will alert the developer, preventing potential errors.

Additionally, FastAPI allows for the inclusion of additional information and validation for parameters, making it easier to document and understand the purpose of each parameter. This information is also included in the OpenAPI schema path operation, which aids in the automatic generation of documentation and provides a user-friendly interface.

While the default value approach is commonly used, it is recommended to use the Annotated module instead. This alternative offers several advantages and can improve code readability, maintainability, and reusability. For developers who find the use of default values uncomfortable, the Required module from Pydantic can be imported and utilized as an alternative.

Understanding the Internals of PostgreSQL:
To fully comprehend the functioning of PostgreSQL, it is crucial to explore its internal structure. A PostgreSQL database cluster consists of a base directory, which contains subdirectories and files. Each database within the cluster is represented by a subdirectory under the base directory, with tables and indexes stored as individual files within the respective database subdirectory.

Object identifiers (OIDs) are used to internally manage all database objects in PostgreSQL. OIDs of databases and heap tables are stored in the pg_database and pg_class tables, respectively. A PostgreSQL server operates on a single host and manages a single database cluster.

Tablespaces in PostgreSQL are directories that store data outside of the base directory. Tables and indexes smaller than 1GB are stored in a single file within the database directory. When the file size exceeds 1GB, PostgreSQL creates new files (e.g., relfilenode.1, relfilenode.2) to accommodate the data.

Furthermore, each table is associated with two additional files - the free space map (_fsm) and the visibility map (_vm). The free space map provides information about the available capacity on each page within the table file, while the visibility map stores details about page visibility. Indexes, on the other hand, only have individual free space maps.

Within a data file, such as a heap table or index, pages (or blocks) of fixed length are used. By default, these pages are 8192 bytes (8 KB) long. The B-tree index scan method is commonly used, where PostgreSQL reads an index file containing index tuples. Each tuple consists of an index key and a target heap tuple identifier (TID). Once the desired index tuple is found, PostgreSQL retrieves the corresponding heap tuple using the TID value.

Actionable Advice:

  1. Ensure that you explicitly declare and validate query parameters in FastAPI to prevent potential errors. Utilize the default value approach or consider using the Annotated module for improved code readability and maintainability.
  2. Familiarize yourself with the internal structure of PostgreSQL, including the concept of a database cluster, tablespaces, and the organization of tables and indexes. This knowledge will aid in optimizing database performance and troubleshooting.
  3. When working with PostgreSQL, pay attention to the associated files, such as the free space map and visibility map. Understanding their purpose and how they contribute to data storage and retrieval can help in fine-tuning database operations.

Conclusion:
By exploring the topics of query parameters and string validations in FastAPI, as well as gaining insights into the internals of PostgreSQL, developers can enhance their skills and make informed decisions when working with these technologies. Remember to explicitly declare and validate query parameters in FastAPI, consider alternative approaches like the Annotated module, and familiarize yourself with the internal structure of PostgreSQL for optimal database management.

Sources

← Back to Library

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)

Start Hatching 🐣