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 1sOCTV-00CyJ4-9s for pgsql-general@arkaria.postgresql.org; Mon, 01 Jul 2024 08:33:25 +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 1sOCTT-00Gzit-L9 for pgsql-general@arkaria.postgresql.org; Mon, 01 Jul 2024 08:33:23 +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.94.2) (envelope-from ) id 1sOCTT-00GziZ-9j for pgsql-general@lists.postgresql.org; Mon, 01 Jul 2024 08:33:23 +0000 Received: from mail-oa1-x2b.google.com ([2001:4860:4864:20::2b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sOCTR-00404P-4k for pgsql-general@lists.postgresql.org; Mon, 01 Jul 2024 08:33:22 +0000 Received: by mail-oa1-x2b.google.com with SMTP id 586e51a60fabf-25cc6962c24so1216689fac.3 for ; Mon, 01 Jul 2024 01:33:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719822800; x=1720427600; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=kG6WzYGHdUDlPeFT/1Isvor+dm5/+BZqYcsLPKT7/4A=; b=L43oU84V4o19+hRKiUSwMh6fj3Po4xmwfNS3lMh1qkLgaC82MNR/FF+TRj6Iu1A4Ff rCRW86LfHQz5Dra+e0XnvxZLrmKWVI4irp9AR9XxEJISETsCQJ42LLNWwO8Z3Jr+s97v czXpdRVYbAvGQ4KHLTtf5BiNFUqRWRj4/yHsrqNr2UwlDLm7d/xoK+vsbAvVjDC+gb8K D4b92MhRhNC6M6AlLml3yX1SYUBzpzBg9iPnfNiZdlqXh3PQhCf/wTNtwHNiSERJxhMN ITQcYvn/nh7TzyV3BlvED/TEHukaTN8Mprx3CJAQShM+XCb7uhNXpnQMWDWUXaWoHmkj xJ4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719822800; x=1720427600; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=kG6WzYGHdUDlPeFT/1Isvor+dm5/+BZqYcsLPKT7/4A=; b=Nf3pMw7x/3h3RAme7KvigTTgaH1YUWI+WHxyZCny7wnNvbzbsp82cer+io9YMS25fL csBzm1USZ1Rjhm7idZABRH10kiGg6E5KKw0ZQb98K4RSIHdslRgU+3ZhYSvLP6BPruBB Zo3Zfxcr6uSLYgrU7Yokj+tiZ8E7sehriInGMnVBWdsMCmlr2nzcPZEOYSEuViPtQ6v8 3MbjfJ+HGTIwMJ63OU+pS7/32vcImkkTmi4lN/L7Hx3pWD+ixXNfHTPy1s4GcKW6jRLG ZO85ugTTf8Bf7JrDxmKVc3RzE7BJn+TSYcfhZRhdX9RVxONtrLYi7eYMQLjlxweNqhpR ImgA== X-Gm-Message-State: AOJu0YwCS7PQTyZ9NcbtYXLfXTD97ezd6bjN7pIVIlCVk6gN9E7/0yiO aZYcFLF44Lc4DZQ+iDcl88JbRZSaUbc7TgKtP+744OtXHbfFk6fCpWtc399L6CC3BkmlwPUX+Mw a9gphdHNT6UZgQVL71PhdT6kzFwQ3cQ== X-Google-Smtp-Source: AGHT+IGBv7oAkBgmUCBUgM9ZG5wDkGXGg9mtFjodc9C8AGlUZsF/YkIWbRG4K6q8vfz8cXJltYIFcjabeDQddPjVrfg= X-Received: by 2002:a05:6871:8a6:b0:25c:dac8:66c8 with SMTP id 586e51a60fabf-25db34a9410mr2805411fac.1.1719822800286; Mon, 01 Jul 2024 01:33:20 -0700 (PDT) MIME-Version: 1.0 From: Dominique Devienne Date: Mon, 1 Jul 2024 10:33:09 +0200 Message-ID: Subject: MVCC: Using function doing INSERT and returning PK in WHERE clause of SELECT statement To: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi. So for business rule reason, INSERT'ing a new row is wrapped in a DEFINER function, that returns the newly inserted row's OK (and integral ID). And in the code calling that function, I was SELECT * FROM table WHERE ID = insert_row_via_func(....) to "save a round trip", combining the insertion and the select. But of course, it didn't work, and in hindsight, the SCN for the SELECT is chosen before the INSERT done inside the function, so the new row is not seen, and the select returns nothing. The work-around is easy, just make two statements. But of course that's two round-trips. I looked at DO blocks, to perform both statements in the same rtrip. But DO block don't return anything. I guess I could have two functions, the existing one just returning the ID, and another returning the table row type, and the latter calling the former? That's just more code to maintain. Is my analysis correct? In terms of the problem? And the solutions / work-arounds? Thanks for any insights. --DD