Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vmxMR-000c6p-2K for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Feb 2026 17:05:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vmxLR-000W8r-1f for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Feb 2026 17:04:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vmxLR-000W8h-0T for pgsql-hackers@lists.postgresql.org; Mon, 02 Feb 2026 17:04:13 +0000 Received: from mail-oi1-x233.google.com ([2607:f8b0:4864:20::233]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vmxLO-00000000hGM-1WbI for pgsql-hackers@postgresql.org; Mon, 02 Feb 2026 17:04:12 +0000 Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-45c87d82bd2so3161313b6e.1 for ; Mon, 02 Feb 2026 09:04:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770051847; x=1770656647; darn=postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=esv+z8tcxyn0bCJbO4jYxXUdNwo/p4LgmCGVXsB3tTc=; b=SLFirgAfrRnV3FOJ3UfJgVHo967GVcw/Wsg4fXwv3hinSQ3JBJbdAc4NmLlQqz5Kha v9TG+9rQ+9RMHoixkyPoajV8ybzojKZGCGx9jdWWE1v68wJEGSVTbAVUYSVJ+7xPNHV/ +e3wwqTGWj4+j6qaiXDaPKfOjDpypw87C2aHX9ngNOvIJfFWdj2rDX1TlKr8xYjWFXMV TIG7romWwJS0Yjkonm4tpBDD4mOgOmgJuTipq02dYTcYJOEILJZ66BOL5Ypz1dp9g5u4 801bbJZ9WwY/ekDq5sOq1KevSkcey8lWxeLI77UL52RVrmRVcJMgIk+9Poo/TLJf9t1b F19g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770051847; x=1770656647; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=esv+z8tcxyn0bCJbO4jYxXUdNwo/p4LgmCGVXsB3tTc=; b=CXR5ki+8pp9dSllkNT4XWPFnTtFmpAHc/5vwITabqz6TtglneCDzYjWKJQgifQCKqf d819fklFMgY6z6yfFcdksi6l1aUOLpHlaGz74A1WOF5zSj5jruXUnYhP+9I1RspVOM8V boR04DbDBD+pGvizio4vQQAbDrdegtYOiNQENoU3s2ixpqVcEkiAjTl6TAw4CBm7awaM eQyXJ/OX7d1k5zwVnmXVx0Cbcov7i57JENYdZ0KvL1SCnSXhsM7c2fFK2mibpBVTGgk6 ZK0n1izq4ShFszGDlPaJ1QxI//Is9WxRwqk7FQVG1uoZWKvZWNvfUF1yF5umFdxEU0DW KRiw== X-Forwarded-Encrypted: i=1; AJvYcCWAm6M7kfltg21oBuXgztlrCCfNJWH/xcmtsooQHEdrN7j2Tx1e32VtI1shj650SDhsiMEZBd7yeEs1/r0Q@postgresql.org X-Gm-Message-State: AOJu0YzL+0qxOI5HsULkSoSM3Trvk2xKqKLCydTZr1/rC/Ut5jBcnEfT dbk/YgRvN+suH/CVzD1n6VZOiJwXuHnlCBrmcMcgBWlefVG757bltV8A X-Gm-Gg: AZuq6aIReOUAV+eAG7/p7hyieQ3qs5TrjGilGeiwdQvc4QHrP7R/Sc/lhbV276PdSZZ wWc8S5ip9r10jwBA3NgzmdTnQNm8K7llXI1mSWIBQsXaMNYYuJJH18F6XemnVZzA99p1Kjo7vfr 5TtsIFMpJBj5EfqGdtk7IopWYXuV2vytA/dfMKuUaNssb+f2wSPJ7+uP+TOVWyxWcnusJvoGE6/ 5kGjQvtP/jcNNBJiAbtd2V4zV6f3KhuNwPMYLvTE92YigTapt0ecNnw/ZEH6DAbx19JomQzvV25 TBkbWJcH4rO75OvM8glI2dD9r2LraCZ9g10G54Nth1gDhf8uCKT0SLnnl0PfyaYuNwaRnUnrWqH 642Qf/hyio8+oMode1Iy5yKcOdg7S/AnVxACWHkyOLSsBBbIJObzoEQYrJDOyJLPgGQIBjqyXqK DZo4sh10ua5vkcarh/rzfUmuIhsPBpMB1SCdbcBZsxkwi3VwftSVCostVfAdQPa3GUtUtw/PC2Y JSo X-Received: by 2002:a05:6808:169e:b0:45e:fdf4:b928 with SMTP id 5614622812f47-45f34bb972dmr5007747b6e.24.1770051846905; Mon, 02 Feb 2026 09:04:06 -0800 (PST) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4095756cb0bsm11657931fac.18.2026.02.02.09.04.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Feb 2026 09:04:05 -0800 (PST) Date: Mon, 2 Feb 2026 11:04:03 -0600 From: Nathan Bossart To: Gilles Darold Cc: Zsolt Parragi , Japin Li , Yuefei Shi , songjinzhou , PostgreSQL Hackers , Andrew Dunstan , Tom Lane , liu xiaohui , Steven Niu Subject: Re: Pasword expiration warning Message-ID: References: <197512e0-8116-4776-a600-822d29ee68a6@darold.net> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="4xWawkXCylSumhVl" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <197512e0-8116-4776-a600-822d29ee68a6@darold.net> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --4xWawkXCylSumhVl Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Fri, Jan 30, 2026 at 12:33:54PM +0100, Gilles Darold wrote: > Here a new v12 version of the patch. Changes are the following: Thanks. I spent some time preparing this for commit, and I came up with the attached. Notable changes include: * Renamed the parameter to password_expiration_warning_threshold. It's a mouthful, but I thought it was more descriptive. * Changed the units for the parameter to minutes. I can't imagine anyone needs more granularity than hours or days, let alone seconds, so IMO minutes is a good middle ground. * I added a new "connection warnings" infrastructure that we can reuse if/when we want to emit warnings for MD5 passwords. * Moved the warning messages to Port. ClientConnectionInfo appears to be meant only for parallel workers, and I don't think we will ever want to emit connection warnings there. * Moved the tests into 001_password.pl. I'm a bit concerned about these tests being flaky, but I've tried setting the VALID UNTIL dates to make spurious failures virtually impossible. WDYT? -- nathan --4xWawkXCylSumhVl Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v13-0001-Add-password-expiration-warnings.patch From cb31a261878a7b7e839c9503418831cf23f3be08 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 2 Feb 2026 10:07:05 -0600 Subject: [PATCH v13 1/1] Add password expiration warnings. This commit adds a new parameter called parameter_expiration_warning_threshold that controls when the server begins emitting imminent-password-expiration warnings upon successful password authentication. By default, this parameter is set to 7 days, but this functionality can be disabled by setting it to 0. This patch also introduces a new "connection warning" infrastructure that can be reused elsewhere. For example, we may want to emit warnings about the use of MD5 passwords for a couple of releases before removing MD5 password support. Author: Gilles Darold Reviewed-by: Japin Li Reviewed-by: songjinzhou Reviewed-by: liu xiaohui Reviewed-by: Yuefei Shi Reviewed-by: Steven Niu Reviewed-by: Soumya S Murali Reviewed-by: Euler Taveira Reviewed-by: Zsolt Parragi Discussion: https://postgr.es/m/129bcfbf-47a6-e58a-190a-62fc21a17d03%40migops.com --- doc/src/sgml/config.sgml | 22 ++++++ src/backend/libpq/crypt.c | 57 +++++++++++++-- src/backend/utils/init/postinit.c | 72 +++++++++++++++++++ src/backend/utils/misc/guc_parameters.dat | 10 +++ src/backend/utils/misc/postgresql.conf.sample | 3 +- src/include/libpq/crypt.h | 3 + src/include/libpq/libpq-be.h | 9 +++ src/test/authentication/t/001_password.pl | 34 +++++++++ 8 files changed, 204 insertions(+), 6 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 5560b95ee60..5b9ab15bc92 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -1157,6 +1157,28 @@ include_dir 'conf.d' + + password_expiration_warning_threshold (integer) + + password_expiration_warning_threshold configuration parameter + + + + + When this parameter is greater than zero, the server will emit a + WARNING upon successful password authentication if + less than this amount of time remains until the authenticated role's + password expires. Note that a role's password only expires if a date + was specified in a VALID UNTIL clause for + CREATE ROLE or ALTER ROLE. If + this value is specified without units, it is taken as minutes. The + default is 7 days. This parameter can only set in the + postgresql.conf file or on the server command + line. + + + + md5_password_warnings (boolean) diff --git a/src/backend/libpq/crypt.c b/src/backend/libpq/crypt.c index 52722060451..3d86eb9a245 100644 --- a/src/backend/libpq/crypt.c +++ b/src/backend/libpq/crypt.c @@ -19,11 +19,16 @@ #include "common/md5.h" #include "common/scram-common.h" #include "libpq/crypt.h" +#include "libpq/libpq-be.h" #include "libpq/scram.h" #include "utils/builtins.h" +#include "utils/memutils.h" #include "utils/syscache.h" #include "utils/timestamp.h" +/* Time before password expiration warnings. */ +int password_expiration_warning_threshold = 10080; + /* Enables deprecation warnings for MD5 passwords. */ bool md5_password_warnings = true; @@ -71,13 +76,55 @@ get_role_password(const char *role, const char **logdetail) ReleaseSysCache(roleTup); /* - * Password OK, but check to be sure we are not past rolvaliduntil + * Password OK, but check to be sure we are not past rolvaliduntil or + * password_expiration_warning_threshold. */ - if (!isnull && vuntil < GetCurrentTimestamp()) + if (!isnull) { - *logdetail = psprintf(_("User \"%s\" has an expired password."), - role); - return NULL; + TimestampTz expire_time = vuntil - GetCurrentTimestamp(); + + /* + * If we're past rolvaliduntil, the connection attempt should fail, so + * update logdetail and return NULL. + */ + if (expire_time < 0) + { + *logdetail = psprintf(_("User \"%s\" has an expired password."), + role); + return NULL; + } + + /* + * If we're past the warning threshold, the connection attempt should + * succeed, but we still want to emit a warning. To do so, we queue + * the warning message using StoreConnectionWarning() so that it will + * be emitted at the end of InitPostgres(), and we return normally. + */ + if (expire_time / USECS_PER_MINUTE < password_expiration_warning_threshold) + { + MemoryContext oldcontext; + TimestampTz days; + TimestampTz hours; + TimestampTz minutes; + char *warning; + char *detail; + + oldcontext = MemoryContextSwitchTo(TopMemoryContext); + + days = expire_time / USECS_PER_DAY; + hours = (expire_time % USECS_PER_DAY) / USECS_PER_HOUR; + minutes = (expire_time % USECS_PER_HOUR) / USECS_PER_MINUTE; + + warning = pstrdup(_("role password will expire soon")); + detail = psprintf(_("The password for role \"%s\" will expire in " + INT64_FORMAT " day(s), " INT64_FORMAT + " hour(s), " INT64_FORMAT " minute(s)."), + role, days, hours, minutes); + + StoreConnectionWarning(warning, detail); + + MemoryContextSwitchTo(oldcontext); + } } return shadow_pass; diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 3f401faf3de..f9a963c0a47 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -70,6 +70,8 @@ #include "utils/syscache.h" #include "utils/timeout.h" +static bool ConnectionWarningsEmitted = false; + static HeapTuple GetDatabaseTuple(const char *dbname); static HeapTuple GetDatabaseTupleByOid(Oid dboid); static void PerformAuthentication(Port *port); @@ -85,6 +87,7 @@ static void ClientCheckTimeoutHandler(void); static bool ThereIsAtLeastOneRole(void); static void process_startup_options(Port *port, bool am_superuser); static void process_settings(Oid databaseid, Oid roleid); +static void EmitConnectionWarnings(void); /*** InitPostgres support ***/ @@ -1232,6 +1235,9 @@ InitPostgres(const char *in_dbname, Oid dboid, /* close the transaction we started above */ if (!bootstrap) CommitTransactionCommand(); + + /* send any WARNINGs we've accumulated during initialization */ + EmitConnectionWarnings(); } /* @@ -1446,3 +1452,69 @@ ThereIsAtLeastOneRole(void) return result; } + +/* + * Stores a warning message to be sent at the end of InitPostgres(). "detail" + * can be NULL, but "msg" cannot. + * + * NB: Caller should ensure the strings are allocated in a long-lived context + * like TopMemoryContext. + */ +void +StoreConnectionWarning(char *msg, char *detail) +{ + MemoryContext oldcontext; + + Assert(msg); + + if (ConnectionWarningsEmitted) + elog(ERROR, "StoreConnectionWarning() called after EmitConnectionWarnings()"); + + oldcontext = MemoryContextSwitchTo(TopMemoryContext); + + MyProcPort->warning_msgs = lappend(MyProcPort->warning_msgs, msg); + MyProcPort->warning_details = lappend(MyProcPort->warning_details, detail); + + MemoryContextSwitchTo(oldcontext); +} + +/* + * Sends the warning messages saved for the end of InitPostgres() and frees the + * strings and lists. + * + * NB: This can only be called once per backend. + */ +static void +EmitConnectionWarnings(void) +{ + ListCell *lc_msg; + ListCell *lc_detail; + + if (ConnectionWarningsEmitted) + elog(ERROR, "EmitConnectionWarnings() called more than once"); + else + ConnectionWarningsEmitted = true; + + if (MyProcPort == NULL) + return; + + forboth(lc_msg, MyProcPort->warning_msgs, + lc_detail, MyProcPort->warning_details) + { + char *msg = (char *) lfirst(lc_msg); + char *detail = (char *) lfirst(lc_detail); + + ereport(WARNING, + (errmsg("%s", msg), + detail ? errdetail("%s", detail) : 0)); + + pfree(msg); + if (detail) + pfree(detail); + } + + list_free(MyProcPort->warning_msgs); + list_free(MyProcPort->warning_details); + MyProcPort->warning_msgs = NIL; + MyProcPort->warning_details = NIL; +} diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat index f0260e6e412..6add1f37b4e 100644 --- a/src/backend/utils/misc/guc_parameters.dat +++ b/src/backend/utils/misc/guc_parameters.dat @@ -2242,6 +2242,16 @@ options => 'password_encryption_options', }, +{ name => 'password_expiration_warning_threshold', type => 'int', context => 'PGC_SIGHUP', group => 'CONN_AUTH_AUTH', + short_desc => 'Time before password expiration warnings.', + long_desc => '0 means not to emit these warnings.', + flags => 'GUC_UNIT_MIN', + variable => 'password_expiration_warning_threshold', + boot_val => '10080', + min => '0', + max => 'INT_MAX', +}, + { name => 'plan_cache_mode', type => 'enum', context => 'PGC_USERSET', group => 'QUERY_TUNING_OTHER', short_desc => 'Controls the planner\'s selection of custom or generic plan.', long_desc => 'Prepared statements can have custom and generic plans, and the planner will attempt to choose which is better. This can be set to override the default behavior.', diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index c4f92fcdac8..6575a37405c 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -96,7 +96,8 @@ #authentication_timeout = 1min # 1s-600s #password_encryption = scram-sha-256 # scram-sha-256 or (deprecated) md5 #scram_iterations = 4096 -#md5_password_warnings = on # display md5 deprecation warnings? +#password_expiration_warning_threshold = 7d # time before expiration warnings +#md5_password_warnings = on # display md5 deprecation warnings? #oauth_validator_libraries = '' # comma-separated list of trusted validator modules # GSSAPI using Kerberos diff --git a/src/include/libpq/crypt.h b/src/include/libpq/crypt.h index f01886e1098..081817972d5 100644 --- a/src/include/libpq/crypt.h +++ b/src/include/libpq/crypt.h @@ -25,6 +25,9 @@ */ #define MAX_ENCRYPTED_PASSWORD_LEN (512) +/* Time before password expiration warnings. */ +extern PGDLLIMPORT int password_expiration_warning_threshold; + /* Enables deprecation warnings for MD5 passwords. */ extern PGDLLIMPORT bool md5_password_warnings; diff --git a/src/include/libpq/libpq-be.h b/src/include/libpq/libpq-be.h index 921b2daa4ff..fa382261fb1 100644 --- a/src/include/libpq/libpq-be.h +++ b/src/include/libpq/libpq-be.h @@ -238,6 +238,13 @@ typedef struct Port char *raw_buf; ssize_t raw_buf_consumed, raw_buf_remaining; + + /* + * Content of warning messages to send to the client upon successful + * authentication. + */ + List *warning_msgs; + List *warning_details; } Port; /* @@ -367,4 +374,6 @@ extern int pq_setkeepalivesinterval(int interval, Port *port); extern int pq_setkeepalivescount(int count, Port *port); extern int pq_settcpusertimeout(int timeout, Port *port); +extern void StoreConnectionWarning(char *msg, char *detail); + #endif /* LIBPQ_BE_H */ diff --git a/src/test/authentication/t/001_password.pl b/src/test/authentication/t/001_password.pl index f4d65ba7bae..0ec9aa9f4e8 100644 --- a/src/test/authentication/t/001_password.pl +++ b/src/test/authentication/t/001_password.pl @@ -68,8 +68,24 @@ $node->init; $node->append_conf('postgresql.conf', "log_connections = on\n"); # Needed to allow connect_fails to inspect postmaster log: $node->append_conf('postgresql.conf', "log_min_messages = debug2"); +$node->append_conf('postgresql.conf', "password_expiration_warning_threshold = '1100d'"); $node->start; +# Set up roles for password_expiration_warning_threshold test +my $current_year = 1900 + ${ [ localtime(time) ] }[5]; +my $expire_year = $current_year - 1; +$node->safe_psql( + 'postgres', + "CREATE ROLE expired LOGIN VALID UNTIL '$expire_year-01-01' PASSWORD 'pass'"); +$expire_year = $current_year + 2; +$node->safe_psql( + 'postgres', + "CREATE ROLE expiration_warnings LOGIN VALID UNTIL '$expire_year-01-01' PASSWORD 'pass'"); +$expire_year = $current_year + 5; +$node->safe_psql( + 'postgres', + "CREATE ROLE no_warnings LOGIN VALID UNTIL '$expire_year-01-01' PASSWORD 'pass'"); + # Test behavior of log_connections GUC # # There wasn't another test file where these tests obviously fit, and we don't @@ -531,6 +547,24 @@ $node->connect_fails( qr/authentication method requirement "!password,!md5,!scram-sha-256" failed: server requested SCRAM-SHA-256 authentication/ ); +# Test password_expiration_warning_threshold +$node->connect_fails( + "user=expired dbname=postgres", + "connection fails due to expired password", + expected_stderr => + qr/password authentication failed for user "expired"/ +); +$node->connect_ok( + "user=expiration_warnings dbname=postgres", + "connection succeeds with password expiration warning", + expected_stderr => + qr/role password will expire soon/ +); +$node->connect_ok( + "user=no_warnings dbname=postgres", + "connection succeeds with no password expiration warning" +); + # Test SYSTEM_USER <> NULL with parallel workers. $node->safe_psql( 'postgres', -- 2.50.1 (Apple Git-155) --4xWawkXCylSumhVl--