aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-10-06 16:03:48 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2024-10-06 16:03:48 -0400
commit5de77b609cbeffcb0b9a570f3848c60bc99ca7fa (patch)
tree040df672443e99d12c90dcd661e514a2141edb2d
parentce17de580f90929127fc6290301258e921156bea (diff)
downloadpostgresql-5de77b609cbeffcb0b9a570f3848c60bc99ca7fa.tar.gz
postgresql-5de77b609cbeffcb0b9a570f3848c60bc99ca7fa.zip
Ignore not-yet-defined Portals in pg_cursors view.
pg_cursor() supposed that any Portal it finds in the hash table must have sourceText set up, but there's an edge case where that is not so. A newly-created Portal has sourceText = NULL, and that doesn't change until PortalDefineQuery is called. In SPI_cursor_open_internal, we perform GetCachedPlan between CreatePortal and PortalDefineQuery, and it's possible for user-defined code to execute during that planning and cause a fetch from the pg_cursors view, resulting in a null-pointer-dereference crash. (It looks like the same could happen in exec_bind_message, but I've not tried to provoke a failure there.) I considered trying to fix this by setting sourceText sooner, but there may be instances of this same calling pattern in extensions, and we couldn't be sure they'd get the memo promptly. It seems better to redefine pg_cursor as not showing Portals that have not yet had PortalDefineQuery called on them, which we can do by just skipping them if sourceText is still NULL. (Before a1c692358, pg_cursor would instead return a row with NULL in the statement column. We could revert to that behavior but it doesn't really seem like a better definition, especially since our documentation doesn't suggest that the column could be NULL.) Per report from PetSerAl. Back-patch to all supported branches. Discussion: https://postgr.es/m/CAKygsHTBXLXjwV43kpZa+Cs+XTiaeeJiZdL4cPBm9f4MTdw7wg@mail.gmail.com
-rw-r--r--src/backend/utils/mmgr/portalmem.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.c
index 06dfa85f04d..fa5123a2ed1 100644
--- a/src/backend/utils/mmgr/portalmem.c
+++ b/src/backend/utils/mmgr/portalmem.c
@@ -1151,6 +1151,9 @@ pg_cursor(PG_FUNCTION_ARGS)
/* report only "visible" entries */
if (!portal->visible)
continue;
+ /* also ignore it if PortalDefineQuery hasn't been called yet */
+ if (!portal->sourceText)
+ continue;
values[0] = CStringGetTextDatum(portal->name);
values[1] = CStringGetTextDatum(portal->sourceText);