<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shiv Iyer</title>
    <description>The latest articles on Forem by Shiv Iyer (@shiviyer).</description>
    <link>https://forem.com/shiviyer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1250416%2Ffba27393-a20d-48ba-b3d4-48ef370f9d91.jpeg</url>
      <title>Forem: Shiv Iyer</title>
      <link>https://forem.com/shiviyer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shiviyer"/>
    <language>en</language>
    <item>
      <title>Common pitfalls and solutions for mysqldump/xtrabackup-based SSTs</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Sat, 15 Feb 2025 11:43:58 +0000</pubDate>
      <link>https://forem.com/shiviyer/common-pitfalls-and-solutions-for-mysqldumpxtrabackup-based-ssts-2j74</link>
      <guid>https://forem.com/shiviyer/common-pitfalls-and-solutions-for-mysqldumpxtrabackup-based-ssts-2j74</guid>
      <description>&lt;p&gt;State Snapshot Transfers (SST) are critical for maintaining Galera Cluster health, but misconfigurations and resource constraints often lead to failures. Below are common pitfalls and solutions for &lt;code&gt;mysqldump&lt;/code&gt;/&lt;code&gt;xtrabackup&lt;/code&gt;-based SSTs, informed by recent cluster management best practices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common SST Errors &amp;amp; Fixes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Flow Control Overload During Heavy Operations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symptoms&lt;/strong&gt;: Cluster stalls during &lt;code&gt;mysqldump&lt;/code&gt; or &lt;code&gt;OPTIMIZE TABLE&lt;/code&gt; commands, with warnings like &lt;code&gt;WSREP: TO isolation failed&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Root Cause&lt;/strong&gt;: Write-set replication overwhelms cluster bandwidth, triggering flow control pauses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Adjust flow control parameters
wsrep_provider_options = "gcs.fc_limit=500; gcs.fc_master_slave=YES; gcs.fc_factor=1.0"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Monitor &lt;code&gt;wsrep_flow_control_paused&lt;/code&gt; to validate improvements.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Xtrabackup Authentication Failures
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symptoms&lt;/strong&gt;: SST aborts with &lt;code&gt;Access denied&lt;/code&gt; errors despite correct credentials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Root Cause&lt;/strong&gt;: Mismatched &lt;code&gt;wsrep_sst_auth&lt;/code&gt; values or missing MySQL user privileges.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;Ensure uniformity across nodes:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wsrep_sst_auth = "sst_user:secure_password"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Grant &lt;code&gt;RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT&lt;/code&gt; to the SST user.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  3. Version Incompatibility
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symptoms&lt;/strong&gt;: SST hangs or crashes due to mismatched &lt;code&gt;xtrabackup&lt;/code&gt;/Galera versions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;Use identical &lt;code&gt;xtrabackup&lt;/code&gt; versions on all nodes.&lt;/li&gt;
&lt;li&gt;For Galera 8.0.22+, prefer the &lt;code&gt;clone&lt;/code&gt; method for MySQL-native SSTs.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  4. Network &amp;amp; Port Configuration Issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symptoms&lt;/strong&gt;: Joiner nodes stuck in &lt;code&gt;Waiting on SST&lt;/code&gt; state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Root Cause&lt;/strong&gt;: Blocked ports (4567, 4568) or misconfigured firewalls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Verify port accessibility&lt;/span&gt;
nc &lt;span class="nt"&gt;-zv&lt;/span&gt; &amp;lt;donor_ip&amp;gt; 4568
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Whitelist SST ports in firewalls and SELinux.&lt;/p&gt;
&lt;h3&gt;
  
  
  5. Partial Transfers &amp;amp; Node Crashes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symptoms&lt;/strong&gt;: Donor crashes mid-SST, leaving &lt;code&gt;rsync&lt;/code&gt;/&lt;code&gt;xtrabackup&lt;/code&gt; processes orphaned.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;Terminate stalled processes manually:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pkill &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s1"&gt;'wsrep_sst|rsync|xtrabackup'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Enable crash-safe SST scripts with &lt;code&gt;wsrep_sst_receive&lt;/code&gt; logging.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  SST Method Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Speed&lt;/th&gt;
&lt;th&gt;Donor Blocking&lt;/th&gt;
&lt;th&gt;Requirements&lt;/th&gt;
&lt;th&gt;Best For&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mysqldump&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Slow&lt;/td&gt;
&lt;td&gt;Full&lt;/td&gt;
&lt;td&gt;Minimal setup&lt;/td&gt;
&lt;td&gt;Small datasets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;xtrabackup&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Partial (DDLs)&lt;/td&gt;
&lt;td&gt;Consistent InnoDB configs&lt;/td&gt;
&lt;td&gt;Live clusters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rsync&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Full&lt;/td&gt;
&lt;td&gt;Identical filesystem layouts&lt;/td&gt;
&lt;td&gt;Homogeneous environments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;clone&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Minimal&lt;/td&gt;
&lt;td&gt;MySQL 8.0.22+&lt;/td&gt;
&lt;td&gt;Cloud-native clusters&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Proactive SST Management
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prefer IST Over SST&lt;/strong&gt;: Use Incremental State Transfers for rejoining nodes with minor lag.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor Metrics&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wsrep_local_state_comment&lt;/code&gt;: Track &lt;code&gt;Joiner&lt;/code&gt;/&lt;code&gt;Donor&lt;/code&gt; states.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wsrep_sst_donor_rejects&lt;/code&gt;: Identify donor eligibility issues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scriptable Customization&lt;/strong&gt;: Use &lt;code&gt;wsrep_sst_method = script&lt;/code&gt; with custom handlers for edge cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By addressing these pitfalls through configuration hardening and monitoring, administrators can reduce SST-related downtime by up to 70%. For large-scale deployments, integrate automated health checks using tools like Galera Manager to preemptively flag SST risks.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://minervadb.xyz/forecast-mysql-iops/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2025%2F02%2Fshutterstock_1307086.jpg" height="600" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://minervadb.xyz/forecast-mysql-iops/" rel="noopener noreferrer" class="c-link"&gt;
          Forecast MySQL IOPS - MySQL Consulting - MySQL DBA Support
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Forecast MySQL IOPS - MySQL Consulting - MySQL DBA Support - MySQL Tips - MySQL Remote DBA - MySQL Troubleshooting
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2020%2F10%2Fcropped-LogoColorTextRight-32x32.jpeg" width="32" height="32"&gt;
        minervadb.xyz
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;




&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://minervadb.xyz/large-scale-postgresql-database-migrations/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2025%2F02%2Fshutterstock_2354828227-scaled.jpg" height="533" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://minervadb.xyz/large-scale-postgresql-database-migrations/" rel="noopener noreferrer" class="c-link"&gt;
          PostgreSQL Database Migration: Best Practices
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Optimize your PostgreSQL database migration with best practices for seamless transitions, performance tuning, and minimal downtime
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2020%2F10%2Fcropped-LogoColorTextRight-32x32.jpeg" width="32" height="32"&gt;
        minervadb.xyz
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>mysql</category>
      <category>galeracluster</category>
      <category>dba</category>
      <category>mysqltips</category>
    </item>
    <item>
      <title>Performance Tips for Developers Using Postgres and pgvector</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Wed, 12 Feb 2025 09:15:35 +0000</pubDate>
      <link>https://forem.com/shiviyer/performance-tips-for-developers-using-postgres-and-pgvector-l7g</link>
      <guid>https://forem.com/shiviyer/performance-tips-for-developers-using-postgres-and-pgvector-l7g</guid>
      <description>&lt;p&gt;PostgreSQL with pgvector offers powerful capabilities for vector similarity searches, but optimizing performance requires careful consideration. Here are key performance tips for developers using Postgres and pgvector:&lt;/p&gt;

&lt;h2&gt;
  
  
  Indexing Strategies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use Appropriate Indexes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Implement vector indexes for large datasets to enable approximate nearest neighbor (ANN) searching[4].&lt;/li&gt;
&lt;li&gt;Consider HNSW indexes for better query performance, especially with pgvector 0.5 and later versions[4].&lt;/li&gt;
&lt;li&gt;Balance index usage, as excessive indexing can negatively impact overall database performance[3].&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Optimize Index Parameters
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Adjust the &lt;code&gt;list&lt;/code&gt; size for your index based on your dataset size[4].&lt;/li&gt;
&lt;li&gt;A general guideline is to set &lt;code&gt;lists = number of rows / 1000&lt;/code&gt;[4].&lt;/li&gt;
&lt;li&gt;Fine-tune the &lt;code&gt;probes&lt;/code&gt; parameter:

&lt;ul&gt;
&lt;li&gt;For tables up to 1 million rows: set &lt;code&gt;probes = lists / 10&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For larger datasets: set &lt;code&gt;probes = sqrt(lists)&lt;/code&gt;[6]&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Query Optimization
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Leverage EXPLAIN ANALYZE
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use the EXPLAIN ANALYZE command to understand query execution plans and identify performance bottlenecks[8].&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Refine Query Structure
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Break complex queries into smaller, more manageable parts[8].&lt;/li&gt;
&lt;li&gt;Use JOINs instead of subqueries where possible for better performance[1].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Database Design
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Partitioning
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Consider partitioning large tables to improve query performance and data management[3][18].&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Normalize and Denormalize Wisely
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Properly normalize your database schema to ensure data integrity and reduce redundancy[1].&lt;/li&gt;
&lt;li&gt;Consider strategic denormalization for read-heavy workloads to improve query speed[1].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Hardware and Configuration
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Optimize Hardware Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Ensure sufficient RAM for caching data and reducing disk I/O[1].&lt;/li&gt;
&lt;li&gt;Use SSDs for improved read and write performance, especially for random access operations[1].&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tune PostgreSQL Settings
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Adjust &lt;code&gt;shared_buffers&lt;/code&gt; to about 25-40% of total system RAM[1].&lt;/li&gt;
&lt;li&gt;Configure &lt;code&gt;work_mem&lt;/code&gt; appropriately for complex query operations[1].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Vector-Specific Optimizations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Choose Appropriate Distance Metrics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Prefer inner-product to L2 or Cosine distances if your vectors are normalized (e.g., for &lt;code&gt;text-embedding-ada-002&lt;/code&gt;)[2].&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pre-warm the Database
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Implement a warm-up technique before transitioning to production to ensure optimal performance[2].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Monitoring and Maintenance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Regular VACUUM and ANALYZE
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Schedule regular VACUUM operations to prevent table bloat and maintain performance[1].&lt;/li&gt;
&lt;li&gt;Use ANALYZE to update statistics, helping the query planner make better decisions[1].&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Monitor and Adjust
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Continuously monitor query performance and adjust indexes and configurations as your dataset grows[4].&lt;/li&gt;
&lt;li&gt;Reindex and review settings when your data volume increases significantly (e.g., by 50% or more)[4].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By implementing these tips, developers can significantly improve the performance of their PostgreSQL and pgvector implementations, ensuring efficient and scalable vector similarity searches in their applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;[1] PostgreSQL tuning: 6 things you can do to improve DB performance &lt;a href="https://www.instaclustr.com/education/postgresql-tuning-6-things-you-can-do-to-improve-db-performance/" rel="noopener noreferrer"&gt;https://www.instaclustr.com/education/postgresql-tuning-6-things-you-can-do-to-improve-db-performance/&lt;/a&gt;&lt;br&gt;
[2] pgvector 0.4.0 performance - Supabase &lt;a href="https://supabase.com/blog/pgvector-performance" rel="noopener noreferrer"&gt;https://supabase.com/blog/pgvector-performance&lt;/a&gt;&lt;br&gt;
[3] PostgreSQL Performance Tuning and Optimization Guide - Sematext &lt;a href="https://sematext.com/blog/postgresql-performance-tuning/" rel="noopener noreferrer"&gt;https://sematext.com/blog/postgresql-performance-tuning/&lt;/a&gt;&lt;br&gt;
[4] Performance Tips Using Postgres and pgvector | Crunchy Data Blog &lt;a href="https://www.crunchydata.com/blog/pgvector-performance-for-developers" rel="noopener noreferrer"&gt;https://www.crunchydata.com/blog/pgvector-performance-for-developers&lt;/a&gt;&lt;br&gt;
[5] General Guide to PostgreSQL Performance Tuning and Optimization &lt;a href="https://www.devart.com/dbforge/postgresql/studio/postgresql-performance-tuning-and-optimization.html" rel="noopener noreferrer"&gt;https://www.devart.com/dbforge/postgresql/studio/postgresql-performance-tuning-and-optimization.html&lt;/a&gt;&lt;br&gt;
[6] Optimize pgvector search - Neon Docs &lt;a href="https://neon.tech/docs/ai/ai-vector-search-optimization" rel="noopener noreferrer"&gt;https://neon.tech/docs/ai/ai-vector-search-optimization&lt;/a&gt;&lt;br&gt;
[7] PostgreSQL Performance Tuning: Optimize Your Database Server &lt;a href="https://www.enterprisedb.com/postgres-tutorials/introduction-postgresql-performance-tuning-and-optimization" rel="noopener noreferrer"&gt;https://www.enterprisedb.com/postgres-tutorials/introduction-postgresql-performance-tuning-and-optimization&lt;/a&gt;&lt;br&gt;
[8] Tips for PostgreSQL Query Optimization: EXPLAIN ANALYZE - EDB &lt;a href="https://www.enterprisedb.com/blog/postgresql-query-optimization-performance-tuning-with-explain-analyze" rel="noopener noreferrer"&gt;https://www.enterprisedb.com/blog/postgresql-query-optimization-performance-tuning-with-explain-analyze&lt;/a&gt;&lt;br&gt;
[9] Tips for Optimizing PostgreSQL Queries - Airbyte &lt;a href="https://airbyte.com/blog/tips-for-optimizing-postgresql-queries" rel="noopener noreferrer"&gt;https://airbyte.com/blog/tips-for-optimizing-postgresql-queries&lt;/a&gt;&lt;br&gt;
[10] Documentation: 17: Chapter 14. Performance Tips - PostgreSQL &lt;a href="https://www.postgresql.org/docs/current/performance-tips.html" rel="noopener noreferrer"&gt;https://www.postgresql.org/docs/current/performance-tips.html&lt;/a&gt;&lt;br&gt;
[11] Optimize performance when using pgvector in Azure Database for ... &lt;a href="https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-optimize-performance-pgvector" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-optimize-performance-pgvector&lt;/a&gt;&lt;br&gt;
[12] Best Practices for Postgres Performance - Timescale &lt;a href="https://www.timescale.com/learn/postgres-performance-best-practices" rel="noopener noreferrer"&gt;https://www.timescale.com/learn/postgres-performance-best-practices&lt;/a&gt;&lt;br&gt;
[13] Optimizing vector search performance with pgvector - Neon &lt;a href="https://neon.tech/blog/optimizing-vector-search-performance-with-pgvector" rel="noopener noreferrer"&gt;https://neon.tech/blog/optimizing-vector-search-performance-with-pgvector&lt;/a&gt;&lt;br&gt;
[14] 13 Tips to Improve PostgreSQL Insert Performance - Timescale &lt;a href="https://www.timescale.com/blog/13-tips-to-improve-postgresql-insert-performance" rel="noopener noreferrer"&gt;https://www.timescale.com/blog/13-tips-to-improve-postgresql-insert-performance&lt;/a&gt;&lt;br&gt;
[15] The 150x pgvector speedup: a year-in-review | Jonathan Katz &lt;a href="https://jkatz05.com/post/postgres/pgvector-performance-150x-speedup/" rel="noopener noreferrer"&gt;https://jkatz05.com/post/postgres/pgvector-performance-150x-speedup/&lt;/a&gt;&lt;br&gt;
[16] PostgreSQL performance tips you have never seen before | Citus Con &lt;a href="https://www.youtube.com/watch?v=m8ogrogKjXo" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=m8ogrogKjXo&lt;/a&gt;&lt;br&gt;
[17] pgvector 0.6.0: 30x faster with parallel index builds - Supabase &lt;a href="https://supabase.com/blog/pgvector-fast-builds" rel="noopener noreferrer"&gt;https://supabase.com/blog/pgvector-fast-builds&lt;/a&gt;&lt;br&gt;
[18] Mastering PostgreSQL for Developers - Remote.It &lt;a href="https://www.remote.it/resources/mastering-postgresql-for-developers" rel="noopener noreferrer"&gt;https://www.remote.it/resources/mastering-postgresql-for-developers&lt;/a&gt;&lt;br&gt;
[19] Faster similarity search performance with pgvector indexes &lt;a href="https://cloud.google.com/blog/products/databases/faster-similarity-search-performance-with-pgvector-indexes?hl=en" rel="noopener noreferrer"&gt;https://cloud.google.com/blog/products/databases/faster-similarity-search-performance-with-pgvector-indexes?hl=en&lt;/a&gt;&lt;br&gt;
[20] Speed up PostgreSQL® pgvector queries with indexes - Aiven &lt;a href="https://aiven.io/developer/postgresql-pgvector-indexes" rel="noopener noreferrer"&gt;https://aiven.io/developer/postgresql-pgvector-indexes&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>vectordatabase</category>
      <category>sql</category>
    </item>
    <item>
      <title>What are some practical use cases for the MERGE command in data warehousing?</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Wed, 12 Feb 2025 09:10:04 +0000</pubDate>
      <link>https://forem.com/shiviyer/what-are-some-practical-use-cases-for-the-merge-command-in-data-warehousing-50da</link>
      <guid>https://forem.com/shiviyer/what-are-some-practical-use-cases-for-the-merge-command-in-data-warehousing-50da</guid>
      <description>&lt;p&gt;The MERGE command in SQL, particularly in data warehousing scenarios, offers several practical use cases that streamline data manipulation and improve efficiency. Here are some key applications:&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Synchronization
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Slowly Changing Dimensions (SCD)
&lt;/h3&gt;

&lt;p&gt;MERGE is particularly useful for maintaining Slowly Changing Dimensions in data warehouses[3]. It allows for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inserting new records&lt;/li&gt;
&lt;li&gt;Updating existing records&lt;/li&gt;
&lt;li&gt;Flagging or removing outdated records&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This process can be accomplished in a single, atomic operation, ensuring data consistency.&lt;/p&gt;

&lt;h3&gt;
  
  
  Incremental Updates
&lt;/h3&gt;

&lt;p&gt;MERGE facilitates efficient incremental updates from operational databases to data warehouses[2]. This is crucial for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Periodic data movement&lt;/li&gt;
&lt;li&gt;Keeping warehouse data in sync with source systems&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ETL Processes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Data Loading
&lt;/h3&gt;

&lt;p&gt;MERGE simplifies data loading from external sources, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Foreign data wrappers&lt;/li&gt;
&lt;li&gt;Staged and batched process jobs[16]&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Change Data Capture (CDC)
&lt;/h3&gt;

&lt;p&gt;MERGE is effective for implementing and validating change data capture processes[1]. It allows for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conditional insertion of new data&lt;/li&gt;
&lt;li&gt;Updating changed records&lt;/li&gt;
&lt;li&gt;Deleting obsolete information&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Inventory Management
&lt;/h2&gt;

&lt;p&gt;MERGE can be used to update inventory levels based on sales data. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Updating product quantities&lt;/li&gt;
&lt;li&gt;Removing products when inventory reaches zero[4]&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Customer Data Management
&lt;/h2&gt;

&lt;p&gt;MERGE is valuable for maintaining up-to-date customer information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inserting new customer records&lt;/li&gt;
&lt;li&gt;Updating existing customer details&lt;/li&gt;
&lt;li&gt;Handling complex scenarios like merging duplicate accounts&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Sensor Data Processing
&lt;/h2&gt;

&lt;p&gt;For IoT and sensor-based systems, MERGE can efficiently handle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Streaming data from remote sensors&lt;/li&gt;
&lt;li&gt;Periodic and intermittent data measures&lt;/li&gt;
&lt;li&gt;Updating last known values while preserving creation timestamps[16]&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data Deduplication
&lt;/h2&gt;

&lt;p&gt;MERGE allows for complex operations like deduplicating data in a single statement[13]. This is particularly useful in scenarios where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple data sources may contain overlapping information&lt;/li&gt;
&lt;li&gt;Historical data needs to be consolidated&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By leveraging the MERGE command, data warehouse professionals can significantly simplify their ETL processes, improve data quality, and ensure more efficient data management across various use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;[1] Use the new SQL commands MERGE and QUALIFY to implement ... &lt;a href="https://aws.amazon.com/blogs/big-data/use-the-new-sql-commands-merge-and-qualify-to-implement-and-validate-change-data-capture-in-amazon-redshift/" rel="noopener noreferrer"&gt;https://aws.amazon.com/blogs/big-data/use-the-new-sql-commands-merge-and-qualify-to-implement-and-validate-change-data-capture-in-amazon-redshift/&lt;/a&gt;&lt;br&gt;
[2] SQL Server - How to Use Merge Statement? - DbSchema &lt;a href="https://dbschema.com/2023/07/16/sqlserver/merge-statement/" rel="noopener noreferrer"&gt;https://dbschema.com/2023/07/16/sqlserver/merge-statement/&lt;/a&gt;&lt;br&gt;
[3] Understanding the SQL MERGE statement - SQLShack &lt;a href="https://www.sqlshack.com/understanding-the-sql-merge-statement/" rel="noopener noreferrer"&gt;https://www.sqlshack.com/understanding-the-sql-merge-statement/&lt;/a&gt;&lt;br&gt;
[4] MERGE (Transact-SQL) - SQL Server - Microsoft Learn &lt;a href="https://learn.microsoft.com/pl-pl/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver16" rel="noopener noreferrer"&gt;https://learn.microsoft.com/pl-pl/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver16&lt;/a&gt;&lt;br&gt;
[5] The PostgreSQL MERGE command – a useful tool to make your ... &lt;a href="https://www.postgresql.fastware.com/blog/the-postgresql-merge-command-a-useful-tool-to-make-your-code-more-efficient" rel="noopener noreferrer"&gt;https://www.postgresql.fastware.com/blog/the-postgresql-merge-command-a-useful-tool-to-make-your-code-more-efficient&lt;/a&gt;&lt;br&gt;
[6] MERGE - Oracle Help Center &lt;a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/MERGE.html" rel="noopener noreferrer"&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/MERGE.html&lt;/a&gt;&lt;br&gt;
[7] The MERGE Command Has Finally Arrived in Postgres 15! - EDB &lt;a href="https://www.enterprisedb.com/blog/merge-command-has-finally-arrived-postgres-15" rel="noopener noreferrer"&gt;https://www.enterprisedb.com/blog/merge-command-has-finally-arrived-postgres-15&lt;/a&gt;&lt;br&gt;
[8] Documentation: 17: MERGE - PostgreSQL &lt;a href="https://www.postgresql.org/docs/current/sql-merge.html" rel="noopener noreferrer"&gt;https://www.postgresql.org/docs/current/sql-merge.html&lt;/a&gt;&lt;br&gt;
[9] Merging a database - IBM &lt;a href="https://www.ibm.com/docs/en/szmfrz/2.5.1?topic=guide-merging-database" rel="noopener noreferrer"&gt;https://www.ibm.com/docs/en/szmfrz/2.5.1?topic=guide-merging-database&lt;/a&gt;&lt;br&gt;
[10] Mastering Data Manipulation with MERGE Command in PostgreSQL ... &lt;a href="https://data-nerd.blog/2023/06/12/merge-in-postgresql-15-for-easy-data-manipulation/" rel="noopener noreferrer"&gt;https://data-nerd.blog/2023/06/12/merge-in-postgresql-15-for-easy-data-manipulation/&lt;/a&gt;&lt;br&gt;
[11] Stage and Merge Data Warehouse Replication - Oracle Help Center &lt;a href="https://docs.oracle.com/en/middleware/goldengate/big-data/19.1/gadbd/stage-and-merge-data-warehouse-replication.html" rel="noopener noreferrer"&gt;https://docs.oracle.com/en/middleware/goldengate/big-data/19.1/gadbd/stage-and-merge-data-warehouse-replication.html&lt;/a&gt;&lt;br&gt;
[12] Postgres merge example using other table - Stack Overflow &lt;a href="https://stackoverflow.com/questions/61772837/postgres-merge-example-using-other-table" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/61772837/postgres-merge-example-using-other-table&lt;/a&gt;&lt;br&gt;
[13] MERGE INTO | Databricks on AWS &lt;a href="https://docs.databricks.com/en/sql/language-manual/delta-merge-into.html" rel="noopener noreferrer"&gt;https://docs.databricks.com/en/sql/language-manual/delta-merge-into.html&lt;/a&gt;&lt;br&gt;
[14] Performing MERGE in PostgreSQL - OptimalBI &lt;a href="https://www.optimalbi.com/post/performing-merge-in-postgresql" rel="noopener noreferrer"&gt;https://www.optimalbi.com/post/performing-merge-in-postgresql&lt;/a&gt;&lt;br&gt;
[15] Data Merging Essentials: Process, Benefits and Use-Cases | Astera &lt;a href="https://www.astera.com/type/blog/data-merging/" rel="noopener noreferrer"&gt;https://www.astera.com/type/blog/data-merging/&lt;/a&gt;&lt;br&gt;
[16] A Look at Postgres 15: MERGE Command with Examples &lt;a href="https://www.crunchydata.com/blog/a-look-at-postgres-15-merge-command-with-examples" rel="noopener noreferrer"&gt;https://www.crunchydata.com/blog/a-look-at-postgres-15-merge-command-with-examples&lt;/a&gt;&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>vectordatabase</category>
      <category>analytics</category>
      <category>sql</category>
    </item>
    <item>
      <title>Easier Upgrades and Image Management for Postgres in Kubernetes</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Wed, 12 Feb 2025 08:35:37 +0000</pubDate>
      <link>https://forem.com/shiviyer/easier-upgrades-and-image-management-for-postgres-in-kubernetes-32nd</link>
      <guid>https://forem.com/shiviyer/easier-upgrades-and-image-management-for-postgres-in-kubernetes-32nd</guid>
      <description>&lt;p&gt;Upgrading and managing PostgreSQL in Kubernetes has become significantly easier with recent advancements in Kubernetes operators and tools. Here's an overview of the current state and best practices for PostgreSQL upgrades and image management in Kubernetes:&lt;/p&gt;

&lt;h2&gt;
  
  
  Major Version Upgrades
&lt;/h2&gt;

&lt;p&gt;Major version upgrades for PostgreSQL in Kubernetes have traditionally been challenging, but new tools and methods are making this process more manageable:&lt;/p&gt;

&lt;h3&gt;
  
  
  CloudNativePG Approach
&lt;/h3&gt;

&lt;p&gt;CloudNativePG, a Kubernetes operator for PostgreSQL, offers multiple options for major version upgrades[6]:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Major offline upgrades&lt;/strong&gt; using the import feature&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Seamless major online upgrades&lt;/strong&gt; utilizing import and logical replication&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In-place offline upgrades&lt;/strong&gt; using &lt;code&gt;pg_upgrade&lt;/code&gt; (upcoming feature)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This flexibility allows users to choose the upgrade method that best fits their specific requirements and downtime constraints.&lt;/p&gt;

&lt;h3&gt;
  
  
  Crunchy Postgres for Kubernetes
&lt;/h3&gt;

&lt;p&gt;Crunchy Postgres for Kubernetes has implemented a streamlined process for major version upgrades[7]. This operator-based approach simplifies the upgrade process, making it more accessible and less error-prone for users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Image Management
&lt;/h2&gt;

&lt;p&gt;Effective image management is crucial for maintaining and upgrading PostgreSQL in Kubernetes:&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom Images
&lt;/h3&gt;

&lt;p&gt;While not always necessary, custom PostgreSQL images can be beneficial in certain scenarios[5]:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CI/CD pipelines&lt;/strong&gt;: Custom images allow for specific configurations to be baked into the image, adhering to immutable infrastructure principles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specialized requirements&lt;/strong&gt;: When specific extensions or configurations are needed that aren't available in standard images.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Related Images Feature
&lt;/h3&gt;

&lt;p&gt;Crunchy Postgres for Kubernetes introduced a "related images" feature[7], which simplifies image management by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allowing easier updates to PostgreSQL and related components&lt;/li&gt;
&lt;li&gt;Streamlining the process of keeping all components in sync&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;p&gt;To ensure smooth upgrades and efficient image management:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Regular updates&lt;/strong&gt;: Keep your PostgreSQL instances up to date with minor version updates to simplify major upgrades when necessary[7].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Testing&lt;/strong&gt;: Always test upgrades in a non-production environment before applying them to production databases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Backup strategy&lt;/strong&gt;: Implement a robust backup strategy to safeguard data during upgrades[5].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Kubernetes operators&lt;/strong&gt;: Leverage specialized PostgreSQL operators for Kubernetes, which often provide built-in upgrade and management capabilities[6][7].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consider logical replication&lt;/strong&gt;: For minimal downtime during major upgrades, consider using logical replication methods[6].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Immutable infrastructure&lt;/strong&gt;: When possible, treat your PostgreSQL instances as immutable and replace them entirely during upgrades rather than modifying existing instances[5].&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these practices and leveraging the latest tools and operators, managing PostgreSQL upgrades and images in Kubernetes becomes more straightforward and less risky. As the ecosystem continues to evolve, we can expect even more improvements in this area, further simplifying database management in Kubernetes environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;p&gt;[1] kube-pg-upgrade.md - GitHub &lt;a href="https://github.com/containerinfra/kube-pg-upgrade/blob/main/docs/kube-pg-upgrade.md" rel="noopener noreferrer"&gt;https://github.com/containerinfra/kube-pg-upgrade/blob/main/docs/kube-pg-upgrade.md&lt;/a&gt;&lt;br&gt;
[2] How to Use the Postgres Docker Official Image &lt;a href="https://www.docker.com/blog/how-to-use-the-postgres-docker-official-image/" rel="noopener noreferrer"&gt;https://www.docker.com/blog/how-to-use-the-postgres-docker-official-image/&lt;/a&gt;&lt;br&gt;
[3] Upgrade a PostgreSQL pod to next major version - Avisi Cloud &lt;a href="https://docs.avisi.cloud/docs/runbooks/upgrade-postgres-on-k8s" rel="noopener noreferrer"&gt;https://docs.avisi.cloud/docs/runbooks/upgrade-postgres-on-k8s&lt;/a&gt;&lt;br&gt;
[4] Postgres Major Version Upgrade &lt;a href="https://access.crunchydata.com/documentation/postgres-operator/latest/guides/major-postgres-version-upgrade" rel="noopener noreferrer"&gt;https://access.crunchydata.com/documentation/postgres-operator/latest/guides/major-postgres-version-upgrade&lt;/a&gt;&lt;br&gt;
[5] How to deploy Postgres on Kubernetes - Refine &lt;a href="https://refine.dev/blog/postgres-on-kubernetes/" rel="noopener noreferrer"&gt;https://refine.dev/blog/postgres-on-kubernetes/&lt;/a&gt;&lt;br&gt;
[6] PostgreSQL Major Upgrades with CloudNativePG and Kubernetes ... &lt;a href="https://www.enterprisedb.com/blog/current-state-major-postgresql-upgrades-cloudnativepg-kubernetes" rel="noopener noreferrer"&gt;https://www.enterprisedb.com/blog/current-state-major-postgresql-upgrades-cloudnativepg-kubernetes&lt;/a&gt;&lt;br&gt;
[7] Easier Upgrades and Image Management for Postgres in Kubernetes &lt;a href="https://www.crunchydata.com/blog/easier-upgrades-and-image-management-for-postgres-in-kubernetes" rel="noopener noreferrer"&gt;https://www.crunchydata.com/blog/easier-upgrades-and-image-management-for-postgres-in-kubernetes&lt;/a&gt;&lt;br&gt;
[8] Recommended architectures for PostgreSQL in Kubernetes | CNCF &lt;a href="https://www.cncf.io/blog/2023/09/29/recommended-architectures-for-postgresql-in-kubernetes/" rel="noopener noreferrer"&gt;https://www.cncf.io/blog/2023/09/29/recommended-architectures-for-postgresql-in-kubernetes/&lt;/a&gt;&lt;br&gt;
[9] Easier Upgrades and Image Management for Postgres in Kubernetes &lt;a href="https://www.reddit.com/r/kubernetes/comments/ye8eps/easier_upgrades_and_image_management_for_postgres/" rel="noopener noreferrer"&gt;https://www.reddit.com/r/kubernetes/comments/ye8eps/easier_upgrades_and_image_management_for_postgres/&lt;/a&gt;&lt;br&gt;
[10] How to Deploy Postgres to Kubernetes Cluster - DigitalOcean &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-deploy-postgres-to-kubernetes-cluster" rel="noopener noreferrer"&gt;https://www.digitalocean.com/community/tutorials/how-to-deploy-postgres-to-kubernetes-cluster&lt;/a&gt;&lt;br&gt;
[11] How to Build Scalable and Reliable PostgreSQL Systems on ... &lt;a href="https://www.cloudraft.io/blog/postgresql-on-kubernetes" rel="noopener noreferrer"&gt;https://www.cloudraft.io/blog/postgresql-on-kubernetes&lt;/a&gt;&lt;br&gt;
[12] Posts by Andrew L'Ecuyer | PostgreSQL Blog - Crunchy Data &lt;a href="https://www.crunchydata.com/blog/author/andrew-lecuyer" rel="noopener noreferrer"&gt;https://www.crunchydata.com/blog/author/andrew-lecuyer&lt;/a&gt;&lt;br&gt;
[13] Run and Manage PostgreSQL Database on Kubernetes - KubeDB &lt;a href="https://kubedb.com/kubernetes/databases/run-and-manage-postgres-on-kubernetes/" rel="noopener noreferrer"&gt;https://kubedb.com/kubernetes/databases/run-and-manage-postgres-on-kubernetes/&lt;/a&gt;&lt;br&gt;
[14] PostgreSQL docker image and deployment strategy - Stack Overflow &lt;a href="https://stackoverflow.com/questions/45440490/postgresql-docker-image-and-deployment-strategy" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/45440490/postgresql-docker-image-and-deployment-strategy&lt;/a&gt;&lt;br&gt;
[15] Upgrade bitnami postgresql image k8s - kubernetes - Stack Overflow &lt;a href="https://stackoverflow.com/questions/69898410/upgrade-bitnami-postgresql-image-k8s/69899094" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/69898410/upgrade-bitnami-postgresql-image-k8s/69899094&lt;/a&gt;&lt;br&gt;
[16] EDB Postgres for Kubernetes v1 - Installation and upgrades &lt;a href="https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/installation_upgrade/" rel="noopener noreferrer"&gt;https://www.enterprisedb.com/docs/postgres_for_kubernetes/latest/installation_upgrade/&lt;/a&gt;&lt;br&gt;
[17] Provisioning Postgres from Docker to Kubernetes - DEV Community &lt;a href="https://dev.to/arctype/provisioning-postgres-from-docker-to-kubernetes-5djf"&gt;https://dev.to/arctype/provisioning-postgres-from-docker-to-kubernetes-5djf&lt;/a&gt;&lt;br&gt;
[18] How to use Kubernetes to deploy Postgres - Sumo Logic &lt;a href="https://www.sumologic.com/blog/kubernetes-deploy-postgres/" rel="noopener noreferrer"&gt;https://www.sumologic.com/blog/kubernetes-deploy-postgres/&lt;/a&gt;&lt;br&gt;
[19] Kubernetes Upgrade - CloudNativePG &lt;a href="https://cloudnative-pg.io/documentation/1.16/kubernetes_upgrade/" rel="noopener noreferrer"&gt;https://cloudnative-pg.io/documentation/1.16/kubernetes_upgrade/&lt;/a&gt;&lt;br&gt;
[20] How to upgrade postgresql inside a Kubernetes pod? - Stack Overflow &lt;a href="https://stackoverflow.com/questions/64850625/how-to-upgrade-postgresql-inside-a-kubernetes-pod" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/64850625/how-to-upgrade-postgresql-inside-a-kubernetes-pod&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgressql</category>
      <category>dba</category>
      <category>kubernetes</category>
      <category>sql</category>
    </item>
    <item>
      <title>Choice of Table Column Types and Order When Migrating to PostgreSQL</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Tue, 11 Feb 2025 09:43:41 +0000</pubDate>
      <link>https://forem.com/shiviyer/choice-of-table-column-types-and-order-when-migrating-to-postgresql-5gp2</link>
      <guid>https://forem.com/shiviyer/choice-of-table-column-types-and-order-when-migrating-to-postgresql-5gp2</guid>
      <description>&lt;p&gt;When migrating to PostgreSQL, selecting appropriate column types and optimizing their order is crucial for maximizing performance and storage efficiency. Here's a detailed technical guide on these considerations:&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Type Selection
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Numeric Types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Choose the most appropriate integer type based on your data range:

&lt;ul&gt;
&lt;li&gt;SMALLINT: 2 bytes, range -32,768 to 32,767&lt;/li&gt;
&lt;li&gt;INTEGER: 4 bytes, range -2,147,483,648 to 2,147,483,647&lt;/li&gt;
&lt;li&gt;BIGINT: 8 bytes, range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;For decimal numbers:

&lt;ul&gt;
&lt;li&gt;NUMERIC/DECIMAL: variable-length, up to 131,072 digits before the decimal point and up to 16,383 digits after&lt;/li&gt;
&lt;li&gt;REAL: 4 bytes, 6 decimal digits precision&lt;/li&gt;
&lt;li&gt;DOUBLE PRECISION: 8 bytes, 15 decimal digits precision&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Character Types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;VARCHAR(n): variable-length with limit, 1 byte + actual string length&lt;/li&gt;
&lt;li&gt;TEXT: variable unlimited length, 1 byte + actual string length&lt;/li&gt;
&lt;li&gt;CHAR(n): fixed-length, blank-padded&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Special Types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;SERIAL types: 4-byte auto-incrementing integer&lt;/li&gt;
&lt;li&gt;BIGSERIAL: 8-byte auto-incrementing integer&lt;/li&gt;
&lt;li&gt;JSON: text-based storage of JSON data&lt;/li&gt;
&lt;li&gt;JSONB: binary storage of JSON data, supports indexing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Column Order Optimization
&lt;/h2&gt;

&lt;p&gt;Optimize column order to minimize padding and improve CPU cache efficiency:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Place 8-byte alignment columns first (BIGINT, TIMESTAMP, DOUBLE PRECISION)&lt;/li&gt;
&lt;li&gt;Follow with 4-byte alignment columns (INTEGER, REAL)&lt;/li&gt;
&lt;li&gt;Then 2-byte alignment columns (SMALLINT)&lt;/li&gt;
&lt;li&gt;Finally, variable-length fields (TEXT, VARCHAR, JSONB)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example of an optimized table structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;optimized_table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="nb"&gt;TIME&lt;/span&gt; &lt;span class="k"&gt;ZONE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="nb"&gt;DOUBLE&lt;/span&gt; &lt;span class="nb"&gt;PRECISION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="nb"&gt;SMALLINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This ordering minimizes internal fragmentation and reduces the total row size.&lt;/p&gt;
&lt;h2&gt;
  
  
  Advanced Optimization Techniques
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use NUMERIC(p,s) instead of DECIMAL(p,s) for better performance in arithmetic operations&lt;/li&gt;
&lt;li&gt;Implement partial indexes for frequently queried subsets of data&lt;/li&gt;
&lt;li&gt;Utilize BRIN indexes for large tables with naturally ordered data&lt;/li&gt;
&lt;li&gt;Consider using UNLOGGED tables for temporary or cache-like data to improve write performance&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Implement CHECK constraints to enforce data integrity at the database level&lt;/li&gt;
&lt;li&gt;Use EXPLAIN ANALYZE to examine query execution plans and identify optimization opportunities&lt;/li&gt;
&lt;li&gt;Regularly run VACUUM and ANALYZE to maintain optimal performance and up-to-date statistics&lt;/li&gt;
&lt;li&gt;Consider using CLUSTER command to physically reorder table data based on an index&lt;/li&gt;
&lt;li&gt;Utilize partitioning for very large tables to improve query performance and manageability&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By meticulously selecting data types, optimizing column order, and implementing these advanced techniques, you can significantly enhance your PostgreSQL database's performance, particularly for large-scale or high-traffic applications where even minor optimizations can yield substantial benefits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sources&lt;/strong&gt;&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://minervadb.xyz/novalidate-parallel-constraints-postgresql/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2025%2F01%2Fshutterstock_1990799453-scaled.jpg" height="476" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://minervadb.xyz/novalidate-parallel-constraints-postgresql/" rel="noopener noreferrer" class="c-link"&gt;
          NoValidate and Parallel Constraints in PostgreSQL - DBA Tips
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Can you implement NoValidate and Parallel Constraints in PostgreSQL? Oracle to PostgreSQL Migration Blog series - PostgreSQL Support
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2020%2F10%2Fcropped-LogoColorTextRight-32x32.jpeg" width="32" height="32"&gt;
        minervadb.xyz
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;




&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://minervadb.xyz/innodb-commit-rollback-savepoint/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2025%2F01%2Fshutterstock_2501760525-scaled.jpg" height="485" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://minervadb.xyz/innodb-commit-rollback-savepoint/" rel="noopener noreferrer" class="c-link"&gt;
          Implementing COMMIT, ROLLBACK, and SAVEPOINT in InnoDB
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Mastering Transaction Management in InnoDB: Optimizing COMMIT, ROLLBACK, and SAVEPOINT for Performance and Integrity - MySQL DBA Support
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2020%2F10%2Fcropped-LogoColorTextRight-32x32.jpeg" width="32" height="32"&gt;
        minervadb.xyz
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://chistadata.com/common-clickhouse-analytical-models/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchistadata.com%2Fwp-content%2Fuploads%2F2025%2F01%2Fshutterstock_340812701-scaled.jpg" height="473" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://chistadata.com/common-clickhouse-analytical-models/" rel="noopener noreferrer" class="c-link"&gt;
          Common ClickHouse Analytical Models
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Most Common ClickHouse Analytical Models
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchistadata.com%2Fwp-content%2Fuploads%2F2021%2F05%2Fcropped-Logo-1-32x32.jpg" width="32" height="32"&gt;
        chistadata.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>postgressql</category>
      <category>dba</category>
      <category>sql</category>
      <category>opensource</category>
    </item>
    <item>
      <title>How can I use the MongoDB explain plan to troubleshoot query performance</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Fri, 24 Jan 2025 19:55:31 +0000</pubDate>
      <link>https://forem.com/shiviyer/how-can-i-use-the-mongodb-explain-plan-to-troubleshoot-query-performance-34cm</link>
      <guid>https://forem.com/shiviyer/how-can-i-use-the-mongodb-explain-plan-to-troubleshoot-query-performance-34cm</guid>
      <description>&lt;p&gt;&lt;a href="https://minervadb.xyz" rel="noopener noreferrer"&gt;MongoDB's&lt;/a&gt; explain plan is a powerful tool for troubleshooting query performance. Here's how you can use it effectively:&lt;/p&gt;

&lt;h2&gt;
  
  
  Running an Explain Plan
&lt;/h2&gt;

&lt;p&gt;To generate an explain plan, you can use the following methods:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For find queries:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;For aggregation pipelines:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;pipeline&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;For other operations:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding Explain Output
&lt;/h2&gt;

&lt;p&gt;The explain plan provides detailed information about query execution in several key areas:&lt;/p&gt;

&lt;h3&gt;
  
  
  Query Planner
&lt;/h3&gt;

&lt;p&gt;This section shows the plan selected by the query optimizer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;winningPlan&lt;/strong&gt;: Indicates the chosen execution plan&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;rejectedPlans&lt;/strong&gt;: Lists alternative plans that were considered but not used&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Execution Stats
&lt;/h3&gt;

&lt;p&gt;This part offers insights into the actual query execution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;nReturned&lt;/strong&gt;: Number of documents returned&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;totalKeysExamined&lt;/strong&gt;: Number of index keys scanned&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;totalDocsExamined&lt;/strong&gt;: Number of documents scanned&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;executionTimeMillis&lt;/strong&gt;: Total execution time&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Index Usage
&lt;/h3&gt;

&lt;p&gt;Look for the following indicators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;IXSCAN&lt;/strong&gt;: Indicates an index was used&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;COLLSCAN&lt;/strong&gt;: Suggests a full collection scan, which may be inefficient for large datasets&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Troubleshooting Tips
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compare documents scanned vs. returned&lt;/strong&gt;: A high ratio of examined to returned documents may indicate a need for better indexing[1].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Check for COLLSCAN&lt;/strong&gt;: If you see this instead of IXSCAN, consider adding an appropriate index[1].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Analyze execution time&lt;/strong&gt;: Look at the executionTimeMillis to identify slow queries[3].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Examine rejected plans&lt;/strong&gt;: Understanding why certain plans were rejected can help in optimizing indexes or query structure[4].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use different verbosity modes&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;queryPlanner: Default mode, shows the winning plan&lt;/li&gt;
&lt;li&gt;executionStats: Includes execution statistics&lt;/li&gt;
&lt;li&gt;allPlansExecution: Provides data on all considered plans[4]&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Iterate and refine&lt;/strong&gt;: Use the explain plan results to make incremental improvements to your queries and indexes[7].&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By leveraging the explain plan effectively, you can identify performance bottlenecks, optimize indexing strategies, and refine query structures to significantly improve MongoDB query performance.&lt;/p&gt;

&lt;p&gt;Sources&lt;br&gt;
[1] MongoDB: Investigate Queries with explain() and Index Usage (part 2) &lt;a href="https://www.percona.com/blog/mongodb-investigate-queries-with-explain-index-usage-part-2/" rel="noopener noreferrer"&gt;https://www.percona.com/blog/mongodb-investigate-queries-with-explain-index-usage-part-2/&lt;/a&gt;&lt;br&gt;
[2] Index analysis using Explain command- Scaler Topics &lt;a href="https://www.scaler.com/topics/mongodb/index-analysis-using-explain-command/" rel="noopener noreferrer"&gt;https://www.scaler.com/topics/mongodb/index-analysis-using-explain-command/&lt;/a&gt;&lt;br&gt;
[3] Interpret Explain Plan Results - MongoDB Manual v8.0 &lt;a href="https://www.mongodb.com/docs/manual/tutorial/analyze-query-plan/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/manual/tutorial/analyze-query-plan/&lt;/a&gt;&lt;br&gt;
[4] explain - MongoDB Manual v8.0 &lt;a href="https://www.mongodb.com/docs/manual/reference/command/explain/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/manual/reference/command/explain/&lt;/a&gt;&lt;br&gt;
[5] Explain Results - MongoDB Manual v5.0 &lt;a href="https://www.mongodb.com/docs/v5.0/reference/explain-results/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/v5.0/reference/explain-results/&lt;/a&gt;&lt;br&gt;
[6] Track and Troubleshoot MongoDB Performance With Datadog ... &lt;a href="https://www.datadoghq.com/blog/mongodb-database-monitoring/" rel="noopener noreferrer"&gt;https://www.datadoghq.com/blog/mongodb-database-monitoring/&lt;/a&gt;&lt;br&gt;
[7] How to Optimize MongoDB Queries for Peak Performance? &lt;a href="https://embarkingonvoyage.com/blog/technologies/how-to-optimize-mongodb-queries-for-peak-performance/" rel="noopener noreferrer"&gt;https://embarkingonvoyage.com/blog/technologies/how-to-optimize-mongodb-queries-for-peak-performance/&lt;/a&gt;&lt;br&gt;
[8] Using Explain Plans - Practical MongoDB Aggregations Book &lt;a href="https://www.practical-mongodb-aggregations.com/guides/explain.html" rel="noopener noreferrer"&gt;https://www.practical-mongodb-aggregations.com/guides/explain.html&lt;/a&gt;&lt;br&gt;
[9] db.collection.explain() - MongoDB Manual v8.0 &lt;a href="https://www.mongodb.com/docs/manual/reference/method/db.collection.explain/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/manual/reference/method/db.collection.explain/&lt;/a&gt;&lt;br&gt;
[10] cursor.explain() - MongoDB Manual v8.0 &lt;a href="https://www.mongodb.com/docs/manual/reference/method/cursor.explain/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/manual/reference/method/cursor.explain/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
      <category>dba</category>
      <category>database</category>
    </item>
    <item>
      <title>What are the performance matrices used in MongoDB Query Performance Troubleshooting?</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Fri, 24 Jan 2025 19:53:24 +0000</pubDate>
      <link>https://forem.com/shiviyer/what-are-the-performance-matrices-used-in-mongodb-query-performance-troubleshooting-4hpo</link>
      <guid>https://forem.com/shiviyer/what-are-the-performance-matrices-used-in-mongodb-query-performance-troubleshooting-4hpo</guid>
      <description>&lt;p&gt;&lt;a href="https://minervadb.xyz/mongodb-support/" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt; offers several performance metrics that are crucial for troubleshooting query performance issues. Here are the key metrics used in MongoDB query performance troubleshooting:&lt;/p&gt;

&lt;h2&gt;
  
  
  Query Execution Metrics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Query Targeting&lt;/strong&gt;: This metric helps identify if queries are using indexes effectively[2].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution Time&lt;/strong&gt;: Measures the latency of read and write operations. Read operations taking more than 100ms are generally considered slow[3].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scanned vs. Returned Ratio&lt;/strong&gt;: Indicates how many documents were scanned to return the result set. A high ratio suggests inefficient queries or missing indexes[4].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Resource Utilization Metrics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CPU Utilization&lt;/strong&gt;: High CPU usage can indicate complex queries or insufficient indexing[2].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Utilization&lt;/strong&gt;: Monitors the usage of RAM, which is crucial for caching frequently accessed data[2].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storage Metrics&lt;/strong&gt;: Tracks disk I/O and storage capacity[2].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Database Operation Metrics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Operation Counts&lt;/strong&gt;: Tracks the number of insert, query, update, and delete operations per second[3].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connections&lt;/strong&gt;: Monitors the number of active client connections[3].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Index Performance Metrics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Index Usage&lt;/strong&gt;: Measures how frequently indexes are used in queries[1].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index Size&lt;/strong&gt;: Tracks the size of indexes, which can impact write performance if too large[4].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Replication Metrics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Replication Lag&lt;/strong&gt;: Measures the delay between operations on the primary and their replication to secondaries[4].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cache Performance
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cache Hit Ratio&lt;/strong&gt;: Indicates how often requested data is found in memory cache versus disk[9].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dirty Cache Percentage&lt;/strong&gt;: Shows the proportion of modified data in the cache waiting to be written to disk[9].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Latency Metrics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read Latency&lt;/strong&gt;: Measures the time taken for read operations[11].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write Latency&lt;/strong&gt;: Tracks the duration of write operations[11].&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Profiler Metrics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Slow Query Log&lt;/strong&gt;: Captures queries that exceed a specified execution time threshold[6].&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query Shapes&lt;/strong&gt;: Provides information on common query patterns and their performance[6].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By monitoring these metrics, database administrators can identify performance bottlenecks, optimize query execution, and ensure efficient resource allocation for MongoDB deployments.&lt;/p&gt;

&lt;p&gt;Sources&lt;br&gt;
[1] Track and Troubleshoot MongoDB Performance With Datadog ... &lt;a href="https://www.datadoghq.com/blog/mongodb-database-monitoring/" rel="noopener noreferrer"&gt;https://www.datadoghq.com/blog/mongodb-database-monitoring/&lt;/a&gt;&lt;br&gt;
[2] How to Monitor MongoDB Performance Metrics Using the ... - GeoPITS &lt;a href="https://www.geopits.com/blog/mongodb-performance-metrics.html" rel="noopener noreferrer"&gt;https://www.geopits.com/blog/mongodb-performance-metrics.html&lt;/a&gt;&lt;br&gt;
[3] MongoDB Monitoring | Beginner's Guide to MongoDB performance ... &lt;a href="https://signoz.io/blog/mongodb-monitoring/" rel="noopener noreferrer"&gt;https://signoz.io/blog/mongodb-monitoring/&lt;/a&gt;&lt;br&gt;
[4] MongoDB Performance Tuning Questions &lt;a href="https://www.mongodb.com/developer/products/mongodb/performance-tuning-tips/" rel="noopener noreferrer"&gt;https://www.mongodb.com/developer/products/mongodb/performance-tuning-tips/&lt;/a&gt;&lt;br&gt;
[5] How to Optimize MongoDB Queries for Performance? &lt;a href="https://www.geeksforgeeks.org/how-to-optimize-mongodb-queries-for-performance/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/how-to-optimize-mongodb-queries-for-performance/&lt;/a&gt;&lt;br&gt;
[6] Analyze Query Performance - MongoDB Manual v8.0 &lt;a href="https://www.mongodb.com/docs/manual/tutorial/evaluate-operation-performance/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/manual/tutorial/evaluate-operation-performance/&lt;/a&gt;&lt;br&gt;
[7] How to Troubleshoot Slow Queries in MongoDB - Netdata &lt;a href="https://www.netdata.cloud/academy/how-to-troubleshoot-slow-queries-in-mongodb/" rel="noopener noreferrer"&gt;https://www.netdata.cloud/academy/how-to-troubleshoot-slow-queries-in-mongodb/&lt;/a&gt;&lt;br&gt;
[8] How do you troubleshoot MongoDB performance issues? - Dragonfly &lt;a href="https://www.dragonflydb.io/faq/mongodb-performance-troubleshooting" rel="noopener noreferrer"&gt;https://www.dragonflydb.io/faq/mongodb-performance-troubleshooting&lt;/a&gt;&lt;br&gt;
[9] Review Available Metrics - MongoDB Atlas &lt;a href="https://www.mongodb.com/docs/atlas/review-available-metrics/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/atlas/review-available-metrics/&lt;/a&gt;&lt;br&gt;
[10] How To Monitor MongoDB And What Metrics To Monitor &lt;a href="https://www.mongodb.com/resources/products/capabilities/how-to-monitor-mongodb-and-what-metrics-to-monitor" rel="noopener noreferrer"&gt;https://www.mongodb.com/resources/products/capabilities/how-to-monitor-mongodb-and-what-metrics-to-monitor&lt;/a&gt;&lt;br&gt;
[11] Monitoring MongoDB Performance Metrics (WiredTiger) - Datadog &lt;a href="https://www.datadoghq.com/blog/monitoring-mongodb-performance-metrics-wiredtiger/" rel="noopener noreferrer"&gt;https://www.datadoghq.com/blog/monitoring-mongodb-performance-metrics-wiredtiger/&lt;/a&gt;&lt;br&gt;
[12] How To Monitor MongoDB's Performance - DigitalOcean &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-monitor-mongodb-s-performance" rel="noopener noreferrer"&gt;https://www.digitalocean.com/community/tutorials/how-to-monitor-mongodb-s-performance&lt;/a&gt;&lt;br&gt;
[13] Optimize Query Performance - MongoDB Manual v8.0 &lt;a href="https://www.mongodb.com/docs/manual/tutorial/optimize-query-performance-with-indexes-and-projections/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/manual/tutorial/optimize-query-performance-with-indexes-and-projections/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
      <category>dba</category>
    </item>
    <item>
      <title>How can I optimize the performance of an aggregation pipeline in MongoDB</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Fri, 24 Jan 2025 04:34:27 +0000</pubDate>
      <link>https://forem.com/shiviyer/how-can-i-optimize-the-performance-of-an-aggregation-pipeline-in-mongodb-3ep</link>
      <guid>https://forem.com/shiviyer/how-can-i-optimize-the-performance-of-an-aggregation-pipeline-in-mongodb-3ep</guid>
      <description>&lt;p&gt;To optimize the performance of an aggregation pipeline in MongoDB, you can implement several strategies:&lt;/p&gt;

&lt;h2&gt;
  
  
  Efficient Use of Indexes
&lt;/h2&gt;

&lt;p&gt;Utilize indexes effectively, especially for the $match and $sort stages. Create appropriate indexes on fields frequently used in these operations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createIndex&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;field1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;field2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pipeline Stage Optimization
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Early Filtering with $match
&lt;/h3&gt;

&lt;p&gt;Place $match stages as early as possible in the pipeline to reduce the number of documents processed in subsequent stages[1][5]. This significantly improves performance by filtering out unnecessary data early:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;completed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2024&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// Other stages...&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Strategic Use of $project
&lt;/h3&gt;

&lt;p&gt;Use $project early in the pipeline to limit the fields passed to subsequent stages, reducing the amount of data being processed[1][2]:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;field1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;field2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// Other stages...&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Careful Placement of $sort and $limit
&lt;/h3&gt;

&lt;p&gt;When using $sort with $limit, place $limit immediately after $sort to reduce the number of documents that need to be sorted[4]:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// Other stages...&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Minimize Resource-Intensive Operations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Avoid Unnecessary $group Operations
&lt;/h3&gt;

&lt;p&gt;The $group stage can be resource-intensive. Use it judiciously and consider alternative approaches when possible[3].&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimize $lookup Usage
&lt;/h3&gt;

&lt;p&gt;When using $lookup for joining collections, ensure the foreign collection has appropriate indexes and consider filtering data before the $lookup stage[3].&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory Management
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use allowDiskUse Option
&lt;/h3&gt;

&lt;p&gt;For large datasets or complex operations that may exceed the 100MB memory limit, use the allowDiskUse option[2]:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;allowDiskUse&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Performance Analysis
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Utilize Explain Plans
&lt;/h3&gt;

&lt;p&gt;Use MongoDB's explain feature to analyze the performance of your aggregation queries and identify bottlenecks[4]:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;executionStats&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pipeline Coalescence
&lt;/h2&gt;

&lt;p&gt;Combine multiple stages when possible. For example, merge multiple $match stages into one or combine $match and $project stages for efficiency[1].&lt;/p&gt;

&lt;h2&gt;
  
  
  Indexing for $lookup and $sort
&lt;/h2&gt;

&lt;p&gt;Ensure that fields used in $lookup and $sort operations are properly indexed to improve performance[5][11].&lt;/p&gt;

&lt;p&gt;By implementing these optimization techniques, you can significantly improve the performance of your MongoDB aggregation pipelines, especially when dealing with large datasets or complex operations.&lt;/p&gt;

&lt;p&gt;Sources&lt;br&gt;
[1] Aggregation Pipeline Optimization - GeeksforGeeks &lt;a href="https://www.geeksforgeeks.org/aggregation-pipeline-optimization/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/aggregation-pipeline-optimization/&lt;/a&gt;&lt;br&gt;
[2] MongoDB Aggregation Pipeline &lt;a href="https://www.mongodb.com/resources/products/capabilities/aggregation-pipeline" rel="noopener noreferrer"&gt;https://www.mongodb.com/resources/products/capabilities/aggregation-pipeline&lt;/a&gt;&lt;br&gt;
[3] How can you speed up MongoDB aggregate queries? - Dragonfly &lt;a href="https://www.dragonflydb.io/faq/mongodb-speed-up-aggregate" rel="noopener noreferrer"&gt;https://www.dragonflydb.io/faq/mongodb-speed-up-aggregate&lt;/a&gt;&lt;br&gt;
[4] Optimizing Aggregation Pipelines for Performance - Diginode &lt;a href="https://diginode.in/mongodb/optimizing-aggregation-pipelines-for-performance/" rel="noopener noreferrer"&gt;https://diginode.in/mongodb/optimizing-aggregation-pipelines-for-performance/&lt;/a&gt;&lt;br&gt;
[5] Aggregation Pipeline Optimization - MongoDB Manual v8.0 &lt;a href="https://www.mongodb.com/docs/manual/core/aggregation-pipeline-optimization/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/manual/core/aggregation-pipeline-optimization/&lt;/a&gt;&lt;br&gt;
[6] MongoDB Aggregation: tutorial with examples and exercises &lt;a href="https://studio3t.com/knowledge-base/articles/mongodb-aggregation-framework/" rel="noopener noreferrer"&gt;https://studio3t.com/knowledge-base/articles/mongodb-aggregation-framework/&lt;/a&gt;&lt;br&gt;
[7] Improving Aggregation Performance on MongoDB - SingleStore &lt;a href="https://www.singlestore.com/blog/improving-aggregation-performance-on-mongodb/" rel="noopener noreferrer"&gt;https://www.singlestore.com/blog/improving-aggregation-performance-on-mongodb/&lt;/a&gt;&lt;br&gt;
[8] Pipeline Performance Considerations &lt;a href="https://www.practical-mongodb-aggregations.com/guides/performance.html" rel="noopener noreferrer"&gt;https://www.practical-mongodb-aggregations.com/guides/performance.html&lt;/a&gt;&lt;br&gt;
[9] MongoDB Aggregation Pipeline - Tips and Principles &lt;a href="https://dev.to/jagadeeshmusali/mongodb-aggregation-pipeline-tips-and-principles-11i0"&gt;https://dev.to/jagadeeshmusali/mongodb-aggregation-pipeline-tips-and-principles-11i0&lt;/a&gt;&lt;br&gt;
[10] Aggregation pipeline faster than find() method? : r/mongodb - Reddit &lt;a href="https://www.reddit.com/r/mongodb/comments/11zeu6w/aggregation_pipeline_faster_than_find_method/" rel="noopener noreferrer"&gt;https://www.reddit.com/r/mongodb/comments/11zeu6w/aggregation_pipeline_faster_than_find_method/&lt;/a&gt;&lt;br&gt;
[11] Speed Up Aggregation Pipeline - Working with Data - MongoDB &lt;a href="https://www.mongodb.com/community/forums/t/speed-up-aggregation-pipeline/126875" rel="noopener noreferrer"&gt;https://www.mongodb.com/community/forums/t/speed-up-aggregation-pipeline/126875&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
      <category>data</category>
      <category>database</category>
    </item>
    <item>
      <title>How to implement an aggregation framework in MongoDB?</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Fri, 24 Jan 2025 04:26:47 +0000</pubDate>
      <link>https://forem.com/shiviyer/how-to-implement-an-aggregation-framework-in-mongodb-4a8h</link>
      <guid>https://forem.com/shiviyer/how-to-implement-an-aggregation-framework-in-mongodb-4a8h</guid>
      <description>&lt;p&gt;MongoDB's Aggregation Framework is a powerful tool for processing and analyzing data within the database. It allows you to perform complex operations on collections, transforming and combining documents to produce computed results. Here's how to implement an aggregation framework in MongoDB:&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Aggregation Pipeline
&lt;/h2&gt;

&lt;p&gt;The aggregation framework uses a pipeline concept, where documents pass through a series of stages. Each stage performs a specific operation on the input documents and passes the results to the next stage. The basic structure of an aggregation pipeline is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;$stage1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;$stage2&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// ... more stages&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common Aggregation Stages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  $match
&lt;/h3&gt;

&lt;p&gt;The $match stage filters documents, similar to a find() query[1]. It's often used early in the pipeline to reduce the number of documents processed in subsequent stages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  $group
&lt;/h3&gt;

&lt;p&gt;The $group stage groups documents by a specified expression and can perform calculations on grouped data[1]:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$cust_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$amount&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  $sort
&lt;/h3&gt;

&lt;p&gt;The $sort stage orders the documents based on specified fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;$sort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;totalQuantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  $project
&lt;/h3&gt;

&lt;p&gt;The $project stage reshapes documents, specifying which fields to include or exclude[7]:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;$project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implementing an Aggregation Pipeline
&lt;/h2&gt;

&lt;p&gt;Here's a step-by-step guide to implement an aggregation pipeline:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Connect to MongoDB&lt;/strong&gt;: Ensure you're connected to your MongoDB instance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Choose the Collection&lt;/strong&gt;: Select the collection you want to perform aggregation on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Define the Pipeline&lt;/strong&gt;: Create an array of stages that define your aggregation logic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Execute the Aggregation&lt;/strong&gt;: Use the aggregate() method on your collection with the defined pipeline.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;medium&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;totalQuantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$quantity&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;totalQuantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pipeline filters for medium-sized orders, groups them by name, calculates the total quantity, and sorts the results in descending order[1][5].&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices and Optimization
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Indexes&lt;/strong&gt;: Ensure relevant fields are indexed, especially for $match and $sort stages[3].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Order of Operations&lt;/strong&gt;: Place $match and $limit stages early in the pipeline to reduce the number of documents processed[10].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid Memory Limitations&lt;/strong&gt;: Use the allowDiskUse option for large datasets that exceed the 100MB memory limit[10]:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;allowDiskUse&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use Explain Plans&lt;/strong&gt;: Analyze your pipeline performance using explain():
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Leverage the $lookup Stage&lt;/strong&gt;: For joining data from multiple collections[3].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Aggregation Operators&lt;/strong&gt;: Utilize built-in operators like $sum, $avg, $max, $min for calculations[5].&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these guidelines and understanding the various stages and operators available in the MongoDB Aggregation Framework, you can efficiently implement complex data processing pipelines directly within your database[3][5][10].&lt;/p&gt;

&lt;p&gt;Sources&lt;br&gt;
[1] What Is Aggregation In MongoDB? &lt;a href="https://www.mongodb.com/resources/products/capabilities/aggregation" rel="noopener noreferrer"&gt;https://www.mongodb.com/resources/products/capabilities/aggregation&lt;/a&gt;&lt;br&gt;
[2] MongoDB - Aggregation - TutorialsPoint &lt;a href="https://www.tutorialspoint.com/mongodb/mongodb_aggregation.htm" rel="noopener noreferrer"&gt;https://www.tutorialspoint.com/mongodb/mongodb_aggregation.htm&lt;/a&gt;&lt;br&gt;
[3] MongoDB Best Practices: Optimizing Performance and Schema ... &lt;a href="https://baransel.dev/post/mongodb-best-practices/" rel="noopener noreferrer"&gt;https://baransel.dev/post/mongodb-best-practices/&lt;/a&gt;&lt;br&gt;
[4] Aggregation Operations - MongoDB Manual v8.0 &lt;a href="https://www.mongodb.com/docs/manual/aggregation/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/manual/aggregation/&lt;/a&gt;&lt;br&gt;
[5] MongoDB Aggregation: tutorial with examples and exercises &lt;a href="https://studio3t.com/knowledge-base/articles/mongodb-aggregation-framework/" rel="noopener noreferrer"&gt;https://studio3t.com/knowledge-base/articles/mongodb-aggregation-framework/&lt;/a&gt;&lt;br&gt;
[6] Aggregation in MongoDB - GeeksforGeeks &lt;a href="https://www.geeksforgeeks.org/aggregation-in-mongodb/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/aggregation-in-mongodb/&lt;/a&gt;&lt;br&gt;
[7] Introduction to MongoDB Aggregation Framework - Prisma &lt;a href="https://www.prisma.io/dataguide/mongodb/mongodb-aggregation-framework" rel="noopener noreferrer"&gt;https://www.prisma.io/dataguide/mongodb/mongodb-aggregation-framework&lt;/a&gt;&lt;br&gt;
[8] Create an Aggregation Pipeline - MongoDB Compass &lt;a href="https://www.mongodb.com/docs/compass/current/create-agg-pipeline/" rel="noopener noreferrer"&gt;https://www.mongodb.com/docs/compass/current/create-agg-pipeline/&lt;/a&gt;&lt;br&gt;
[9] MongoDB Aggregation Course &lt;a href="https://learn.mongodb.com/courses/mongodb-aggregation" rel="noopener noreferrer"&gt;https://learn.mongodb.com/courses/mongodb-aggregation&lt;/a&gt;&lt;br&gt;
[10] Pipeline Performance Considerations &lt;a href="https://www.practical-mongodb-aggregations.com/guides/performance.html" rel="noopener noreferrer"&gt;https://www.practical-mongodb-aggregations.com/guides/performance.html&lt;/a&gt;&lt;br&gt;
[11] MongoDB Aggregation Pipeline - Tips and Principles &lt;a href="https://dev.to/jagadeeshmusali/mongodb-aggregation-pipeline-tips-and-principles-11i0"&gt;https://dev.to/jagadeeshmusali/mongodb-aggregation-pipeline-tips-and-principles-11i0&lt;/a&gt;&lt;br&gt;
[12] MongoDB Aggregation Pipelines - W3Schools &lt;a href="https://www.w3schools.com/mongodb/mongodb_aggregations_intro.php" rel="noopener noreferrer"&gt;https://www.w3schools.com/mongodb/mongodb_aggregations_intro.php&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
      <category>dba</category>
      <category>devops</category>
    </item>
    <item>
      <title>How to build multi-tenancy in PostgreSQL for developing SaaS applications?</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Fri, 24 Jan 2025 04:24:55 +0000</pubDate>
      <link>https://forem.com/shiviyer/how-to-build-multi-tenancy-in-postgresql-for-developing-saas-applications-4b6</link>
      <guid>https://forem.com/shiviyer/how-to-build-multi-tenancy-in-postgresql-for-developing-saas-applications-4b6</guid>
      <description>&lt;p&gt;Building multi-tenancy in PostgreSQL for SaaS applications can be achieved through several approaches, each with its own advantages and trade-offs. Here are the main strategies for implementing multi-tenancy in PostgreSQL:&lt;/p&gt;

&lt;h2&gt;
  
  
  Shared Database, Shared Schema
&lt;/h2&gt;

&lt;p&gt;In this approach, all tenants share the same database and schema, with a tenant identifier column used to distinguish between different tenants' data.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a &lt;code&gt;tenant_id&lt;/code&gt; column to all tables that contain tenant-specific data&lt;/li&gt;
&lt;li&gt;Use Row-Level Security (RLS) policies to enforce data isolation between tenants&lt;/li&gt;
&lt;li&gt;Implement database roles and permissions to manage access control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example RLS policy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt; &lt;span class="n"&gt;tenant_isolation_policy&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;mytable&lt;/span&gt;
    &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.current_tenant'&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Efficient resource utilization&lt;/li&gt;
&lt;li&gt;Easier maintenance and updates&lt;/li&gt;
&lt;li&gt;Simplified backup and restore processes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Potential for data leakage if not implemented correctly&lt;/li&gt;
&lt;li&gt;May require more complex application logic to handle tenant isolation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Shared Database, Separate Schemas
&lt;/h2&gt;

&lt;p&gt;This model uses a single database but creates a separate schema for each tenant.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new schema for each tenant&lt;/li&gt;
&lt;li&gt;Use search_path to switch between tenant schemas&lt;/li&gt;
&lt;li&gt;Implement schema-level permissions for access control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example schema creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="n"&gt;tenant_123&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;search_path&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;tenant_123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better logical separation between tenants&lt;/li&gt;
&lt;li&gt;Easier to implement tenant-specific customizations&lt;/li&gt;
&lt;li&gt;Simplified query structure (no need for tenant_id in WHERE clauses)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Higher operational complexity for schema management&lt;/li&gt;
&lt;li&gt;Potential performance impact with a large number of schemas&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Database per Tenant
&lt;/h2&gt;

&lt;p&gt;In this approach, each tenant gets their own dedicated database.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new database for each tenant&lt;/li&gt;
&lt;li&gt;Use connection pooling to manage multiple database connections&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strongest isolation between tenants&lt;/li&gt;
&lt;li&gt;Easier to meet specific compliance requirements&lt;/li&gt;
&lt;li&gt;Simplified backup and restore per tenant&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Higher operational complexity&lt;/li&gt;
&lt;li&gt;Potentially higher infrastructure costs&lt;/li&gt;
&lt;li&gt;Challenges with cross-tenant operations&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Hybrid Approach
&lt;/h2&gt;

&lt;p&gt;Combine multiple strategies based on tenant requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use shared database/schema for smaller tenants&lt;/li&gt;
&lt;li&gt;Provide dedicated databases for larger tenants or those with specific needs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flexibility to meet diverse tenant requirements&lt;/li&gt;
&lt;li&gt;Better resource allocation based on tenant needs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increased complexity in managing different models&lt;/li&gt;
&lt;li&gt;Potential challenges in maintaining consistency across models&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use database roles and permissions to enforce access control&lt;/li&gt;
&lt;li&gt;Implement connection pooling for efficient resource utilization&lt;/li&gt;
&lt;li&gt;Use prepared statements to improve query performance&lt;/li&gt;
&lt;li&gt;Regularly monitor and optimize database performance&lt;/li&gt;
&lt;li&gt;Implement robust error handling and connection validation&lt;/li&gt;
&lt;li&gt;Consider using extensions like Citus for horizontal scaling of multi-tenant databases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When choosing a multi-tenancy strategy, consider factors such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Number of tenants&lt;/li&gt;
&lt;li&gt;Data volume per tenant&lt;/li&gt;
&lt;li&gt;Regulatory requirements&lt;/li&gt;
&lt;li&gt;Need for tenant-specific customizations&lt;/li&gt;
&lt;li&gt;Operational complexity you can manage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By carefully evaluating these factors and implementing the appropriate multi-tenancy model, you can build scalable and secure SaaS applications using PostgreSQL.&lt;/p&gt;

&lt;p&gt;Sources&lt;br&gt;
[1] patroni.log &lt;a href="https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/48594683/bf2a6a10-660c-468d-8c51-df8616ec6e5b/patroni.log" rel="noopener noreferrer"&gt;https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/48594683/bf2a6a10-660c-468d-8c51-df8616ec6e5b/patroni.log&lt;/a&gt;&lt;br&gt;
[2] What is multi-tenancy? Pros, cons, and best practices &lt;a href="https://workos.com/blog/what-is-multi-tenancy-pros-cons-best-practices" rel="noopener noreferrer"&gt;https://workos.com/blog/what-is-multi-tenancy-pros-cons-best-practices&lt;/a&gt;&lt;br&gt;
[3] Multi-tenancy - EF Core &lt;a href="https://learn.microsoft.com/en-us/ef/core/miscellaneous/multitenancy" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/ef/core/miscellaneous/multitenancy&lt;/a&gt;&lt;br&gt;
[4] Using Postgres in a Multi-Tenant SaaS &lt;a href="https://postgresconf.org/conferences/PostgresWorld_Webinars_2024/program/proposals/using-postgres-in-a-multi-tenant-saas-securing-everyone-s-data" rel="noopener noreferrer"&gt;https://postgresconf.org/conferences/PostgresWorld_Webinars_2024/program/proposals/using-postgres-in-a-multi-tenant-saas-securing-everyone-s-data&lt;/a&gt;&lt;br&gt;
[5] Multi-tenant SaaS partitioning models for PostgreSQL &lt;a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/partitioning-models.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/partitioning-models.html&lt;/a&gt;&lt;br&gt;
[6] What are the best practices in building multi-tenancy ... &lt;a href="https://stackoverflow.com/questions/717105/what-are-the-best-practices-in-building-multi-tenancy-applications" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/717105/what-are-the-best-practices-in-building-multi-tenancy-applications&lt;/a&gt;&lt;br&gt;
[7] How to Implement Multitenancy in Cloud Computing &lt;a href="https://www.permit.io/blog/multitenancy-in-cloud" rel="noopener noreferrer"&gt;https://www.permit.io/blog/multitenancy-in-cloud&lt;/a&gt;&lt;br&gt;
[8] Implementing managed PostgreSQL for multi-tenant SaaS ... &lt;a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/welcome.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/welcome.html&lt;/a&gt;&lt;br&gt;
[9] Designing Your Postgres Database for Multi-tenancy &lt;a href="https://www.crunchydata.com/blog/designing-your-postgres-database-for-multi-tenancy" rel="noopener noreferrer"&gt;https://www.crunchydata.com/blog/designing-your-postgres-database-for-multi-tenancy&lt;/a&gt;&lt;br&gt;
[10] Approaches to implementing multi-tenancy in SaaS ... &lt;a href="https://developers.redhat.com/articles/2022/05/09/approaches-implementing-multi-tenancy-saas-applications" rel="noopener noreferrer"&gt;https://developers.redhat.com/articles/2022/05/09/approaches-implementing-multi-tenancy-saas-applications&lt;/a&gt;&lt;br&gt;
[11] Designing a Multi-tenant SAAS Database with Postgres RLS &lt;a href="https://stackoverflow.com/questions/70243282/designing-a-multi-tenant-saas-database-with-postgres-rls" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/70243282/designing-a-multi-tenant-saas-database-with-postgres-rls&lt;/a&gt;&lt;br&gt;
[12] How to Implement Multi-Tenancy - Broadcom Techdocs &lt;a href="https://techdocs.broadcom.com/us/en/ca-enterprise-software/business-management/ca-service-management/17-4/administering/configure-ca-service-desk-manager/setting-up-multi-tenancy/how-to-implement-multi-tenancy.html" rel="noopener noreferrer"&gt;https://techdocs.broadcom.com/us/en/ca-enterprise-software/business-management/ca-service-management/17-4/administering/configure-ca-service-desk-manager/setting-up-multi-tenancy/how-to-implement-multi-tenancy.html&lt;/a&gt;&lt;br&gt;
[13] How to Build &amp;amp; Scale a Multi-Tenant SaaS Application &lt;a href="https://acropolium.com/blog/build-scale-a-multi-tenant-saas/" rel="noopener noreferrer"&gt;https://acropolium.com/blog/build-scale-a-multi-tenant-saas/&lt;/a&gt;&lt;br&gt;
[14] Implementing multi-tenancy in Spanner | Solutions &lt;a href="https://cloud.google.com/solutions/implementing-multi-tenancy-cloud-spanner" rel="noopener noreferrer"&gt;https://cloud.google.com/solutions/implementing-multi-tenancy-cloud-spanner&lt;/a&gt;&lt;br&gt;
[15] Good multi-tenant architecture for saas : r/dotnet &lt;a href="https://www.reddit.com/r/dotnet/comments/1acrx5r/good_multitenant_architecture_for_saas/" rel="noopener noreferrer"&gt;https://www.reddit.com/r/dotnet/comments/1acrx5r/good_multitenant_architecture_for_saas/&lt;/a&gt;&lt;br&gt;
[16] Multi-Tenancy Explained. From Fundamentals to ... &lt;a href="https://www.zenarmor.com/docs/network-basics/what-is-multi-tenancy" rel="noopener noreferrer"&gt;https://www.zenarmor.com/docs/network-basics/what-is-multi-tenancy&lt;/a&gt;&lt;br&gt;
[17] Multitenant SaaS patterns - Azure SQL Database &lt;a href="https://learn.microsoft.com/en-us/azure/azure-sql/database/saas-tenancy-app-design-patterns?view=azuresql-db" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/azure/azure-sql/database/saas-tenancy-app-design-patterns?view=azuresql-db&lt;/a&gt;&lt;br&gt;
[18] What is multi-tenancy (multi-tenant architecture)? &lt;a href="https://www.techtarget.com/whatis/definition/multi-tenancy" rel="noopener noreferrer"&gt;https://www.techtarget.com/whatis/definition/multi-tenancy&lt;/a&gt;&lt;br&gt;
[19] Building Multi-Tenant RAG Applications With PostgreSQL &lt;a href="https://www.timescale.com/blog/building-multi-tenant-rag-applications-with-postgresql-choosing-the-right-approach" rel="noopener noreferrer"&gt;https://www.timescale.com/blog/building-multi-tenant-rag-applications-with-postgresql-choosing-the-right-approach&lt;/a&gt;&lt;br&gt;
[20] Multitenant Saas product - DB size &amp;amp; performance &lt;a href="https://www.reddit.com/r/PostgreSQL/comments/k2qkd6/multitenant_saas_product_db_size_performance/" rel="noopener noreferrer"&gt;https://www.reddit.com/r/PostgreSQL/comments/k2qkd6/multitenant_saas_product_db_size_performance/&lt;/a&gt;&lt;br&gt;
[21] Multi-tenancy implementation with PostgreSQL &lt;a href="https://blog.logto.io/implement-multi-tenancy" rel="noopener noreferrer"&gt;https://blog.logto.io/implement-multi-tenancy&lt;/a&gt;&lt;br&gt;
[22] Multi-Tenant Apps &amp;amp; Postgres That Scales Out &lt;a href="https://www.citusdata.com/use-cases/multi-tenant-apps/" rel="noopener noreferrer"&gt;https://www.citusdata.com/use-cases/multi-tenant-apps/&lt;/a&gt;&lt;br&gt;
[23] Handling multi-tenancy with PostgreSQL &lt;a href="https://www.reddit.com/r/PostgreSQL/comments/13yo5rb/handling_multitenancy_with_postgresql/" rel="noopener noreferrer"&gt;https://www.reddit.com/r/PostgreSQL/comments/13yo5rb/handling_multitenancy_with_postgresql/&lt;/a&gt;&lt;br&gt;
[24] Multi-tenant data isolation with PostgreSQL Row Level ... &lt;a href="https://aws.amazon.com/pt/blogs/database/multi-tenant-data-isolation-with-postgresql-row-level-security/" rel="noopener noreferrer"&gt;https://aws.amazon.com/pt/blogs/database/multi-tenant-data-isolation-with-postgresql-row-level-security/&lt;/a&gt;&lt;br&gt;
[25] Multi-Tenancy on PostgreSQL : An Introduction &lt;a href="https://opensource-db.com/multi-tenancy-on-postgres/" rel="noopener noreferrer"&gt;https://opensource-db.com/multi-tenancy-on-postgres/&lt;/a&gt;&lt;br&gt;
[26] Strategies for Using PostgreSQL as a Database for Multi- ... &lt;a href="https://dev.to/lbelkind/strategies-for-using-postgresql-as-a-database-for-multi-tenant-services-4abd"&gt;https://dev.to/lbelkind/strategies-for-using-postgresql-as-a-database-for-multi-tenant-services-4abd&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>saas</category>
      <category>sql</category>
    </item>
    <item>
      <title>How to build multi-tenancy in PostgreSQL for developing SaaS applications?</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Fri, 17 Jan 2025 08:57:54 +0000</pubDate>
      <link>https://forem.com/shiviyer/how-to-build-multi-tenancy-in-postgresql-for-developing-saas-applications-p81</link>
      <guid>https://forem.com/shiviyer/how-to-build-multi-tenancy-in-postgresql-for-developing-saas-applications-p81</guid>
      <description>&lt;p&gt;Building multi-tenancy in &lt;a href="https://minervadb.xyz" rel="noopener noreferrer"&gt;PostgreSQL&lt;/a&gt; for SaaS applications can be achieved through several approaches, each with its own advantages and trade-offs. Here are the main strategies for implementing multi-tenancy in PostgreSQL:&lt;/p&gt;

&lt;h2&gt;
  
  
  Shared Database, Shared Schema
&lt;/h2&gt;

&lt;p&gt;In this approach, all tenants share the same database and schema, with a tenant identifier column used to distinguish between different tenants' data.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a &lt;code&gt;tenant_id&lt;/code&gt; column to all tables that contain tenant-specific data&lt;/li&gt;
&lt;li&gt;Use Row-Level Security (RLS) policies to enforce data isolation between tenants&lt;/li&gt;
&lt;li&gt;Implement database roles and permissions to manage access control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example RLS policy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt; &lt;span class="n"&gt;tenant_isolation_policy&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;mytable&lt;/span&gt;
    &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.current_tenant'&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Efficient resource utilization&lt;/li&gt;
&lt;li&gt;Easier maintenance and updates&lt;/li&gt;
&lt;li&gt;Simplified backup and restore processes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Potential for data leakage if not implemented correctly&lt;/li&gt;
&lt;li&gt;May require more complex application logic to handle tenant isolation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Shared Database, Separate Schemas
&lt;/h2&gt;

&lt;p&gt;This model uses a single database but creates a separate schema for each tenant.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new schema for each tenant&lt;/li&gt;
&lt;li&gt;Use search_path to switch between tenant schemas&lt;/li&gt;
&lt;li&gt;Implement schema-level permissions for access control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example schema creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="n"&gt;tenant_123&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;search_path&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;tenant_123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better logical separation between tenants&lt;/li&gt;
&lt;li&gt;Easier to implement tenant-specific customizations&lt;/li&gt;
&lt;li&gt;Simplified query structure (no need for tenant_id in WHERE clauses)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Higher operational complexity for schema management&lt;/li&gt;
&lt;li&gt;Potential performance impact with a large number of schemas&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Database per Tenant
&lt;/h2&gt;

&lt;p&gt;In this approach, each tenant gets their own dedicated database.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new database for each tenant&lt;/li&gt;
&lt;li&gt;Use connection pooling to manage multiple database connections&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strongest isolation between tenants&lt;/li&gt;
&lt;li&gt;Easier to meet specific compliance requirements&lt;/li&gt;
&lt;li&gt;Simplified backup and restore per tenant&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Higher operational complexity&lt;/li&gt;
&lt;li&gt;Potentially higher infrastructure costs&lt;/li&gt;
&lt;li&gt;Challenges with cross-tenant operations&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Hybrid Approach
&lt;/h2&gt;

&lt;p&gt;Combine multiple strategies based on tenant requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use shared database/schema for smaller tenants&lt;/li&gt;
&lt;li&gt;Provide dedicated databases for larger tenants or those with specific needs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flexibility to meet diverse tenant requirements&lt;/li&gt;
&lt;li&gt;Better resource allocation based on tenant needs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increased complexity in managing different models&lt;/li&gt;
&lt;li&gt;Potential challenges in maintaining consistency across models&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use database roles and permissions to enforce access control&lt;/li&gt;
&lt;li&gt;Implement connection pooling for efficient resource utilization&lt;/li&gt;
&lt;li&gt;Use prepared statements to improve query performance&lt;/li&gt;
&lt;li&gt;Regularly monitor and optimize database performance&lt;/li&gt;
&lt;li&gt;Implement robust error handling and connection validation&lt;/li&gt;
&lt;li&gt;Consider using extensions like Citus for horizontal scaling of multi-tenant databases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When choosing a multi-tenancy strategy, consider factors such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Number of tenants&lt;/li&gt;
&lt;li&gt;Data volume per tenant&lt;/li&gt;
&lt;li&gt;Regulatory requirements&lt;/li&gt;
&lt;li&gt;Need for tenant-specific customizations&lt;/li&gt;
&lt;li&gt;Operational complexity you can manage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By carefully evaluating these factors and implementing the appropriate multi-tenancy model, you can build scalable and secure SaaS applications using PostgreSQL.&lt;/p&gt;

&lt;p&gt;Sources&lt;br&gt;
[1] patroni.log &lt;a href="https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/48594683/bf2a6a10-660c-468d-8c51-df8616ec6e5b/patroni.log" rel="noopener noreferrer"&gt;https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/48594683/bf2a6a10-660c-468d-8c51-df8616ec6e5b/patroni.log&lt;/a&gt;&lt;br&gt;
[2] What is multi-tenancy? Pros, cons, and best practices &lt;a href="https://workos.com/blog/what-is-multi-tenancy-pros-cons-best-practices" rel="noopener noreferrer"&gt;https://workos.com/blog/what-is-multi-tenancy-pros-cons-best-practices&lt;/a&gt;&lt;br&gt;
[3] Multi-tenancy - EF Core &lt;a href="https://learn.microsoft.com/en-us/ef/core/miscellaneous/multitenancy" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/ef/core/miscellaneous/multitenancy&lt;/a&gt;&lt;br&gt;
[4] Using Postgres in a Multi-Tenant SaaS &lt;a href="https://postgresconf.org/conferences/PostgresWorld_Webinars_2024/program/proposals/using-postgres-in-a-multi-tenant-saas-securing-everyone-s-data" rel="noopener noreferrer"&gt;https://postgresconf.org/conferences/PostgresWorld_Webinars_2024/program/proposals/using-postgres-in-a-multi-tenant-saas-securing-everyone-s-data&lt;/a&gt;&lt;br&gt;
[5] Multi-tenant SaaS partitioning models for PostgreSQL &lt;a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/partitioning-models.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/partitioning-models.html&lt;/a&gt;&lt;br&gt;
[6] What are the best practices in building multi-tenancy ... &lt;a href="https://stackoverflow.com/questions/717105/what-are-the-best-practices-in-building-multi-tenancy-applications" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/717105/what-are-the-best-practices-in-building-multi-tenancy-applications&lt;/a&gt;&lt;br&gt;
[7] How to Implement Multitenancy in Cloud Computing &lt;a href="https://www.permit.io/blog/multitenancy-in-cloud" rel="noopener noreferrer"&gt;https://www.permit.io/blog/multitenancy-in-cloud&lt;/a&gt;&lt;br&gt;
[8] Implementing managed PostgreSQL for multi-tenant SaaS ... &lt;a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/welcome.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/welcome.html&lt;/a&gt;&lt;br&gt;
[9] Designing Your Postgres Database for Multi-tenancy &lt;a href="https://www.crunchydata.com/blog/designing-your-postgres-database-for-multi-tenancy" rel="noopener noreferrer"&gt;https://www.crunchydata.com/blog/designing-your-postgres-database-for-multi-tenancy&lt;/a&gt;&lt;br&gt;
[10] Approaches to implementing multi-tenancy in SaaS ... &lt;a href="https://developers.redhat.com/articles/2022/05/09/approaches-implementing-multi-tenancy-saas-applications" rel="noopener noreferrer"&gt;https://developers.redhat.com/articles/2022/05/09/approaches-implementing-multi-tenancy-saas-applications&lt;/a&gt;&lt;br&gt;
[11] Designing a Multi-tenant SAAS Database with Postgres RLS &lt;a href="https://stackoverflow.com/questions/70243282/designing-a-multi-tenant-saas-database-with-postgres-rls" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/70243282/designing-a-multi-tenant-saas-database-with-postgres-rls&lt;/a&gt;&lt;br&gt;
[12] How to Implement Multi-Tenancy - Broadcom Techdocs &lt;a href="https://techdocs.broadcom.com/us/en/ca-enterprise-software/business-management/ca-service-management/17-4/administering/configure-ca-service-desk-manager/setting-up-multi-tenancy/how-to-implement-multi-tenancy.html" rel="noopener noreferrer"&gt;https://techdocs.broadcom.com/us/en/ca-enterprise-software/business-management/ca-service-management/17-4/administering/configure-ca-service-desk-manager/setting-up-multi-tenancy/how-to-implement-multi-tenancy.html&lt;/a&gt;&lt;br&gt;
[13] How to Build &amp;amp; Scale a Multi-Tenant SaaS Application &lt;a href="https://acropolium.com/blog/build-scale-a-multi-tenant-saas/" rel="noopener noreferrer"&gt;https://acropolium.com/blog/build-scale-a-multi-tenant-saas/&lt;/a&gt;&lt;br&gt;
[14] Implementing multi-tenancy in Spanner | Solutions &lt;a href="https://cloud.google.com/solutions/implementing-multi-tenancy-cloud-spanner" rel="noopener noreferrer"&gt;https://cloud.google.com/solutions/implementing-multi-tenancy-cloud-spanner&lt;/a&gt;&lt;br&gt;
[15] Good multi-tenant architecture for saas : r/dotnet &lt;a href="https://www.reddit.com/r/dotnet/comments/1acrx5r/good_multitenant_architecture_for_saas/" rel="noopener noreferrer"&gt;https://www.reddit.com/r/dotnet/comments/1acrx5r/good_multitenant_architecture_for_saas/&lt;/a&gt;&lt;br&gt;
[16] Multi-Tenancy Explained. From Fundamentals to ... &lt;a href="https://www.zenarmor.com/docs/network-basics/what-is-multi-tenancy" rel="noopener noreferrer"&gt;https://www.zenarmor.com/docs/network-basics/what-is-multi-tenancy&lt;/a&gt;&lt;br&gt;
[17] Multitenant SaaS patterns - Azure SQL Database &lt;a href="https://learn.microsoft.com/en-us/azure/azure-sql/database/saas-tenancy-app-design-patterns?view=azuresql-db" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/azure/azure-sql/database/saas-tenancy-app-design-patterns?view=azuresql-db&lt;/a&gt;&lt;br&gt;
[18] What is multi-tenancy (multi-tenant architecture)? &lt;a href="https://www.techtarget.com/whatis/definition/multi-tenancy" rel="noopener noreferrer"&gt;https://www.techtarget.com/whatis/definition/multi-tenancy&lt;/a&gt;&lt;br&gt;
[19] Building Multi-Tenant RAG Applications With PostgreSQL &lt;a href="https://www.timescale.com/blog/building-multi-tenant-rag-applications-with-postgresql-choosing-the-right-approach" rel="noopener noreferrer"&gt;https://www.timescale.com/blog/building-multi-tenant-rag-applications-with-postgresql-choosing-the-right-approach&lt;/a&gt;&lt;br&gt;
[20] Multitenant Saas product - DB size &amp;amp; performance &lt;a href="https://www.reddit.com/r/PostgreSQL/comments/k2qkd6/multitenant_saas_product_db_size_performance/" rel="noopener noreferrer"&gt;https://www.reddit.com/r/PostgreSQL/comments/k2qkd6/multitenant_saas_product_db_size_performance/&lt;/a&gt;&lt;br&gt;
[21] Multi-tenancy implementation with PostgreSQL &lt;a href="https://blog.logto.io/implement-multi-tenancy" rel="noopener noreferrer"&gt;https://blog.logto.io/implement-multi-tenancy&lt;/a&gt;&lt;br&gt;
[22] Multi-Tenant Apps &amp;amp; Postgres That Scales Out &lt;a href="https://www.citusdata.com/use-cases/multi-tenant-apps/" rel="noopener noreferrer"&gt;https://www.citusdata.com/use-cases/multi-tenant-apps/&lt;/a&gt;&lt;br&gt;
[23] Handling multi-tenancy with PostgreSQL &lt;a href="https://www.reddit.com/r/PostgreSQL/comments/13yo5rb/handling_multitenancy_with_postgresql/" rel="noopener noreferrer"&gt;https://www.reddit.com/r/PostgreSQL/comments/13yo5rb/handling_multitenancy_with_postgresql/&lt;/a&gt;&lt;br&gt;
[24] Multi-tenant data isolation with PostgreSQL Row Level ... &lt;a href="https://aws.amazon.com/pt/blogs/database/multi-tenant-data-isolation-with-postgresql-row-level-security/" rel="noopener noreferrer"&gt;https://aws.amazon.com/pt/blogs/database/multi-tenant-data-isolation-with-postgresql-row-level-security/&lt;/a&gt;&lt;br&gt;
[25] Multi-Tenancy on PostgreSQL : An Introduction &lt;a href="https://opensource-db.com/multi-tenancy-on-postgres/" rel="noopener noreferrer"&gt;https://opensource-db.com/multi-tenancy-on-postgres/&lt;/a&gt;&lt;br&gt;
[26] Strategies for Using PostgreSQL as a Database for Multi- ... &lt;a href="https://dev.to/lbelkind/strategies-for-using-postgresql-as-a-database-for-multi-tenant-services-4abd"&gt;https://dev.to/lbelkind/strategies-for-using-postgresql-as-a-database-for-multi-tenant-services-4abd&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>saas</category>
      <category>sql</category>
    </item>
    <item>
      <title>What are the benefits of using bounded quantifiers in regex</title>
      <dc:creator>Shiv Iyer</dc:creator>
      <pubDate>Fri, 10 Jan 2025 19:13:40 +0000</pubDate>
      <link>https://forem.com/shiviyer/what-are-the-benefits-of-using-bounded-quantifiers-in-regex-jo6</link>
      <guid>https://forem.com/shiviyer/what-are-the-benefits-of-using-bounded-quantifiers-in-regex-jo6</guid>
      <description>&lt;p&gt;Bounded quantifiers in regular expressions offer several significant benefits:&lt;/p&gt;

&lt;h2&gt;
  
  
  Improved Performance
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced Backtracking&lt;/strong&gt;: By specifying a maximum limit, bounded quantifiers prevent excessive backtracking, which can lead to catastrophic performance issues with large inputs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Faster Matching&lt;/strong&gt;: The regex engine can optimize its matching strategy when it knows the upper and lower bounds of repetitions.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Enhanced Precision
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Increased Accuracy&lt;/strong&gt;: Bounded quantifiers allow you to define more precise patterns, reducing false positives in matches.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Better Data Validation&lt;/strong&gt;: They're particularly useful for validating input of a specific length or range, such as phone numbers or postal codes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Resource Management
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Controlled Memory Usage&lt;/strong&gt;: By limiting the number of repetitions, you prevent potential out-of-memory errors that can occur with unbounded patterns on large inputs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Predictable Execution Time&lt;/strong&gt;: Bounded quantifiers help ensure that regex operations complete within a reasonable timeframe, even on varying input sizes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Improved Readability and Maintainability
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clear Intent&lt;/strong&gt;: Bounded quantifiers make the regex pattern's intent clearer to other developers who may need to maintain the code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier Debugging&lt;/strong&gt;: When troubleshooting, having explicit bounds makes it easier to understand and modify the pattern if needed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Consider this pattern for matching a US phone number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\d{3}-\d{3}-\d{4}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This pattern is more precise and efficient than an unbounded alternative like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\d+-\d+-\d+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;By using bounded quantifiers, you create more robust, efficient, and maintainable regular expressions.&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://minervadb.xyz/row-store-index/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2024%2F12%2Fshutterstock_171071111.jpg" height="372" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://minervadb.xyz/row-store-index/" rel="noopener noreferrer" class="c-link"&gt;
          PostgreSQL Row Store Indexes: Exploring Indexing Options
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Discover whether PostgreSQL supports Row Store Indexes and how they impact database performance and query optimization."
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2020%2F10%2Fcropped-LogoColorTextRight-32x32.jpeg" width="32" height="32"&gt;
        minervadb.xyz
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;




&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://minervadb.xyz/redis-tuning/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2023%2F04%2Fshutterstock_642668056-scaled.jpg" height="445" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://minervadb.xyz/redis-tuning/" rel="noopener noreferrer" class="c-link"&gt;
          Redis Optimization:Performance Tuning for High Traffic applications
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Redis Optimization for high-traffic apps with tips on memory, persistence, and connection settings for top performance.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fminervadb.xyz%2Fwp-content%2Fuploads%2F2020%2F10%2Fcropped-LogoColorTextRight-32x32.jpeg" width="32" height="32"&gt;
        minervadb.xyz
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://chistadata.com/maintenance-plan-for-optimal-clickhouse-infrastructure-operations/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchistadata.com%2Fwp-content%2Fuploads%2F2025%2F01%2Fshutterstock_2524031289-scaled.jpg" height="530" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://chistadata.com/maintenance-plan-for-optimal-clickhouse-infrastructure-operations/" rel="noopener noreferrer" class="c-link"&gt;
          Maintenance Plan for Optimal ClickHouse Infrastructure Operations
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          ClickHouse Maintenance Plan for Performance, Scalability, and High Availability - ClickHouse DBA Support - ClickHouse
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fchistadata.com%2Fwp-content%2Fuploads%2F2021%2F05%2Fcropped-Logo-1-32x32.jpg" width="32" height="32"&gt;
        chistadata.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>regex</category>
      <category>sql</category>
      <category>postgres</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
