Skip to content

Commit 2f8f70f

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 2f90c48 commit 2f8f70f

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
@@ -182,6 +182,7 @@ enum irq_type_t
182182
irq_dbb_ss_definer, // get database sql security value
183183
irq_out_proc_param_dep, // check output procedure parameter dependency
184184
irq_l_pub_tab_state, // lookup publication state for a table
185+
irq_l_index_cnstrt, // lookup index for constraint
185186

186187
irq_MAX
187188
};

src/jrd/met.epp

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

20802080

20812081

2082+
void MET_lookup_index_for_cnstrt(thread_db* tdbb,
2083+
MetaName& index_name,
2084+
const MetaName& constraint_name)
2085+
{
2086+
/**************************************
2087+
*
2088+
* M E T _ l o o k u p _ i n d e x _ f o r _ c n s t r t
2089+
*
2090+
**************************************
2091+
*
2092+
* Functional description
2093+
* Lookup index name from constraint name, if one exists.
2094+
* index_name is output parameter.
2095+
*
2096+
**************************************/
2097+
SET_TDBB(tdbb);
2098+
Attachment* attachment = tdbb->getAttachment();
2099+
2100+
index_name = "";
2101+
AutoCacheRequest request(tdbb, irq_l_index_cnstrt, IRQ_REQUESTS);
2102+
2103+
FOR(REQUEST_HANDLE request)
2104+
X IN RDB$RELATION_CONSTRAINTS WITH X.RDB$CONSTRAINT_NAME EQ constraint_name.c_str()
2105+
{
2106+
if (!X.RDB$INDEX_NAME.NULL)
2107+
index_name = X.RDB$INDEX_NAME;
2108+
}
2109+
END_FOR
2110+
}
2111+
2112+
20822113
void MET_lookup_cnstrt_for_index(thread_db* tdbb,
20832114
MetaName& constraint_name,
20842115
const MetaName& index_name)
@@ -2091,7 +2122,7 @@ void MET_lookup_cnstrt_for_index(thread_db* tdbb,
20912122
*
20922123
* Functional description
20932124
* Lookup constraint name from index name, if one exists.
2094-
* Calling routine must pass a buffer of at least 32 bytes.
2125+
* constraint_name is output parameter.
20952126
*
20962127
**************************************/
20972128
SET_TDBB(tdbb);
@@ -2122,7 +2153,7 @@ void MET_lookup_cnstrt_for_trigger(thread_db* tdbb,
21222153
*
21232154
* Functional description
21242155
* Lookup constraint name from trigger name, if one exists.
2125-
* Calling routine must pass a buffer of at least 32 bytes.
2156+
* constraint_name and relation_name are output parameters.
21262157
*
21272158
**************************************/
21282159
SET_TDBB(tdbb);
@@ -2499,7 +2530,7 @@ void MET_lookup_index(thread_db* tdbb,
24992530
*
25002531
* Functional description
25012532
* Lookup index name from relation and index number.
2502-
* Calling routine must pass a buffer of at least 32 bytes.
2533+
* index_name is output parameter.
25032534
*
25042535
**************************************/
25052536
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
@@ -622,10 +622,18 @@ void Applier::insertRecord(thread_db* tdbb, TraNumber traNum,
622622
fb_assert(error[2] == isc_arg_string);
623623
fb_assert(error[3] != 0);
624624

625-
const char* idxName = reinterpret_cast<const char*>(error[3]);
625+
const MetaName nameFromErr(reinterpret_cast<const char*>(error[3]));
626+
MetaName idxName;
627+
if (error[1] == isc_no_dup)
628+
idxName = nameFromErr;
629+
else if (!m_constraintIndexMap.get(nameFromErr, idxName))
630+
{
631+
MET_lookup_index_for_cnstrt(tdbb, idxName, nameFromErr);
632+
m_constraintIndexMap.put(nameFromErr, idxName);
633+
}
626634

627635
index_desc idx;
628-
const auto indexed = lookupRecord(tdbb, relation, record, m_bitmap, idx, idxName);
636+
const auto indexed = lookupRecord(tdbb, relation, record, m_bitmap, idx, &idxName);
629637

630638
AutoPtr<Record> cleanup;
631639

@@ -1071,7 +1079,7 @@ bool Applier::compareKey(thread_db* tdbb, jrd_rel* relation, const index_desc& i
10711079
bool Applier::lookupRecord(thread_db* tdbb,
10721080
jrd_rel* relation, Record* record,
10731081
RecordBitmap* bitmap,
1074-
index_desc& idx, const char* idxName)
1082+
index_desc& idx, const MetaName* idxName)
10751083
{
10761084
RecordBitmap::reset(bitmap);
10771085

@@ -1083,16 +1091,16 @@ bool Applier::lookupRecord(thread_db* tdbb,
10831091
}
10841092

10851093
bool haveIdx = false;
1086-
if (idxName)
1094+
if (idxName && idxName->hasData())
10871095
{
10881096
SLONG foundRelId;
10891097
IndexStatus idxStatus;
1090-
SLONG idx_id = MET_lookup_index_name(tdbb, idxName, &foundRelId, &idxStatus);
1098+
SLONG idx_id = MET_lookup_index_name(tdbb, *idxName, &foundRelId, &idxStatus);
10911099

10921100
fb_assert(idxStatus == MET_object_active);
10931101
fb_assert(foundRelId == relation->rel_id);
10941102

1095-
haveIdx = (idxStatus == MET_object_active) && (foundRelId == relation->rel_id) &&
1103+
haveIdx = (idx_id >= 0) && (idxStatus == MET_object_active) && (foundRelId == relation->rel_id) &&
10961104
BTR_lookup(tdbb, relation, idx_id, &idx, relation->getPages(tdbb));
10971105
}
10981106

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
Jrd::jrd_req* request)
129130
: PermanentStorage(pool),
130131
m_txnMap(pool), m_database(pool, database),
131-
m_request(request), m_bitmap(FB_NEW_POOL(pool) RecordBitmap(pool)), m_record(NULL)
132+
m_request(request), m_bitmap(FB_NEW_POOL(pool) RecordBitmap(pool)), m_record(NULL),
133+
m_constraintIndexMap(pool)
132134
{}
133135

134136
static Applier* create(thread_db* tdbb);
@@ -154,6 +156,7 @@ namespace Jrd
154156
Firebird::AutoPtr<RecordBitmap> m_bitmap;
155157
Record* m_record;
156158
JReplicator* m_interface;
159+
ConstraintIndexMap m_constraintIndexMap;
157160

158161
void startTransaction(thread_db* tdbb, TraNumber traNum);
159162
void prepareTransaction(thread_db* tdbb, TraNumber traNum);
@@ -190,7 +193,7 @@ namespace Jrd
190193
Record* record1, Record* record2);
191194
bool lookupRecord(thread_db* tdbb, jrd_rel* relation,
192195
Record* record, RecordBitmap* bitmap,
193-
index_desc& idx, const char* idxName = nullptr);
196+
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)