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 1wPvZ9-00198N-15 for pgsql-hackers@arkaria.postgresql.org; Thu, 21 May 2026 05:03:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wPvZ7-0097Xz-0Y for pgsql-hackers@arkaria.postgresql.org; Thu, 21 May 2026 05:03:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wPvZ6-0097Xr-2E for pgsql-hackers@lists.postgresql.org; Thu, 21 May 2026 05:03:25 +0000 Received: from mail-yw1-x1133.google.com ([2607:f8b0:4864:20::1133]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wPvZ5-0000000031f-3afy for pgsql-hackers@postgresql.org; Thu, 21 May 2026 05:03:24 +0000 Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-7c2fa14795aso43110147b3.1 for ; Wed, 20 May 2026 22:03:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1779339803; cv=none; d=google.com; s=arc-20240605; b=WNkBVJzLIQPqDNJhoCegCsivXUMm0RVuN2W41z0jTSsbPLnd5CLhb1EYoMzWRwAOMs 5ztibS/E0aFlLV/ndEsTwvJUpq4Q17U63MwHkKY6K7LMNx/225pWt6aQ/tIIzH1viy4O 22ZO9pYVN2qIiPrU5W4sUZ5s3D1xMytMjkgErXphGQm1xy3hueWR4zJDyII0qbpDuZm/ Lnw2kYvCoAOHtl5c5wTBVae2W2kiF1WodI4Ht+3+NHOj3MbxHbLXdaS0/j8sFBGvQA/d DJ1E6n1LfVtoo2ML/X9aH/DdfQXF/JMcU+V/XN6/cp5ILpCGdwKsGoEUhChe/GFRp+gr ak1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=IeS4LKzc0M2hhAaV1Uov3wn9Lek2CGXbhODIKOJsp5w=; fh=ltS1EponGG659cYh5oWNwZ15n7uwkn6fe9sMg2sv2eY=; b=QVXEZgKG6Znc4kGLGqW/Jjzs7LWp5pZl3AJHDEAPfduWKbd9A9KVS9ZhOl/+zJSF1Q yoQUjW+wmBO/NmJaIG/29pzxHQRS5G6HDOgCcCL3x4jrWS/JuALxEupLIwBlO3iJei02 P05gnap9szHkSLDS6u/VDBjETRpXO1qWKn7j5zXn4bNnYmm2Gc9Ce9XwlKPEjuG84dRB WkxaU5eFWhewPcwjHwRkUW6ZjUB1lm3UrYVoC7qbgZH6kwAcwhIPwLU9VS8CFVVcaxCu ar66pHLTZPE4JUbcyKRCJitiLRgwZjDoTcBBIPNB5YCHyxTC3UDon/bkd0xdg6Gdcabo v1Ow==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779339803; x=1779944603; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=IeS4LKzc0M2hhAaV1Uov3wn9Lek2CGXbhODIKOJsp5w=; b=ZuMaTrfOiAXIxDxBVTsEfWnlV55Efnv41oo5aXL3IhBKfSsFqhChrkNPzBgvQ2nLLs W6R/RP6Zem01QgXVdyyOIW/wVBkzj4CFADwYX8P1zMVdSeFNpQPuO8fhWT/mR8bRHaBI K65SopzLjJSC6Jj4eLsAxtxQA84LDHIMJq1vQM03dJqdvMaE0IRKh81FmoSZNdpKw58h 9gU1//UbPSndLYWWUlXXWLI6M3OUML/P3R7xf4yVUFaYsMBRrXg2RJY33M1zK6zoF8s3 Zows8qcbFb7I6g7JnkX9cbwQslIMWD9Xjkk7uKmobrFrRUAtUShj0c8vmT5gJ0FrW/wr HmxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779339803; x=1779944603; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=IeS4LKzc0M2hhAaV1Uov3wn9Lek2CGXbhODIKOJsp5w=; b=USGVrcYONRy+oTwMVly1Aj7+YXmDQKmsheF8fr6OQRzdUTgWf1O4RE1bGYRR+xmDp3 KmpenOZBhnAGmUP8t1ExJht7+cCVRBJzCfeOFy7h9sIHhh8FxSfDftlKFAqW919uLA+x Cul23bVEwnAjgoiKg0WjI8htdTaCNHXf5JkLUJ47sn5ZfxbGVQXwBvHMgVfJRAIMQtu2 Swsgi0IWR4VkevsiIvqdsGg4VH0Z2OzTcFj1JrCPx7vZk2OvLIdugLM1kPluiNGVXPAg JMThKLtZJNTZA5t5/6BQBD1Q3SfPNiHSD1VWyWYlQNkbsM0apiVvirdCdIGIJ2nhxPSq 1Vig== X-Forwarded-Encrypted: i=1; AFNElJ+D56aVgu8uqC8mGwdm2bnyaGcgOyll0J8oEr7/Av3DxMu4GPNnt4YGQEWyJx81ttjTjJxlpF86prLcHWS2@postgresql.org X-Gm-Message-State: AOJu0YxgJUZDsgIdDntvP8vDDLvEeGAO+r5I11/b4xRJh7mg/duYqBql PEsBs6kCaSA02BIle3aSBTMtECuKSmdeWpCdS4CBUpo7UgnEh6SfbXa1v63kPwbWsbzi7anaj0d 2a8tdC4E3yJtN9YUh/ianTlAZztZD5Iw= X-Gm-Gg: Acq92OFqBEg8EVTxNYNDUxcyz5xJs1KZmovfVXsi0e14S9Irum+E0ZTyEkFSSxCpyum Cuj9+DYjeNdYqnHHGoimm106/LDRdAJcpVraPxPIaAsW5/iUGeBxYxse1dk+RNhTcZCg5Pqet1n +p29RgkcjxBXJC29WhUEGKklkFBiJQmADyT3G4aD82pYeKBZCJKLWu+GgmW1bJ2IgRT9awdD/RU d7nbtZHVTGq0SVN5sqMJ7Ey0Od5PUmvwtK4PGVi+l+SDWGuvG1ln6Jt0AaMkOAuadERu8BN7Cdt a+hizufMWQ== X-Received: by 2002:a05:690e:4843:b0:65c:7306:e17e with SMTP id 956f58d0204a3-65eae2a0213mr655421d50.44.1779339802803; Wed, 20 May 2026 22:03:22 -0700 (PDT) MIME-Version: 1.0 References: <978D8971-08C3-4AAD-AE8B-976D753C882A@gmail.com> In-Reply-To: <978D8971-08C3-4AAD-AE8B-976D753C882A@gmail.com> From: vignesh C Date: Thu, 21 May 2026 10:33:09 +0530 X-Gm-Features: AVHnY4IAIXiPJmadz40lnlddYQOeReX5KZhH9I5fAjoe9WNwk_4wfqqQU_9WT9g Message-ID: Subject: Re: Set notice receiver before libpq connection startup To: Chao Li Cc: Fujii Masao , PostgreSQL-development Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 21 May 2026 at 07:40, Chao Li wrote: > > > > > On May 20, 2026, at 17:19, Fujii Masao wrote: > > > > On Wed, May 20, 2026 at 4:21=E2=80=AFPM Chao Li wrote: > >> > >> Hi, > >> > >> While testing =E2=80=9CLog remote NOTICE, WARNING, and similar message= s using ereport()=E2=80=9D, I noticed that libpqsrv_notice_receiver is only= installed after libpqsrv_connect() finishes. As a result, NOTICE messages = generated during connection establishment are missed by ereport() and are s= till printed to stderr. > >> > >> To reproduce the issue, I created a separate database called remotedb = and defined a login trigger that emits a NOTICE message: > >> ``` > >> CREATE DATABASE remotedb; > >> > >> \c remotedb > >> > >> CREATE OR REPLACE FUNCTION repro_login_notice() > >> RETURNS event_trigger > >> LANGUAGE plpgsql AS $$ > >> BEGIN > >> RAISE NOTICE 'startup notice from remotedb login trigger'; > >> END; > >> $$; > >> > >> CREATE EVENT TRIGGER repro_login_notice_trg > >> ON login > >> EXECUTE FUNCTION repro_login_notice(); > >> > >> ALTER EVENT TRIGGER repro_login_notice_trg ENABLE ALWAYS; > >> ``` > >> > >> Then, from another database: > >> ``` > >> evantest=3D# create extension dblink; > >> CREATE EXTENSION > >> evantest=3D# SELECT dblink_connect('host=3D127.0.0.1 port=3D5432 dbnam= e=3Dremotedb user=3Dchaol sslmode=3Ddisable gssencmode=3Ddisable'); > >> dblink_connect > >> ---------------- > >> OK > >> (1 row) > >> ``` > >> > >> In the system log, the NOTICE message is printed directly: > >> ``` > >> 2026-05-20 13:02:19.350 CST [24909] STATEMENT: SELECT dblink_connect(= 'host=3D127.0.0.1 port=3D5432 dbname=3Dremotedb user=3Dchaol sslmode=3Ddisa= ble gssencmode=3Ddisable'); > >> NOTICE: startup notice from remotedb login trigger > >> ``` > >> > >> To fix that, I think we should install libpqsrv_notice_receiver before= libpqsrv_connect_internal(). In the attached patch, I added two helpers: l= ibpqsrv_connect_with_notice_receiver() and libpqsrv_connect_params_with_not= ice_receiver(). > >> > >> With the fix, the NOTICE message now looks like this: > >> ``` > >> 2026-05-20 14:44:49.296 CST [45567] LOG: received message via remote = connection: NOTICE: startup notice from remotedb login trigger > >> 2026-05-20 14:44:49.296 CST [45567] STATEMENT: SELECT dblink_connect(= 'host=3D127.0.0.1 port=3D5432 dbname=3Dremotedb user=3Dchaol sslmode=3Ddisa= ble gssencmode=3Ddisable'); > >> ``` > >> > >> Please see the attached patch for details. > > > > Thanks for the report and patch! > > > > I'd prefer to avoid adding notice-receiver-specific wrappers such as > > libpqsrv_connect_with_notice_receiver(). Instead, how about splitting > > libpqsrv_connect() into two steps: libpqsrv_connect_start(), which perf= orms > > libpqsrv_connect_prepare() and PQconnectStart(), and > > libpqsrv_connect_complete(), which runs libpqsrv_connect_internal()? > > > > With this approach, callers could invoke PQsetNoticeReceiver() after > > libpqsrv_connect_start() returns but before libpqsrv_connect_complete()= is > > called. This would allow startup-time notices to be handled correctly w= ithout > > introducing a dedicated wrapper function. > > > > Compared to the *_with_notice_receiver() approach, this design is more > > general because it exposes the phase between PQconnectStart() and conne= ction > > completion. It could also support other kinds of per-connection setup i= n the > > future, not just notice receiver installation. Thought? > > > > Regards, > > > > -- > > Fujii Masao > > The idea sounds good to me, so v2 is implemented following that idea. > > A few things I want to point out abut v2: > > * Since libpqsrv_connect_complete() would only wrap libpqsrv_connect_inte= rnal(), I just renamed libpqsrv_connect_internal() to libpqsrv_connect_comp= lete(). > * Since libpqsrv_connect() is now split into two phases, libpqsrv_connect= _complete() must be called even if conn is NULL, because it may need to cal= l ReleaseExternalFD(). I mentioned this in the header comment of libpqsrv_c= onnect_start(). > * In the postgres_fdw/connection.c change, I introduced a new local varia= ble, start_conn, to keep the original logic unchanged. Because there is a P= G_TRY/PG_CATCH block, conn is assigned only after libpqsrv_connect_complete= () finishes successfully. Thanks for reporting this issue. I was able to reproduce the issue with the steps provided and your patch fixes the issue. Few comments: 1) No need of conn variable here, we can directly return PQconnectStart(conninfo) in this function: +static inline PGconn * +libpqsrv_connect_start(const char *conninfo) +{ + PGconn *conn =3D NULL; + + libpqsrv_connect_prepare(); + + conn =3D PQconnectStart(conninfo); + + return conn; +} 2) Similarly here too: +static inline PGconn * +libpqsrv_connect_params_start(const char *const *keywords, + const char *const *values, + int expand_dbname) { PGconn *conn =3D NULL; libpqsrv_connect_prepare(); - conn =3D PQconnectStart(conninfo); - - libpqsrv_connect_internal(conn, wait_event_info); + conn =3D PQconnectStartParams(keywords, values, expand_dbname); return conn; } Regards, Vignesh