PostgreSQL parent-child table query all child data - use custom function query

pgsql function query code

select find_space_tree_list_by_nodeid('1','1') 

 Schematic diagram of query results

 Get subset function code

CREATE OR REPLACE FUNCTION "public"."find_space_tree_list_by_nodeid"("nodeid" varchar, "viewid" varchar)
  RETURNS "pg_catalog"."varchar" AS $BODY$
	-- Routine body goes here...
   DECLARE 
	 sTemp VARCHAR:=nodeId;
    sTempChd VARCHAR:=nodeId;
		BEGIN
		WHILE sTempChd is not null loop
	 if sTemp<>sTempChd  then 
	  		 sTemp:=concat(sTemp,',',sTempChd);
	 END IF;
     SELECT string_agg(id,'') INTO sTempChd FROM public.t_bim_space where view_id=viewId 
		 and parent_id=ANY (string_to_array(sTempChd,','));
    END loop;
	RETURN sTemp;
END$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100

Note: Unlike the mysql function, the function name of pgsql must be lowercase, otherwise an error will be reported!

related information

In PostgreSQL, custom functions can be created to meet specific business needs. Custom functions allow you to write your own SQL statements and logic and encapsulate them as reusable functions. The following is a detailed explanation of PostgreSQL custom functions:

  1. Function Creation: Use the CREATE FUNCTION statement to create custom functions. Specify the function's name, parameter list, return type, and function body (that is, the actual logic of the function). For example:

    CREATE FUNCTION my_function(param1 data_type, param2 data_type) 
    RETURNS return_type AS
    $$
    -- 函数逻辑代码
    $$
    LANGUAGE sql; -- 或者指定其他支持的语言,如plpgsql、plpython等
    
  2. Parameters: You can define the parameters required by the function. Parameters can be input parameters, output parameters, or both. Each parameter has a data type and a parameter name. For example:

    CREATE FUNCTION my_function(param1 integer, param2 text)
    ...
    
  3. Return type: Define the return type of the function, that is, the result type after the function is executed. The return type can be a scalar type (such as integer, text, etc.), a table type or a composite type (such as a record), etc.

  4. Function body: The function body is the actual logic part of the function, which includes SQL statements, flow control statements (such as IF-ELSE, FOR loops, etc.) and variable declarations. Depending on the chosen language, function bodies can use different syntax and features.

  5. Language: PostgreSQL supports multiple programming languages ​​to write custom functions, such as SQL, PL/pgSQL, PL/Python, etc. You can select the appropriate language according to your needs.

  6. Calling method: After creating a custom function, it can be called through the expression in the SELECT statement or other functions. You can use custom functions just like built-in functions.

  7. Function overloading: In PostgreSQL, you can create multiple function definitions for the same function name, but the parameter types or number must be different. This is known as function overloading. The system will determine which function definition to call based on the parameters actually passed.

  8. Function deletion: Use the DROP FUNCTION statement to delete the created custom function. For example:

    DROP FUNCTION my_function(param1 data_type, param2 data_type);

Custom functions are very powerful and flexible in PostgreSQL, and complex logic processing can be written according to specific business needs. They provide a way to encapsulate and reuse code, and can be seamlessly integrated with SQL statements, making database operations more efficient and convenient.

PostgreSQL is an open source object-relational database management system (ORDBMS) respected for its reliability, scalability, and feature richness. It supports a wide range of data types, has advanced transaction processing capabilities, and provides rich features and scalability options.

The following is a detailed analysis of PostgreSQL:

  1. Data model: PostgreSQL adopts the data model of a relational database and supports common relational database concepts such as tables, views, and indexes. Each database consists of multiple schemas, and each schema contains multiple tables. Tables can define columns, constraints and triggers, etc.

  2. Datatypes: PostgreSQL provides a wide range of built-in datatypes, including integers, floats, characters, date/time, booleans, and more. In addition, complex data types such as arrays, JSON, XML, network addresses, and geometric data are supported. At the same time, users can also customize data types to meet specific needs.

  3. Scalability: PostgreSQL has excellent scalability, and its functions can be enhanced by adding additional modules. These modules are called extensions, which can add new data types, functions, index types, and query optimization strategies. The PostgreSQL community also provides a large number of extensions, such as PostGIS for geographic information systems, pgcrypto for data encryption, and more.

  4. Transaction processing: PostgreSQL supports ACID (atomicity, consistency, isolation, and durability) transaction attributes to ensure data integrity and consistency. It uses multi-version concurrency control (MVCC) to achieve highly concurrent transaction processing, allowing multiple transactions to read and modify data concurrently without interfering with each other.

  5. Query Language: PostgreSQL uses Structured Query Language (SQL) as its main query language. It supports standard SQL syntax and provides some extended functions, such as window functions, recursive query, full-text search, etc. In addition, PostgreSQL also supports stored procedures and triggers, which can execute complex business logic in the database.

  6. Advanced features: PostgreSQL has some advanced features, such as partial index, table space management, hot backup, logical replication, etc. Partial indexes allow indexes to be created on a subset of the dataset, improving query performance. Tablespace management allows data files to be stored in different physical locations, providing better storage management. Hot backup and logical replication provide a reliable data backup and replication mechanism.

  7. Multi-language support: PostgreSQL supports the integration of multiple programming languages, including Java, Python, Ruby, C/C++, etc. Users can use these programming languages ​​to write stored procedures, triggers and custom functions, and interact with the database.

  8. Security: PostgreSQL provides strong security features, including authentication, access control, and data encryption. It supports multiple authentication methods, such as password verification, certificate verification, etc. Access control can be fine-grained through roles and permissions. The data encryption feature allows encrypted storage of sensitive data.

To sum up, PostgreSQL is a powerful and flexible open source relational database management system. It has extensive data type support, advanced transaction capabilities, scalability options, and security features. Whether it is a small application or a large enterprise-level system, PostgreSQL can provide stable and reliable data storage and processing solutions.

Recommended related articles

"MYSQL parent-child table building to query all child data - use custom function query"

Guess you like

Origin blog.csdn.net/weixin_40986713/article/details/119913345