Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:59:02+00:00 2026-06-13T15:59:02+00:00

I’ve created a function in PostgreSQL that works on a couple of tables and

  • 0

I’ve created a function in PostgreSQL that works on a couple of tables and assembles the result based on a parameter passed. It works quite well and from the client’s perspective it looks like a normal table with an additional parameter (i.e. it can be queried like a usual select query).

In order to keep hiding the information about the underlying data, I would also like to create a way to write to the table. I thought I’d create a trigger that executes a function which would basically update the relevant underlying tables. However, I wasn’t able to create a trigger on a function. Is there any way of creating a trigger on anything else but a table? I was also considering of using a view (as I believe on can create a trigger on a view), but then I would need to change the function to become a view which doesn’t suit as I wouldn’t be able to pass in a parameter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:59:03+00:00Added an answer on June 13, 2026 at 3:59 pm

    You appear to want to create the equivalent of an updatable view over a function, where you can INSERT, UPDATE or DELETE on a function. This is not possible.

    If you’re providing a function-like interface, provide functions for data modification as well.

    Alternately, turn your function into a view. Allow them to restrict the view with a WHERE clause instead of passing parameters to a function. Then use a view trigger (PostgreSQL 9.1 and above) or rules (PostgreSQL 9.0 and below, do not use on newer versions if possible) to enable INSERT, UPDATE and DELETE on the view. See CREATE TRIGGER and CREATE RULE. Rules are tricky, so use a view trigger by preference.

    PostgreSQL is pretty clever about pushing filters down into views, so SELECT * FROM some_view WHERE some_col = 4 will not generally scan the whole view then filter it. It will “push” the WHERE clause into the view’s query and execute that. So if your view is SELECT * FROM some_table WHERE NOT is_archived PostgreSQL will actually land up executing the equivalent of SELECT * FROM some_table WHERE (NOT is_archived) AND some_col = 4. Because of this you can and often do get a completely different query plan when querying a view for just a few rows vs all rows. There’s an example of this in a post I wrote a while ago.

    Just wrapping your function in a view won’t work well unless it’s an SQL function that’s STABLE and not IMMUTABLE (so it can be inlined). You’re best off extracting the SQL from the function and creating the view based on the same SQL, rather than basing it on the function its self.

    If your function parameters aren’t used in simple WHERE clauses then it gets more complicated, because there’s no way to pass the parameter into the view like you can in a function. You can create a view over all possible values of the parameter then filter that, but that can lead to truly awful performance if Pg seq-scans the view. In these more complex cases I’d want to offer a function call interface for making modifications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.