Skip to content

Commit 6e8a586

Browse files
committed
Merge pull request #8586 from XaBbl4/gh-8139
Fix for #8139: Add lookup index name by constraint for correct resolve conflict on replica
1 parent b15c5ea commit 6e8a586

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_dbb_ss_definer, // get database sql security value
185185
irq_out_proc_param_dep, // check output procedure 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
@@ -2084,6 +2084,37 @@ void MET_load_trigger(thread_db* tdbb,
20842084

20852085

20862086

2087+
void MET_lookup_index_for_cnstrt(thread_db* tdbb,
2088+
MetaName& index_name,
2089+
const MetaName& constraint_name)
2090+
{
2091+
/**************************************
2092+
*
2093+
* M E T _ l o o k u p _ i n d e x _ f o r _ c n s t r t
2094+
*
2095+
**************************************
2096+
*
2097+
* Functional description
2098+
* Lookup index name from constraint name, if one exists.
2099+
* index_name is output parameter.
2100+
*
2101+
**************************************/
2102+
SET_TDBB(tdbb);
2103+
Attachment* attachment = tdbb->getAttachment();
2104+
2105+
index_name = "";
2106+
AutoCacheRequest request(tdbb, irq_l_index_cnstrt, IRQ_REQUESTS);
2107+
2108+
FOR(REQUEST_HANDLE request)
2109+
X IN RDB$RELATION_CONSTRAINTS WITH X.RDB$CONSTRAINT_NAME EQ constraint_name.c_str()
2110+
{
2111+
if (!X.RDB$INDEX_NAME.NULL)
2112+
index_name = X.RDB$INDEX_NAME;
2113+
}
2114+
END_FOR
2115+
}
2116+
2117+
20872118
void MET_lookup_cnstrt_for_index(thread_db* tdbb,
20882119
MetaName& constraint_name,
20892120
const MetaName& index_name)
@@ -2096,7 +2127,7 @@ void MET_lookup_cnstrt_for_index(thread_db* tdbb,
20962127
*
20972128
* Functional description
20982129
* Lookup constraint name from index name, if one exists.
2099-
* Calling routine must pass a buffer of at least 32 bytes.
2130+
* constraint_name is output parameter.
21002131
*
21012132
**************************************/
21022133
SET_TDBB(tdbb);
@@ -2127,7 +2158,7 @@ void MET_lookup_cnstrt_for_trigger(thread_db* tdbb,
21272158
*
21282159
* Functional description
21292160
* Lookup constraint name from trigger name, if one exists.
2130-
* Calling routine must pass a buffer of at least 32 bytes.
2161+
* constraint_name and relation_name are output parameters.
21312162
*
21322163
**************************************/
21332164
SET_TDBB(tdbb);
@@ -2504,7 +2535,7 @@ void MET_lookup_index(thread_db* tdbb,
25042535
*
25052536
* Functional description
25062537
* Lookup index name from relation and index number.
2507-
* Calling routine must pass a buffer of at least 32 bytes.
2538+
* index_name is output parameter.
25082539
*
25092540
**************************************/
25102541
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)