Querying PostgreSQL, MySQL, and SQL Server Directly from Databricks

Querying PostgreSQL, MySQL, and SQL Server Directly from Databricks 

Querying live OLTP (Transactional) data through Amazon Redshift’s Federated Query layer from Databricks involves bridging Databricks Lakehouse Federation with Amazon Redshift's own external schema federation. 

This configuration creates a multi-layered virtualization pipeline: Databricks queries Redshift via JDBC, and Redshift simultaneously pushes subqueries down to your operational OLTP databases (like Amazon RDS/Aurora for PostgreSQL or MySQL) on the fly. The conceptual architecture, setup procedures, and performance implications are detailed below.

1. Conceptual Architecture & Data Flow
When you execute a query in Databricks to fetch transactional data, it flows through two federation boundaries:
[ Databricks (Lakehouse Federation) ]
               │
               ▼  (JDBC Read Connection with Query Pushdown)
[ Amazon Redshift (OLAP Warehouse Layer) ]
               │
               ▼  (Redshift Federated Query Layer via Secrets Manager)
[ Operational OLTP Databases (RDS/Aurora Postgres or MySQL) ]

  1. Databricks Layer: A user or notebook runs a standard SQL query targeting a Redshift "Foreign Catalog" registered via Unity Catalog.
  2. Redshift Layer (The Bridge): Redshift acts as a proxy. It parses the incoming query and recognizes that the requested tables reside in its own configured EXTERNAL SCHEMA pointing to the OLTP instance.
  3. OLTP Layer: Redshift evaluates what logic can be executed remotely, optimizes the execution plan, and pushes the filtered SQL down to the RDS or Aurora instance. Only the filtered, structural query rows are sent back to Redshift, which compiles them and streams them up to the Databricks cluster. 
2. Step-by-Step Configuration Guide
Step A: Configure Redshift to Query the OLTP Database
Before connecting Databricks, Redshift must be able to natively access your transactional data.
  • Store Credentials: Save your OLTP database login credentials securely inside AWS Secrets Manager.
  • IAM Roles: Attach a policy to your Amazon Redshift IAM role allowing it to read that AWS Secrets Manager secret and access the VPC where your OLTP database runs.
  • Map the External Schema: In your Redshift query editor, link your operational database to an external schema:
    CREATE EXTERNAL SCHEMA oltp_live
    FROM POSTGRES -- or MYSQL
    DATABASE 'your_oltp_db_name'
    SCHEMA 'public'
    URI 'your-rds-endpoint.amazonaws.com'
    IAM_ROLE 'arn:aws:iam::123456789012:role/MyRedshiftRole'
    SECRET_ARN 'arn:aws:us-east-1:123456789012:secret:OltpSecret'
    • Step B: Connect Databricks to Amazon Redshift via Unity Catalog
      Using Databricks Lakehouse Federation, create a live managed hook into your Redshift cluster.
    • Open the Catalog Explorer in Databricks, click Add, and select Create a connection.
    • Set the connection type to Redshift and supply your endpoint, port 5439, and Redshift user credentials.
    • Create a Foreign Catalog mapping directly to that connection:
    • CREATE FOREIGN CATALOG redshift_federated
    • USING CONNECTION MyRedshiftConnection
    • OPTIONS (database 'your_redshift_dw_name');
    Assign permissions to your data engineering or analytics groups inside Unity Catalog.
    Step C: Querying via Databricks
    Once registered, you can effortlessly run federated queries natively combining operational OLTP information with Delta tables directly inside Databricks:
    SELECT c.customer_id,  c.status,
                   sum(o.order_amount) as total_spent
    FROM redshift_federated.oltp_live.orders o  -- Live RDS Database table
    JOIN main.gold.customer_profiles c            -- Native Databricks Delta table
    ON o.customer_id = c.customer_id
    GROUP BY 1, 2;
    3. Critical Performance & Architectural Considerations
    While double federation completely eliminates the need for expensive, brittle ETL orchestration pipelines, it demands strict guardrails to prevent system failures.
    • Query Pushdown Efficiency: Databricks attempts to pass execution tasks to Redshift, which tries to push them to the OLTP node. If you use Spark-specific functions or join complex tables, the pushdown might break. This forces Databricks to pull broad datasets over the network to compute them locally, introducing substantial latency.
    • Transactional Source Strain: OLTP engines (like Postgres or MySQL) are tuned for highly efficient transactional processing, not analytical aggregations. Complex GROUP BY statements or large unfiltered SELECT * commands routed from Databricks can lock operational tables and severely impact client-facing applications.
    • Read-Only Constraints: This architecture is strictly read-only. You cannot utilize Databricks SQL or Spark commands to write updates or append new entries directly into your production tables via this federated pipeline. 
    • Network & Cross-Region Costs: Keep your Databricks Workspace, Redshift Cluster, and RDS Database running inside the same AWS Availability Zone or Region. Cross-region network requests will quickly escalate cloud billing and inflate query response times.
    Redshift Federation concept vs Redshift Spectrum

    While both features allow Amazon Redshift to query data outside its local cluster storage without loading it first, they serve completely different use cases, target different external data sources, and scale differently.
    Here is the direct breakdown of Redshift Federated Query vs. Redshift Spectrum
    1. Core Structural Differences
    • Redshift Federated Query is designed for operational databases. It allows Redshift to query live, transactional relational databases (OLTP) like Amazon RDS or Amazon Aurora (PostgreSQL and MySQL). It relies on the remote database's engine to process the queries.
    • Redshift Spectrum is designed for data lakes. It allows Redshift to query massive unstructured or semi-structured datasets stored as flat files (like Parquet, ORC, CSV, or JSON) directly in Amazon S3. It utilizes its own dedicated, serverless fleet of computing resources to scan data.
    2. Feature Comparison Matrix
    FeatureRedshift Federated QueryRedshift Spectrum
    Primary Target SourceOperational Databases (RDS, Aurora PostgreSQL/MySQL).Data Lakes (Amazon S3 bucket files).
    Data Types / FormatsRelational database tables and rows.Parquet, ORC, CSV, JSON, Avro, etc.
    Compute ResourcePushes processing down to the target database engine.Uses an independent, serverless fleet managed by AWS.
    Primary Use CaseReal-time operational reporting and immediate ETL ingestion.Querying historical data, cold data storage, and massive logs.
    Storage CostCharged by standard RDS/Aurora provisioned database rates.Charged by standard Amazon S3 storage rates (very low).
    Query CostFree feature (but consumes CPU/IO on your target OLTP database).$5 per Terabyte of data scanned by Spectrum.
    Scalability LimitLimited by the hardware/instance size of the target database.Virtually unlimited scaling (scales to exabytes seamlessly).
    Metadata CatalogConfigured directly via an EXTERNAL SCHEMA pointing to a DB connection.Relies on AWS Glue Data Catalog or Athena Data Catalog.

    3. Detailed Architectural Comparison
    How Redshift Federated Query Works
    Federated Query acts as a secure proxy pipeline. When a query is executed, Redshift's query optimizer evaluates the query plan, isolates the parts of the query that belong to the remote OLTP database, and generates a remote SQL command.
    • The Pushdown Mechanism: It pushes predicates (like WHERE clause filters) and aggregations directly to the RDS instance.
    • Performance Trap: If your query forces a massive table scan on a production database, it can exhaust the production database's CPU, slow down customer-facing applications, and cause lock contention.
    How Redshift Spectrum Works
    Spectrum does not touch relational engines. When you query an S3 external table, Redshift distributes the execution plan to thousands of temporary, serverless "Spectrum worker nodes." 
    • The Serverless Scan: These workers scan your S3 bucket files concurrently, handle projection and filtering in parallel, stream the highly compressed results back to your core Redshift cluster, and join them with your local disk tables.
    • Performance Trap: If your S3 data is not optimized (e.g., partitioned by date, or saved in a columnar format like Parquet), Spectrum will scan unnecessary gigabytes of data, causing your AWS bill to spike and performance to degrade.
    4. Summary: When to Use Which?
    • Use Federated Query if: You need a real-time dashboard showing today's live e-commerce transactions, or you want to build an incremental nightly ETL pipeline to pull Yesterday's new data from an app's MySQL database into Redshift.
    • Use Redshift Spectrum if: You have 50 Terabytes of application event logs from 2024 stored cheaply in S3, and you want to run historical trend analysis or join it with your local high-value data warehouse tables without paying for local Redshift storage disks.
    Are you evaluating these two options to optimize data storage costs, or are you designing an architecture for a specific hybrid workload (e.g., combining live operational data with historical data)? Let me know, and I can sketch out a recommended design pattern for your exact data footprint.

    Comments

    Popular posts from this blog

    The Complete Guide to DBT (Data Build Tool) File Structure and YAML Configurations

    Getting Started with DBT Core

    A Deep Dive into dbt debug and Logs