Trigger that will build one row from several rows











up vote
1
down vote

favorite












I mean:



INSERT INTO test VALUES(1, 'message'), (2, 'message'), (3, 'message);


triggering will cause the result in the table to look like this:



1, E'messagenmessagenmessage'


How to forbid inserting rows and then continue operations on the transferred data in the insert?
I am using postresgql.










share|improve this question
























  • Should we assume the 3 combined 'message' strings are the combined values of the 3 rows from the INSERT statement?
    – eurotrash
    Nov 19 at 11:48










  • Yes, exactly this I mean.
    – Bocian
    Nov 19 at 13:02










  • You can create on statement trigger that selects inserted rows and changes them to the format you desired and then deletes old records from the table.
    – JustMe
    Nov 19 at 13:57










  • I think that this cannot be done. I also don't understand the requirement.
    – Laurenz Albe
    Nov 19 at 18:44















up vote
1
down vote

favorite












I mean:



INSERT INTO test VALUES(1, 'message'), (2, 'message'), (3, 'message);


triggering will cause the result in the table to look like this:



1, E'messagenmessagenmessage'


How to forbid inserting rows and then continue operations on the transferred data in the insert?
I am using postresgql.










share|improve this question
























  • Should we assume the 3 combined 'message' strings are the combined values of the 3 rows from the INSERT statement?
    – eurotrash
    Nov 19 at 11:48










  • Yes, exactly this I mean.
    – Bocian
    Nov 19 at 13:02










  • You can create on statement trigger that selects inserted rows and changes them to the format you desired and then deletes old records from the table.
    – JustMe
    Nov 19 at 13:57










  • I think that this cannot be done. I also don't understand the requirement.
    – Laurenz Albe
    Nov 19 at 18:44













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I mean:



INSERT INTO test VALUES(1, 'message'), (2, 'message'), (3, 'message);


triggering will cause the result in the table to look like this:



1, E'messagenmessagenmessage'


How to forbid inserting rows and then continue operations on the transferred data in the insert?
I am using postresgql.










share|improve this question















I mean:



INSERT INTO test VALUES(1, 'message'), (2, 'message'), (3, 'message);


triggering will cause the result in the table to look like this:



1, E'messagenmessagenmessage'


How to forbid inserting rows and then continue operations on the transferred data in the insert?
I am using postresgql.







postgresql plpgsql database-trigger






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 11:09









a_horse_with_no_name

288k46434529




288k46434529










asked Nov 19 at 11:06









Bocian

107




107












  • Should we assume the 3 combined 'message' strings are the combined values of the 3 rows from the INSERT statement?
    – eurotrash
    Nov 19 at 11:48










  • Yes, exactly this I mean.
    – Bocian
    Nov 19 at 13:02










  • You can create on statement trigger that selects inserted rows and changes them to the format you desired and then deletes old records from the table.
    – JustMe
    Nov 19 at 13:57










  • I think that this cannot be done. I also don't understand the requirement.
    – Laurenz Albe
    Nov 19 at 18:44


















  • Should we assume the 3 combined 'message' strings are the combined values of the 3 rows from the INSERT statement?
    – eurotrash
    Nov 19 at 11:48










  • Yes, exactly this I mean.
    – Bocian
    Nov 19 at 13:02










  • You can create on statement trigger that selects inserted rows and changes them to the format you desired and then deletes old records from the table.
    – JustMe
    Nov 19 at 13:57










  • I think that this cannot be done. I also don't understand the requirement.
    – Laurenz Albe
    Nov 19 at 18:44
















Should we assume the 3 combined 'message' strings are the combined values of the 3 rows from the INSERT statement?
– eurotrash
Nov 19 at 11:48




Should we assume the 3 combined 'message' strings are the combined values of the 3 rows from the INSERT statement?
– eurotrash
Nov 19 at 11:48












Yes, exactly this I mean.
– Bocian
Nov 19 at 13:02




Yes, exactly this I mean.
– Bocian
Nov 19 at 13:02












You can create on statement trigger that selects inserted rows and changes them to the format you desired and then deletes old records from the table.
– JustMe
Nov 19 at 13:57




You can create on statement trigger that selects inserted rows and changes them to the format you desired and then deletes old records from the table.
– JustMe
Nov 19 at 13:57












I think that this cannot be done. I also don't understand the requirement.
– Laurenz Albe
Nov 19 at 18:44




I think that this cannot be done. I also don't understand the requirement.
– Laurenz Albe
Nov 19 at 18:44












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










In Postgres 10+ you can use a transition table in an AFTER trigger, see Example 43.7. Auditing with Transition Tables. Assuming that id is a primary key (or unique):



create table my_table(id int primary key, message text);


you can update one and delete the remaining inserted rows:



create or replace function after_insert_on_my_table()
returns trigger language plpgsql as $$
declare r record;
begin
select
array_agg(id) as ids,
array_to_string(array_agg(message), e'n') as message
from new_table
into r;

update my_table
set message = r.message
where id = r.ids[1];

delete from my_table
where id = any(r.ids[2:]);

return null;
end $$;


In a trigger definition declare a transition table (as new_table):



create trigger after_insert_on_my_table
after insert on my_table
referencing new table as new_table
for each statement
execute procedure after_insert_on_my_table();


In earlier versions of Postgres you can simulate a transition table introduced in Postgres 10.



Test it in db<>fiddle.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373298%2ftrigger-that-will-build-one-row-from-several-rows%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    In Postgres 10+ you can use a transition table in an AFTER trigger, see Example 43.7. Auditing with Transition Tables. Assuming that id is a primary key (or unique):



    create table my_table(id int primary key, message text);


    you can update one and delete the remaining inserted rows:



    create or replace function after_insert_on_my_table()
    returns trigger language plpgsql as $$
    declare r record;
    begin
    select
    array_agg(id) as ids,
    array_to_string(array_agg(message), e'n') as message
    from new_table
    into r;

    update my_table
    set message = r.message
    where id = r.ids[1];

    delete from my_table
    where id = any(r.ids[2:]);

    return null;
    end $$;


    In a trigger definition declare a transition table (as new_table):



    create trigger after_insert_on_my_table
    after insert on my_table
    referencing new table as new_table
    for each statement
    execute procedure after_insert_on_my_table();


    In earlier versions of Postgres you can simulate a transition table introduced in Postgres 10.



    Test it in db<>fiddle.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      In Postgres 10+ you can use a transition table in an AFTER trigger, see Example 43.7. Auditing with Transition Tables. Assuming that id is a primary key (or unique):



      create table my_table(id int primary key, message text);


      you can update one and delete the remaining inserted rows:



      create or replace function after_insert_on_my_table()
      returns trigger language plpgsql as $$
      declare r record;
      begin
      select
      array_agg(id) as ids,
      array_to_string(array_agg(message), e'n') as message
      from new_table
      into r;

      update my_table
      set message = r.message
      where id = r.ids[1];

      delete from my_table
      where id = any(r.ids[2:]);

      return null;
      end $$;


      In a trigger definition declare a transition table (as new_table):



      create trigger after_insert_on_my_table
      after insert on my_table
      referencing new table as new_table
      for each statement
      execute procedure after_insert_on_my_table();


      In earlier versions of Postgres you can simulate a transition table introduced in Postgres 10.



      Test it in db<>fiddle.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        In Postgres 10+ you can use a transition table in an AFTER trigger, see Example 43.7. Auditing with Transition Tables. Assuming that id is a primary key (or unique):



        create table my_table(id int primary key, message text);


        you can update one and delete the remaining inserted rows:



        create or replace function after_insert_on_my_table()
        returns trigger language plpgsql as $$
        declare r record;
        begin
        select
        array_agg(id) as ids,
        array_to_string(array_agg(message), e'n') as message
        from new_table
        into r;

        update my_table
        set message = r.message
        where id = r.ids[1];

        delete from my_table
        where id = any(r.ids[2:]);

        return null;
        end $$;


        In a trigger definition declare a transition table (as new_table):



        create trigger after_insert_on_my_table
        after insert on my_table
        referencing new table as new_table
        for each statement
        execute procedure after_insert_on_my_table();


        In earlier versions of Postgres you can simulate a transition table introduced in Postgres 10.



        Test it in db<>fiddle.






        share|improve this answer












        In Postgres 10+ you can use a transition table in an AFTER trigger, see Example 43.7. Auditing with Transition Tables. Assuming that id is a primary key (or unique):



        create table my_table(id int primary key, message text);


        you can update one and delete the remaining inserted rows:



        create or replace function after_insert_on_my_table()
        returns trigger language plpgsql as $$
        declare r record;
        begin
        select
        array_agg(id) as ids,
        array_to_string(array_agg(message), e'n') as message
        from new_table
        into r;

        update my_table
        set message = r.message
        where id = r.ids[1];

        delete from my_table
        where id = any(r.ids[2:]);

        return null;
        end $$;


        In a trigger definition declare a transition table (as new_table):



        create trigger after_insert_on_my_table
        after insert on my_table
        referencing new table as new_table
        for each statement
        execute procedure after_insert_on_my_table();


        In earlier versions of Postgres you can simulate a transition table introduced in Postgres 10.



        Test it in db<>fiddle.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 23:24









        klin

        54.1k54574




        54.1k54574






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373298%2ftrigger-that-will-build-one-row-from-several-rows%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga