Skip to content

Query hangs when using forced PLAN that works automatically in Firebird 5 #8741

@RaulWW

Description

@RaulWW

Environment
Firebird Version: 5.0.3
Operating System: Windows 11
Architecture: x64
Page Size: 4096

Problem Description
When executing a query, Firebird 5 automatically generates and successfully uses a specific execution plan in ~1 second. However, when the exact same plan is manually specified using the PLAN clause, the query hangs indefinitely.
Expected Behavior
The manually forced plan should execute with the same performance as the automatically chosen plan.
Actual Behavior
Query hangs/freezes when using the forced plan, requiring connection termination.

Reproduction Steps

  1. Working Query (Automatic Plan)
    SELECT FIRST 100
    N.NFS,
    N.NOTA_FISCAL,
    N.CHAVE_NFE,
    N.SERIE,
    N.DT_EMISSAO,
    N.TOTAL_NOTA,
    N.CLI_CODIGO,
    N.PDV_CODIGO,
    N.PDV_NUMERO_VENDA,
    N.CP_CODIGO,
    COALESCE(N.CLI_NOME, C.CLI_NOME) as CLI_NOME,
    N.PESO_BRUTO,
    N.REP_CODIGO
    FROM NF_SAIDA N
    JOIN CLIENTES C
    ON N.EMPRESA = C.EMPRESA
    AND N.CLI_CODIGO = C.CLI_CODIGO
    WHERE N.EMPRESA = 1
    AND N.STATUS = 2
    ORDER BY N.EMPRESA, N.DT_EMISSAO DESC, N.NFS DESC, N.NOTA_FISCAL;
    Result: Executes successfully in ~1 second
    Automatic Plan Generated: PLAN SORT (HASH (N INDEX (IDX_NFS_STATUS), C INDEX (FK_CLI_FP_ENTRADA)))
  2. Hanging Query (Forced Plan)
    same query but adding:
    PLAN SORT (HASH (N INDEX (IDX_NFS_STATUS), C INDEX (FK_CLI_FP_ENTRADA)))

Result: Query hangs indefinitely
Additional Information
Tested Variations (All Hang)
PLAN SORT (N INDEX (IDX_NFS_STATUS), C INDEX (FK_CLI_FP_ENTRADA))
PLAN (N INDEX (IDX_NFS_STATUS), C INDEX (FK_CLI_FP_ENTRADA))
PLAN SORT (MERGE (N INDEX (IDX_NFS_STATUS), C INDEX (FK_CLI_FP_ENTRADA)))

Database Schema (Relevant Parts)
NF_SAIDA table structure
CREATE TABLE NF_SAIDA (
EMPRESA INTEGER,
NFS INTEGER,
STATUS INTEGER,
DT_EMISSAO DATE,
CLI_CODIGO INTEGER,
-- ... other columns
);

-- CLIENTES table structure
CREATE TABLE CLIENTES (
EMPRESA INTEGER,
CLI_CODIGO INTEGER,
CLI_NOME VARCHAR(100),
-- ... other columns
);

-- Relevant indexes
CREATE INDEX IDX_NFS_STATUS ON NF_SAIDA (EMPRESA, STATUS);
CREATE INDEX FK_CLI_FP_ENTRADA ON CLIENTES (EMPRESA, CLI_CODIGO);
Steps Taken

Recomputed all statistics using SET STATISTICS
No database structure changes made
Same database, same data, same connection
Tested with different PLAN variations
All forced plans hang, automatic plan works perfectly

Monitoring Information
When the query hangs with forced plan:

No error messages in firebird.log
Connection remains active but unresponsive
Must terminate connection to recover
No apparent memory or CPU spikes

Hypothesis
This appears to be a bug in Firebird 5 where the plan parser or executor handles manually specified plans differently than automatically generated ones, even when they appear identical.

Note: I can provide a minimal test database if needed to reproduce the issue.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions