diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 16e005844db012aeb46d53a168b725bde45284c6..cbcd571efdc54f0367aceff96dab10589b41d3e5 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -2917,7 +2917,7 @@ case OP_Savepoint: { assert(pSavepoint == psql_txn->pSavepoint); psql_txn->pSavepoint = pSavepoint->pNext; } else { - psql_txn->fk_deferred_count = + txn->fk_deferred_count = pSavepoint->tnt_savepoint->fk_deferred_count; } } @@ -4861,7 +4861,7 @@ case OP_FkCounter: { !p->auto_commit) { struct txn *txn = in_txn(); assert(txn != NULL && txn->psql_txn != NULL); - txn->psql_txn->fk_deferred_count += pOp->p2; + txn->fk_deferred_count += pOp->p2; } else { p->nFkConstraint += pOp->p2; } @@ -4885,7 +4885,7 @@ case OP_FkIfZero: { /* jump */ !p->auto_commit) { struct txn *txn = in_txn(); assert(txn != NULL && txn->psql_txn != NULL); - if (txn->psql_txn->fk_deferred_count == 0) + if (txn->fk_deferred_count == 0) goto jump_to_p2; } else { if (p->nFkConstraint == 0) diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index e7accc7452061cd11e2f94fab1b71cbf8bf6f1dd..bbbb99c7048d780faadaa36c97eb03c915821641 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -87,7 +87,6 @@ sql_alloc_txn() return NULL; } txn->pSavepoint = NULL; - txn->fk_deferred_count = 0; return txn; } @@ -1988,8 +1987,7 @@ int sqlVdbeCheckFk(Vdbe * p, int deferred) { struct txn *txn = in_txn(); - if ((deferred && txn != NULL && txn->psql_txn != NULL && - txn->psql_txn->fk_deferred_count > 0) || + if ((deferred && txn != NULL && txn->fk_deferred_count > 0) || (!deferred && p->nFkConstraint > 0)) { p->is_aborted = true; p->errorAction = ON_CONFLICT_ACTION_ABORT; diff --git a/src/box/txn.c b/src/box/txn.c index 53ebfc05354218221059c493362078bf26891732..b57240846fb3166804dce0872f7e6f62140d34a3 100644 --- a/src/box/txn.c +++ b/src/box/txn.c @@ -222,6 +222,7 @@ txn_begin() txn->engine = NULL; txn->engine_tx = NULL; txn->psql_txn = NULL; + txn->fk_deferred_count = 0; txn->fiber = NULL; fiber_set_txn(fiber(), txn); /* fiber_on_yield is initialized by engine on demand */ @@ -537,12 +538,9 @@ txn_prepare(struct txn *txn) * foreign key constraints must not be violated. * If not so, just rollback transaction. */ - if (txn->psql_txn != NULL) { - struct sql_txn *sql_txn = txn->psql_txn; - if (sql_txn->fk_deferred_count != 0) { - diag_set(ClientError, ER_FOREIGN_KEY_CONSTRAINT); - return -1; - } + if (txn->fk_deferred_count != 0) { + diag_set(ClientError, ER_FOREIGN_KEY_CONSTRAINT); + return -1; } /* * Perform transaction conflict resolution. Engine == NULL when @@ -754,8 +752,7 @@ box_txn_savepoint() } svp->stmt = stailq_last(&txn->stmts); svp->in_sub_stmt = txn->in_sub_stmt; - if (txn->psql_txn != NULL) - svp->fk_deferred_count = txn->psql_txn->fk_deferred_count; + svp->fk_deferred_count = txn->fk_deferred_count; return svp; } @@ -782,8 +779,7 @@ box_txn_rollback_to_savepoint(box_txn_savepoint_t *svp) return -1; } txn_rollback_to_svp(txn, svp->stmt); - if (txn->psql_txn != NULL) - txn->psql_txn->fk_deferred_count = svp->fk_deferred_count; + txn->fk_deferred_count = svp->fk_deferred_count; return 0; } diff --git a/src/box/txn.h b/src/box/txn.h index 37d90932b386b50ce851c84a4060024488902781..f795cb76fb9d82f6908cd566f747051fc4dc2049 100644 --- a/src/box/txn.h +++ b/src/box/txn.h @@ -135,11 +135,6 @@ extern double too_long_threshold; struct sql_txn { /** List of active SQL savepoints. */ struct Savepoint *pSavepoint; - /** - * This variables transfer deferred constraints from one - * VDBE to the next in the same transaction. - */ - uint32_t fk_deferred_count; }; /** @@ -213,6 +208,18 @@ struct txn { struct trigger fiber_on_stop; /** Commit and rollback triggers. */ struct rlist on_commit, on_rollback; + /** + * This member represents counter of deferred foreign key + * violations within transaction. DEFERRED mode means + * that until transaction is committed violations are + * allowed to appear. However, transaction can't be + * committed in presence of violations, i.e. if this + * counter is not equal to zero. In the normal mode + * violations of FK are checked at the end of each + * statement processing. Note that at the moment it is + * SQL specific property. + */ + uint32_t fk_deferred_count; struct sql_txn *psql_txn; };