#include "dbms_pipe.hpp" #include "dbms_pipe_buffer.hpp" #include "dbms_pipe_queue.hpp" #include "dbms_pipe_registry.hpp" extern "C" { #include "catalog/objectaccess.h" #include "catalog/pg_proc.h" #include "catalog/pg_type_d.h" #include "miscadmin.h" #include "parser/parse_func.h" #include "utils/acl.h" #include "utils/builtins.h" #include "utils/date.h" #include "utils/elog.h" #include "utils/lsyscache.h" #include "utils/timestamp.h" #include "utils/varlena.h" } extern "C" { PG_FUNCTION_INFO_V1(dbms_pipe_maxwait); PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_varchar2); PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_number); PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_date); PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_timestamp); PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_raw); PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_rowid); PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_varchar2); PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_number); PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_date); PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_raw); PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_rowid); PG_FUNCTION_INFO_V1(dbms_pipe_next_item_type); PG_FUNCTION_INFO_V1(dbms_pipe_reset_buffer); PG_FUNCTION_INFO_V1(dbms_pipe_create_pipe); PG_FUNCTION_INFO_V1(dbms_pipe_remove_pipe); PG_FUNCTION_INFO_V1(dbms_pipe_send_message_ovr1); PG_FUNCTION_INFO_V1(dbms_pipe_send_message_ovr2); PG_FUNCTION_INFO_V1(dbms_pipe_send_message_ovr3); PG_FUNCTION_INFO_V1(dbms_pipe_receive_message_ovr1); PG_FUNCTION_INFO_V1(dbms_pipe_receive_message_ovr2); PG_FUNCTION_INFO_V1(dbms_pipe_receive_message_ovr3); PG_FUNCTION_INFO_V1(dbms_pipe_set_credential_name); PG_FUNCTION_INFO_V1(dbms_pipe_set_location_uri); PG_FUNCTION_INFO_V1(dbms_pipe_get_credential_name); PG_FUNCTION_INFO_V1(dbms_pipe_get_location_uri); PG_FUNCTION_INFO_V1(dbms_pipe_purge_ovr1); PG_FUNCTION_INFO_V1(dbms_pipe_purge_ovr2); PG_FUNCTION_INFO_V1(dbms_pipe_purge_ovr3); PG_FUNCTION_INFO_V1(dbms_pipe_unique_session_name); } static void dbms_pipe_report_not_supported(const char *name) { ereport(NOTICE, (errmsg("Функция DBMS_PIPE.%s не поддерживается.", name))); } static void dbms_pipe_raise_invalid_null_parameter() { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ORA-23319: parameter value null is not appropriate"))); } static void dbms_pipe_validate_nonnegative(int32 value) { if (value < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("ORA-23319: parameter value %d is not appropriate", value))); } static void dbms_pipe_raise_privilege_error() { ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), errmsg("ORA-23322: Privilege error accessing pipe"))); } static int32 dbms_pipe_invoke_cache_function(const text *cache_func, const text *pipename) { Oid argument_types[1] = {VARCHAR2OID}; List *const function_name = textToQualifiedNameList(const_cast(cache_func)); const Oid function_oid = LookupFuncName(function_name, 1, argument_types, false); if (get_func_rettype(function_oid) != INT4OID) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("DBMS_PIPE cache function must return integer"))); const AclResult acl_result = object_aclcheck( ProcedureRelationId, function_oid, GetUserId(), ACL_EXECUTE); if (acl_result != ACLCHECK_OK) aclcheck_error(acl_result, OBJECT_FUNCTION, get_func_name(function_oid)); InvokeFunctionExecuteHook(function_oid); FmgrInfo function_info; fmgr_info(function_oid, &function_info); return DatumGetInt32(FunctionCall1( &function_info, PointerGetDatum(const_cast(pipename)))); } /* Значение DATE передаётся с точностью до секунды, без округления. */ static Timestamp dbms_pipe_truncate_date_seconds(Timestamp value) { if (TIMESTAMP_NOT_FINITE(value)) return value; const int64 fraction = value % USECS_PER_SEC; value -= fraction; /* Для отрицательной эпохи нужен floor, а не округление к нулю. */ if (fraction < 0) value -= USECS_PER_SEC; return value; } static int32 dbms_pipe_receive_to_local_buffer(const DbmsPipeNameKey *key, int32 timeout, DbmsPipeReceiveMode mode) { const int32 result = dbms_pipe_queue_receive(key, timeout, mode); if (result != 0) dbms_pipe_buffer_receive_failed(); return result; } Datum dbms_pipe_maxwait(PG_FUNCTION_ARGS) { PG_RETURN_INT32(86400000); } Datum dbms_pipe_pack_message_varchar2(PG_FUNCTION_ARGS) { const bool is_null = PG_ARGISNULL(0); const text *item = is_null ? nullptr : PG_GETARG_TEXT_PP(0); dbms_pipe_buffer_pack_varchar2(item, is_null); PG_RETURN_VOID(); } Datum dbms_pipe_pack_message_number(PG_FUNCTION_ARGS) { const bool is_null = PG_ARGISNULL(0); Numeric item = is_null ? nullptr : PG_GETARG_NUMERIC(0); dbms_pipe_buffer_pack_number(item, is_null); PG_RETURN_VOID(); } Datum dbms_pipe_pack_message_date(PG_FUNCTION_ARGS) { const bool is_null = PG_ARGISNULL(0); const Timestamp item = is_null ? 0 : date2timestamp_safe(PG_GETARG_DATEADT(0), nullptr); dbms_pipe_buffer_pack_date(item, is_null); PG_RETURN_VOID(); } Datum dbms_pipe_pack_message_timestamp(PG_FUNCTION_ARGS) { const bool is_null = PG_ARGISNULL(0); const Timestamp item = is_null ? 0 : dbms_pipe_truncate_date_seconds(PG_GETARG_TIMESTAMP(0)); dbms_pipe_buffer_pack_date(item, is_null); PG_RETURN_VOID(); } Datum dbms_pipe_pack_message_raw(PG_FUNCTION_ARGS) { const bool is_null = PG_ARGISNULL(0); const bytea *item = is_null ? nullptr : PG_GETARG_BYTEA_PP(0); dbms_pipe_buffer_pack_raw(item, is_null); PG_RETURN_VOID(); } Datum dbms_pipe_pack_message_rowid(PG_FUNCTION_ARGS) { const bool is_null = PG_ARGISNULL(0); const text *item = is_null ? nullptr : DatumGetTextPP(PG_GETARG_DATUM(0)); dbms_pipe_buffer_pack_rowid(item, is_null); PG_RETURN_VOID(); } /* * SQL-результат не должен ссылаться на долгоживущий входящий буфер: * следующий RECEIVE_MESSAGE или PURGE может сбросить его контекст. * Копия принадлежит контексту текущего SQL-вызова. */ static void * dbms_pipe_copy_unpacked_value(const DbmsPipeUnpackedItem &item) { void *const result = palloc(item.data_bytes); memcpy(result, item.data, item.data_bytes); return result; } Datum dbms_pipe_unpack_message_varchar2(PG_FUNCTION_ARGS) { const DbmsPipeUnpackedItem item = dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_VARCHAR2); if (item.is_null) PG_RETURN_NULL(); PG_RETURN_TEXT_P(static_cast(dbms_pipe_copy_unpacked_value(item))); } Datum dbms_pipe_unpack_message_number(PG_FUNCTION_ARGS) { const DbmsPipeUnpackedItem item = dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_NUMBER); if (item.is_null) PG_RETURN_NULL(); PG_RETURN_NUMERIC(static_cast(dbms_pipe_copy_unpacked_value(item))); } Datum dbms_pipe_unpack_message_date(PG_FUNCTION_ARGS) { const DbmsPipeUnpackedItem item = dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_DATE); if (item.is_null) PG_RETURN_NULL(); if (item.data_bytes != sizeof(Timestamp)) ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("corrupt DBMS_PIPE DATE payload"))); Timestamp value; memcpy(&value, item.data, sizeof(value)); PG_RETURN_TIMESTAMP(value); } Datum dbms_pipe_unpack_message_raw(PG_FUNCTION_ARGS) { const DbmsPipeUnpackedItem item = dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_RAW); if (item.is_null) PG_RETURN_NULL(); PG_RETURN_BYTEA_P(static_cast(dbms_pipe_copy_unpacked_value(item))); } Datum dbms_pipe_unpack_message_rowid(PG_FUNCTION_ARGS) { const DbmsPipeUnpackedItem item = dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_ROWID); if (item.is_null) PG_RETURN_NULL(); PG_RETURN_POINTER(dbms_pipe_copy_unpacked_value(item)); } Datum dbms_pipe_next_item_type(PG_FUNCTION_ARGS) { PG_RETURN_INT32(dbms_pipe_buffer_next_item_type()); } Datum dbms_pipe_reset_buffer(PG_FUNCTION_ARGS) { dbms_pipe_buffer_reset(); PG_RETURN_VOID(); } Datum dbms_pipe_create_pipe(PG_FUNCTION_ARGS) { const bool pipename_is_null = PG_ARGISNULL(0); const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0); const DbmsPipeNameKey key = dbms_pipe_make_name_key(pipename, pipename_is_null); if (PG_ARGISNULL(1)) dbms_pipe_raise_invalid_null_parameter(); const int32 maxpipesize = PG_GETARG_INT32(1); dbms_pipe_validate_nonnegative(maxpipesize); const bool is_private = PG_ARGISNULL(2) ? true : PG_GETARG_BOOL(2); const bool singleton = PG_ARGISNULL(3) ? false : PG_GETARG_BOOL(3); if (PG_ARGISNULL(4)) dbms_pipe_raise_invalid_null_parameter(); const int32 shelflife = PG_GETARG_INT32(4); dbms_pipe_validate_nonnegative(shelflife); if (!singleton && shelflife > 0) PG_RETURN_INT32(7); dbms_pipe_queue_prepare(); bool found; DbmsPipeRegistryEntry *const entry = dbms_pipe_registry_find_or_insert(&key, &found); const Oid current_user = GetUserId(); if (!found) { dbms_pipe_registry_initialize_entry(entry, maxpipesize, is_private, singleton, shelflife); dbms_pipe_registry_release(entry); PG_RETURN_INT32(0); } if (entry->is_private != is_private || (entry->is_private && entry->owner_id != current_user)) { dbms_pipe_registry_release(entry); dbms_pipe_raise_privilege_error(); } if (!entry->singleton && singleton) { if (entry->message_count > 1) { dbms_pipe_registry_release(entry); PG_RETURN_INT32(6); } entry->singleton = true; entry->shelf_life = shelflife; } else if (entry->singleton && !singleton) dbms_pipe_queue_convert_singleton_to_ordinary(entry); else if (entry->singleton) entry->shelf_life = shelflife; dbms_pipe_registry_release(entry); PG_RETURN_INT32(0); } Datum dbms_pipe_remove_pipe(PG_FUNCTION_ARGS) { const bool pipename_is_null = PG_ARGISNULL(0); const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0); const DbmsPipeNameKey key = dbms_pipe_make_name_key(pipename, pipename_is_null); dbms_pipe_queue_prepare(); DbmsPipeRegistryEntry *const entry = dbms_pipe_registry_find(&key, true); if (entry == nullptr) PG_RETURN_INT32(0); if (entry->is_private && entry->owner_id != GetUserId()) { dbms_pipe_registry_release(entry); dbms_pipe_raise_privilege_error(); } dbms_pipe_queue_discard_messages(entry); dbms_pipe_registry_delete(entry); PG_RETURN_INT32(0); } Datum dbms_pipe_send_message_ovr1(PG_FUNCTION_ARGS) { const bool pipename_is_null = PG_ARGISNULL(0); const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0); const DbmsPipeNameKey key = dbms_pipe_make_name_key(pipename, pipename_is_null); if (PG_ARGISNULL(1)) dbms_pipe_raise_invalid_null_parameter(); const int32 timeout = PG_GETARG_INT32(1); dbms_pipe_validate_nonnegative(timeout); if (PG_ARGISNULL(2)) dbms_pipe_raise_invalid_null_parameter(); const int32 maxpipesize = PG_GETARG_INT32(2); dbms_pipe_validate_nonnegative(maxpipesize); const bool singleton = PG_ARGISNULL(3) ? false : PG_GETARG_BOOL(3); if (PG_ARGISNULL(4)) dbms_pipe_raise_invalid_null_parameter(); const int32 shelflife = PG_GETARG_INT32(4); dbms_pipe_validate_nonnegative(shelflife); DbmsPipeSerializedMessage message = dbms_pipe_buffer_serialize_outgoing(); const int32 result = dbms_pipe_queue_send( &key, &message, timeout, maxpipesize, singleton, shelflife); if (message.data != nullptr) pfree(message.data); if (result == 0) dbms_pipe_buffer_send_succeeded(); PG_RETURN_INT32(result); } Datum dbms_pipe_send_message_ovr2(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("SEND_MESSAGE"); PG_RETURN_NULL(); } Datum dbms_pipe_send_message_ovr3(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("SEND_MESSAGE"); PG_RETURN_NULL(); } Datum dbms_pipe_receive_message_ovr1(PG_FUNCTION_ARGS) { const bool pipename_is_null = PG_ARGISNULL(0); const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0); const DbmsPipeNameKey key = dbms_pipe_make_name_key(pipename, pipename_is_null); if (PG_ARGISNULL(1)) dbms_pipe_raise_invalid_null_parameter(); const int32 timeout = PG_GETARG_INT32(1); dbms_pipe_validate_nonnegative(timeout); if (PG_ARGISNULL(2)) PG_RETURN_INT32(dbms_pipe_receive_to_local_buffer( &key, timeout, DbmsPipeReceiveMode::NORMAL)); const text *const cache_func = PG_GETARG_TEXT_PP(2); int32 result = dbms_pipe_receive_to_local_buffer( &key, 0, DbmsPipeReceiveMode::SINGLETON_CACHE); if (result == 0 || result == 8) PG_RETURN_INT32(result); const int32 cache_result = dbms_pipe_invoke_cache_function(cache_func, pipename); if (cache_result != 0) { dbms_pipe_buffer_receive_failed(); PG_RETURN_INT32(cache_result); } /* Время выполнения cache_func не расходует RECEIVE_MESSAGE timeout. */ result = dbms_pipe_receive_to_local_buffer( &key, timeout, DbmsPipeReceiveMode::SINGLETON_CACHE); PG_RETURN_INT32(result); } Datum dbms_pipe_receive_message_ovr2(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("RECEIVE_MESSAGE"); PG_RETURN_NULL(); } Datum dbms_pipe_receive_message_ovr3(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("RECEIVE_MESSAGE"); PG_RETURN_NULL(); } Datum dbms_pipe_set_credential_name(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("SET_CREDENTIAL_NAME"); PG_RETURN_VOID(); } Datum dbms_pipe_set_location_uri(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("SET_LOCATION_URI"); PG_RETURN_VOID(); } Datum dbms_pipe_get_credential_name(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("GET_CREDENTIAL_NAME"); PG_RETURN_NULL(); } Datum dbms_pipe_get_location_uri(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("GET_LOCATION_URI"); PG_RETURN_NULL(); } Datum dbms_pipe_purge_ovr1(PG_FUNCTION_ARGS) { const bool pipename_is_null = PG_ARGISNULL(0); const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0); const DbmsPipeNameKey key = dbms_pipe_make_name_key(pipename, pipename_is_null); dbms_pipe_queue_purge(&key); dbms_pipe_buffer_purge_read_state(); PG_RETURN_VOID(); } Datum dbms_pipe_purge_ovr2(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("PURGE"); PG_RETURN_VOID(); } Datum dbms_pipe_purge_ovr3(PG_FUNCTION_ARGS) { dbms_pipe_report_not_supported("PURGE"); PG_RETURN_VOID(); } Datum dbms_pipe_unique_session_name(PG_FUNCTION_ARGS) { static constexpr uint64 max_session_id = UINT64CONST(0xFFFFFFFFFFFF); static char session_name[sizeof("ORA$PIPE$000000000000")] = {}; if (session_name[0] == '\0') { const uint64 session_id = dbms_pipe_registry_next_session_id(); if (session_id == 0 || session_id > max_session_id) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("DBMS_PIPE session name space is exhausted"))); const int written = snprintf(session_name, sizeof(session_name), "ORA$PIPE$%012" PRIX64, session_id); if (written != static_cast(sizeof(session_name) - 1)) elog(ERROR, "could not format DBMS_PIPE session name"); } PG_RETURN_TEXT_P(cstring_to_text(session_name)); }