Skip to content

Commit

Permalink
Fixed error code difference occurring due to parallel worker enforced (
Browse files Browse the repository at this point in the history
…#260)

When any error raised from parallel worker then Postgres by default does not share message_id (part of edata) to
leader worker. Babelfish's error mapping framework relies on message_id to map to correct T-SQL error code. But
since it is no more available to leader worker, T-SQL error code would not be found and default error code will be used.
This will also affect the transactional behaviour of given error.

In order to fix this issue, we need to store if given worker is spawned in the context of Babelfish or not to share error
message_id. Hence, this changes made changes in two data structure namely Port and FixedParallelState to store
context about Babelfish

Extension changes: babelfish-for-postgresql/babelfish_extensions#2047
Task: BABEL-4539
Signed-off-by: Dipesh Dhameliya <[email protected]>
  • Loading branch information
Deepesh125 authored Dec 18, 2023
1 parent 9e835c5 commit 4b6d741
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/backend/access/transam/parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ typedef struct FixedParallelState
TimestampTz stmt_ts;
SerializableXactHandle serializable_xact_handle;

/* Track if parallel worker is spawned in the context of Babelfish */
bool babelfish_context;

/* Mutex protects remaining fields. */
slock_t mutex;

Expand Down Expand Up @@ -332,6 +335,7 @@ InitializeParallelDSM(ParallelContext *pcxt)
fps->xact_ts = GetCurrentTransactionStartTimestamp();
fps->stmt_ts = GetCurrentStatementStartTimestamp();
fps->serializable_xact_handle = ShareSerializableXact();
fps->babelfish_context = MyProcPort->is_tds_conn;
SpinLockInit(&fps->mutex);
fps->last_xlog_end = 0;
shm_toc_insert(pcxt->toc, PARALLEL_KEY_FIXED, fps);
Expand Down Expand Up @@ -1595,3 +1599,13 @@ LookupParallelWorkerFunction(const char *libraryname, const char *funcname)
return (parallel_worker_main_type)
load_external_function(libraryname, funcname, true, NULL);
}

/*
* IsBabelfishParallelWorker - return bool based on whether given worker is
* spawned in Babelfish context.
*/
bool
IsBabelfishParallelWorker(void)
{
return (IsParallelWorker() && MyFixedParallelState->babelfish_context);
}
4 changes: 2 additions & 2 deletions src/backend/executor/execMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -818,9 +818,9 @@ InitPlan(QueryDesc *queryDesc, int eflags)
int i;

/*
* Do permissions checks if not parallel worker
* Do permissions checks if not Babelfish parallel worker
*/
if (!(sql_dialect == SQL_DIALECT_TSQL && IsParallelWorker()))
if (!IsBabelfishParallelWorker())
ExecCheckRTPerms(rangeTable, true);

/*
Expand Down
10 changes: 10 additions & 0 deletions src/backend/libpq/pqmq.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ pq_parse_errornotice(StringInfo msg, ErrorData *edata)
case PG_DIAG_SOURCE_FUNCTION:
edata->funcname = pstrdup(value);
break;
case PG_DIAG_MESSAGE_ID:
if (MyProcPort->is_tds_conn)
{
edata->message_id = (const char *) pstrdup(value);
}
else
{
elog(ERROR, "Unexpected error field message_id is found");
}
break;
default:
elog(ERROR, "unrecognized error field code: %d", (int) code);
break;
Expand Down
18 changes: 18 additions & 0 deletions src/backend/utils/error/elog.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#endif

#include "access/transam.h"
#include "access/parallel.h"
#include "access/xact.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
Expand Down Expand Up @@ -1718,6 +1719,14 @@ ThrowErrorData(ErrorData *edata)
if (edata->internalquery)
newedata->internalquery = pstrdup(edata->internalquery);

/*
* Generally, vanilla postgres does not share messaged_id with leader node from
* parallel worker. But case of Babelfish where message_id is needed to find
* T-SQL error code; below hook is defined to handle message_id for Babelfish.
*/
if (edata->message_id)
newedata->message_id = (const char *) pstrdup(edata->message_id);

MemoryContextSwitchTo(oldcontext);
recursion_depth--;

Expand Down Expand Up @@ -3250,6 +3259,15 @@ send_message_to_frontend(ErrorData *edata)
err_sendstring(&msgbuf, edata->funcname);
}

if (IsBabelfishParallelWorker())
{
if (edata->message_id)
{
pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_ID);
err_sendstring(&msgbuf, edata->message_id);
}
}

pq_sendbyte(&msgbuf, '\0'); /* terminator */

pq_endmessage(&msgbuf);
Expand Down
3 changes: 3 additions & 0 deletions src/include/access/parallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ extern void ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end);

extern void ParallelWorkerMain(Datum main_arg);

/* Below helpers are added to support parallel workers in Babelfish context */
extern bool IsBabelfishParallelWorker(void);

#endif /* PARALLEL_H */
1 change: 1 addition & 0 deletions src/include/libpq/libpq-be.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ typedef struct Port
SSL *ssl;
X509 *peer;
#endif
bool is_tds_conn;
} Port;

#ifdef USE_SSL
Expand Down
1 change: 1 addition & 0 deletions src/include/postgres_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ typedef PG_INT64_TYPE pg_int64;
#define PG_DIAG_SOURCE_FILE 'F'
#define PG_DIAG_SOURCE_LINE 'L'
#define PG_DIAG_SOURCE_FUNCTION 'R'
#define PG_DIAG_MESSAGE_ID 'I'

#endif /* POSTGRES_EXT_H */

0 comments on commit 4b6d741

Please sign in to comment.