Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table MON$COMPILED_STATEMENTS and columns #7050

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/dsql/dsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2197,7 +2197,9 @@ static void sql_info(thread_db* tdbb,
{
const bool detailed = (item == isc_info_sql_explain_plan);
string plan = tdbb->getAttachment()->stringToUserCharSet(tdbb,
OPT_get_plan(tdbb, request->req_request, detailed));
OPT_get_plan(tdbb,
(request->req_request ? request->req_request->getStatement() : nullptr),
detailed));

if (plan.hasData())
{
Expand Down
1 change: 1 addition & 0 deletions src/jrd/Attachment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Jrd::Attachment::Attachment(MemoryPool* pool, Database* dbb, JProvider* provider
att_ss_user(NULL),
att_user_ids(*pool),
att_active_snapshots(*pool),
att_statements(*pool),
att_requests(*pool),
att_lock_owner_id(Database::getLockOwnerId()),
att_backup_state_counter(0),
Expand Down
1 change: 1 addition & 0 deletions src/jrd/Attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ class Attachment : public pool_alloc<type_att>
StableAttachmentPart* att_stable;

public:
Firebird::SortedArray<JrdStatement*> att_statements; // Statements belonging to attachment
Firebird::SortedArray<jrd_req*> att_requests; // Requests belonging to attachment
Lock* att_id_lock; // Attachment lock (if any)
AttNumber att_attachment_id; // Attachment ID
Expand Down
6 changes: 6 additions & 0 deletions src/jrd/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "../jrd/RandomGenerator.h"
#include "../common/os/guid.h"
#include "../common/os/os_utils.h"
#include "../jrd/ods.h"
#include "../jrd/sbm.h"
#include "../jrd/flu.h"
#include "../jrd/RuntimeStatistics.h"
Expand Down Expand Up @@ -582,6 +583,11 @@ class Database : public pool_alloc<type_dbb>
return (dbb_replica_mode == mode);
}

USHORT getEncodedOdsVersion() const
{
return ENCODE_ODS(dbb_ods_version, dbb_minor_version);
}

private:
Database(MemoryPool* p, Firebird::IPluginConfig* pConf, bool shared)
: dbb_permanent(p),
Expand Down
16 changes: 11 additions & 5 deletions src/jrd/JrdStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ JrdStatement* JrdStatement::makeStatement(thread_db* tdbb, CompilerScratch* csb,
if (internalFlag)
statement->flags |= FLAG_INTERNAL;

tdbb->getAttachment()->att_statements.add(statement);

return statement;
}

Expand Down Expand Up @@ -407,7 +409,6 @@ jrd_req* JrdStatement::getRequest(thread_db* tdbb, USHORT level)

// Create the request.
jrd_req* const request = FB_NEW_POOL(*pool) jrd_req(attachment, this, parentStats);
request->setRequestId(dbb->generateStatementId());

requests[level] = request;

Expand Down Expand Up @@ -645,14 +646,19 @@ void JrdStatement::release(thread_db* tdbb)
for (jrd_req** instance = requests.begin(); instance != requests.end(); ++instance)
EXE_release(tdbb, *instance);

const auto attachment = tdbb->getAttachment();

FB_SIZE_T pos;
if (attachment->att_statements.find(this, pos))
attachment->att_statements.remove(pos);
else
fb_assert(false);

sqlText = NULL;

// Sub statement pool is the same of the main statement, so don't delete it.
if (!parentStatement)
{
Jrd::Attachment* const att = tdbb->getAttachment();
att->deletePool(pool);
}
attachment->deletePool(pool);
}

// Check that we have enough rights to access all resources this list of triggers touches.
Expand Down
8 changes: 8 additions & 0 deletions src/jrd/JrdStatement.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class JrdStatement : public pool_alloc<type_req>
static JrdStatement* makeStatement(thread_db* tdbb, CompilerScratch* csb, bool internalFlag);
static jrd_req* makeRequest(thread_db* tdbb, CompilerScratch* csb, bool internalFlag);

StmtNumber getStatementId() const
{
if (!id)
id = JRD_get_thread_data()->getDatabase()->generateStatementId();
return id;
}

const Routine* getRoutine() const;
bool isActive() const;

Expand All @@ -68,6 +75,7 @@ class JrdStatement : public pool_alloc<type_req>
unsigned flags; // statement flags
unsigned blrVersion;
ULONG impureSize; // Size of impure area
mutable StmtNumber id; // statement identifier
Firebird::Array<record_param> rpbsSetup;
Firebird::Array<jrd_req*> requests; // vector of requests
ExternalAccessList externalList; // Access to procedures/triggers to be checked
Expand Down
80 changes: 72 additions & 8 deletions src/jrd/Monitoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ MonitoringSnapshot::MonitoringSnapshot(thread_db* tdbb, MemoryPool& pool)
RecordBuffer* const dbb_buffer = allocBuffer(tdbb, pool, rel_mon_database);
RecordBuffer* const att_buffer = allocBuffer(tdbb, pool, rel_mon_attachments);
RecordBuffer* const tra_buffer = allocBuffer(tdbb, pool, rel_mon_transactions);
RecordBuffer* const cmp_stmt_buffer = dbb->getEncodedOdsVersion() >= ODS_13_1 ?
allocBuffer(tdbb, pool, rel_mon_compiled_statements) :
nullptr;
RecordBuffer* const stmt_buffer = allocBuffer(tdbb, pool, rel_mon_statements);
RecordBuffer* const call_buffer = allocBuffer(tdbb, pool, rel_mon_calls);
RecordBuffer* const io_stat_buffer = allocBuffer(tdbb, pool, rel_mon_io_stats);
Expand Down Expand Up @@ -561,6 +564,9 @@ MonitoringSnapshot::MonitoringSnapshot(thread_db* tdbb, MemoryPool& pool)
case rel_mon_transactions:
buffer = tra_buffer;
break;
case rel_mon_compiled_statements:
buffer = cmp_stmt_buffer;
break;
case rel_mon_statements:
buffer = stmt_buffer;
break;
Expand Down Expand Up @@ -1039,7 +1045,7 @@ void Monitoring::putAttachment(SnapshotData::DumpRecord& record, const Jrd::Atta
// statement timeout, milliseconds
record.storeInteger(f_mon_att_stmt_timeout, attachment->getStatementTimeout());

if (ENCODE_ODS(dbb->dbb_ods_version, dbb->dbb_minor_version) >= ODS_13_1)
if (dbb->getEncodedOdsVersion() >= ODS_13_1)
{
char timeZoneBuffer[TimeZoneUtil::MAX_SIZE];
TimeZoneUtil::format(timeZoneBuffer, sizeof(timeZoneBuffer), attachment->att_current_timezone);
Expand Down Expand Up @@ -1134,11 +1140,49 @@ void Monitoring::putTransaction(SnapshotData::DumpRecord& record, const jrd_tra*
}


void Monitoring::putStatement(SnapshotData::DumpRecord& record, const JrdStatement* statement, const string& plan)
{
fb_assert(statement);

record.reset(rel_mon_compiled_statements);

// compiled statement id
record.storeInteger(f_mon_cmp_stmt_id, statement->getStatementId());

// sql text
if (statement->sqlText)
record.storeString(f_mon_cmp_stmt_sql_text, *statement->sqlText);

// explained plan
if (plan.hasData())
record.storeString(f_mon_cmp_stmt_expl_plan, plan);

// object name/type
if (const auto routine = statement->getRoutine())
{
if (routine->getName().package.hasData())
record.storeString(f_mon_cmp_stmt_pkg_name, routine->getName().package);

record.storeString(f_mon_cmp_stmt_name, routine->getName().identifier);
record.storeInteger(f_mon_cmp_stmt_type, routine->getObjectType());
}
else if (!statement->triggerName.isEmpty())
{
record.storeString(f_mon_cmp_stmt_name, statement->triggerName);
record.storeInteger(f_mon_cmp_stmt_type, obj_trigger);
}

record.write();
}


void Monitoring::putRequest(SnapshotData::DumpRecord& record, const jrd_req* request,
const string& plan)
{
fb_assert(request);

const auto dbb = request->req_attachment->att_database;

record.reset(rel_mon_statements);

// request id
Expand Down Expand Up @@ -1181,6 +1225,10 @@ void Monitoring::putRequest(SnapshotData::DumpRecord& record, const jrd_req* req

// statement timeout, milliseconds
record.storeInteger(f_mon_stmt_timeout, request->req_timeout);

if (dbb->getEncodedOdsVersion() >= ODS_13_1)
record.storeInteger(f_mon_stmt_cmp_stmt_id, statement->getStatementId());

record.write();

putStatistics(record, request->req_stats, stat_id, stat_statement);
Expand All @@ -1192,7 +1240,9 @@ void Monitoring::putCall(SnapshotData::DumpRecord& record, const jrd_req* reques
{
fb_assert(request);

const auto dbb = request->req_attachment->att_database;
const jrd_req* initialRequest = request->req_caller;

while (initialRequest->req_caller)
{
initialRequest = initialRequest->req_caller;
Expand Down Expand Up @@ -1241,6 +1291,9 @@ void Monitoring::putCall(SnapshotData::DumpRecord& record, const jrd_req* reques
record.storeInteger(f_mon_call_src_column, request->req_src_column);
}

if (dbb->getEncodedOdsVersion() >= ODS_13_1)
record.storeInteger(f_mon_call_cmp_stmt_id, statement->getStatementId());

// statistics
const int stat_id = fb_utils::genUniqueId();
record.storeGlobalId(f_mon_call_stat_id, getGlobalId(stat_id));
Expand Down Expand Up @@ -1432,18 +1485,29 @@ void Monitoring::dumpAttachment(thread_db* tdbb, Attachment* attachment)
}
}

if (dbb->getEncodedOdsVersion() >= ODS_13_1)
{
// Statement information

for (const auto statement : attachment->att_statements)
{
if (!(statement->flags & (JrdStatement::FLAG_INTERNAL | JrdStatement::FLAG_SYS_TRIGGER)))
{
const string plan = OPT_get_plan(tdbb, statement, true);
putStatement(record, statement, plan);
}
}
}

// Request information

for (const jrd_req* const* i = attachment->att_requests.begin();
i != attachment->att_requests.end();
++i)
for (const auto request : attachment->att_requests)
{
const jrd_req* const request = *i;
const auto statement = request->getStatement();

if (!(request->getStatement()->flags &
(JrdStatement::FLAG_INTERNAL | JrdStatement::FLAG_SYS_TRIGGER)))
if (!(statement->flags & (JrdStatement::FLAG_INTERNAL | JrdStatement::FLAG_SYS_TRIGGER)))
{
const string plan = OPT_get_plan(tdbb, request, true);
const string plan = OPT_get_plan(tdbb, statement, true);
putRequest(record, request, plan);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/jrd/Monitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ class Monitoring

static void putAttachment(SnapshotData::DumpRecord&, const Attachment*);
static void putTransaction(SnapshotData::DumpRecord&, const jrd_tra*);
static void putStatement(SnapshotData::DumpRecord&, const JrdStatement*, const Firebird::string&);
static void putRequest(SnapshotData::DumpRecord&, const jrd_req*, const Firebird::string&);
static void putCall(SnapshotData::DumpRecord&, const jrd_req*);
static void putStatistics(SnapshotData::DumpRecord&, const RuntimeStatistics&, int, int);
Expand Down
9 changes: 6 additions & 3 deletions src/jrd/dfw.epp
Original file line number Diff line number Diff line change
Expand Up @@ -2810,6 +2810,10 @@ static bool create_expression_index(thread_db* tdbb, SSHORT phase, DeferredWork*
{
tdbb->setTransaction(current_transaction);
tdbb->setRequest(current_request);

// Get rid of the expression statement.
idx.idx_expression_statement->release(tdbb);

throw;
}

Expand All @@ -2818,9 +2822,8 @@ static bool create_expression_index(thread_db* tdbb, SSHORT phase, DeferredWork*

DFW_update_index(work->dfw_name.c_str(), idx.idx_id, selectivity, transaction);

// Get rid of the pool containing the expression tree

attachment->deletePool(new_pool);
// Get rid of the expression statement.
idx.idx_expression_statement->release(tdbb);
}
break;

Expand Down
9 changes: 8 additions & 1 deletion src/jrd/exe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,14 @@ void EXE_start(thread_db* tdbb, jrd_req* request, jrd_tra* transaction)
if (transaction->tra_flags & TRA_prepared)
ERR_post(Arg::Gds(isc_req_no_trans));

JrdStatement* statement = request->getStatement();
const auto dbb = tdbb->getDatabase();
const auto statement = request->getStatement();

// Generate request id.
request->setRequestId(
request->isRequestIdUnassigned() && request->isRoot() ?
statement->getStatementId() :
dbb->generateStatementId());

/* Post resources to transaction block. In particular, the interest locks
on relations/indices are copied to the transaction, which is very
Expand Down
3 changes: 3 additions & 0 deletions src/jrd/names.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,6 @@ NAME("MON$SESSION_TIMEZONE", nam_mon_session_tz)
NAME("RDB$KEYWORDS", nam_keywords)
NAME("RDB$KEYWORD_NAME", nam_keyword_name)
NAME("RDB$KEYWORD_RESERVED", nam_keyword_reserved)

NAME("MON$COMPILED_STATEMENTS", nam_mon_compiled_statements)
NAME("MON$COMPILED_STATEMENT_ID", nam_mon_cmp_stmt_id)
6 changes: 3 additions & 3 deletions src/jrd/opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ static const UCHAR sort_dtypes[] =
};


string OPT_get_plan(thread_db* tdbb, const jrd_req* request, bool detailed)
string OPT_get_plan(thread_db* tdbb, const JrdStatement* statement, bool detailed)
{
/**************************************
*
Expand All @@ -455,9 +455,9 @@ string OPT_get_plan(thread_db* tdbb, const jrd_req* request, bool detailed)
**************************************/
string plan;

if (request)
if (statement)
{
const Array<const RecordSource*>& fors = request->getStatement()->fors;
const Array<const RecordSource*>& fors = statement->fors;

for (FB_SIZE_T i = 0; i < fors.getCount(); i++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/jrd/opt_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Jrd {
class MapNode;
}

Firebird::string OPT_get_plan(Jrd::thread_db* tdbb, const Jrd::jrd_req* request, bool detailed);
Firebird::string OPT_get_plan(Jrd::thread_db* tdbb, const Jrd::JrdStatement* statement, bool detailed);
Jrd::RecordSource* OPT_compile(Jrd::thread_db* tdbb, Jrd::CompilerScratch* csb,
Jrd::RseNode* rse, Jrd::BoolExprNodeStack* parent_stack);
void OPT_compile_relation(Jrd::thread_db* tdbb, Jrd::jrd_rel* relation, Jrd::CompilerScratch* csb,
Expand Down
12 changes: 12 additions & 0 deletions src/jrd/relations.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ RELATION(nam_mon_statements, rel_mon_statements, ODS_11_1, rel_virtual)
FIELD(f_mon_stmt_expl_plan, nam_mon_expl_plan, fld_source, 0, ODS_11_1)
FIELD(f_mon_stmt_timeout, nam_stmt_timeout, fld_stmt_timeout, 0, ODS_13_0)
FIELD(f_mon_stmt_timer, nam_stmt_timer, fld_stmt_timer, 0, ODS_13_0)
FIELD(f_mon_stmt_cmp_stmt_id, nam_mon_cmp_stmt_id, fld_stmt_id, 0, ODS_13_1)
END_RELATION

// Relation 37 (MON$CALL_STACK)
Expand All @@ -575,6 +576,7 @@ RELATION(nam_mon_calls, rel_mon_calls, ODS_11_1, rel_virtual)
FIELD(f_mon_call_src_column, nam_mon_src_column, fld_src_info, 0, ODS_11_1)
FIELD(f_mon_call_stat_id, nam_mon_stat_id, fld_stat_id, 0, ODS_11_1)
FIELD(f_mon_call_pkg_name, nam_mon_pkg_name, fld_pkg_name, 0, ODS_12_0)
FIELD(f_mon_call_cmp_stmt_id, nam_mon_cmp_stmt_id, fld_stmt_id, 0, ODS_13_1)
END_RELATION

// Relation 38 (MON$IO_STATS)
Expand Down Expand Up @@ -741,3 +743,13 @@ RELATION(nam_keywords, rel_keywords, ODS_13_1, rel_virtual)
FIELD(f_keyword_name, nam_keyword_name, fld_keyword_name, 0, ODS_13_1)
FIELD(f_keyword_reserved, nam_keyword_reserved, fld_keyword_reserved, 0, ODS_13_1)
END_RELATION

// Relation 55 (MON$COMPILED_STATEMENTS)
RELATION(nam_mon_compiled_statements, rel_mon_compiled_statements, ODS_13_1, rel_virtual)
FIELD(f_mon_cmp_stmt_id, nam_mon_cmp_stmt_id, fld_stmt_id, 0, ODS_13_1)
FIELD(f_mon_cmp_stmt_sql_text, nam_mon_sql_text, fld_source, 0, ODS_13_1)
FIELD(f_mon_cmp_stmt_expl_plan, nam_mon_expl_plan, fld_source, 0, ODS_13_1)
FIELD(f_mon_cmp_stmt_name, nam_mon_obj_name, fld_gnr_name, 0, ODS_13_1)
FIELD(f_mon_cmp_stmt_type, nam_mon_obj_type, fld_obj_type, 0, ODS_13_1)
FIELD(f_mon_cmp_stmt_pkg_name, nam_mon_pkg_name, fld_pkg_name, 0, ODS_13_1)
END_RELATION
17 changes: 16 additions & 1 deletion src/jrd/req.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,25 @@ class jrd_req : public pool_alloc<type_req>
CS_METADATA : req_attachment->att_charset;
}

bool isRoot() const
{
return statement->requests.hasData() && this == statement->requests[0];
}

bool isRequestIdUnassigned() const
{
return req_id == 0;
}

StmtNumber getRequestId() const
{
if (!req_id)
req_id = JRD_get_thread_data()->getDatabase()->generateStatementId();
{
req_id = isRoot() ?
statement->getStatementId() :
JRD_get_thread_data()->getDatabase()->generateStatementId();
}

return req_id;
}

Expand Down
Loading