Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f3QPr-0005xv-Dr for pgsql-general@arkaria.postgresql.org; Tue, 03 Apr 2018 18:12:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f3QPp-0005zV-S8 for pgsql-general@arkaria.postgresql.org; Tue, 03 Apr 2018 18:12:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f3QPp-0005zM-Ew for pgsql-general@lists.postgresql.org; Tue, 03 Apr 2018 18:12:17 +0000 Received: from mail-pl0-x22c.google.com ([2607:f8b0:400e:c01::22c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f3QPm-00068a-E7 for pgsql-general@lists.postgresql.org; Tue, 03 Apr 2018 18:12:15 +0000 Received: by mail-pl0-x22c.google.com with SMTP id g20-v6so9937003plo.9 for ; Tue, 03 Apr 2018 11:12:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=GOXVqHgS+5ZXwUH5kozyRcd2VKv/wvYZKD7PBLQ/fmI=; b=mbvOHV7WNm/dngq3U5MJ3FJb+LPYNsnuKOsqw+ly+xSjjtX1aYFtNIi0NOoq+J76gS rGqhSdBLXpBF4VUEdty9Ba/Zh/caPvFZcYzZyKcLo0HD0RjusOFWEJLP+4rKEjNDGRRu P0wXOrp0JkPHy2I/ZTiQj117JmhO6TPmOkLy6SwwQsm73IL3vxykp7w/qluK/vKeZMVb S6ixy2fBvr8fqaR7mzmOHQSVxdaXWJLfHLpvyci9z84eCla9t4e+6pZY7tAMAMgvIYB4 UjdCjmdevy7EALpGBmgl4zWk4jREjZUMcDQ/9wxAxrIwuKZPoluPNOOBeJ2cBq09bHL7 cH3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=GOXVqHgS+5ZXwUH5kozyRcd2VKv/wvYZKD7PBLQ/fmI=; b=E84frHlEwUir2BDhA2g95d23jA8n0tuZuNITDoFZQAb6q+nhPLSVD7vlkp+J4cZroh im+qtY53PV7EXXFlIKYCmjal0QYRgFV92xlrl6QygO3LLGG0ICtBh2eEfNTdwJmvBm/M hp6aQ39SferpuxBUJUtFLMSCbdfoN3WkfDZ3YeYJNhLCR6G9QuTMIa7K8huhtJ/G728i c/X99RRgYF7O1wO50zkSiesHCqU8UAA++ffDLE0p12XfM9eG+CBJf49kYlhqGp05cVEd vOglceqd5qnZ2Wi2SjaOf8+VkbRz9LIO3xs85ma/Jahyt5xF5JV/ZJKbrMMpGwVEpOrK b26A== X-Gm-Message-State: ALQs6tBvsvKJCRFpwT2kSvFZZT0bap5smrflwMP6rnrh6z8JeUsAgYz3 VD6r3zX3l4lGvJ8k2e5tld1pMVjk X-Google-Smtp-Source: AIpwx49T2MvRlMA1cIuoyH4zCNy7qO708chP4KHG/r3EVA0CZ8LJ6dkDMe5b/1+txmjnuQiNZfDxAA== X-Received: by 10.99.116.8 with SMTP id p8mr1013036pgc.327.1522779132834; Tue, 03 Apr 2018 11:12:12 -0700 (PDT) Received: from [10.104.134.14] ([155.100.47.6]) by smtp.gmail.com with ESMTPSA id p1sm6374940pgr.67.2018.04.03.11.12.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 03 Apr 2018 11:12:12 -0700 (PDT) Subject: Re: single sql, multiple schemas, one result set To: pgsql-general@lists.postgresql.org References: <1522777660304-0.post@n3.nabble.com> From: Rob Sargent Message-ID: <22e7e414-0a46-faf1-6669-c786544b60d6@gmail.com> Date: Tue, 3 Apr 2018 12:12:11 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <1522777660304-0.post@n3.nabble.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04/03/2018 11:47 AM, PegoraroF10 wrote: > Suppose a DB with dozens of schemas with same structure. > DB > Schema1 > Table1 > Table2 > Schema2 > Table1 > Table2 > Schema3 > Table1 > Table2 > Then we want to execute a SQL on specific schemas and the result of it could > be a UNION ALL. So, how could be a function that runs that SQL on each > schema and results just on result set ? > > Then I would use something like ... > select * from SQLFromMultipleShema(Array['Schema1,Schema3'],'select count(*) > from Table2'); > > > > > -- > Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html > Should work if you add a tag to every table referenced in the sql sent in: select * from SQLFromMultipleShema(Array['Schema1,Schema3'],'select count(*) from SCHEMA_NAME.Table2') Then, in a loop over the schema name array replacing the SCHEMA_NAME as you build up a union of your injected sql. Or perhaps loop over the schame name array reseting search_path as you build up a temp table of the result from the sql passed in as an argument. Pretty sure you're going to end up need "dynamic sql";