Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Upv7A-00030P-7G for pgsql-general@arkaria.postgresql.org; Fri, 21 Jun 2013 06:46:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Upv79-0000SP-Ng for pgsql-general@arkaria.postgresql.org; Fri, 21 Jun 2013 06:46:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Upv78-0000SJ-Lo for pgsql-general@postgresql.org; Fri, 21 Jun 2013 06:46:30 +0000 Received: from mimolette.dalibo.net ([212.85.154.222]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Upv76-0000Ql-2p for pgsql-general@postgresql.org; Fri, 21 Jun 2013 06:46:30 +0000 Received: from [192.168.0.28] (ip-68.net-81-220-133.standre.rev.numericable.fr [81.220.133.68]) by mimolette.dalibo.net (Postfix) with ESMTPSA id 635EF4500CBA; Fri, 21 Jun 2013 08:46:25 +0200 (CEST) Message-ID: <51C3F6BF.2060806@dalibo.com> Date: Fri, 21 Jun 2013 08:46:23 +0200 From: Vik Fearing User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: Jeff Janes , pgsql-general@postgresql.org Subject: Re: Easiest way to compare the results of two queries row by row and column by column References: <1371766705.15253.44.camel@localhost.localdomain> In-Reply-To: Content-Type: multipart/alternative; boundary="------------090305080301070709080503" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org This is a multi-part message in MIME format. --------------090305080301070709080503 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 06/21/2013 01:07 AM, Jeff Janes wrote: > On Thu, Jun 20, 2013 at 3:18 PM, Jason Long > > wrote: > > Can someone suggest the easiest way to compare the results from two > queries to make sure they are identical? > > I am rewriting a large number of views and I want to make sure that > nothing is changes in the results. > > Something like > > select compare_results('select * from v_old', 'select * from v_new'); > > > I'd run: > > select * from v_old > except > select * from v_new ; > > And then > > select * from v_new > except > select * from v_old ; > > Both should return no rows. This is my solution as well. > However, if the queries can contain duplicate rows this will not > detect differences in the number of times a row is replicated, i.e. if > one query has a row 2 times and the other has it 3 times. If you need > to detect such cases, I'd probably \copy out each query to a file, > then use system tools to sort and diff the files. No need, just use EXCEPT ALL. Basically, the following query should return nothing: (TABLE v_old EXCEPT ALL TABLE v_new) UNION ALL (TABLE v_new EXCEPT ALL TABLE v_old); -- Vik --------------090305080301070709080503 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 06/21/2013 01:07 AM, Jeff Janes wrote:
On Thu, Jun 20, 2013 at 3:18 PM, Jason Long <mailing.lists@octgsoftware.com> wrote:
Can someone suggest the easiest way to compare the results from two
queries to make sure they are identical?

I am rewriting a large number of views and I want to make sure that
nothing is changes in the results.

Something like

select compare_results('select * from v_old', 'select * from v_new');

I'd run:

select * from v_old 
except
select * from v_new ;

And then 

select * from v_new
except
select * from v_old ;

Both should return no rows.

This is my solution as well.

However, if the queries can contain duplicate rows this will not detect differences in the number of times a row is replicated, i.e. if one query has a row 2 times and the other has it 3 times.  If you need to detect such cases, I'd probably \copy out each query to a file, then use system tools to sort and diff the files.

No need, just use EXCEPT ALL.

Basically, the following query should return nothing:

(TABLE v_old EXCEPT ALL TABLE v_new)
UNION ALL
(TABLE v_new EXCEPT ALL TABLE v_old);

--
Vik

--------------090305080301070709080503--