{"id":20873,"date":"2024-02-17T11:03:15","date_gmt":"2024-02-17T17:03:15","guid":{"rendered":"http:\/\/www.designandexecute.com\/designs\/?p=20873"},"modified":"2024-09-17T11:04:39","modified_gmt":"2024-09-17T17:04:39","slug":"logical-vs-physical-backups-in-postgresql","status":"publish","type":"post","link":"https:\/\/www.designandexecute.com\/designs\/logical-vs-physical-backups-in-postgresql\/","title":{"rendered":"Logical VS Physical Backups in PostgreSQL"},"content":{"rendered":"\n<p>In PostgreSQL, <strong>logical backups<\/strong> and <strong>physical backups<\/strong> serve different purposes, and each has distinct characteristics, benefits, and use cases. Below is a breakdown of the key differences between these two types of backups.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Definition<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>: A logical backup consists of SQL statements that represent the schema and data of the database. It extracts the data as SQL commands (e.g., <code>INSERT<\/code>, <code>CREATE<\/code>, etc.) that can be used to recreate the database objects and populate them with data.<\/li><li><strong>Physical Backup<\/strong>: A physical backup involves copying the actual files that PostgreSQL uses to store database data, including the entire data directory (tables, indexes, configuration files, etc.) and, optionally, Write-Ahead Logs (WAL files) for point-in-time recovery (PITR).<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Tools Used<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>:<\/li><li>Performed using <code>pg_dump<\/code> (for a single database) or <code>pg_dumpall<\/code> (for all databases in the PostgreSQL instance).<\/li><li>The output is usually a text file (SQL script) or a binary file that can be restored later.<\/li><li><strong>Physical Backup<\/strong>:<\/li><li>Achieved using tools like <code>pg_basebackup<\/code>, low-level file system copies, or third-party backup tools.<\/li><li>Physical backups directly copy the PostgreSQL data directory (<code>PGDATA<\/code>) and WAL files.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Granularity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>:<\/li><li>Can back up individual tables, schemas, or the entire database.<\/li><li>Allows selective export of specific objects (like only backing up a certain schema or table).<\/li><li><strong>Physical Backup<\/strong>:<\/li><li>Always backs up the entire database cluster (all databases on the server). You cannot selectively back up individual databases, schemas, or tables.<\/li><li>Includes configuration files, transaction logs (WAL), and other critical files.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>Backup Format<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>:<\/li><li>Stored as SQL text files (<code>.sql<\/code>) or a custom binary format (when using <code>pg_dump<\/code> with the <code>-Fc<\/code> option).<\/li><li>This backup is portable across PostgreSQL versions and architectures, as it\u2019s just a set of SQL commands.<\/li><li><strong>Physical Backup<\/strong>:<\/li><li>A byte-for-byte copy of the data directory and WAL files. These backups are not portable across PostgreSQL versions or between different hardware architectures.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>Restoration Process<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>:<\/li><li>Restored using <code>psql<\/code> (for SQL format backups) or <code>pg_restore<\/code> (for custom format backups).<\/li><li>The restoration process recreates the schema and inserts the data back into the database.<\/li><li>Restoration can be slow for large databases because data must be reinserted row by row.<\/li><li><strong>Physical Backup<\/strong>:<\/li><li>Restored by copying the files back into the PostgreSQL data directory.<\/li><li>Typically much faster for large databases since the entire database is restored as files, without needing to process individual SQL statements.<\/li><li>For point-in-time recovery (PITR), you apply WAL files after restoring the base backup.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>Performance Impact During Backup<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>:<\/li><li>The database remains online, but logical backups can impact performance, especially in large databases with a high write load. This is because <code>pg_dump<\/code> reads data directly from the database tables.<\/li><li>It uses CPU and memory resources, especially if complex queries or large datasets are involved.<\/li><li><strong>Physical Backup<\/strong>:<\/li><li>Can be performed with minimal impact on performance, especially if done with <code>pg_basebackup<\/code>, which uses PostgreSQL&#8217;s internal mechanisms to take a consistent snapshot of the data directory without locking tables.<\/li><li>However, file system-level copies (without <code>pg_basebackup<\/code>) may require the database to be stopped to ensure consistency unless you use special tools (e.g., LVM snapshots).<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>Consistency<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>:<\/li><li>Ensures consistency of the database at the time the backup is taken. However, if the database is under heavy write operations, you may need to use options like <code>--serializable-deferrable<\/code> to ensure a consistent snapshot.<\/li><li><strong>Physical Backup<\/strong>:<\/li><li>Always consistent if taken correctly (e.g., via <code>pg_basebackup<\/code> or file system snapshot). When combined with WAL archiving, you can restore the database to a precise point in time (PITR).<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>Backup Size<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>:<\/li><li>Logical backups are typically larger than physical backups for the same database because SQL commands and other metadata are included. Compression options (<code>-Fc<\/code> for <code>pg_dump<\/code>) can help reduce the size, but this requires extra processing.<\/li><li><strong>Physical Backup<\/strong>:<\/li><li>Physical backups are generally more space-efficient since they are raw data files. They are essentially a direct copy of the database&#8217;s data directory, without the overhead of SQL statements.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>Use Cases<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical Backup<\/strong>:<\/li><li>Best for smaller databases, selective backups (e.g., a single table or schema), and when migrating data across PostgreSQL versions or architectures.<\/li><li>Suitable for cross-platform migrations since SQL dumps are portable.<\/li><li>Ideal for version upgrades where a dump\/restore method is required.<\/li><li><strong>Physical Backup<\/strong>:<\/li><li>Ideal for large databases, minimizing downtime during restoration, and when fast recovery is necessary (e.g., disaster recovery).<\/li><li>Best for point-in-time recovery (PITR) scenarios, replication setups, or when you want an exact replica of the database server.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>Advantages and Disadvantages<\/strong><\/h3>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>Feature<\/th><th>Logical Backup<\/th><th>Physical Backup<\/th><\/tr><\/thead><tbody><tr><td><strong>Portability<\/strong><\/td><td>Highly portable across versions\/architectures<\/td><td>Not portable across different versions\/architectures<\/td><\/tr><tr><td><strong>Granularity<\/strong><\/td><td>Can backup individual objects (tables, schemas)<\/td><td>Backs up the entire database cluster<\/td><\/tr><tr><td><strong>Performance Impact<\/strong><\/td><td>Higher (especially for large databases)<\/td><td>Lower impact on performance<\/td><\/tr><tr><td><strong>Backup Size<\/strong><\/td><td>Larger due to SQL text<\/td><td>Generally smaller (raw data files)<\/td><\/tr><tr><td><strong>Consistency<\/strong><\/td><td>Consistent but may require options like <code>--serializable<\/code> for accuracy<\/td><td>Always consistent (snapshot + WAL)<\/td><\/tr><tr><td><strong>Restoration Speed<\/strong><\/td><td>Slower, as it requires running SQL statements<\/td><td>Fast, restoring files is quicker<\/td><\/tr><tr><td><strong>Use Case<\/strong><\/td><td>Suitable for small\/medium DBs and cross-version migrations<\/td><td>Ideal for large DBs, replication, and PITR<\/td><\/tr><\/tbody><\/table>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Logical backups<\/strong> (via <code>pg_dump<\/code>\/<code>pg_dumpall<\/code>) are useful for portability, smaller databases, selective backups, and schema migrations. They are slower to restore but more flexible.<\/li><li><strong>Physical backups<\/strong> (via <code>pg_basebackup<\/code> or file system copies) are best for large databases, exact replication, fast disaster recovery, and point-in-time recovery (PITR). They are faster to restore but less flexible for migration or selective recovery.<\/li><\/ul>\n\n\n\n<p>Both types of backups can play an important role in a comprehensive backup and disaster recovery strategy, depending on the needs of the environment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PostgreSQL, logical backups and physical backups serve different purposes, and each has distinct characteristics, benefits, and use cases. Below is a breakdown of the key differences between these two types of backups. 1. Definition Logical Backup: A logical backup consists of SQL statements that represent the schema and data of the database. It extracts [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":20862,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-20873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bi-data-warehouse"],"jetpack_featured_media_url":"https:\/\/www.designandexecute.com\/designs\/wp-content\/uploads\/2024\/09\/242-2423721_logo-postgresql-4263414132.jpg","_links":{"self":[{"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/posts\/20873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/comments?post=20873"}],"version-history":[{"count":1,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/posts\/20873\/revisions"}],"predecessor-version":[{"id":20874,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/posts\/20873\/revisions\/20874"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/media\/20862"}],"wp:attachment":[{"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/media?parent=20873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/categories?post=20873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.designandexecute.com\/designs\/wp-json\/wp\/v2\/tags?post=20873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}