commit afb70959a6d46054eb65e9c4b0a8f61d1c87b91b Author: Jacob Champion Date: Fri Jun 10 10:48:07 2022 -0700 squash! Allow parallel workers to use pg_session_authn_id() Per review, switch the global name to ClientConnectionInfo. diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 27eda766b1..bc93101ff7 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -76,7 +76,7 @@ #define PARALLEL_KEY_REINDEX_STATE UINT64CONST(0xFFFFFFFFFFFF000C) #define PARALLEL_KEY_RELMAPPER_STATE UINT64CONST(0xFFFFFFFFFFFF000D) #define PARALLEL_KEY_UNCOMMITTEDENUMS UINT64CONST(0xFFFFFFFFFFFF000E) -#define PARALLEL_KEY_PROCINFO UINT64CONST(0xFFFFFFFFFFFF000F) +#define PARALLEL_KEY_CLIENTCONNINFO UINT64CONST(0xFFFFFFFFFFFF000F) /* Fixed-size parallel state. */ typedef struct FixedParallelState @@ -213,7 +213,7 @@ InitializeParallelDSM(ParallelContext *pcxt) Size reindexlen = 0; Size relmapperlen = 0; Size uncommittedenumslen = 0; - Size procinfolen = 0; + Size clientconninfolen = 0; Size segsize = 0; int i; FixedParallelState *fps; @@ -274,8 +274,8 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_estimate_chunk(&pcxt->estimator, relmapperlen); uncommittedenumslen = EstimateUncommittedEnumsSpace(); shm_toc_estimate_chunk(&pcxt->estimator, uncommittedenumslen); - procinfolen = EstimateParallelProcInfoSpace(); - shm_toc_estimate_chunk(&pcxt->estimator, procinfolen); + clientconninfolen = EstimateClientConnectionInfoSpace(); + shm_toc_estimate_chunk(&pcxt->estimator, clientconninfolen); /* If you add more chunks here, you probably need to add keys. */ shm_toc_estimate_keys(&pcxt->estimator, 12); @@ -356,7 +356,7 @@ InitializeParallelDSM(ParallelContext *pcxt) char *session_dsm_handle_space; char *entrypointstate; char *uncommittedenumsspace; - char *procinfospace; + char *clientconninfospace; Size lnamelen; /* Serialize shared libraries we have loaded. */ @@ -427,11 +427,11 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_insert(pcxt->toc, PARALLEL_KEY_UNCOMMITTEDENUMS, uncommittedenumsspace); - /* Serialize our ParallelProcInfo. */ - procinfospace = shm_toc_allocate(pcxt->toc, procinfolen); - SerializeParallelProcInfo(procinfolen, procinfospace); - shm_toc_insert(pcxt->toc, PARALLEL_KEY_PROCINFO, - procinfospace); + /* Serialize our ClientConnectionInfo. */ + clientconninfospace = shm_toc_allocate(pcxt->toc, clientconninfolen); + SerializeClientConnectionInfo(clientconninfolen, clientconninfospace); + shm_toc_insert(pcxt->toc, PARALLEL_KEY_CLIENTCONNINFO, + clientconninfospace); /* Allocate space for worker information. */ pcxt->worker = palloc0(sizeof(ParallelWorkerInfo) * pcxt->nworkers); @@ -1281,7 +1281,7 @@ ParallelWorkerMain(Datum main_arg) char *reindexspace; char *relmapperspace; char *uncommittedenumsspace; - char *procinfospace; + char *clientconninfospace; StringInfoData msgbuf; char *session_dsm_handle_space; Snapshot tsnapshot; @@ -1491,9 +1491,10 @@ ParallelWorkerMain(Datum main_arg) false); RestoreUncommittedEnums(uncommittedenumsspace); - /* Restore the ParallelProcInfo. */ - procinfospace = shm_toc_lookup(toc, PARALLEL_KEY_PROCINFO, false); - RestoreParallelProcInfo(procinfospace); + /* Restore the ClientConnectionInfo. */ + clientconninfospace = shm_toc_lookup(toc, PARALLEL_KEY_CLIENTCONNINFO, + false); + RestoreClientConnectionInfo(clientconninfospace); /* Attach to the leader's serializable transaction, if SERIALIZABLE. */ AttachSerializableXact(fps->serializable_xact_handle); diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c index 2e5fe2cc19..6a499efecd 100644 --- a/src/backend/libpq/auth.c +++ b/src/backend/libpq/auth.c @@ -342,7 +342,7 @@ auth_failed(Port *port, int status, const char *logdetail) * authorization will fail later. * * The provided string will be copied into TopMemoryContext, to match the - * lifetime of MyParallelProcInfo, so it is safe to pass a string that is + * lifetime of MyClientConnectionInfo, so it is safe to pass a string that is * managed by an external library. */ static void @@ -350,7 +350,7 @@ set_authn_id(Port *port, const char *id) { Assert(id); - if (MyParallelProcInfo.authn_id) + if (MyClientConnectionInfo.authn_id) { /* * An existing authn_id should never be overwritten; that means two @@ -361,17 +361,17 @@ set_authn_id(Port *port, const char *id) ereport(FATAL, (errmsg("authentication identifier set more than once"), errdetail_log("previous identifier: \"%s\"; new identifier: \"%s\"", - MyParallelProcInfo.authn_id, id))); + MyClientConnectionInfo.authn_id, id))); } - MyParallelProcInfo.authn_id = MemoryContextStrdup(TopMemoryContext, id); + MyClientConnectionInfo.authn_id = MemoryContextStrdup(TopMemoryContext, id); if (Log_connections) { ereport(LOG, errmsg("connection authenticated: identity=\"%s\" method=%s " "(%s:%d)", - MyParallelProcInfo.authn_id, + MyClientConnectionInfo.authn_id, hba_authname(port->hba->auth_method), HbaFileName, port->hba->linenumber)); } @@ -1910,7 +1910,7 @@ auth_peer(hbaPort *port) set_authn_id(port, pw->pw_name); ret = check_usermap(port->hba->usermap, port->user_name, - MyParallelProcInfo.authn_id, false); + MyClientConnectionInfo.authn_id, false); return ret; #else diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c index 24a06bf933..97c827fb9a 100644 --- a/src/backend/utils/adt/name.c +++ b/src/backend/utils/adt/name.c @@ -275,10 +275,10 @@ session_user(PG_FUNCTION_ARGS) Datum pg_session_authn_id(PG_FUNCTION_ARGS) { - if (!MyParallelProcInfo.authn_id) + if (!MyClientConnectionInfo.authn_id) PG_RETURN_NULL(); - PG_RETURN_TEXT_P(cstring_to_text(MyParallelProcInfo.authn_id)); + PG_RETURN_TEXT_P(cstring_to_text(MyClientConnectionInfo.authn_id)); } diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 408fa8953d..1bbe1eaa17 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -935,48 +935,49 @@ GetUserNameFromId(Oid roleid, bool noerr) /* ------------------------------------------------------------------------ * Parallel connection state * - * MyParallelProcInfo contains pieces of information about the client that need - * to be synced to parallel workers when they initialize. Over time, this list - * will probably grow, and may subsume some of the "user state" variables above. + * ClientConnectionInfo contains pieces of information about the client that + * need to be synced to parallel workers when they initialize. Over time, this + * list will probably grow, and may subsume some of the "user state" variables + * above. *------------------------------------------------------------------------- */ -ParallelProcInfo MyParallelProcInfo; +ClientConnectionInfo MyClientConnectionInfo; /* - * Calculate the space needed to serialize MyParallelProcInfo. + * Calculate the space needed to serialize MyClientConnectionInfo. */ Size -EstimateParallelProcInfoSpace(void) +EstimateClientConnectionInfoSpace(void) { Size size = 1; - if (MyParallelProcInfo.authn_id) - size = add_size(size, strlen(MyParallelProcInfo.authn_id) + 1); + if (MyClientConnectionInfo.authn_id) + size = add_size(size, strlen(MyClientConnectionInfo.authn_id) + 1); return size; } /* - * Serialize MyParallelProcInfo for use by parallel workers. + * Serialize MyClientConnectionInfo for use by parallel workers. */ void -SerializeParallelProcInfo(Size maxsize, char *start_address) +SerializeClientConnectionInfo(Size maxsize, char *start_address) { /* * First byte is an indication of whether or not authn_id has been set to * non-NULL, to differentiate that case from the empty string. */ Assert(maxsize > 0); - start_address[0] = MyParallelProcInfo.authn_id ? 1 : 0; + start_address[0] = MyClientConnectionInfo.authn_id ? 1 : 0; start_address++; maxsize--; - if (MyParallelProcInfo.authn_id) + if (MyClientConnectionInfo.authn_id) { Size len; - len = strlcpy(start_address, MyParallelProcInfo.authn_id, maxsize) + 1; + len = strlcpy(start_address, MyClientConnectionInfo.authn_id, maxsize) + 1; Assert(len <= maxsize); maxsize -= len; start_address += len; @@ -984,22 +985,22 @@ SerializeParallelProcInfo(Size maxsize, char *start_address) } /* - * Restore MyParallelProcInfo from its serialized representation. + * Restore MyClientConnectionInfo from its serialized representation. */ void -RestoreParallelProcInfo(char *procinfo) +RestoreClientConnectionInfo(char *conninfo) { - if (procinfo[0] == 0) + if (conninfo[0] == 0) { - MyParallelProcInfo.authn_id = NULL; - procinfo++; + MyClientConnectionInfo.authn_id = NULL; + conninfo++; } else { - procinfo++; - MyParallelProcInfo.authn_id = MemoryContextStrdup(TopMemoryContext, - procinfo); - procinfo += strlen(procinfo) + 1; + conninfo++; + MyClientConnectionInfo.authn_id = MemoryContextStrdup(TopMemoryContext, + conninfo); + conninfo += strlen(conninfo) + 1; } } diff --git a/src/include/libpq/libpq-be.h b/src/include/libpq/libpq-be.h index f381e958ee..c900411fdd 100644 --- a/src/include/libpq/libpq-be.h +++ b/src/include/libpq/libpq-be.h @@ -99,9 +99,13 @@ typedef struct #endif /* - * Fields from Port that need to be copied over to parallel workers go into the - * ParallelProcInfo. The same rules apply for allocations here as for Port (must - * be malloc'd or palloc'd in TopMemoryContext). + * Fields describing the client connection, that also need to be copied over to + * parallel workers, go into the ClientConnectionInfo rather than Port. The same + * rules apply for allocations here as for Port (must be malloc'd or palloc'd in + * TopMemoryContext). + * + * If you add a struct member here, remember to also handle serialization in + * SerializeClientConnectionInfo() et al. */ typedef struct { @@ -117,7 +121,7 @@ typedef struct * example if the "trust" auth method is in use. */ const char *authn_id; -} ParallelProcInfo; +} ClientConnectionInfo; /* * This is used by the postmaster in its communication with frontends. It @@ -335,7 +339,7 @@ extern ssize_t be_gssapi_write(Port *port, void *ptr, size_t len); #endif /* ENABLE_GSS */ extern PGDLLIMPORT ProtocolVersion FrontendProtocol; -extern PGDLLIMPORT ParallelProcInfo MyParallelProcInfo; +extern PGDLLIMPORT ClientConnectionInfo MyClientConnectionInfo; /* TCP keepalives configuration. These are no-ops on an AF_UNIX socket. */ diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h index 55ad268700..c06796fe4a 100644 --- a/src/include/miscadmin.h +++ b/src/include/miscadmin.h @@ -486,9 +486,9 @@ extern bool has_rolreplication(Oid roleid); typedef void (*shmem_request_hook_type) (void); extern PGDLLIMPORT shmem_request_hook_type shmem_request_hook; -extern Size EstimateParallelProcInfoSpace(void); -extern void SerializeParallelProcInfo(Size maxsize, char *start_address); -extern void RestoreParallelProcInfo(char *procinfo); +extern Size EstimateClientConnectionInfoSpace(void); +extern void SerializeClientConnectionInfo(Size maxsize, char *start_address); +extern void RestoreClientConnectionInfo(char *procinfo); /* in executor/nodeHash.c */ extern size_t get_hash_memory_limit(void);