Skip to content

Commit 3b19a10

Browse files
authored
Merge pull request #8586 from XaBbl4/gh-8139
Fix for #8139: Add lookup index name by constraint for correct resolve conflict on replica
2 parents 7a71a8e + 581aae1 commit 3b19a10

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

src/jrd/irq.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ enum irq_type_t
184184
irq_proc_param_dep, // check procedure parameter dependency
185185
irq_func_param_dep, // check function parameter dependency
186186
irq_l_pub_tab_state, // lookup publication state for a table
187+
irq_l_index_cnstrt, // lookup index for constraint
187188

188189
irq_MAX
189190
};

src/jrd/met.epp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,37 @@ void MET_load_trigger(thread_db* tdbb,
20372037

20382038

20392039

2040+
void MET_lookup_index_for_cnstrt(thread_db* tdbb,
2041+
MetaName& index_name,
2042+
const MetaName& constraint_name)
2043+
{
2044+
/**************************************
2045+
*
2046+
* M E T _ l o o k u p _ i n d e x _ f o r _ c n s t r t
2047+
*
2048+
**************************************
2049+
*
2050+
* Functional description
2051+
* Lookup index name from constraint name, if one exists.
2052+
* index_name is output parameter.
2053+
*
2054+
**************************************/
2055+
SET_TDBB(tdbb);
2056+
Attachment* attachment = tdbb->getAttachment();
2057+
2058+
index_name = "";
2059+
AutoCacheRequest request(tdbb, irq_l_index_cnstrt, IRQ_REQUESTS);
2060+
2061+
FOR(REQUEST_HANDLE request)
2062+
X IN RDB$RELATION_CONSTRAINTS WITH X.RDB$CONSTRAINT_NAME EQ constraint_name.c_str()
2063+
{
2064+
if (!X.RDB$INDEX_NAME.NULL)
2065+
index_name = X.RDB$INDEX_NAME;
2066+
}
2067+
END_FOR
2068+
}
2069+
2070+
20402071
void MET_lookup_cnstrt_for_index(thread_db* tdbb,
20412072
MetaName& constraint_name,
20422073
const MetaName& index_name)
@@ -2049,7 +2080,7 @@ void MET_lookup_cnstrt_for_index(thread_db* tdbb,
20492080
*
20502081
* Functional description
20512082
* Lookup constraint name from index name, if one exists.
2052-
* Calling routine must pass a buffer of at least 32 bytes.
2083+
* constraint_name is output parameter.
20532084
*
20542085
**************************************/
20552086
SET_TDBB(tdbb);
@@ -2080,7 +2111,7 @@ void MET_lookup_cnstrt_for_trigger(thread_db* tdbb,
20802111
*
20812112
* Functional description
20822113
* Lookup constraint name from trigger name, if one exists.
2083-
* Calling routine must pass a buffer of at least 32 bytes.
2114+
* constraint_name and relation_name are output parameters.
20842115
*
20852116
**************************************/
20862117
SET_TDBB(tdbb);
@@ -2457,7 +2488,7 @@ void MET_lookup_index(thread_db* tdbb,
24572488
*
24582489
* Functional description
24592490
* Lookup index name from relation and index number.
2460-
* Calling routine must pass a buffer of at least 32 bytes.
2491+
* index_name is output parameter.
24612492
*
24622493
**************************************/
24632494
SET_TDBB(tdbb);

src/jrd/met_proto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void MET_load_db_triggers(Jrd::thread_db*, int);
9797
void MET_load_ddl_triggers(Jrd::thread_db* tdbb);
9898
bool MET_load_exception(Jrd::thread_db*, Jrd::ExceptionItem&);
9999
void MET_load_trigger(Jrd::thread_db*, Jrd::jrd_rel*, const Jrd::MetaName&, Jrd::TrigVector**);
100+
void MET_lookup_index_for_cnstrt(Jrd::thread_db*, Jrd::MetaName& index_name, const Jrd::MetaName& constraint);
100101
void MET_lookup_cnstrt_for_index(Jrd::thread_db*, Jrd::MetaName& constraint, const Jrd::MetaName& index_name);
101102
void MET_lookup_cnstrt_for_trigger(Jrd::thread_db*, Jrd::MetaName&, Jrd::MetaName&, const Jrd::MetaName&);
102103
void MET_lookup_exception(Jrd::thread_db*, SLONG, /* OUT */ Jrd::MetaName&, /* OUT */ Firebird::string*);

src/jrd/replication/Applier.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,18 @@ void Applier::insertRecord(thread_db* tdbb, TraNumber traNum,
634634
fb_assert(error[2] == isc_arg_string);
635635
fb_assert(error[3] != 0);
636636

637-
const char* idxName = reinterpret_cast<const char*>(error[3]);
637+
const MetaName nameFromErr(reinterpret_cast<const char*>(error[3]));
638+
MetaName idxName;
639+
if (error[1] == isc_no_dup)
640+
idxName = nameFromErr;
641+
else if (!m_constraintIndexMap.get(nameFromErr, idxName))
642+
{
643+
MET_lookup_index_for_cnstrt(tdbb, idxName, nameFromErr);
644+
m_constraintIndexMap.put(nameFromErr, idxName);
645+
}
638646

639647
index_desc idx;
640-
const auto indexed = lookupRecord(tdbb, relation, record, idx, idxName);
648+
const auto indexed = lookupRecord(tdbb, relation, record, idx, &idxName);
641649

642650
AutoPtr<Record> cleanup;
643651

@@ -1087,7 +1095,7 @@ bool Applier::compareKey(thread_db* tdbb, jrd_rel* relation, const index_desc& i
10871095

10881096
bool Applier::lookupRecord(thread_db* tdbb,
10891097
jrd_rel* relation, Record* record,
1090-
index_desc& idx, const char* idxName)
1098+
index_desc& idx, const MetaName* idxName)
10911099
{
10921100
RecordBitmap::reset(m_bitmap);
10931101

@@ -1099,16 +1107,16 @@ bool Applier::lookupRecord(thread_db* tdbb,
10991107
}
11001108

11011109
bool haveIdx = false;
1102-
if (idxName)
1110+
if (idxName && idxName->hasData())
11031111
{
11041112
SLONG foundRelId;
11051113
IndexStatus idxStatus;
1106-
SLONG idx_id = MET_lookup_index_name(tdbb, idxName, &foundRelId, &idxStatus);
1114+
SLONG idx_id = MET_lookup_index_name(tdbb, *idxName, &foundRelId, &idxStatus);
11071115

11081116
fb_assert(idxStatus == MET_object_active);
11091117
fb_assert(foundRelId == relation->rel_id);
11101118

1111-
haveIdx = (idxStatus == MET_object_active) && (foundRelId == relation->rel_id) &&
1119+
haveIdx = (idx_id >= 0) && (idxStatus == MET_object_active) && (foundRelId == relation->rel_id) &&
11121120
BTR_lookup(tdbb, relation, idx_id, &idx, relation->getPages(tdbb));
11131121
}
11141122

src/jrd/replication/Applier.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace Jrd
3737
{
3838
typedef Firebird::GenericMap<Firebird::Pair<Firebird::NonPooled<TraNumber, jrd_tra*> > > TransactionMap;
3939
typedef Firebird::HalfStaticArray<bid, 16> BlobList;
40+
typedef Firebird::GenericMap<MetaNamePair> ConstraintIndexMap;
4041
/*
4142
class ReplicatedTransaction : public Firebird::IReplicatedTransaction
4243
{
@@ -128,7 +129,8 @@ namespace Jrd
128129
Request* request, bool cascade)
129130
: PermanentStorage(pool),
130131
m_txnMap(pool), m_database(pool, database),
131-
m_request(request), m_enableCascade(cascade)
132+
m_request(request), m_enableCascade(cascade),
133+
m_constraintIndexMap(pool)
132134
{}
133135

134136
static Applier* create(thread_db* tdbb);
@@ -155,6 +157,7 @@ namespace Jrd
155157
Record* m_record = nullptr;
156158
JReplicator* m_interface;
157159
const bool m_enableCascade;
160+
ConstraintIndexMap m_constraintIndexMap;
158161

159162
void startTransaction(thread_db* tdbb, TraNumber traNum);
160163
void prepareTransaction(thread_db* tdbb, TraNumber traNum);
@@ -190,7 +193,7 @@ namespace Jrd
190193
const index_desc& idx,
191194
Record* record1, Record* record2);
192195
bool lookupRecord(thread_db* tdbb, jrd_rel* relation,
193-
Record* record, index_desc& idx, const char* idxName = nullptr);
196+
Record* record, index_desc& idx, const MetaName* idxName = nullptr);
194197

195198
const Format* findFormat(thread_db* tdbb, jrd_rel* relation, ULONG length);
196199

0 commit comments

Comments
 (0)