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 1qnVqZ-00B6sk-2z for pgsql-sql@arkaria.postgresql.org; Tue, 03 Oct 2023 03:13:19 +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 1qnVpZ-0008wL-8d for pgsql-sql@arkaria.postgresql.org; Tue, 03 Oct 2023 03:12:17 +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 1qnVpY-0008wD-VC for pgsql-sql@lists.postgresql.org; Tue, 03 Oct 2023 03:12:17 +0000 Received: from mail-oo1-xc35.google.com ([2607:f8b0:4864:20::c35]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qnVpW-007tqc-2r for pgsql-sql@postgresql.org; Tue, 03 Oct 2023 03:12:15 +0000 Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-57ba5f05395so277569eaf.1 for ; Mon, 02 Oct 2023 20:12:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696302613; x=1696907413; darn=postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=jv9yn7QuuLQEDajEN6Anx2Mjk7JeLtMZF4cG5k369Sw=; b=b42FOiah1eZfdG1PB5MPaLmHw7L+Csd6ET4k3fdtYFDfErKT8mXXqiusYe7VX1GS1J omZKgQTDzoW6MOshNr7uwkypJ6/o8xYal0NaBQ85UlwF3AnSXH9uI6Re4pxZJavuHlYn Thx2zmvhs+dl77wcCygXmX41IGnT3z3snwi658LDzQR9pNexuUx2bNmjOfz5jD0s9Pcr wyKmn71/OaEnQ/Tc5vtPy62s8kZD+AQ1CXnXRWNXOscYdIFapjLywHzAdm7OLbiBDbx/ zTK/fmPNQWbdfY5TXKfmYWvNOxYPaYfMhrwDD7lyvx+Mo4zkIl+2bM1z4oPUbGSgBtsM sYDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696302613; x=1696907413; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jv9yn7QuuLQEDajEN6Anx2Mjk7JeLtMZF4cG5k369Sw=; b=uZJlrKH27RpGSi545JcaQ3q8RoPlj8rqLRADLcZ6fCf1iWXR5P/o4RR2Ru/Ii35ODY imcf2UJZg+7BrqWBJgii+LOXLHrwPte354SxeePsU8Q5vT6tKKq1m8p0/nyHbW8641Vd 6iu35EYbJRCxeqHbYJSS6NCxIJLjCodP5p/jxkwISGidl05IpNLWiy7jxmxZsnZv6QjN y0nteT3PCSERbiU6qzMs1i9IgJYo774+rARP7Ykw1bXrliAOFBbWDWyRyRYiofgNC+DZ JppAW6/ddi5A6L7vHdW2usX2EFod9xHyetl1k+6lXaWCkpjSKy/E/xMIJDNq+aVl9heo ik9w== X-Gm-Message-State: AOJu0YzHGW9XfShrSZDuatK9fyneY7MdWq1AvQEIgidh04P8mQJn7TlN a7Db3mBjEuj9Ttduj98oYNpdu/EoBBNY//eWZJs= X-Google-Smtp-Source: AGHT+IHzGM3i2dPjbuo7sDroqE/1XWF/W1BMUc2z9EnMyKZPUobzbe3IZRj2bT/z2ArexmMB+HfrGxVjnFruf/p8y70= X-Received: by 2002:a4a:2a4a:0:b0:57b:3ee3:1abe with SMTP id x10-20020a4a2a4a000000b0057b3ee31abemr12734677oox.5.1696302613436; Mon, 02 Oct 2023 20:10:13 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a05:6802:1c87:b0:4f6:384b:17ef with HTTP; Mon, 2 Oct 2023 20:10:12 -0700 (PDT) In-Reply-To: <2206102.1696302184@sss.pgh.pa.us> References: <2206102.1696302184@sss.pgh.pa.us> From: "David G. Johnston" Date: Mon, 2 Oct 2023 20:10:12 -0700 Message-ID: Subject: Re: A situation with one parent table and 3 child tables To: Tom Lane Cc: JORGE MALDONADO , "pgsql-sql@postgresql.org" Content-Type: multipart/alternative; boundary="000000000000b49d360606c73a9c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000b49d360606c73a9c Content-Type: text/plain; charset="UTF-8" On Monday, October 2, 2023, Tom Lane wrote: > "David G. Johnston" writes: > > On Monday, October 2, 2023, JORGE MALDONADO > wrote: > >> I have one parent table (*table_p*) with 3 child tables (*table_ch1*, > *table_ch2 > >> *and *table_ch3*). Each record of the parent table can be associated > with > >> 1 and only 1 child table records. This means that: > >> > >> * Some records of the *table_p* will link to records of *table_ch1* > >> * Some records of the *table_p* will link to records of *table_ch2* > >> * Some records of the *table_p* will link to records of *table_ch3* > >> > >> At first look, this does not make very much sense to me. I thought about > >> considering 3 parent tables, one for each child table. However, the 3 > >> parent tables would have the same exact structure and I would like to > know > >> if there is a workaround for this issue. > > > You are thinking of it backwards. Your chN tables will have FK pointing > > back to the p table. I suggest adding some kind of type column to the p > > table indicating which chN table the row belongs to. > > Do you need that? I was wondering about converting the 3 child tables > into a partitioned table. Then you can query them separately when > you need to, but you can also treat them as one table --- and you > can set up one FK constraint between that and the parent table. > This sounds like a typical subclassing (animal -> {dog,cat,moose}) structure where I am assuming the children have different subtype-specific columns. David J. --000000000000b49d360606c73a9c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Monday, October 2, 2023, Tom Lane <tgl@sss.pgh.pa.us> wrote:
&= quot;David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Monday, October 2, 2023, JORGE MALDONADO <jorgemal1960@gmail.com> wrote:
>> I have one parent table (*table_p*) with 3 child tables (*table_ch= 1*, *table_ch2
>> *and *table_ch3*). Each record of the parent table can be associat= ed with
>> 1 and only 1 child table records. This means that:
>>
>> * Some records of the *table_p* will link to records of *table_ch1= *
>> * Some records of the *table_p* will link to records of *table_ch2= *
>> * Some records of the *table_p* will link to records of *table_ch3= *
>>
>> At first look, this does not make very much sense to me. I thought= about
>> considering 3 parent tables, one for each child table. However, th= e 3
>> parent tables would have the same exact structure and I would like= to know
>> if there is a workaround for this issue.

> You are thinking of it backwards.=C2=A0 Your chN tables will have FK p= ointing
> back to the p table.=C2=A0 I suggest adding some kind of type column t= o the p
> table indicating which chN table the row belongs to.

Do you need that?=C2=A0 I was wondering about converting the 3 child tables=
into a partitioned table.=C2=A0 Then you can query them separately when
you need to, but you can also treat them as one table --- and you
can set up one FK constraint between that and the parent table.

This sounds like a typical subclassing (an= imal -> {dog,cat,moose}) structure where I am assuming the children have= different subtype-specific columns.

David J.
=C2=A0
--000000000000b49d360606c73a9c--