<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Mismatch of spark.catalog.tableExists and SHOW TABLES in streams in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/166874#M55624</link>
    <description>&lt;P&gt;Consider this minimal example of a streaming workflow:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os
from pyspark.sql import functions as F, DataFrame
from typing import Literal

# Change these if you want to replicate
CATALOG, SCHEMA = 'partner_demo_catalog', 'ad_sales_demo'

spark.sql(f'use {CATALOG}.{SCHEMA}')
log_file = os.path.join(os.getcwd(), 'debug.txt')

def exists(table_name: str, method: Literal['spark', 'sql']) -&amp;gt; bool:

    # Bugged
    if method == 'spark':
        return spark.catalog.tableExists(table_name)
    
    # Working fine
    return spark.sql('show tables').filter(F.col('tableName') == table_name).count() &amp;gt; 0

def foreach_batch(batch_df: DataFrame, batch_id: int) -&amp;gt; None:

    # Write logs to a text file for simplicity
    with open(log_file, 'a') as f:
        
        f.write(f'batch_id: {batch_id}\n')
        
        # Check if the table is in the catalog
        if exists('t', 'sql'): f.write('Before stream, SQL: IN\n')
        else: f.write('Before stream, SQL: OUT\n')
        if exists('t', 'spark'): f.write('Before stream, Spark: IN!\n')
        else: f.write('Before stream, Spark: OUT!\n')

        # Write the table to the catalog
        batch_df.write.format("delta").mode('overwrite').saveAsTable('t')
        
        # Check if the table is in the catalog again
        if exists('t', 'sql'): f.write('After stream, SQL: IN\n')
        else: f.write('After stream, SQL: OUT\n')
        if exists('t', 'spark'): f.write('After stream, Spark: IN!\n')
        else: f.write('After stream, Spark: OUT!\n')
        
        f.write('\n')

def test_stream(df: str, i: int) -&amp;gt; None:
    (
        spark
        .readStream.table(df)
        .writeStream.foreachBatch(foreach_batch)

        # Pass a different checkpoint location just to trigger the stream again with no changes
        .option("checkpointLocation", f'{os.getcwd()}/checkpoints_test_{i}')

        .trigger(availableNow=True)
        .start()
        .awaitTermination()
    )

spark.sql('drop table if exists t')
spark.sql('drop table if exists src')

df_src=spark.createDataFrame([('Alice', 1)], ['name', 'age'])
df_src.write.format("delta").mode('overwrite').saveAsTable('src')

test_stream('src', 0)
test_stream('src', 1)&lt;/LI-CODE&gt;&lt;P&gt;As it can be seen, the script creates a toy table `src`, from which a stream is created and triggered twice. Inside `foreachBatch` I create a new toy table `t`. I check whether spark can see the new table in two ways: `spark.catalog.tableExists()` and Spark SQL's `SHOW TABLES`. For simplicity I am logging to a regular .txt file. The contents of it after running the script (using serverless compute and unity catalog) are the following:&lt;/P&gt;&lt;PRE&gt;batch_id: 0&lt;BR /&gt;Before stream, SQL: OUT&lt;BR /&gt;Before stream, Spark: OUT!&lt;BR /&gt;After stream, SQL: IN&lt;BR /&gt;After stream, Spark: IN!&lt;BR /&gt;&lt;BR /&gt;batch_id: 0&lt;BR /&gt;Before stream, SQL: IN&lt;BR /&gt;Before stream, Spark: OUT! # Key line here&lt;BR /&gt;After stream, SQL: IN&lt;BR /&gt;After stream, Spark: IN!&lt;/PRE&gt;&lt;P&gt;This means that `spark.catalog.tableExists()` fails to see the table `t` even though it clearly exists in the catalog. Could anyone clarify what is going on and whether that behavior is intended? According to Genie, that is "a bug in the Spark Connect foreachBatch implementation".&lt;/P&gt;&lt;P&gt;I am using different checkpoint paths just so that I can trigger the full stream again without changing the source table. Still, using the same path and updating the table between streaming runs does not change the behavior. Using `batch_df`'s spark session has no effect either.&lt;/P&gt;</description>
    <pubDate>Mon, 31 Aug 2026 08:31:24 GMT</pubDate>
    <dc:creator>i4ata</dc:creator>
    <dc:date>2026-08-31T08:31:24Z</dc:date>
    <item>
      <title>Mismatch of spark.catalog.tableExists and SHOW TABLES in streams</title>
      <link>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/166874#M55624</link>
      <description>&lt;P&gt;Consider this minimal example of a streaming workflow:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os
from pyspark.sql import functions as F, DataFrame
from typing import Literal

# Change these if you want to replicate
CATALOG, SCHEMA = 'partner_demo_catalog', 'ad_sales_demo'

spark.sql(f'use {CATALOG}.{SCHEMA}')
log_file = os.path.join(os.getcwd(), 'debug.txt')

def exists(table_name: str, method: Literal['spark', 'sql']) -&amp;gt; bool:

    # Bugged
    if method == 'spark':
        return spark.catalog.tableExists(table_name)
    
    # Working fine
    return spark.sql('show tables').filter(F.col('tableName') == table_name).count() &amp;gt; 0

def foreach_batch(batch_df: DataFrame, batch_id: int) -&amp;gt; None:

    # Write logs to a text file for simplicity
    with open(log_file, 'a') as f:
        
        f.write(f'batch_id: {batch_id}\n')
        
        # Check if the table is in the catalog
        if exists('t', 'sql'): f.write('Before stream, SQL: IN\n')
        else: f.write('Before stream, SQL: OUT\n')
        if exists('t', 'spark'): f.write('Before stream, Spark: IN!\n')
        else: f.write('Before stream, Spark: OUT!\n')

        # Write the table to the catalog
        batch_df.write.format("delta").mode('overwrite').saveAsTable('t')
        
        # Check if the table is in the catalog again
        if exists('t', 'sql'): f.write('After stream, SQL: IN\n')
        else: f.write('After stream, SQL: OUT\n')
        if exists('t', 'spark'): f.write('After stream, Spark: IN!\n')
        else: f.write('After stream, Spark: OUT!\n')
        
        f.write('\n')

def test_stream(df: str, i: int) -&amp;gt; None:
    (
        spark
        .readStream.table(df)
        .writeStream.foreachBatch(foreach_batch)

        # Pass a different checkpoint location just to trigger the stream again with no changes
        .option("checkpointLocation", f'{os.getcwd()}/checkpoints_test_{i}')

        .trigger(availableNow=True)
        .start()
        .awaitTermination()
    )

spark.sql('drop table if exists t')
spark.sql('drop table if exists src')

df_src=spark.createDataFrame([('Alice', 1)], ['name', 'age'])
df_src.write.format("delta").mode('overwrite').saveAsTable('src')

test_stream('src', 0)
test_stream('src', 1)&lt;/LI-CODE&gt;&lt;P&gt;As it can be seen, the script creates a toy table `src`, from which a stream is created and triggered twice. Inside `foreachBatch` I create a new toy table `t`. I check whether spark can see the new table in two ways: `spark.catalog.tableExists()` and Spark SQL's `SHOW TABLES`. For simplicity I am logging to a regular .txt file. The contents of it after running the script (using serverless compute and unity catalog) are the following:&lt;/P&gt;&lt;PRE&gt;batch_id: 0&lt;BR /&gt;Before stream, SQL: OUT&lt;BR /&gt;Before stream, Spark: OUT!&lt;BR /&gt;After stream, SQL: IN&lt;BR /&gt;After stream, Spark: IN!&lt;BR /&gt;&lt;BR /&gt;batch_id: 0&lt;BR /&gt;Before stream, SQL: IN&lt;BR /&gt;Before stream, Spark: OUT! # Key line here&lt;BR /&gt;After stream, SQL: IN&lt;BR /&gt;After stream, Spark: IN!&lt;/PRE&gt;&lt;P&gt;This means that `spark.catalog.tableExists()` fails to see the table `t` even though it clearly exists in the catalog. Could anyone clarify what is going on and whether that behavior is intended? According to Genie, that is "a bug in the Spark Connect foreachBatch implementation".&lt;/P&gt;&lt;P&gt;I am using different checkpoint paths just so that I can trigger the full stream again without changing the source table. Still, using the same path and updating the table between streaming runs does not change the behavior. Using `batch_df`'s spark session has no effect either.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2026 08:31:24 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/166874#M55624</guid>
      <dc:creator>i4ata</dc:creator>
      <dc:date>2026-08-31T08:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: Mismatch of spark.catalog.tableExists and SHOW TABLES in streams</title>
      <link>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/166924#M55627</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/249207"&gt;@i4ata&lt;/a&gt;&amp;nbsp;&lt;SPAN&gt;Its generally due to client side metadata caching within the Spark Connect architecture. Spark Connect checks a local client side cache instead of doing a fresh look up against the meta store when calling methods like spark.catalog.tableExists(). Since table creation in the foreachBatch executes on the server side directly against Unity Catalog, it did not trigger an automatic invalidation of client session's catalog cache. As a result, subsequent calls to tableExists() continue reading the stale client-side state.&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;BR /&gt;&lt;DIV&gt;Using SQL commands such as SHOW TABLES bypasses client cache sending a direct query to the server and reading the latest state from Unity Catalog in real time. Spark Connect’s decoupled client-server model is to minimize repetitive network trips for catalog operations as the general behavior&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;You can rely on server-side SQL operations such as SHOW TABLES or wrapping a DESCRIBE TABLE query in error handling to ensure using the live state of the Unity Catalog tables in jobs.&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 31 Aug 2026 14:07:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/166924#M55627</guid>
      <dc:creator>balajij8</dc:creator>
      <dc:date>2026-08-31T14:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: Mismatch of spark.catalog.tableExists and SHOW TABLES in streams</title>
      <link>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/175538#M57017</link>
      <description>&lt;P&gt;Hello &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/249207"&gt;@i4ata&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Following up on the answer above by&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/210897"&gt;@balajij8&lt;/a&gt;&amp;nbsp; with a test run of your script and one detail on where the lookup happens. Thank you for posting a complete script with its log output. I could rerun it almost as written.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Short answer:&lt;/STRONG&gt; I could not reproduce the mismatch on serverless today. I ran your pattern as serverless jobs on &lt;A href="https://docs.databricks.com/aws/en/release-notes/serverless/environment-version" target="_blank"&gt;environment versions&lt;/A&gt; 1 to 6, and from one live serverless session kept open for 30 minutes. &lt;CODE&gt;spark.catalog.tableExists()&lt;/CODE&gt; agreed with &lt;CODE&gt;SHOW TABLES&lt;/CODE&gt; before and after every write. Both checks run on the server, so if the mismatch comes back, the stale answer comes from the server session, and a new session should clear it.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LingeshK_0-1793787282778.png" style="width: 999px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/32938i720B8055ED610A84/image-size/large?v=v2&amp;amp;px=999" role="button" title="LingeshK_0-1793787282778.png" alt="LingeshK_0-1793787282778.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;How the lookup works:&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;No client-side cache:&lt;/STRONG&gt; In the Spark Connect Python client, &lt;A href="https://github.com/apache/spark/blob/v4.0.1/python/pyspark/sql/connect/catalog.py#L218-L226" target="_blank"&gt;tableExists builds a TableExists plan and sends it to the server on every call&lt;/A&gt;. The client keeps no catalog cache of its own, so it cannot hold a stale copy. This adjusts the client-side explanation above. Its advice to rely on a SQL check still holds.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;One session:&lt;/STRONG&gt; In every run, the notebook, &lt;CODE&gt;spark&lt;/CODE&gt; inside your function and &lt;CODE&gt;batch_df.sparkSession&lt;/CODE&gt; reported the same session ID. That matches what you saw when you switched to the session of &lt;CODE&gt;batch_df&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Where caching does live:&lt;/STRONG&gt; The serverless limitations page says &lt;A href="https://docs.databricks.com/aws/en/compute/serverless/limitations#caching-limitations" target="_blank"&gt;metadata is cached in serverless compute sessions&lt;/A&gt;. To clear the session context, it says to reset the serverless compute resource or start a new session.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Server upgrades:&lt;/STRONG&gt; &lt;A href="https://docs.databricks.com/aws/en/release-notes/serverless/environment-version#what-are-environment-versions" target="_blank"&gt;Environment versions give a stable client API so that Databricks can upgrade the server independently&lt;/A&gt;. A server-side fix would change the result on every environment version at once, which fits what I saw. No release note mentions this behaviour, so that part is my reading.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;STRONG&gt;Q1.&lt;/STRONG&gt; "Could anyone clarify what is going on and whether that behavior is intended?"&lt;/P&gt;
&lt;P&gt;The documented caching limitation covers switching catalogs. Here is the behaviour I see now, with run 2 checked before the write each time:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Your script as posted:&lt;/STRONG&gt; Both checks saw the table.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;A three-part name in tableExists:&lt;/STRONG&gt; Both checks saw the table.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;A name the session had never looked up, with no DROP first:&lt;/STRONG&gt; Both checks saw the table.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Your whole script three times in one session:&lt;/STRONG&gt; Both checks saw the table each time. A tableExists call from the notebook before the table existed did not change that.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;The same live session 15 and 30 minutes later:&lt;/STRONG&gt; Both checks saw the table.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;STRONG&gt;Some prerequisites to consider from my observation:&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Testing:&lt;/STRONG&gt; Serverless jobs on AWS, plus one interactive session from &lt;A href="https://docs.databricks.com/aws/en/dev-tools/databricks-connect" target="_blank"&gt;Databricks Connect&lt;/A&gt; 17.3 that stayed open for 33 minutes. I did not use the notebook UI, and a notebook can stay attached for longer than that.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;If it comes back:&lt;/STRONG&gt; Start a new session, as the limitations page describes, and keep your SQL check as the guard. Cache commands do not help on serverless. The page lists &lt;CODE&gt;spark.catalog.clearCache()&lt;/CODE&gt; and &lt;CODE&gt;CLEAR CACHE&lt;/CODE&gt; as unsupported, and &lt;CODE&gt;REFRESH TABLE&lt;/CODE&gt; returned NOT_SUPPORTED_WITH_SERVERLESS in my test.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Check before you act:&lt;/STRONG&gt; The function below logs the session ID, the current schema and both checks on one line. Call it inside the foreachBatch loop before the write, and the line shows whether the mismatch and the session line up. It uses the same calls as my test.&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;from pyspark.sql import functions as F

def debug_line(sess, name):
    in_sql = sess.sql('SHOW TABLES').filter(F.col('tableName') == name).count() != 0
    in_catalog = sess.catalog.tableExists(name)
    schema = f'{sess.catalog.currentCatalog()}.{sess.catalog.currentDatabase()}'
    return f'session={sess.session_id} schema={schema} sql={in_sql} catalog={in_catalog}'

# Inside foreach_batch, before the write:
# f.write(debug_line(batch_df.sparkSession, 't') + '\n')&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Reference documentation:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/compute/serverless/limitations" target="_blank"&gt;Serverless compute limitations&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/release-notes/serverless/environment-version" target="_blank"&gt;Serverless environment versions&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/dev-tools/databricks-connect" target="_blank"&gt;Databricks Connect&lt;/A&gt;, and the &lt;A href="https://github.com/apache/spark/blob/v4.0.1/python/pyspark/sql/connect/catalog.py" target="_blank"&gt;Spark Connect catalog client source&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Happy to rerun this with any change to the script that brings the mismatch back. Hope this helps.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2026 10:18:30 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/175538#M57017</guid>
      <dc:creator>LingeshK</dc:creator>
      <dc:date>2026-11-04T10:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Mismatch of spark.catalog.tableExists and SHOW TABLES in streams</title>
      <link>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/175557#M57020</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/249207"&gt;@i4ata&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi&amp;nbsp;&amp;nbsp;how if you try with batch specific session (just curious)&lt;/P&gt;&lt;P&gt;def foreach_batch(batch_df, batch_id):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;SPAN class=""&gt;spark_session&lt;/SPAN&gt;&lt;SPAN class=""&gt; = batch_df.sparkSession&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;SPAN class=""&gt;if&lt;/SPAN&gt;&lt;SPAN class=""&gt; spark_session.catalog.tableExists(table_name):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;br&lt;/P&gt;&lt;P&gt;Saurabh&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2026 12:29:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/mismatch-of-spark-catalog-tableexists-and-show-tables-in-streams/m-p/175557#M57020</guid>
      <dc:creator>saurabh18cs</dc:creator>
      <dc:date>2026-11-04T12:29:19Z</dc:date>
    </item>
  </channel>
</rss>

