* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
*
* openGauss is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*
* jit_exec.h
* Interface for jit execution.
*
* IDENTIFICATION
* src/include/storage/mot/jit_exec.h
*
* -------------------------------------------------------------------------
*/
#ifndef JIT_EXEC_H
#define JIT_EXEC_H
#include "postgres.h"
#include "nodes/params.h"
#include "executor/tuptable.h"
#include "nodes/parsenodes.h"
#include "nodes/execnodes.h"
#include "pgstat.h"
#include "jit_def.h"
namespace JitExec {
struct MotJitContext;
struct JitContextPool;
struct JitPlan;
extern bool JitInitialize();
extern void JitDestroy();
extern bool IsMotCodegenEnabled();
extern bool IsMotQueryCodegenEnabled();
extern bool IsMotSPCodegenEnabled();
extern bool IsMotCodegenPrintEnabled();
extern uint32_t GetMotCodegenLimit();
* @brief Queries whether a SQL query to be executed by MM Engine is jittable.
* @param query The parsed SQL query to examine.
* @param queryString The query text.
* @param forcePlan[opt] Specifies whether to force plan generation, even if a JIT source exists.
* @return The JIT plan if the query is jittable, otherwise null.
*/
extern JitPlan* IsJittableQuery(Query* query, const char* queryString, bool forcePlan = false);
* @brief Queries whether a stored procedure to be executed by MM Engine is jittable.
* @param procTuple The stored procedure entry in the system catalog.
* @param functionOid The function identifier.
* @param forcePlan[opt] Specifies whether to force plan generation, even if a JIT source exists.
* @return The JIT plan if the stored procedure is jittable, otherwise null.
*/
extern JitPlan* IsJittableFunction(
PLpgSQL_function* function, HeapTuple procTuple, Oid functionOid, bool forcePlan = false);
extern bool IsInvokeReadyFunction(Query* query);
* @brief Generate jitted code for a query.
* @param query The parsed SQL query for which jitted code is to be generated.
* @param queryString The query text.
* @param jitPlan The JIT plan produced during the call to @ref IsJittable().
* @param The required resulting context usage.
* @return The context of the jitted code required for later execution.
*/
extern MotJitContext* JitCodegenQuery(Query* query, const char* queryString, JitPlan* jitPlan, JitContextUsage usage);
* @brief Utility helper for generating jitted code for a query. It packs together the calls for @ref IsJittableQuery
* and @ref JitCodegenQuery.
* @param query The parsed SQL query for which jitted code is to be generated.
* @param queryString The query text.
* @return The context of the jitted code required for later execution.
*/
extern MotJitContext* TryJitCodegenQuery(Query* query, const char* queryString);
* @brief Generate jitted code for a stored procedure.
* @param function The parsed stored procedure.
* @param procTuple The stored procedure entry in the system catalog.
* @param functionOid The function identifier.
* @param returnSetInfo Return set information for the function (required during parsing).
* @param jitPlan The JIT plan produced during the call to @ref IsJittable().
* @return The context of the jitted code required for later execution.
*/
extern MotJitContext* JitCodegenFunction(PLpgSQL_function* function, HeapTuple procTuple, Oid functionOid,
ReturnSetInfo* returnSetInfo, JitPlan* jitPlan, JitContextUsage usage);
extern void JitResetScan(MotJitContext* jitContext);
* @brief Executed a previously jitted query.
* @param jitContext The context produced by a previous call to @ref JitCodegenQuery().
* @param params The list of bound parameters passed to the query.
* @param[out] slot The slot used for reporting select result.
* @param[out] tuplesProcessed The variable used to report the number of processed tuples.
* @param[out] scanEnded The variable used to report if a range scan ended.
* @return Zero if succeeded, otherwise an error code.
* @note This function may cause transaction abort.
*/
extern int JitExecQuery(
MotJitContext* jitContext, ParamListInfo params, TupleTableSlot* slot, uint64_t* tuplesProcessed, int* scanEnded);
* @brief Executed a previously jitted stored procedure.
* @param jitContext The context produced by a previous call to @ref JitCodegenFunction().
* @param params The list of bound parameters passed to the query.
* @param[out] slot The slot used for reporting select result.
* @param[out] tuplesProcessed The variable used to report the number of processed tuples.
* @param[out] scanEnded The variable used to report if a range scan ended.
* @return Zero if succeeded, otherwise an error code.
* @note This function may cause transaction abort.
*/
extern int JitExecFunction(
MotJitContext* jitContext, ParamListInfo params, TupleTableSlot* slot, uint64_t* tuplesProcessed, int* scanEnded);
* @brief Purges the global cache of JIT source stencils from all entries that refer the given relation or stored
* procedure id.
* @param objectId The external identifier of the relation or stored procedure that triggers the purge.
* @param purgeScope The directly affected JIT source objects. In case of JIT query source, then the object identifier
* parameter denotes a relation id, otherwise it denotes a stored procedure id.
* @param purgeAction Specifies whether to just purge all keys/indexes referring to the given relation, or should the
* JIT context also be set as expired (which triggers re-compilation of the JIT function on sub-sequent access).
* @param funcName The stored procedure name (applicable only if purgeScope is JIT_PURGE_SCOPE_SP).
*/
extern void PurgeJitSourceCache(
uint64_t objectId, JitPurgeScope purgeScope, JitPurgeAction purgeAction, const char* funcName);
* @brief Re-Generate JIT code for all invalidated sub-queries of a stored procedure.
* @param jitContext The context produced by a previous call to @ref JitCodegenFunction().
* @return True if operations succeeded, otherwise false.
*/
extern bool JitReCodegenFunctionQueries(MotJitContext* jitContext);
extern void ForceJitContextInvalidation(MotJitContext* jitContext);
extern bool IsJitContextValid(MotJitContext* jitContext);
extern bool IsJitSubContext(MotJitContext* jitContext);
extern bool TryRevalidateJitContext(MotJitContext* jitContext, TransactionId functionTxnId = InvalidTransactionId);
extern bool IsJitContextPendingCompile(MotJitContext* jitContext);
extern bool IsJitContextDoneCompile(MotJitContext* jitContext);
extern bool IsJitContextErrorCompile(MotJitContext* jitContext);
extern bool GetJitContextCompileState(MotJitContext* jitContext, bool* isPending, bool* isDone, bool* isError);
extern JitContextState GetJitContextState(MotJitContext* jitContext);
* @brief Returns a JIT context back to its source pool.
* @param jitContext The JIT context to free.
*/
extern void FreeJitContext(MotJitContext* jitContext);
* @brief Destroys a JIT context produced by a previous call to JitCodegenQuery.
* @detail All internal resources associated with the context object are released, and the context
* is returned to its source context pool.
* @param jitContext The JIT context to destroy.
* @param[optional] isDropCachedPlan Specifies whether this is from DropCachedPlan (deallocate prepared statement).
*/
extern void DestroyJitContext(MotJitContext* jitContext, bool isDropCachedPlan = false);
extern void FreeSessionJitContextPool(JitContextPool* jitContextPool);
* @brief Releases all resources associated with a plan.
* @param plan The plan to destroy.
*/
extern void JitDestroyPlan(JitPlan* plan);
extern MotJitDetail* MOTGetJitDetail(uint32_t* num);
extern MotJitProfile* MOTGetJitProfile(uint32_t* num);
extern void JitReportParseError(ErrorData* edata, const char* queryString);
extern void CleanupJitSourceTxnState();
extern bool IsInvokeQueryPlan(CachedPlanSource* planSource, Oid* functionOid, TransactionId* functionTxnId);
}
#endif