prestodb/presto · error · PrestoException

HIVE_INVALID_METADATA

HIVE_INVALID_METADATA

Error message

Table %s.%s was dropped during insert

What it means

When the write target is an existing table (non-DIRECT_TO_TARGET_EXISTING_DIRECTORY modes), HiveWriterFactory re-reads the table metadata via pageSinkMetadataProvider at write setup. If the table no longer exists in the metastore — it was dropped between query planning and execution — Presto cannot obtain current metadata and throws HIVE_INVALID_METADATA.

Source

Thrown at presto-hive/src/main/java/com/facebook/presto/hive/HiveWriterFactory.java:235

            else {
                dataColumns.add(new DataColumn(column.getName(), hiveType));
            }
        }
        this.partitionColumnNames = partitionColumnNames.build();
        this.partitionColumnTypes = partitionColumnTypes.build();
        this.dataColumns = dataColumns.build();

        Path writePath;
        if (isCreateTable) {
            this.table = null;
            WriteInfo writeInfo = locationService.getQueryWriteInfo(locationHandle);
            checkArgument(writeInfo.getWriteMode() != DIRECT_TO_TARGET_EXISTING_DIRECTORY, "CREATE TABLE write mode cannot be DIRECT_TO_TARGET_EXISTING_DIRECTORY");
            writePath = writeInfo.getWritePath();
        }
        else {
            Optional<Table> table = pageSinkMetadataProvider.getTable();
            if (!table.isPresent()) {
                throw new PrestoException(HIVE_INVALID_METADATA, format("Table %s.%s was dropped during insert", schemaName, tableName));
            }
            this.table = table.get();
            writePath = locationService.getQueryWriteInfo(locationHandle).getWritePath();
        }

        this.bucketCount = requireNonNull(bucketCount, "bucketCount is null");
        if (bucketCount.isPresent()) {
            checkArgument(bucketCount.getAsInt() < MAX_BUCKET_COUNT, "bucketCount must be smaller than " + MAX_BUCKET_COUNT);
        }

        this.session = requireNonNull(session, "session is null");
        this.nodeManager = requireNonNull(nodeManager, "nodeManager is null");
        this.eventClient = requireNonNull(eventClient, "eventClient is null");

        requireNonNull(hiveSessionProperties, "hiveSessionProperties is null");
        this.sessionProperties = hiveSessionProperties.getSessionProperties().stream()
                .collect(toImmutableMap(PropertyMetadata::getName,
                        entry -> {

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Re-run the INSERT after the table situation settles (recreate or restore the table if the drop was unintended)
  2. Coordinate schedules so DDL (DROP TABLE) does not overlap running writes; use table rename instead of drop-then-recreate during maintenance
  3. Restore the table from backup/trash if the drop was accidental, then re-run the query

Example fix

// before
// DROP TABLE t;  (while INSERT INTO t ... is running)
// after
CREATE TABLE t_new LIKE t;
INSERT INTO t_new SELECT ...;
ALTER TABLE t RENAME TO t_old;  -- swap only after writers finish
ALTER TABLE t_new RENAME TO t;
Defensive patterns

Strategy: try-catch

Validate before calling

// just before INSERT: confirm table exists and lock maintenance windows
Table t = metastore.getTable(schema, table);
if (t == null) throw new IllegalStateException("Table dropped before insert: " + schema + "." + table);

Try / catch

try {
    insertInto(schema, table);
} catch (PrestoException e) {
    if (e.getErrorCode() == HIVE_INVALID_METADATA.toErrorCode() && e.getMessage().contains("was dropped during insert")) {
        // recreate/restore the table and re-run the statement
    } else throw e;
}

Prevention

When it happens

Trigger: Concurrent DROP TABLE schema.table executed after this INSERT/CTAS query started but before its writers were initialized (the !table.isPresent() branch).

Common situations: Long-running INSERT racing with a maintenance DROP/RENAME; orchestration jobs deleting and recreating tables while queries are in flight; cleanup scripts running against live tables.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/56372377b7e4bae2. Report an issue: GitHub.