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.94.2) (envelope-from ) id 1tckTq-002Nbi-9x for pgsql-general@arkaria.postgresql.org; Tue, 28 Jan 2025 12:14:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tckTp-000n8m-DI for pgsql-general@arkaria.postgresql.org; Tue, 28 Jan 2025 12:14:09 +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.94.2) (envelope-from ) id 1tckTp-000n8e-1u for pgsql-general@lists.postgresql.org; Tue, 28 Jan 2025 12:14:09 +0000 Received: from mail-il1-x136.google.com ([2607:f8b0:4864:20::136]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tckTm-001xDW-0A for pgsql-general@postgresql.org; Tue, 28 Jan 2025 12:14:08 +0000 Received: by mail-il1-x136.google.com with SMTP id e9e14a558f8ab-3ce4b009465so13562695ab.0 for ; Tue, 28 Jan 2025 04:14:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilm.com; s=google; t=1738066444; x=1738671244; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ttPD4fV1Xao6POvWRtP1AE/3PFfhZN56eq+rNOMyQlc=; b=Wuawb4u7Q5gLeGMfxdM/mZHDCIxA8bFdUecslN+kl31Pfjg17Hlc1HWQJ2l7a+g3jZ 15Cs9MZE5FWCkbcziZrUGANmM2xoTTMf4+X1HlhWDYlrDXuqesoUOLq7j9PXF3qsiUU3 G2IZdER+SnZ9525t71Yyl7RZuiIEWTeDy4H3I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738066444; x=1738671244; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ttPD4fV1Xao6POvWRtP1AE/3PFfhZN56eq+rNOMyQlc=; b=eI86bAGPT3sMZGFYRajtWxdwydvIXO4QvXs/ZRJUcdrr3YAJiR3+iXU/AADTRHvLN7 O2Le5jmT6e5brrhVZZqy9D5Cx4IhCd7nK07aPdxAwTER5rS0wXZc+IxTfYItFqxEN0cR uk1R4Ri3eu0rIsIAjuVAvgU7J1TJek+3VAxhGYRWsLZ9vq6GLI5BkzmG3z18cAu4ai3X HErzhm+S4YJNkfmoZ7hPEHr+wjadyYfIESYRpG/oBHUWjHJnHtA99nQ1y18pBBmgoiO4 7iEZweGcVpuGQjlLXKe6Q6EO5KwVj2NPPmZn1xL99Gt/zA7H8Y//4MBd8dzWLcLVTd5o iGGw== X-Gm-Message-State: AOJu0Yze5lqAoa+wZk9QytnrbVQP2Sl+1vtkXWDgWr2iPpQpHRemQG1j AGyMI/2AiFpQQLC2cr6mfmK8UyagdOQpPCuNdxBl3im954ad/h+AQ6izGw60eXF2QgjBgUXbXRv NZCaR5DS14PHyG5IituVEUNCm8bzr0MObam+d7Q== X-Gm-Gg: ASbGncvgESybjpj03T7Ouac5xJ6eCWpCZpajTP98R1AC7z2SY8jN3+oQ3/6j5Ymv6mK PfJfrHq90JDKVNzngHtoupBqPVpzpXS/xeVcUNCJgHghIOaGV8/dLZLRq31G4efJomodOiZD4+Q == X-Google-Smtp-Source: AGHT+IGAlNN9myMQRjYVvnC+7zMVWIqyglTbieecyfAALfFFhOHFe4dkjUXe/KAnXzZzcZUPny9DGbcjsyvgbZOxv90= X-Received: by 2002:a05:6e02:983:b0:3cf:b5d2:cf29 with SMTP id e9e14a558f8ab-3cfb5d2d459mr201723005ab.20.1738066444597; Tue, 28 Jan 2025 04:14:04 -0800 (PST) MIME-Version: 1.0 References: <0db12127e1bfc939a66a6ee959346b28cf1c6838.camel@cybertec.at> In-Reply-To: <0db12127e1bfc939a66a6ee959346b28cf1c6838.camel@cybertec.at> From: Jim Vanns Date: Tue, 28 Jan 2025 12:13:53 +0000 X-Gm-Features: AWEUYZlFHkfmY6yvActnJgH4h2xx1z3_BOCs-ENqd3ICfcdKhfZIBOI8W1Lourc Message-ID: Subject: Re: Parallel workers via functions? To: Laurenz Albe Cc: pgsql-general Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks for the reply Laurenz. Inline replies follow... On Tue, 28 Jan 2025 at 04:47, Laurenz Albe wrote: > > On Mon, 2025-01-27 at 18:08 +0000, Jim Vanns wrote: > > If I have a function that is marked 'stable parallel safe' and returns > > a table, can a calling function or procedure (marked volatile parallel > > unsafe) still take advantage of the parallel workers from the first > > function - as the data source. I.e. > > > > func_a(); // selects, returns table, parallel safe > > func_b() { > > insert into foo > > select * from func_a(); // Will func_a still execute parallel > > workers to fetch the data? > > } > > > > Or even if func_b() uses 'create temporary table as select * from > > func_a()' and then insert? > > > > I ask because when I simply call func_a() from a psql shell, I see the > > parallel workers run and everything is nice and swift. But when called > > from a data-modifying function like func_b(), no workers are spawned > > :( Even from the read-part of the code. > > > > Are there differences in functions vs. stored procedures that might > > affect the behaviour of the planner to disregard workers? > > See https://www.postgresql.org/docs/current/when-can-parallel-query-be-used.html Thanks. Yup, read that. Seems easy enough to understand... however... > The problem here is the INSERT. Data modifying statements won't use > parallel query. OK, that's clear enough. > There are exceptions: CREATE TABLE ... AS SELECT ... should be able > to use parallel query. I've been experimenting with this. The problem deepens... It seems that actually, it's the function itself - func_a() in my example above. Even simply calling that from psql doesn't spawn parallel workers to run as part of the query defined in the funcion body. But if I copy the body of the function and paste it into a psql shell, it does parallelise. This function is marked STABLE PARALLEL SAFE though. Are there limitations or restrictions I'm missing!? I'll try to find the time to provide a MRP but I'm hoping somebody will just magically know what the problem is or at least could be! So... I am still confused! This is PG 15.5 BTW. Jim > Yours, > Laurenz Albe -- Jim Vanns Principal Production Engineer Industrial Light & Magic, London