@@ -596,14 +596,30 @@ exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
596
596
else
597
597
{
598
598
char * host = PQhost (pset .db );
599
+ char * hostaddr = PQhostaddr (pset .db );
599
600
600
- /* If the host is an absolute path, the connection is via socket */
601
+ /*
602
+ * If the host is an absolute path, the connection is via socket
603
+ * unless overriden by hostaddr
604
+ */
601
605
if (is_absolute_path (host ))
602
- printf (_ ("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n" ),
603
- db , PQuser (pset .db ), host , PQport (pset .db ));
606
+ {
607
+ if (hostaddr && * hostaddr )
608
+ printf (_ ("You are connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n" ),
609
+ db , PQuser (pset .db ), hostaddr , PQport (pset .db ));
610
+ else
611
+ printf (_ ("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n" ),
612
+ db , PQuser (pset .db ), host , PQport (pset .db ));
613
+ }
604
614
else
605
- printf (_ ("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n" ),
606
- db , PQuser (pset .db ), host , PQport (pset .db ));
615
+ {
616
+ if (hostaddr && * hostaddr && strcmp (host , hostaddr ) != 0 )
617
+ printf (_ ("You are connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n" ),
618
+ db , PQuser (pset .db ), host , hostaddr , PQport (pset .db ));
619
+ else
620
+ printf (_ ("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n" ),
621
+ db , PQuser (pset .db ), host , PQport (pset .db ));
622
+ }
607
623
printSSLInfo ();
608
624
}
609
625
}
@@ -2854,6 +2870,7 @@ do_connect(enum trivalue reuse_previous_specification,
2854
2870
PGconn * o_conn = pset .db ,
2855
2871
* n_conn ;
2856
2872
char * password = NULL ;
2873
+ char * hostaddr = NULL ;
2857
2874
bool keep_password ;
2858
2875
bool has_connection_string ;
2859
2876
bool reuse_previous ;
@@ -2894,12 +2911,27 @@ do_connect(enum trivalue reuse_previous_specification,
2894
2911
}
2895
2912
2896
2913
/* grab missing values from the old connection */
2897
- if (!user && reuse_previous )
2898
- user = PQuser (o_conn );
2899
- if (!host && reuse_previous )
2900
- host = PQhost (o_conn );
2901
- if (!port && reuse_previous )
2902
- port = PQport (o_conn );
2914
+ if (reuse_previous )
2915
+ {
2916
+ if (!user )
2917
+ user = PQuser (o_conn );
2918
+ if (host && strcmp (host , PQhost (o_conn )) == 0 )
2919
+ {
2920
+ /*
2921
+ * if we are targetting the same host, reuse its hostaddr for
2922
+ * consistency
2923
+ */
2924
+ hostaddr = PQhostaddr (o_conn );
2925
+ }
2926
+ if (!host )
2927
+ {
2928
+ host = PQhost (o_conn );
2929
+ /* also set hostaddr for consistency */
2930
+ hostaddr = PQhostaddr (o_conn );
2931
+ }
2932
+ if (!port )
2933
+ port = PQport (o_conn );
2934
+ }
2903
2935
2904
2936
/*
2905
2937
* Any change in the parameters read above makes us discard the password.
@@ -2961,13 +2993,18 @@ do_connect(enum trivalue reuse_previous_specification,
2961
2993
2962
2994
while (true)
2963
2995
{
2964
- #define PARAMS_ARRAY_SIZE 8
2996
+ #define PARAMS_ARRAY_SIZE 9
2965
2997
const char * * keywords = pg_malloc (PARAMS_ARRAY_SIZE * sizeof (* keywords ));
2966
2998
const char * * values = pg_malloc (PARAMS_ARRAY_SIZE * sizeof (* values ));
2967
2999
int paramnum = -1 ;
2968
3000
2969
3001
keywords [++ paramnum ] = "host" ;
2970
3002
values [paramnum ] = host ;
3003
+ if (hostaddr && * hostaddr )
3004
+ {
3005
+ keywords [++ paramnum ] = "hostaddr" ;
3006
+ values [paramnum ] = hostaddr ;
3007
+ }
2971
3008
keywords [++ paramnum ] = "port" ;
2972
3009
values [paramnum ] = port ;
2973
3010
keywords [++ paramnum ] = "user" ;
@@ -3071,14 +3108,27 @@ do_connect(enum trivalue reuse_previous_specification,
3071
3108
param_is_newly_set (PQport (o_conn ), PQport (pset .db )))
3072
3109
{
3073
3110
char * host = PQhost (pset .db );
3111
+ char * hostaddr = PQhostaddr (pset .db );
3074
3112
3075
3113
/* If the host is an absolute path, the connection is via socket */
3076
3114
if (is_absolute_path (host ))
3077
- printf (_ ("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n" ),
3078
- PQdb (pset .db ), PQuser (pset .db ), host , PQport (pset .db ));
3115
+ {
3116
+ if (hostaddr && * hostaddr )
3117
+ printf (_ ("You are now connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n" ),
3118
+ PQdb (pset .db ), PQuser (pset .db ), hostaddr , PQport (pset .db ));
3119
+ else
3120
+ printf (_ ("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n" ),
3121
+ PQdb (pset .db ), PQuser (pset .db ), host , PQport (pset .db ));
3122
+ }
3079
3123
else
3080
- printf (_ ("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n" ),
3081
- PQdb (pset .db ), PQuser (pset .db ), host , PQport (pset .db ));
3124
+ {
3125
+ if (hostaddr && * hostaddr && strcmp (host , hostaddr ) != 0 )
3126
+ printf (_ ("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n" ),
3127
+ PQdb (pset .db ), PQuser (pset .db ), host , hostaddr , PQport (pset .db ));
3128
+ else
3129
+ printf (_ ("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n" ),
3130
+ PQdb (pset .db ), PQuser (pset .db ), host , PQport (pset .db ));
3131
+ }
3082
3132
}
3083
3133
else
3084
3134
printf (_ ("You are now connected to database \"%s\" as user \"%s\".\n" ),
0 commit comments