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 1snvYg-006zsx-9X for pgsql-bugs@arkaria.postgresql.org; Tue, 10 Sep 2024 07:45:07 +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 1snvYf-008ekO-SD for pgsql-bugs@arkaria.postgresql.org; Tue, 10 Sep 2024 07:45:05 +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 1snvYf-008ekG-K4 for pgsql-bugs@lists.postgresql.org; Tue, 10 Sep 2024 07:45:05 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1snvYc-000RCa-3m for pgsql-bugs@postgresql.org; Tue, 10 Sep 2024 07:45:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description; bh=M5sZL+zO479tgZtpBTiePoQ2C6cB1xpi4bEcInhRtD8=; b=fmXk0TTXObkpf5RNK8JSrrFvzk BNfJBUkVLGhMKrm/8SgjWPMv5KXeUuvpoP2Kv01zGOuCoBLnSLY5VwnAOa8nNh3ddCPxLaJiQGf3y jo8kTlOiDgCcW00WV8kmVN8XPl5r2DbayV5OcMdKp5riX6vKB/LStjlMpnc1iPB7500Y=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1snvYc-008Mp0-1G; Tue, 10 Sep 2024 09:45:02 +0200 Date: Tue, 10 Sep 2024 09:45:02 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Sanskar Agrawal Cc: pgsql-bugs mailing list Subject: Re: BUG #18607: UNION ALL discards all foreign key relations + indexes Message-ID: Reply-To: depesz@depesz.com References: <18607-651aa996c1e71d48@postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Sep 10, 2024 at 12:52:21PM +0530, Sanskar Agrawal wrote: > Please find the below DDLS: > > CREATE TABLE test_users ( > user_id SERIAL PRIMARY KEY, > username VARCHAR(50) NOT NULL, > email VARCHAR(100) NOT NULL, > created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP > ); > -- Add an index on the user_id column > CREATE INDEX idx_user_id ON test_users (user_id); > -- Add an index on the email column > CREATE INDEX idx_email ON test_users (email); > INSERT INTO test_users (username, email) VALUES > ('user1', 'user1@example.com'), > ('user2', 'user2@example.com'); > SELECT * FROM temp_users WHERE email = 'user1@example.com' > UNION ALL > SELECT * FROM temp_users WHERE email = 'user2@example.com'; First, please note that your queries run with error: #v+ =$ psql -aX -f z.sql CREATE TABLE test_users ( user_id SERIAL PRIMARY KEY, username VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE -- Add an index on the user_id column CREATE INDEX idx_user_id ON test_users (user_id); CREATE INDEX -- Add an index on the email column CREATE INDEX idx_email ON test_users (email); CREATE INDEX INSERT INTO test_users (username, email) VALUES ('user1', 'user1@example.com'), ('user2', 'user2@example.com'); INSERT 0 2 SELECT * FROM temp_users WHERE email = 'user1@example.com' UNION ALL SELECT * FROM temp_users WHERE email = 'user2@example.com'; psql:z.sql:16: ERROR: relation "temp_users" does not exist LINE 1: SELECT * FROM temp_users WHERE email = 'user1@example.com' ^ #v- You meant test_users, but had temp_users. Fixed that, re-ran: #v+ =$ psql -aX -f z.sql CREATE TABLE test_users ( user_id SERIAL PRIMARY KEY, username VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE -- Add an index on the user_id column CREATE INDEX idx_user_id ON test_users (user_id); CREATE INDEX -- Add an index on the email column CREATE INDEX idx_email ON test_users (email); CREATE INDEX INSERT INTO test_users (username, email) VALUES ('user1', 'user1@example.com'), ('user2', 'user2@example.com'); INSERT 0 2 SELECT * FROM test_users WHERE email = 'user1@example.com' UNION ALL SELECT * FROM test_users WHERE email = 'user2@example.com'; user_id | username | email | created_at ---------+----------+-------------------+--------------------------- 1 | user1 | user1@example.com | 2024-09-10 09:43:05.72212 2 | user2 | user2@example.com | 2024-09-10 09:43:05.72212 (2 rows) #v- So, the queries work. Now, let's see the table: #v+ =$ psql -aX <<< '\d test_users' \d test_users Table "public.test_users" Column | Type | Collation | Nullable | Default ------------+-----------------------------+-----------+----------+--------------------------------------------- user_id | integer | | not null | nextval('test_users_user_id_seq'::regclass) username | character varying(50) | | not null | email | character varying(100) | | not null | created_at | timestamp without time zone | | | CURRENT_TIMESTAMP Indexes: "test_users_pkey" PRIMARY KEY, btree (user_id) "idx_email" btree (email) "idx_user_id" btree (user_id) #v- The index is there. So nothing got dropped. Can you explain what you're seeing, preferably using tested queries that show the problem? Best regards, depesz