Skip to content

Speed up building temp tables for large IN clauses - #8382

Merged
labkey-jeckels merged 2 commits into
release26.11-SNAPSHOTfrom
26.11_fb_temp_table_unnest
Nov 26, 2026
Merged

labkey-jeckels merged 2 commits into
release26.11-SNAPSHOTfrom
26.11_fb_temp_table_unnest

Conversation

@labkey-jeckels

@labkey-jeckels labkey-jeckels commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Rationale

Building the temp table behind a large IN clause took 1–2 s for a 100k-value string list on a busy RDS server, and the table was rebuilt every 5 minutes even while clients kept sending the same list. Locally, the full build for 100k barcodes drops from about 740 ms to about 190 ms.

Changes

  • On PostgreSQL, fill the table with one array-bound unnest() insert instead of batches of single-row inserts
  • Create the table UNLOGGED when it's in the LabKey database, and include the UNLOGGED reset generation in the cache key so a failover or crash recovery can't return an emptied table
  • Make the index unique so the planner can skip de-duplicating the values
  • Expire cached tables 5 minutes after their last use instead of 5 minutes after creation, via a new time-to-idle option on CacheManager
  • Scope the cache key by data source and schema so temp schemas in different databases never share a table
  • Tests for typed lists containing nulls, UNLOGGED creation, and rebuild after a reset

Tasks

  • Claude Code Review
  • Manual Testing - not needed
  • Test Automation

On PostgreSQL, build them UNLOGGED with a single unnest() insert and a unique index, and keep each cached until 5 minutes after its last use.
Block readers during the UNLOGGED reset check, limit the UNIQUE index to PostgreSQL, cap cached tables at one hour, and length-prefix cache key values so different lists can't collide.
@labkey-jeckels labkey-jeckels self-assigned this Nov 24, 2026
if (HeartBeat.currentTimeMillis() - _lastCheck > CHECK_INTERVAL)
{
// Block rather than skip: a reader returning the old generation mid-check could reuse an emptied table
_checkLock.lock();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a more conservative approach to avoid reading a truncated table after a DB switch. I'm lukewarm on how much to worry about this failover scenario.

@labkey-jeckels
labkey-jeckels marked this pull request as ready for review November 24, 2026 01:26
}

@Test
public void testUnlogged()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test for testNotUnlogged?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing an easy way to do that, as it would require an external data source to be configured. The UNLOGGED difference is small in terms of codepath differences, so I think it's low risk.

@labkey-nicka labkey-nicka added this to the 26.11 milestone Nov 25, 2026
@labkey-nicka
labkey-nicka removed their request for review November 25, 2026 23:14
return null;

String cacheKey = getCacheKey(jdbcType, sortedParameters);
DbSchema tempSchema = _tempSchemaSupplier.get();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up, this may conflict with similar changes in #8381 which is targeting develop.

@labkey-jeckels
labkey-jeckels merged commit 4c4156c into release26.11-SNAPSHOT Nov 26, 2026
13 checks passed
@labkey-jeckels
labkey-jeckels deleted the 26.11_fb_temp_table_unnest branch November 26, 2026 00:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants