0

I want to pass an array like [group1, group2, group3] and then filter the Postgres table column groups.

Create or replace function funname(groups text[]) Select * From tableName Where groupscolumn @> groups

Also kindly write the function for the same, getting an error while defining character varying [].

1
  • It's best if you show what you've tried so far and what went wrong, what error you're getting. Also, please clarify your second paragraph: do you mean you need the plain sql query in addition to a function that throws an error when it gets the character varying[] type (instead of text?), or that you are getting an error when you try to define the function accepting or returning this data type. It'd be easier to work this out if you showed your samples and error messages. Commented Jan 27, 2023 at 11:12

3 Answers 3

2

It's unclear to me what you want, but maybe you are looking for the ANY operator?

select * 
from some_unknown_table
where group_column = any(array_parameter);

This requires that the data type of the group_column and the one of the parameter match. E.g. if group_column is text or varchar the parameter needs to be declare as text[]. If group_column is e.g. an integer, the parameter needs to be declared as integer[]

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the unnest function to convert an array in 'table' output and then filter the value that you need, for example:

SELECT * FROM (
SELECT unnest (ARRAY['group1','group2','group3']) AS arr
) AS sub WHERE arr ='group3'

Comments

0

You can use join for groups array and SELECT WHERE group IN groups for filter:

import psycopg2


def filter_groups(group_names):


try:
    # connect to BD
    connection = psycopg2.connect(
        host="host",
        user="user",
        password="pass",
        database="db_name"
    )
except Exception as _ex:
    print("[INFO] Connection error", _ex)
try:
    with connection.cursor() as cursor:
        placeholders = ','.join(['%s'] * len(group_names))
        rsql = f"SELECT * FROM table_name WHERE groups IN ({placeholders})"
        cursor.execute(rsql, group_names)
        rows = cursor.fetchall()
        cursor.close()
except Exception as _ex:
    print("[INFO] Error while working with PostgreSQL", _ex)

if connection:
    connection.close()
    print("[INFO] PostgreSQL connection closed.")
return rows

1 Comment

You can use the array as is - psycopg can map it to a SQL array, so you can simply cursor.execute("SELECT * FROM table_name WHERE groups=ANY(%s)",group_names). You need to correct your indentation as well. Note that this is a plain SQL question with java tag added for a reason unspecified by the OP and you're presenting python code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.