Skip to content

Commit fef6da9

Browse files
committed
libpq: Remove PQservice()
This routine has been introduced as a shortcut to be able to retrieve a service name from an active connection, for psql. Per discussion, and as it is only used by psql, let's remove it to not clutter the libpq API more than necessary. The logic in psql is replaced by lookups of PQconninfoOption for the active connection, instead, updated each time the variables are synced by psql, the prompt shortcut relying on the variable synced. Reported-by: Noah Misch <noah@leadboat.com> Discussion: https://postgr.es/m/20250706161319.c1.nmisch@google.com Backpatch-through: 18
1 parent 9300188 commit fef6da9

File tree

8 files changed

+53
-38
lines changed

8 files changed

+53
-38
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,26 +2740,6 @@ char *PQport(const PGconn *conn);
27402740
</listitem>
27412741
</varlistentry>
27422742

2743-
<varlistentry id="libpq-PQservice">
2744-
<term><function>PQservice</function><indexterm><primary>PQservice</primary></indexterm></term>
2745-
2746-
<listitem>
2747-
<para>
2748-
Returns the service of the active connection.
2749-
2750-
<synopsis>
2751-
char *PQservice(const PGconn *conn);
2752-
</synopsis>
2753-
</para>
2754-
2755-
<para>
2756-
<xref linkend="libpq-PQservice"/> returns <symbol>NULL</symbol> if the
2757-
<parameter>conn</parameter> argument is <symbol>NULL</symbol>.
2758-
Otherwise, if there was no service provided, it returns an empty string.
2759-
</para>
2760-
</listitem>
2761-
</varlistentry>
2762-
27632743
<varlistentry id="libpq-PQtty">
27642744
<term><function>PQtty</function><indexterm><primary>PQtty</primary></indexterm></term>
27652745

src/bin/psql/command.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4480,6 +4480,7 @@ SyncVariables(void)
44804480
{
44814481
char vbuf[32];
44824482
const char *server_version;
4483+
char *service_name;
44834484

44844485
/* get stuff from connection */
44854486
pset.encoding = PQclientEncoding(pset.db);
@@ -4489,12 +4490,16 @@ SyncVariables(void)
44894490
setFmtEncoding(pset.encoding);
44904491

44914492
SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
4492-
SetVariable(pset.vars, "SERVICE", PQservice(pset.db));
44934493
SetVariable(pset.vars, "USER", PQuser(pset.db));
44944494
SetVariable(pset.vars, "HOST", PQhost(pset.db));
44954495
SetVariable(pset.vars, "PORT", PQport(pset.db));
44964496
SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
44974497

4498+
service_name = get_conninfo_value("service");
4499+
SetVariable(pset.vars, "SERVICE", service_name);
4500+
if (service_name)
4501+
pg_free(service_name);
4502+
44984503
/* this bit should match connection_warnings(): */
44994504
/* Try to get full text form of version, might include "devel" etc */
45004505
server_version = PQparameterStatus(pset.db, "server_version");

src/bin/psql/common.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,6 +2531,41 @@ session_username(void)
25312531
return PQuser(pset.db);
25322532
}
25332533

2534+
/*
2535+
* Return the value of option for keyword in the current connection.
2536+
*
2537+
* The caller is responsible for freeing the result value allocated.
2538+
*/
2539+
char *
2540+
get_conninfo_value(const char *keyword)
2541+
{
2542+
PQconninfoOption *opts;
2543+
PQconninfoOption *serviceopt = NULL;
2544+
char *res = NULL;
2545+
2546+
if (pset.db == NULL)
2547+
return NULL;
2548+
2549+
opts = PQconninfo(pset.db);
2550+
if (opts == NULL)
2551+
return NULL;
2552+
2553+
for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
2554+
{
2555+
if (strcmp(opt->keyword, keyword) == 0)
2556+
{
2557+
serviceopt = opt;
2558+
break;
2559+
}
2560+
}
2561+
2562+
/* Take a copy of the value, as it is freed by PQconninfoFree(). */
2563+
if (serviceopt && serviceopt->val != NULL)
2564+
res = pg_strdup(serviceopt->val);
2565+
PQconninfoFree(opts);
2566+
2567+
return res;
2568+
}
25342569

25352570
/* expand_tilde
25362571
*

src/bin/psql/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern bool SendQuery(const char *query);
3939
extern bool is_superuser(void);
4040
extern bool standard_strings(void);
4141
extern const char *session_username(void);
42+
extern char *get_conninfo_value(const char *keyword);
4243

4344
extern void expand_tilde(char **filename);
4445
extern void clean_extended_state(void);

src/bin/psql/prompt.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,12 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
169169
break;
170170
/* service name */
171171
case 's':
172-
if (pset.db && PQservice(pset.db))
173-
strlcpy(buf, PQservice(pset.db), sizeof(buf));
172+
{
173+
const char *service_name = GetVariable(pset.vars, "SERVICE");
174+
175+
if (service_name)
176+
strlcpy(buf, service_name, sizeof(buf));
177+
}
174178
break;
175179
/* backend pid */
176180
case 'p':

src/interfaces/libpq/exports.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,8 @@ PQcancelFinish 202
205205
PQsocketPoll 203
206206
PQsetChunkedRowsMode 204
207207
PQgetCurrentTimeUSec 205
208-
PQservice 206
209-
PQsetAuthDataHook 207
210-
PQgetAuthDataHook 208
211-
PQdefaultAuthDataHook 209
212-
PQfullProtocolVersion 210
213-
appendPQExpBufferVA 211
208+
PQsetAuthDataHook 206
209+
PQgetAuthDataHook 207
210+
PQdefaultAuthDataHook 208
211+
PQfullProtocolVersion 209
212+
appendPQExpBufferVA 210

src/interfaces/libpq/fe-connect.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7461,14 +7461,6 @@ PQdb(const PGconn *conn)
74617461
return conn->dbName;
74627462
}
74637463

7464-
char *
7465-
PQservice(const PGconn *conn)
7466-
{
7467-
if (!conn)
7468-
return NULL;
7469-
return conn->pgservice;
7470-
}
7471-
74727464
char *
74737465
PQuser(const PGconn *conn)
74747466
{

src/interfaces/libpq/libpq-fe.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ extern int PQrequestCancel(PGconn *conn);
400400

401401
/* Accessor functions for PGconn objects */
402402
extern char *PQdb(const PGconn *conn);
403-
extern char *PQservice(const PGconn *conn);
404403
extern char *PQuser(const PGconn *conn);
405404
extern char *PQpass(const PGconn *conn);
406405
extern char *PQhost(const PGconn *conn);

0 commit comments

Comments
 (0)