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 1u1etq-005yCX-Me for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Apr 2025 05:19:59 +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 1u1etn-00ALuL-K5 for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Apr 2025 05:19:55 +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 1u1etn-00ALuD-58 for pgsql-hackers@lists.postgresql.org; Mon, 07 Apr 2025 05:19:55 +0000 Received: from mail-vk1-xa2d.google.com ([2607:f8b0:4864:20::a2d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u1etl-003PLw-1W for pgsql-hackers@postgresql.org; Mon, 07 Apr 2025 05:19:54 +0000 Received: by mail-vk1-xa2d.google.com with SMTP id 71dfb90a1353d-525da75d902so1697555e0c.3 for ; Sun, 06 Apr 2025 22:19:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744003192; x=1744607992; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=O0QCoPaLLVGVM+j7NSycgXkvkY/kC3GX55A3ZW70lac=; b=GTPTPNCpoG8u1f1xUUKkoz1/yI7D7XMYHx17w5RpTieNkagrNRKL0sXCL0rtQssPMr tSjZMKdG/fBGkmbQMuqVIglzfumQIy0ik1wLSSuCOwefBUgeSrd0VhlYnfs3BLUmg02P Z0Ie5JVZvnHN3iHXqZRkMxUGmZclVuZW0M51gy4IjUZmAesTijH4/fp1XRKcMqEelBVB ZXwQTt94M/UDImmsCZVHqbQMv4ZYmBC9FEeqhXX5NsS6trF6lsjvn9ckwhWXTpkooZ3J SH/QscG57Xi3y+QSDa3/iHJikBkFbdPhvwmHzAhEWhLZDiGtu1jy1SsCFKPDlCSxfPom DQnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744003192; x=1744607992; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O0QCoPaLLVGVM+j7NSycgXkvkY/kC3GX55A3ZW70lac=; b=lVt2ctDUwhZP3t1Gzhj14eQVnjgFjI/orygoxDXuWaeFpm4UvBQ0AhC41mSmqv8VOw ypFMkOBdtD6Fe8efjUUZxJRJFFwiodDVkkqIo36C8IaTf8+kwZF4IpXEFivJ58XrHH2O 5uQha8+mKhON21c6TBp9VjFwcUIo4xJPh4INvb8d5XxSZ2Z0Va8GuGx5nyymQOodcGQB lTKFRgbO2fzU8t17XmdB5AziIAxPjjP87sDIXkd04xNCX2bXGQstX7UKEnDEB0bFluIP +sZ+1HNq7yx/uzshzq6OLMQBejhSl22ZggmEFr9bTIsHC9EvelFPO/KVrV7R6f7FV48W Qe9Q== X-Forwarded-Encrypted: i=1; AJvYcCWR5K4DkeA+c6nSEueo3sxNzP2ssLA6CuXGdFXcED2giq0znsKdyTj9pHVQvCJnVJ4jTceTVulHQUDeq9S/@postgresql.org X-Gm-Message-State: AOJu0YzxyH0fhdm9l1AvzYsDEV/crSZiK8wTP0NBGADP7L0ItcUJOD31 tTr9pfwwVUVWSY6fEBdPPpKTpewGsSusQ+7Yw/t2slP708vwV/f/LM7mKaITQADxTQt0sBlKw8/ TPI+uTM8YV/xXGzACcDV85wAZVNA= X-Gm-Gg: ASbGnctUKgiMUeGfKiN1eabd4ATtlhHqnN6zKS9HejcJl3RX12VXOoJhKMZLHuqFAYp Oe4ohPkac5aEGYlk8aWNC9oJi17rV9QNEnwfdsAvOn+2tij/v4nfHjKQ44MDu0fNjNNukeIi6Pv CslMO1RsDhfsa7zCfo1wbiiLVX1x8qSgRMC+RF X-Google-Smtp-Source: AGHT+IHrMeZtjSRhFcpuOEIAevjawCJQBJ/YUVyxmBrN8k0FLJGvuLDu5wE6G1DY4/ywLHIRgxYTBmXIY1sKC+/qY2Y= X-Received: by 2002:a05:6102:3ca3:b0:4bb:e5bf:9c7d with SMTP id ada2fe7eead31-4c86373871emr4616519137.17.1744003191952; Sun, 06 Apr 2025 22:19:51 -0700 (PDT) MIME-Version: 1.0 References: <04c0abe2-e9c5-4ffc-97b2-bedc1e3cd178@eisentraut.org> <3b190de8-910a-4091-82f9-e98cf562d832@postgresfriends.org> In-Reply-To: From: Ashutosh Bapat Date: Mon, 7 Apr 2025 10:49:40 +0530 X-Gm-Features: ATxdqUHGsq_jhNbAm4a8vcxyG2ZHOTM7fED59hrM8q7iTsp7bhFkWnPif8G-H-Y Message-ID: Subject: Re: SQL Property Graph Queries (SQL/PGQ) To: Junwang Zhao Cc: Amit Langote , Vik Fearing , Ajay Pal , Imran Zaheer , Peter Eisentraut , pgsql-hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, Apr 5, 2025 at 6:20=E2=80=AFPM Junwang Zhao wro= te: > > Hi Ashutosh and Peter, > > Since this PGQ feature won't be in PG 18, I'd like to raise a discussion = of > the possibility of implementing the quantifier feature, which I think is = a > quite useful feature in the graph database area. I agree that quantifiers feature is very useful; it's being used in many usecases. However, it's a bit of a complex feature. IMO, we should keep that discussion as well as the patch in a separate thread, so that this patchset doesn't grow too large to review and also discussion in this thread can remain focused. Once we get the current patch set reviewed and committed we can tackle the quantifier problem in a separate discussion. Of course that doesn't mean that we can not start discussion, try POC and even a working patch for quantifier support. Peter may think otherwise. > > I'll start with a graph definition first. > > `Person(id, name, age, sex)` with id as PK > `Knows(id, start_id, end_id, since)` with id as PK, start_id and > end_id FK referencing Person's id > > insert into Person values(1, 'A', 31, 'M'), (2, 'B', 30, 'F'), (3, > 'C', 33, 'M'), (4, 'D', 31, 'F'), (5, 'E', 32, 'M'), (6, 'F', 33, > 'M'); > insert into Knows values (1, 1, 2, '2020'); -- A knows B since 2020 > insert into Knows values (2, 1, 3, '2021'); -- A knows C since 2021 > insert into Knows values (3, 1, 4, '2020'); -- A knows D since 2020 > insert into Knows values (4, 2, 4, '2023'); -- B knows D since 2023 > insert into Knows values (5, 3, 5, '2022'); -- C knows E since 2022 > insert into Knows values (6, 2, 6, '2021'); -- B knows F since 2021 > insert into Knows values (7, 4, 6, '2020'); -- D knows F since 2020 > > Then we create a property graph: > > CREATE property graph new_graph > VERTEX TABLES (Person) > EDGE TABLES (Knows); > > If we want to find A's non-directly known friends within 3 hops, we can q= uery: > > select name from graph_table (new_graph match (a:Person WHERE a.name =3D > 'A') --> (b:Person) --> (c:Person) COLUMNS (c.name)) > union > select name from graph_table (new_graph match (a:Person WHERE a.name =3D > 'A') --> (b:Person) -->(c:Person)-->(d:Person) COLUMNS (d.name)); > > Or if we support quantifier, we can simply the query as: > > select name from graph_table (new_graph match (a:Person WHERE a.name =3D > 'A') -->{2,3} (b:Person) COLUMNS (b.name)); > > In the current design of PostgreSQL, we can rewrite this pattern with > quantifiers to > the union form with some effort. > > But what if the pattern is more complicated, for example: > > 1. select name, since from graph_table (new_graph match (a:Person > WHERE a.name =3D 'A') -[r:Knows]->{2,3} (b:Person) COLUMNS (b.name, > r.since)); > Can we support the r.since column? I guess not, in this case r is a > variable length edge. > > 2. select name, count from graph_table (new_graph match (a:Person > WHERE a.name =3D 'A') -[r:Knows]->{2,3} (b:Person) COLUMNS (b.name, > count(r))); > Can we support this count aggregation(this is called horizontal > aggregation in Oracle's pgql)? How can the executor know the length of > the variable length edge? > > 3. What if the query doesn't specify the Label of edge, and there can > be different edge labels of r, can we easily do the rewrite? > > I did some study of the apache age, they have fixed columns for node > labels(id, agtype) > and edge labels(id, source_id, end_id, agtype), agtype is kind of > json. So they can > resolve the above question easily. > > Above are just my random thoughts of the quantifier feature, I don't have= a copy > of the PGQ standard, so I'd like to hear your opinion about this. > I think the questions you have raised are valid. If we decide to discuss this in a separate thread, I will start that thread just by responding to these questions and design I have in mind. --=20 Best Wishes, Ashutosh Bapat