Skip to content

Commit caa636f

Browse files
committed
[#23648] YSQL: Intoduce shared data between pggate in parallel leader and workers
Summary: Current diff is a preparation diff. The fix for the #23648 issue will be landed separately. The fix of the #23648 issue is required all perform operations has unique serial number. In this case local t-server will be able to wait/reorder operations before execution in case `op_2` will arrive earlier than `op_1`. The `uint64_t` counter is used to provider serial number for each new perform operation. But because postgres's background workers uses same `PgClientSession` object (pg worker process uses same `session_id` as parallel leader process) it is required to share this counter among multiple postgres process. Postgres has the `PROC` structure which is assigned to each process. This structure is stored in shared memory. It is reasonable to place perform serial number counter in this structure. This diff reserves some amount of raw data inside `PROC` structure (i.e. data plcaceholder) and provide access to this data to `pggate` during initialization. `pggate` constructs required objects in this raw data and use them as regular C++ structures (i.e. `std::atomic<uint64_t>` for perform serial number counter instead of `pg_atomic_uint64` or similar). **Note:** - the diff changes the time when pggate object is destroyed (see the call of `YBOnPostgresBackendShutdown()` function). Because pggate uses data in `PROC` it must be destroyed before this structure will be cleared - the diff restores original postgres's interface for the `InitPostgres` process (i.e. `session_id` argument is removed). Upgrade/Rollback safety: The `pg_client.proto` is used for communication between pggate and local t-server only. So any modification of the this file are upgrade/rollback safe. Jira: DB-12559 Test Plan: Jenkins Reviewers: amartsinchyk, pjain, kramanathan, sergei, jason Reviewed By: amartsinchyk Subscribers: jason, yql Tags: #jenkins-ready Differential Revision: https://phorge.dev.yugabyte.com/D46542
1 parent a39e75b commit caa636f

File tree

23 files changed

+211
-87
lines changed

23 files changed

+211
-87
lines changed

src/postgres/src/backend/access/transam/parallel.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,12 +1461,23 @@ ParallelWorkerMain(Datum main_arg)
14611461
* (b) we do not want parallel mode to cause these failures, because that
14621462
* would make use of parallel query plans not transparent to applications.
14631463
*/
1464-
YbBackgroundWorkerInitializeConnectionByOid(fps->database_id,
1465-
fps->authenticated_user_id,
1466-
(fps->parallel_master_is_yb_session ?
1467-
&fps->parallel_master_yb_session_state.session_id :
1468-
NULL),
1469-
BGWORKER_BYPASS_ALLOWCONN);
1464+
if (fps->parallel_master_is_yb_session)
1465+
{
1466+
YbcPgInitPostgresInfo yb_init_info = {
1467+
.parallel_leader_session_id = &fps->parallel_master_yb_session_state.session_id,
1468+
.shared_data = &fps->parallel_leader_pgproc->yb_shared_data
1469+
};
1470+
YbBackgroundWorkerInitializeConnectionByOid(fps->database_id,
1471+
fps->authenticated_user_id,
1472+
BGWORKER_BYPASS_ALLOWCONN,
1473+
&yb_init_info);
1474+
}
1475+
else
1476+
{
1477+
BackgroundWorkerInitializeConnectionByOid(fps->database_id,
1478+
fps->authenticated_user_id,
1479+
BGWORKER_BYPASS_ALLOWCONN);
1480+
}
14701481

14711482
/*
14721483
* Set the client encoding to the database encoding, since that is what

src/postgres/src/backend/bootstrap/bootstrap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ BootstrapModeMain(int argc, char *argv[], bool check_only)
363363
if (pg_link_canary_is_frontend())
364364
elog(ERROR, "backend is incorrectly linked to frontend functions");
365365

366-
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL, NULL);
366+
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL);
367367

368368
/* Initialize stuff for bootstrap-file processing */
369369
for (i = 0; i < MAXATTR; i++)

src/postgres/src/backend/postmaster/autovacuum.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ AutoVacLauncherMain(int argc, char *argv[])
476476
/* Early initialization */
477477
BaseInit();
478478

479-
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL, NULL);
479+
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL);
480480

481481
SetProcessingMode(NormalProcessing);
482482

@@ -1702,8 +1702,7 @@ AutoVacWorkerMain(int argc, char *argv[])
17021702
* Note: if we have selected a just-deleted database (due to using
17031703
* stale stats info), we'll fail and exit here.
17041704
*/
1705-
InitPostgres(NULL, dbid, NULL, InvalidOid, false, true,
1706-
dbname, NULL);
1705+
InitPostgres(NULL, dbid, NULL, InvalidOid, false, true, dbname);
17071706
SetProcessingMode(NormalProcessing);
17081707
set_ps_display(dbname);
17091708
ereport(DEBUG1,

src/postgres/src/backend/postmaster/postmaster.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6166,8 +6166,7 @@ BackgroundWorkerInitializeConnection(const char *dbname, const char *username, u
61666166
username, InvalidOid, /* role to connect as */
61676167
false, /* never honor session_preload_libraries */
61686168
(flags & BGWORKER_BYPASS_ALLOWCONN) != 0, /* ignore datallowconn? */
6169-
NULL, /* no out_dbname */
6170-
NULL); /* session id */
6169+
NULL /* no out_dbname */ );
61716170

61726171
if (yb_enable_ash)
61736172
YbAshSetMetadataForBgworkers();
@@ -6183,8 +6182,8 @@ BackgroundWorkerInitializeConnection(const char *dbname, const char *username, u
61836182
* Connect background worker to a database using OIDs.
61846183
*/
61856184
void
6186-
YbBackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid,
6187-
uint64_t *session_id, uint32 flags)
6185+
YbBackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags,
6186+
const YbcPgInitPostgresInfo *yb_init_info)
61886187
{
61896188
BackgroundWorker *worker = MyBgworkerEntry;
61906189

@@ -6194,12 +6193,12 @@ YbBackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid,
61946193
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
61956194
errmsg("database connection requirement not indicated during registration")));
61966195

6197-
InitPostgres(NULL, dboid, /* database to connect to */
6196+
YbInitPostgres(NULL, dboid, /* database to connect to */
61986197
NULL, useroid, /* role to connect as */
61996198
false, /* never honor session_preload_libraries */
62006199
(flags & BGWORKER_BYPASS_ALLOWCONN) != 0, /* ignore datallowconn? */
62016200
NULL, /* no out_dbname */
6202-
session_id); /* session id */
6201+
yb_init_info);
62036202

62046203
if (yb_enable_ash)
62056204
YbAshSetMetadataForBgworkers();
@@ -6215,7 +6214,7 @@ void
62156214
BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid,
62166215
uint32 flags)
62176216
{
6218-
YbBackgroundWorkerInitializeConnectionByOid(dboid, useroid, NULL, flags);
6217+
YbBackgroundWorkerInitializeConnectionByOid(dboid, useroid, flags, NULL);
62196218
}
62206219

62216220
/*

src/postgres/src/backend/storage/ipc/ipc.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,6 @@ proc_exit(int code)
155155
}
156156
#endif
157157

158-
if (IsYugaByteEnabled())
159-
YBOnPostgresBackendShutdown();
160-
161158
elog(DEBUG3, "exit(%d)", code);
162159

163160
exit(code);

src/postgres/src/backend/storage/lmgr/proc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
#include "utils/timeout.h"
5757
#include "utils/timestamp.h"
5858

59+
/* YB includes */
60+
#include "pg_yb_utils.h"
61+
5962
/* GUC variables */
6063
int DeadlockTimeout = 1000;
6164
int StatementTimeout = 0;
@@ -910,6 +913,9 @@ ProcKill(int code, Datum arg)
910913
MyProc = NULL;
911914
DisownLatch(&proc->procLatch);
912915

916+
if (IsYugaByteEnabled())
917+
YBOnPostgresBackendShutdown();
918+
913919
ReleaseProcToFreeList(proc);
914920

915921
/*

src/postgres/src/backend/tcop/postgres.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6058,8 +6058,7 @@ PostgresMain(const char *dbname, const char *username)
60586058
username, InvalidOid, /* role to connect as */
60596059
!am_walsender, /* honor session_preload_libraries? */
60606060
false, /* don't ignore datallowconn */
6061-
NULL, /* no out_dbname */
6062-
NULL); /* session id */
6061+
NULL /* no out_dbname */ );
60636062

60646063
/*
60656064
* If the PostmasterContext is still around, recycle the space; we don't

src/postgres/src/backend/utils/init/postinit.c

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ static void process_startup_options(Port *port, bool am_superuser);
110110
static void process_settings(Oid databaseid, Oid roleid);
111111

112112
/* YB functions */
113-
static void InitPostgresImpl(const char *in_dbname, Oid dboid,
114-
const char *username, Oid useroid,
115-
bool load_session_libraries,
116-
bool override_allow_connections,
117-
char *out_dbname,
118-
uint64_t *yb_session_id,
119-
bool *yb_sys_table_prefetching_started);
120-
static void YbEnsureSysTablePrefetchingStopped();
121113
static void YbPresetDatabaseCollation(HeapTuple tuple);
122114

123115
/*** InitPostgres support ***/
@@ -725,25 +717,10 @@ InitPostgres(const char *in_dbname, Oid dboid,
725717
const char *username, Oid useroid,
726718
bool load_session_libraries,
727719
bool override_allow_connections,
728-
char *out_dbname,
729-
uint64_t *yb_session_id)
720+
char *out_dbname)
730721
{
731-
bool sys_table_prefetching_started = false;
732-
733-
PG_TRY();
734-
{
735-
InitPostgresImpl(in_dbname, dboid, username, useroid,
736-
load_session_libraries, override_allow_connections,
737-
out_dbname, yb_session_id,
738-
&sys_table_prefetching_started);
739-
}
740-
PG_CATCH();
741-
{
742-
YbEnsureSysTablePrefetchingStopped();
743-
PG_RE_THROW();
744-
}
745-
PG_END_TRY();
746-
YbEnsureSysTablePrefetchingStopped();
722+
YbInitPostgres(in_dbname, dboid, username, useroid, load_session_libraries,
723+
override_allow_connections, out_dbname, NULL);
747724
}
748725

749726
static void
@@ -752,7 +729,7 @@ InitPostgresImpl(const char *in_dbname, Oid dboid,
752729
bool load_session_libraries,
753730
bool override_allow_connections,
754731
char *out_dbname,
755-
uint64_t *yb_session_id,
732+
const YbcPgInitPostgresInfo *yb_init_info,
756733
bool *yb_sys_table_prefetching_started)
757734
{
758735
bool bootstrap = IsBootstrapProcessingMode();
@@ -861,10 +838,7 @@ InitPostgresImpl(const char *in_dbname, Oid dboid,
861838
YbAshSetOneTimeMetadata();
862839

863840
/* Connect to YugaByte cluster. */
864-
if (bootstrap)
865-
YBInitPostgresBackend("postgres", yb_session_id);
866-
else
867-
YBInitPostgresBackend("postgres", yb_session_id);
841+
YBInitPostgresBackend("postgres", yb_init_info);
868842

869843
if (IsYugaByteEnabled() && !bootstrap)
870844
{
@@ -1439,6 +1413,30 @@ YbEnsureSysTablePrefetchingStopped()
14391413
YBCStopSysTablePrefetching();
14401414
}
14411415

1416+
void
1417+
YbInitPostgres(const char *in_dbname, Oid dboid,
1418+
const char *username, Oid useroid,
1419+
bool load_session_libraries,
1420+
bool override_allow_connections,
1421+
char *out_dbname, const YbcPgInitPostgresInfo *yb_init_info)
1422+
{
1423+
bool sys_table_prefetching_started = false;
1424+
1425+
PG_TRY();
1426+
{
1427+
InitPostgresImpl(in_dbname, dboid, username, useroid,
1428+
load_session_libraries, override_allow_connections,
1429+
out_dbname, yb_init_info, &sys_table_prefetching_started);
1430+
}
1431+
PG_CATCH();
1432+
{
1433+
YbEnsureSysTablePrefetchingStopped();
1434+
PG_RE_THROW();
1435+
}
1436+
PG_END_TRY();
1437+
YbEnsureSysTablePrefetchingStopped();
1438+
}
1439+
14421440
/*
14431441
* Check and set database collation once MyDatabaseId is resolved. In YB we
14441442
* need to do this earlier because of prefetching where we may need to

src/postgres/src/backend/utils/misc/pg_yb_utils.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ IpAddressToBytes(YbcPgAshConfig *ash_config)
10981098
}
10991099

11001100
void
1101-
YBInitPostgresBackend(const char *program_name, uint64_t *session_id)
1101+
YBInitPostgresBackend(const char *program_name, const YbcPgInitPostgresInfo *init_info)
11021102
{
11031103
HandleYBStatus(YBCInit(program_name, palloc, cstring_to_text_with_len));
11041104

@@ -1124,7 +1124,12 @@ YBInitPostgresBackend(const char *program_name, uint64_t *session_id)
11241124
ash_config.metadata = &MyProc->yb_ash_metadata;
11251125

11261126
IpAddressToBytes(&ash_config);
1127-
YBCInitPgGate(YbGetTypeTable(), &callbacks, session_id, &ash_config);
1127+
const YbcPgInitPostgresInfo default_init_info = {
1128+
.parallel_leader_session_id = NULL,
1129+
.shared_data = &MyProc->yb_shared_data
1130+
};
1131+
YBCInitPgGate(YbGetTypeTable(), &callbacks,
1132+
init_info ? init_info : &default_init_info, &ash_config);
11281133
YBCInstallTxnDdlHook();
11291134

11301135
/*

src/postgres/src/include/miscadmin.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#ifndef FRONTEND
3333
#include "storage/proc.h" /* for MyProc */
3434
#endif
35-
35+
#include "yb/yql/pggate/ybc_pg_typedefs.h"
3636

3737
#define InvalidPid (-1)
3838

@@ -488,11 +488,18 @@ extern PGDLLIMPORT AuxProcType MyAuxProcType;
488488
extern void pg_split_opts(char **argv, int *argcp, const char *optstr);
489489
extern void InitializeMaxBackends(void);
490490
extern void InitPostgres(const char *in_dbname, Oid dboid,
491+
const char *username, Oid useroid,
492+
bool load_session_libraries,
493+
bool override_allow_connections,
494+
char *out_dbname);
495+
496+
extern void YbInitPostgres(const char *in_dbname, Oid dboid,
491497
const char *username, Oid useroid,
492498
bool load_session_libraries,
493499
bool override_allow_connections,
494500
char *out_dbname,
495-
uint64_t *yb_session_id);
501+
const YbcPgInitPostgresInfo *yb_info);
502+
496503
extern void BaseInit(void);
497504

498505
/* in utils/init/miscinit.c */

0 commit comments

Comments
 (0)