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

Fixed error code difference occurring due to parallel worker enforced #271

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
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 @@ -1583,3 +1587,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 @@ -815,9 +815,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 @@ -305,6 +305,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 @@ -1723,6 +1724,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 @@ -3436,6 +3445,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 @@ -252,6 +252,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 */
Loading