{"record":{"id":"56372377b7e4bae2","repo":"prestodb/presto","slug":"hive-invalid-metadata-563723","errorCode":"HIVE_INVALID_METADATA","errorMessage":"Table %s.%s was dropped during insert","messagePattern":"Table (.+?)\\.(.+?) was dropped during insert","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-hive/src/main/java/com/facebook/presto/hive/HiveWriterFactory.java","lineNumber":235,"sourceCode":"            else {\n                dataColumns.add(new DataColumn(column.getName(), hiveType));\n            }\n        }\n        this.partitionColumnNames = partitionColumnNames.build();\n        this.partitionColumnTypes = partitionColumnTypes.build();\n        this.dataColumns = dataColumns.build();\n\n        Path writePath;\n        if (isCreateTable) {\n            this.table = null;\n            WriteInfo writeInfo = locationService.getQueryWriteInfo(locationHandle);\n            checkArgument(writeInfo.getWriteMode() != DIRECT_TO_TARGET_EXISTING_DIRECTORY, \"CREATE TABLE write mode cannot be DIRECT_TO_TARGET_EXISTING_DIRECTORY\");\n            writePath = writeInfo.getWritePath();\n        }\n        else {\n            Optional<Table> table = pageSinkMetadataProvider.getTable();\n            if (!table.isPresent()) {\n                throw new PrestoException(HIVE_INVALID_METADATA, format(\"Table %s.%s was dropped during insert\", schemaName, tableName));\n            }\n            this.table = table.get();\n            writePath = locationService.getQueryWriteInfo(locationHandle).getWritePath();\n        }\n\n        this.bucketCount = requireNonNull(bucketCount, \"bucketCount is null\");\n        if (bucketCount.isPresent()) {\n            checkArgument(bucketCount.getAsInt() < MAX_BUCKET_COUNT, \"bucketCount must be smaller than \" + MAX_BUCKET_COUNT);\n        }\n\n        this.session = requireNonNull(session, \"session is null\");\n        this.nodeManager = requireNonNull(nodeManager, \"nodeManager is null\");\n        this.eventClient = requireNonNull(eventClient, \"eventClient is null\");\n\n        requireNonNull(hiveSessionProperties, \"hiveSessionProperties is null\");\n        this.sessionProperties = hiveSessionProperties.getSessionProperties().stream()\n                .collect(toImmutableMap(PropertyMetadata::getName,\n                        entry -> {","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-hive/src/main/java/com/facebook/presto/hive/HiveWriterFactory.java#L217-L253","documentation":"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.","triggerScenarios":"Concurrent DROP TABLE schema.table executed after this INSERT/CTAS query started but before its writers were initialized (the !table.isPresent() branch).","commonSituations":"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.","solutions":["Re-run the INSERT after the table situation settles (recreate or restore the table if the drop was unintended)","Coordinate schedules so DDL (DROP TABLE) does not overlap running writes; use table rename instead of drop-then-recreate during maintenance","Restore the table from backup/trash if the drop was accidental, then re-run the query"],"exampleFix":"// before\n// DROP TABLE t;  (while INSERT INTO t ... is running)\n// after\nCREATE TABLE t_new LIKE t;\nINSERT INTO t_new SELECT ...;\nALTER TABLE t RENAME TO t_old;  -- swap only after writers finish\nALTER TABLE t_new RENAME TO t;","handlingStrategy":"try-catch","validationCode":"// just before INSERT: confirm table exists and lock maintenance windows\nTable t = metastore.getTable(schema, table);\nif (t == null) throw new IllegalStateException(\"Table dropped before insert: \" + schema + \".\" + table);","typeGuard":null,"tryCatchPattern":"try {\n    insertInto(schema, table);\n} catch (PrestoException e) {\n    if (e.getErrorCode() == HIVE_INVALID_METADATA.toErrorCode() && e.getMessage().contains(\"was dropped during insert\")) {\n        // recreate/restore the table and re-run the statement\n    } else throw e;\n}","preventionTips":["Never DROP tables while inserts are running; serialize DDL and DML","Use rename-based replacement patterns instead of drop-then-create","Guard maintenance jobs with locks or a metadata check against running queries"],"tags":["hive","metadata","concurrency","dropped-table"],"backgroundTag":"table-dropped-mid-operation","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}