Join tables from two different databases with Query Forge
Query Forge runs one SQL statement over tables that sit behind different agents, for example customers in PostgreSQL and orders in SQL Server, and does as much of the work as it can on each source. This guide runs such a join from a JDBC client, reads how Query Forge plans it, and shows what to do when the tables are large.
In this guide
You will connect a JDBC client, write a join across two agents, read its plan with EXPLAIN, follow a run with EXPLAIN ANALYZE, and see what changes when both tables hold millions of rows. You will also list and stop a running statement from Core Hub.
Quick information
Goal |
Query tables of two agents with one SQL statement and keep it fast as the tables grow |
Audience |
Data engineers and analysts who use a SQL client or a BI tool |
Level |
Intermediate |
Area |
Query Forge |
Applies to |
Gluesync 2.3.3.0 and later |
Before you begin
Make sure that:
-
Core Hub has two pipelines with SQL-capable agents, for example a PostgreSQL source and a SQL Server source. You can query both, see Query Studio data-access policy;
-
You have a Personal API Token (
gsp_…) that covers both agents; -
A JDBC client is installed, such as DBeaver, with the Gluesync JDBC driver. See Connect BI tools to Query Forge;
-
The table names below stand for your own tables. The examples use
crm.customers(PostgreSQL) andsales.orders(SQL Server).
Guide steps
Connect and find your tables
-
Create a connection with the URL
jdbc:gluesync://<corehub-host>:8765, your user name, and your token as the password. -
Open the schema browser. Each SQL-capable agent you may query is a schema, named after its pipeline, or after the pipeline and the agent when a pipeline has several. Its tables are below it.
Write the join
Join the two tables as if they were in one database:
SELECT c.region, COUNT(*) AS orders, SUM(o.amount) AS revenue
FROM crm.customers c
JOIN sales.orders o ON o.customer_id = c.id
WHERE o.order_date >= DATE '2026-01-01'
GROUP BY c.region
ORDER BY revenue DESC;
Query Forge splits the statement. The filter on o.order_date runs on SQL Server. The aggregate is split too: each agent groups its own rows by the join key and the columns it holds, and Query Forge combines the sub-totals, so the orders are not paged into Core Hub. The result is the same as if one database ran the statement.
Read the plan
Put EXPLAIN PLAN FOR in front of the statement, and run it:
EXPLAIN PLAN FOR
SELECT c.region, COUNT(*) AS orders, SUM(o.amount) AS revenue
FROM crm.customers c
JOIN sales.orders o ON o.customer_id = c.id
WHERE o.order_date >= DATE '2026-01-01'
GROUP BY c.region;
The answer is one PLAN cell. Read it for four things:
-
Remote scans. The agent, its dialect and the SQL each agent receives. Literals show as
?, so the plan never shows a value read from your data. -
Join strategies. The strategies Query Forge may take for the join across agents, in the order it tries them.
-
Residual engine. Whether the embedded engine would run the join.
-
Not pushed. Why an operator stays in Query Forge, for example a function the source does not evaluate the same way, or a masked column.
EXPLAIN reads no data, so it is safe to run on any statement. See Explain and analyze a federated query.
Follow a run
EXPLAIN ANALYZE runs the statement and returns a report instead of the rows:
EXPLAIN ANALYZE
SELECT c.region, COUNT(*) AS orders, SUM(o.amount) AS revenue
FROM crm.customers c
JOIN sales.orders o ON o.customer_id = c.id
GROUP BY c.region;
The report gives the total time, the rows read, the join strategy that was taken, and for each agent its pages, rows and time in calls. A statement that read few rows from each agent had a selective side or a pushed aggregate. A statement that read millions of rows from an agent did not: filter it, or aggregate on the source.
Keep it fast with large tables
How a join across agents runs depends on its sides, not only on their size:
| The sides | What Query Forge does |
|---|---|
One side is selective, up to 50,000 rows after its filters |
Reads that side first and asks the other source only for the matching rows, by key. |
Both sides are large and unique on one integer key |
Reads both in key order, page by page, and merges them. Nothing is held in memory. |
Both sides are large and neither is selective or unique |
Hands the join to the embedded engine, which joins and aggregates under a memory limit and spills to disk. |
The first two cases need nothing from you. For the third:
-
Run
EXPLAIN ANALYZEand check that the report names the residual engine for the join, with rows and bytes loaded and the bytes spilled. If it does not,EXPLAINsays why the engine did not take the join, for example a column type it does not carry. -
Ask an administrator to check the engine’s memory and disk,
GLUESYNC_JDBC_RESIDUAL_MEMORY_LIMITandGLUESYNC_JDBC_RESIDUAL_MAX_TEMP_DIRECTORY_SIZE, and to leave room for them outside the Java heap. See Memory and disk of the residual engine. -
Raise
queryTimeouton your connection when the statement may run longer than the default. A client that waits less than the server fails first. See Client response timeout for long queries.
Filtering each side as much as you can helps in every case, because every row an agent returns has to be read.
List and stop a statement
A statement that runs too long can be stopped. In your client, use Stop: with the Gluesync driver on a LAN connection it cancels the statement on the server. An administrator, or you for your own statements, can also stop it from Core Hub:
curl -fsS -H "Authorization: Bearer $GS_TOKEN" https://corehub.example.com/query-forge/statements
curl -fsS -X DELETE -H "Authorization: Bearer $GS_TOKEN" https://corehub.example.com/query-forge/statements/<queryId>
The first call lists the statements running now, with their query id, state, elapsed time and the rows read from each agent. The second stops one, and its client gets SQLState 57014. GET /query-forge/statements/history lists the statements that ended. See Monitor and stop statements.
Troubleshooting
| Symptom | What to do |
|---|---|
The statement fails with SQLState |
It needed more rows from one table than the scan cap allows, or more spill space than the engine may use. The message names the limit. Filter or aggregate on the source, or see Query Forge troubleshooting. |
The statement fails with SQLState |
Query Forge is busy or your token holds its share of the threads. Retry shortly. |
The statement fails with SQLState |
The statement timeout ran out, or a source call did. Make the statement cheaper, or raise |
|
It needs a plain statement. Run it as a normal statement, not as a prepared one. |
Related guides
Contact MOLO17 Support if you need assistance with Query Forge.